FormulaUtils.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. package com.mixsmart.utils;
  2. import cn.hutool.log.StaticLog;
  3. import com.jfireel.expression.Expression;
  4. import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
  5. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  6. import org.apache.poi.ss.usermodel.*;
  7. import java.awt.Color;
  8. import org.jfree.chart.ChartFactory;
  9. import org.jfree.chart.ChartPanel;
  10. import org.jfree.chart.ChartUtils;
  11. import org.jfree.chart.JFreeChart;
  12. import org.jfree.chart.axis.NumberAxis;
  13. import org.jfree.chart.axis.NumberTickUnit;
  14. import org.jfree.chart.plot.PlotOrientation;
  15. import org.jfree.chart.plot.ValueMarker;
  16. import org.jfree.chart.plot.XYPlot;
  17. import org.jfree.chart.renderer.xy.XYSplineRenderer;
  18. import org.jfree.chart.title.TextTitle;
  19. import org.jfree.data.xy.XYSeries;
  20. import org.jfree.data.xy.XYSeriesCollection;
  21. import org.jsoup.Jsoup;
  22. import org.jsoup.nodes.Document;
  23. import org.springblade.common.utils.BaseUtils;
  24. import org.springblade.core.tool.utils.CollectionUtil;
  25. import org.springblade.core.tool.utils.Func;
  26. import org.springblade.core.tool.utils.IoUtil;
  27. import org.springblade.core.tool.utils.StringPool;
  28. import org.springblade.manager.dto.Coords;
  29. import org.springblade.manager.dto.ElementData;
  30. import org.springblade.manager.dto.FormData;
  31. import org.springblade.manager.dto.LocalVariable;
  32. import org.springblade.manager.entity.Formula;
  33. import org.springblade.manager.utils.FileUtils;
  34. import java.awt.*;
  35. import java.awt.Font;
  36. import java.awt.Shape;
  37. import java.awt.geom.Ellipse2D;
  38. import java.io.*;
  39. import java.nio.charset.StandardCharsets;
  40. import java.security.MessageDigest;
  41. import java.security.NoSuchAlgorithmException;
  42. import java.util.*;
  43. import java.util.List;
  44. import java.util.concurrent.atomic.AtomicInteger;
  45. import java.util.regex.Matcher;
  46. import java.util.regex.Pattern;
  47. import java.util.stream.Collectors;
  48. import java.util.stream.IntStream;
  49. import java.util.stream.Stream;
  50. import static java.util.regex.Pattern.*;
  51. /**
  52. * @author yangyj
  53. * @Date 2022/7/14 15:55
  54. * @description TODO
  55. */
  56. public class FormulaUtils {
  57. public static Map<String,Object> triangleSquare(Object ranges){
  58. Map<String,Object> map =new HashMap<>();
  59. if(StringUtils.isEmpty(ranges)){
  60. //z的默认取值范围
  61. ranges="(0,15)";
  62. }
  63. Matcher m = RegexUtils.matcher("(\\-?\\d+)(\\D)(\\+?\\d+)",ranges.toString());
  64. if(m.find()) {
  65. System.out.println();
  66. int min = StringUtils.handObj2Integer(m.group(1));
  67. int max = StringUtils.handObj2Integer(m.group(3));
  68. Integer[] r = pythagorean(min, max);
  69. map.put("X", String.valueOf(r[0]));
  70. map.put("Y", String.valueOf(r[1]));
  71. map.put("Z", String.valueOf(r[2]));
  72. }
  73. return map;
  74. }
  75. /**
  76. * @Description 字符串相似度
  77. * @Param [s1, s2]
  78. * @return double
  79. * @Author yangyj
  80. * @Date 2023.04.12 18:01
  81. **/
  82. public static double getJaccardSimilarity(String s1, String s2) {
  83. Set<Character> set1 = new HashSet<>();
  84. Set<Character> set2 = new HashSet<>();
  85. for (char c : s1.toCharArray()) {
  86. set1.add(c);
  87. }
  88. for (char c : s2.toCharArray()) {
  89. set2.add(c);
  90. }
  91. Set<Character> intersection = new HashSet<>(set1);
  92. intersection.retainAll(set2);
  93. Set<Character> union = new HashSet<>(set1);
  94. union.addAll(set2);
  95. return (double) intersection.size() / union.size();
  96. }
  97. public static Double similarity(String s1,String s2){
  98. return getJaccardSimilarity(parseItemName(s1),parseItemName(s2));
  99. }
  100. /**
  101. * result[0]^2+result[1]^2=result[2]^2 result[] 元素均为正整数
  102. */
  103. public static Integer[] pythagorean(Integer min,Integer max){
  104. Integer[] result = null;
  105. List<Integer[]> list = new ArrayList<>();
  106. for(int i=1;i<=max;i++){
  107. for(int j=1;j<=max;j++){
  108. double tmp = Math.sqrt(Math.pow(i,2)+Math.pow(j,2));
  109. int z= (int) Math.round(tmp);
  110. if(min<z&&z<=max){
  111. Integer[] arr = new Integer[]{ i,j,z};
  112. list.add(arr);
  113. }
  114. }
  115. }
  116. if(ListUtils.isNotEmpty(list)){
  117. Random rm = new Random();
  118. result = list.get(rm.nextInt(list.size()));
  119. }
  120. return result;
  121. }
  122. /*public static void main(String[] args) {
  123. FormData fd = new FormData();
  124. fd.setEName("1111");
  125. List<ElementData> list = new ArrayList<>();
  126. list.add(new ElementData(1,1,1));
  127. test(fd);
  128. System.out.println(fd.getEName());
  129. }*/
  130. /*默认去空*/
  131. public static void write(FormData fd, Object data){
  132. write(fd,data,false);
  133. }
  134. public static void write(FormData fd, Object data,Boolean nullOrBlank ){
  135. if(Func.isEmpty(fd.getValues())){
  136. /*无定位信息不写入*/
  137. return;
  138. }
  139. try {
  140. /*一个单元格且存在多张,全部设置为自动拓展 20230816*/
  141. if(fd.getCoordsList().size()==1&&fd.getValues().size()>1&&fd.getFormula()!=null){
  142. fd.getFormula().setOutm(Formula.FULL);
  143. }
  144. /*写入前清空内容*/
  145. fd.getValues().forEach(t->t.setValue(null));
  146. if(data instanceof List){
  147. List<Object> values = (List<Object>) data;
  148. if(!nullOrBlank){
  149. values=values.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
  150. }
  151. if(values.size()>fd.getValues().size()){
  152. /*当生成的数据超过实际容量的时候,会自动追加页数*/
  153. if(fd.getCoordsList().size()==1){
  154. if(values.stream().filter(CustomFunction::containsZH).anyMatch(e->e.toString().contains("\n"))){
  155. fd.getValues().get(0).setValue(values.stream().filter(Objects::nonNull).map(Object::toString).collect(Collectors.joining()));
  156. }else{
  157. /*日期类型的元素只获取最后一个非空*/
  158. if(StringUtils.isEquals(4,fd.getEType())){
  159. fd.getValues().get(0).setValue(values.stream().filter(StringUtils::isNotEmpty).reduce((first, second) -> second).orElse(null));
  160. }else{
  161. fd.getValues().get(0).setValue(values.stream().map(StringUtils::handleNull).collect(Collectors.joining("、")));
  162. }
  163. }
  164. }else{
  165. // copy(fd,values);
  166. for(int n=0;n<fd.getValues().size();n++){
  167. fd.getValues().get(n).setValue(values.get(n));
  168. }
  169. List<Object> overList=values.stream().skip(fd.getValues().size()).collect(Collectors.toList());
  170. List<Coords> coordsList = fd.getCoordsList();
  171. int addPage=(int)Math.ceil((double)overList.size()/(double)coordsList.size());
  172. fd.setAddPages(addPage);
  173. ElementData last =fd.getValues().get(fd.getValues().size()-1);
  174. int indexBase=last.getIndex()+1;
  175. List<ElementData> addList= new ArrayList<>();
  176. for(int i=0;i<addPage;i++){
  177. for(int j=0;j<coordsList.size();j++){
  178. /*超页就尽管写进去,格式化阶段再加表*/
  179. Coords coords = coordsList.get(j);
  180. Object v=null;
  181. int st=i*coordsList.size()+j;
  182. if(st<overList.size()){
  183. v= overList.get(st);
  184. }
  185. addList.add(new ElementData(indexBase+i,last.getGroupId(),v,coords.getX(),coords.getY()));
  186. }
  187. }
  188. fd.getValues().addAll(addList);
  189. }
  190. }else{
  191. for(int n=0;n<values.size();n++){
  192. fd.getValues().get(n).setValue(values.get(n));
  193. }
  194. }
  195. }else{
  196. if(Formula.FULL.equals(fd.getFormula().getOutm())){
  197. /*填充策略*/
  198. fd.getValues().forEach(e->e.setValue(data));
  199. }else{
  200. fd.getValues().get(0).setValue(data);
  201. }
  202. }
  203. fd.setUpdate(1);
  204. }catch (Exception e){
  205. e.printStackTrace();
  206. }
  207. }
  208. /**从元素名称中解析项目名称,细化项目匹配用*/
  209. public static String parseItemName(String eName){
  210. if (StringUtils.isEmpty(eName)) {
  211. return eName;
  212. }
  213. String str = eName.replaceAll("\\s", "");
  214. Pattern pattern = compile("[((][^\\u4e00-\\u9fa5]+[))]|_+");
  215. String[] candidate = pattern.split(str);
  216. /*非中文非罗马数字1到10*/
  217. String regex = "[^\\u4e00-\\u9fa5\\u2160-\\u2169))((]+";
  218. Pattern p = compile(regex);
  219. return Arrays.stream(candidate)
  220. .filter(s -> !isContainKeywords(s))
  221. .map(s -> filterString(s, p))
  222. .collect(Collectors.joining());
  223. }
  224. /*A15检查内容专用*/
  225. public static String checkItemName(String eName){
  226. if (StringUtils.isEmpty(eName)) {
  227. return eName;
  228. }
  229. /*分割字符串,选取第一个匹配的子串*/
  230. String str = eName.replaceAll("\\s", "");
  231. Pattern pattern = compile("[((][^\\u4e00-\\u9fa5]+[))]|_+");
  232. String[] candidate = pattern.split(str);
  233. String regex = "[^\\u4e00-\\u9fa5]+";
  234. return Arrays.stream(candidate).map(s->s.replaceAll(regex,"")).distinct().filter(StringUtils::isNotEmpty).filter(s->!isContainKeywords2(s)).findFirst().orElse("");
  235. }
  236. private static String filterString(String s, Pattern p) {
  237. s=s.replaceAll("【[^【】]+】","");
  238. Matcher matcher = p.matcher(s);
  239. return matcher.replaceAll("").replaceAll(getRegex(), "").replaceAll("(设计|合格).*","");
  240. }
  241. private static String getRegex() {
  242. return "(在合格标准内|满足设计要求|质量评定|评定|判定|项目|总数|抽测|实测|偏差|尺量)";
  243. }
  244. private static boolean isContainKeywords(String s) {
  245. List<String> keywords = Arrays.asList( ":", "个","附录","抽查","测","求","小于","大于","检查","仪","按","不","各","记录","且","规定","值或实","≤","≥","平均");
  246. return keywords.stream().anyMatch(s::contains);
  247. }
  248. private static boolean isContainKeywords2(String s) {
  249. List<String> keywords = Arrays.asList( "实测项目");
  250. return keywords.stream().anyMatch(s::contains);
  251. }
  252. /*回归·测试变量*/
  253. public static List<String> itemNames =Arrays.asList(
  254. ""
  255. ,"压 实 度 (%)下路床 特重、极重交通荷载等级 设计值"
  256. ,"1△_压 实 度 (%)_下路床_轻、中及重交通 荷载等级_0.3m~0.8m_≧96_≧95_≧94_实测值或实测偏差值"
  257. ,"1△_压 实 度 (%)_下路提_轻、中及重交通 荷载等级_&gt;1.5m_≧93_≧92_≧90_实测值或实测偏差值"
  258. ,"1△_压 实 度 (%)_上路提_轻、中及重交通 荷载等级_0.8m~1.5m_≧94_≧94_≧93_实测值或实测偏差值"
  259. ,"压 实 度 (%)下路提 轻、中及重交通荷载等级 设计值"
  260. ,"压 实 度 (%)下路床 特重、极重交通荷载等级 合格率"
  261. ,"压 实 度 (%)下路提 轻、中及重交通荷载等级\t合格率"
  262. ,"5△_保护层 厚度 (mm)_基础、锚碇、墩台身、墩柱_±10_实测值或实测偏差值"
  263. ,"钢筋骨架尺寸宽、高或直径 (mm)_尺量:按骨架总数30%抽测_±5_实测值或实测偏差值"
  264. ,"钢筋骨架尺寸长 (mm)_±10_尺量:按骨架总数30%抽测_实测值或实测偏差值"
  265. , "受力钢筋间距 (mm)同排 梁、板、拱肋及拱上建筑 设计值"
  266. ,"受力钢筋间距 (mm)同排 梁、板、拱肋及拱上建筑 合格率"
  267. ," 箍筋、构造钢筋、螺旋筋间距(mm) 设计值"
  268. ,"箍筋、构造钢筋、螺旋筋间距(mm) 合格率"
  269. ,"实测项目_桩位 (mm)_群桩_≤100_质量评定_合格判定"
  270. ,"实测项目_桩位 (mm)_群桩_≤100_实测值或实测偏差值"
  271. ,"实测项目_桩位 (mm)_排架桩_实测值或实测偏差值"
  272. ,"实测项目_桩位 (mm)_排架桩_质量评定_合格判定"
  273. ,"实测项目_桩位 (mm)_群桩_≤100_质量评定_合格率(%)"
  274. ,"实测项目_桩位 (mm)_排架桩_质量评定_合格率(%)"
  275. ,"3△_支座高程(mm)_满足设计要求;设 计未要求时±5_水准仪:测每支座中心线_实测值或实测偏差值"
  276. ,"基底承载力(KPa)_不小于设计_直观或动力触探试验_实测值或实测偏差值"
  277. ,"实 测 项 目_花卉数量_满足设计要求_实测值或实测偏差值"
  278. ,"实 测 项 目_2△_草坪、草本地被覆盖率(%)_取弃土场绿 地_≥90_实测值或实测偏差值"
  279. ,"轴线偏位(mm)_全站仪:20m检查3点_实测值或实测偏差值"
  280. ,"1△_基材混合物喷射厚度(mm)_设计厚度±10_实测值或实测偏差值"
  281. ,"1△_混凝土强度 (MPA)_在合格标准内_按附录D检查_实测值或实测偏差值"
  282. ,"边坡坡度_不陡于设计值_水准仪:每200m测2点,且不少于5点_实测值或实测偏差值"
  283. ,"几何尺寸(mm)_±50_尺量:长、宽、高、壁厚各2点_实测值或实测偏差值"
  284. ,"4△_桩长(mm)_不小于设计_查施工记录_实测值或偏差值"
  285. ,"单桩每延米喷粉 (浆)量_不小于设计_查施工记录_实测值或偏差值"
  286. ,"搭接宽度(mm)_≥150【纵向】_尺量:抽查2%_实测值或实测偏差值",
  287. "搭接宽度(mm)_≥50(横向)_尺量:抽查2%_实测值或实测偏差值"
  288. ,"竖直度(mm)_挖孔桩_0.5%桩长,且≤200_铅锤线:每桩检测_实测值或实测偏差值"
  289. , "2△_压浆压力值 (Mpa)_满足施工技术 规范规定_查油压表读书;每管道检查_实测值或实测偏差值"
  290. , "基底承载力(KPa)_不小于设计_实测值或实测偏差值"
  291. ,"1△_受力钢筋间距 (mm)_两排以上间距_±5_实测值或实测偏差值"
  292. ,"1△梁(板)长度 (mm)_±5_实测值或实测偏差值"
  293. ,"墙面平整度(mm)_施工缝、变形缝处≤20_实测值或实测偏差值"
  294. ,"基底承载力(KPa)_不小于设计_实测值或实测偏差值"
  295. ,"1△_拱部超挖(mm)_Ⅱ、Ⅲ、Ⅳ级围岩(中硬岩 、软岩)_平均150,最大250_实测值或实测偏差值"
  296. );
  297. /* public static void main(String[] args) {
  298. itemNames.stream().map(FormulaUtils::parseItemName).forEach(System.out::println);
  299. // itemNames.stream().map(FormulaUtils::checkItemName).forEach(System.out::println);
  300. }*/
  301. public static Object getValue(Cell cell) {
  302. if (cell != null) {
  303. switch (cell.getCellTypeEnum()) {
  304. case STRING:
  305. return cell.getStringCellValue() == null ? null : cell.getStringCellValue().trim();
  306. case NUMERIC:
  307. HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
  308. return dataFormatter.formatCellValue(cell);
  309. case BOOLEAN:
  310. return cell.getBooleanCellValue();
  311. case ERROR:
  312. return cell.getErrorCellValue();
  313. case FORMULA:
  314. try {
  315. return cell.getStringCellValue();
  316. } catch (IllegalStateException e) {
  317. return cell.getNumericCellValue();
  318. }
  319. default:
  320. cell.setCellType(CellType.STRING);
  321. return cell.getStringCellValue() == null ? null : cell.getStringCellValue().trim();
  322. }
  323. }
  324. return null;
  325. }
  326. public static List<ElementData> getElementDataList(String coords,String values){
  327. if(StringUtils.isNotEmpty(coords,values)){
  328. List<Coords> coordsList = Stream.of(coords).flatMap(s -> Arrays.stream(s.split(";"))).map(s -> {
  329. String[] xy = s.split("_");
  330. return new Coords(xy[1], xy[0]);
  331. }).collect(Collectors.toList());
  332. return str2ElementData(values,coordsList,null,null);
  333. }
  334. return Collections.emptyList();
  335. }
  336. public static List<ElementData> str2ElementData(String pg, List<Coords> coordsList ,String code,Integer index){
  337. List<ElementData> eds = new ArrayList<>();
  338. if(StringUtils.isNotEmpty(pg)&&ListUtils.isNotEmpty(coordsList)) {
  339. if(code==null){
  340. code="code";
  341. }
  342. if(index==null){
  343. index=1;
  344. }
  345. String[] val = pg.split("☆");
  346. Map<String, Object> tmpMap = new LinkedHashMap<>();
  347. for (String s : val) {
  348. String[] t = s.split("_\\^_");
  349. String[] c = t[1].split("_");
  350. tmpMap.put(StringUtils.join(code, 0, index, Func.toInt(c[1]), Func.toInt(c[0]), StringPool.AT), t[0]);
  351. }
  352. for (Coords c : coordsList) {
  353. Object data = null;
  354. String key = StringUtils.join(code, 0, index, c.getX(), c.getY(), StringPool.AT);
  355. if (tmpMap.containsKey(key)) {
  356. data = tmpMap.get(key);
  357. }
  358. eds.add(new ElementData(index, 0, data, c.getX(), c.getY()));
  359. }
  360. }
  361. return eds;
  362. }
  363. /**
  364. * @Description Poi 动态执行公式 测试
  365. * @Param [url]
  366. * @Author yangyj
  367. * @Date 2023.05.05 14:28
  368. **/
  369. public static void evaluateFormulaCell(String url) {
  370. try {
  371. url="C:/Users/yangyj/Desktop/test.xlsx";
  372. Workbook workbook = WorkbookFactory.create(new File(url));
  373. Sheet sheet = workbook.getSheetAt(0);
  374. Cell cell = sheet.getRow(0).getCell(0);
  375. FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
  376. CellType cellType = evaluator.evaluateFormulaCellEnum(cell);
  377. if (cellType == CellType.NUMERIC) {
  378. double value = cell.getNumericCellValue();
  379. System.out.println("公式计算结果:" + value);
  380. } else if (cellType == CellType.STRING) {
  381. String value = cell.getStringCellValue();
  382. System.out.println("公式计算结果:" + value);
  383. }
  384. cell.setCellFormula("B1+C1+D1");
  385. evaluator.clearAllCachedResultValues();
  386. cellType = evaluator.evaluateFormulaCellEnum(cell);
  387. if (cellType == CellType.NUMERIC) {
  388. double value = cell.getNumericCellValue();
  389. System.out.println("公式计算结果:" + value);
  390. } else if (cellType == CellType.STRING) {
  391. String value = cell.getStringCellValue();
  392. System.out.println("公式计算结果:" + value);
  393. }
  394. }catch (IOException | InvalidFormatException e){
  395. e.printStackTrace();
  396. }
  397. }
  398. public static Map<String, String> getElementCell(String uri){
  399. return getElementCell(uri,null);
  400. }
  401. public static Map<String, String> getElementCell(String uri,String key) {
  402. try {
  403. InputStream inputStreamByUrl = FileUtils.getInputStreamByUrl(uri);
  404. String filter=" [keyname]";
  405. if(Func.isNotBlank(key)){
  406. filter="[keyname^="+key+"__]";
  407. }
  408. Document document=Jsoup.parse(IoUtil.readToString(inputStreamByUrl));
  409. Map<String,String> result= document
  410. .select("table").first()
  411. .select(filter).stream()
  412. .map(d -> d.attr("keyname")).filter(StringUtils::isNotEmpty).map(e -> e.split("__"))
  413. .collect(
  414. Collectors.toMap(
  415. b -> b[0],
  416. b -> b[1],
  417. (v1, v2) -> v1 + ";" + v2
  418. )
  419. );
  420. if(result.size()>0){
  421. for(Map.Entry<String,String> entry:result.entrySet()){
  422. entry.setValue(FormulaUtils.coordsSorted(entry.getValue()));
  423. }
  424. }
  425. return result;
  426. }catch (Exception e){
  427. e.printStackTrace();
  428. return new HashMap<>();
  429. }
  430. }
  431. public static List<ElementData> setScale(Integer scale, List<ElementData> data){
  432. if(scale==null){
  433. scale=StringUtils.getScale(data.stream().map(ElementData::getValue).filter(StringUtils::isDouble).collect(Collectors.toList()));
  434. }
  435. Integer finalScale = scale;
  436. return data.stream().peek(e->{if(StringUtils.isDouble(e.getValue())){e.setValue(StringUtils.number2StringZero(e.getValue(),finalScale));}}).collect(Collectors.toList());
  437. }
  438. /* public static void main(String[] args) {
  439. Map<String,String> map=getElementCell("/www/wwwroot/Users/hongchuangyanfa/Desktop/privateUrl/1694630551069130752.html","key_22");
  440. System.out.println(map);
  441. }*/
  442. /**
  443. * @Description 定位信息排序
  444. * @Param [coords]
  445. * @return java.lang.String
  446. * @Author yangyj
  447. * @Date 2023.07.11 15:39
  448. **/
  449. public static String coordsSorted(String coords){
  450. if(StringUtils.isNotEmpty(coords)){
  451. List<String> dataList=Arrays.asList(coords.split(";"));
  452. if(dataList.size()>2){
  453. LinkedList<Integer> list=dataList.stream().map(e->e.split("_")[1]).distinct().map(Integer::parseInt).sorted(Comparator.comparingInt(e->e)).collect(Collectors.toCollection(LinkedList::new));
  454. if(list.getLast()-list.getFirst()>list.size()-1){
  455. coords=dataList.stream()
  456. .sorted(Comparator.comparingInt((String str) -> Integer.parseInt(str.split("_")[1]))
  457. .thenComparingInt(str -> Integer.parseInt(str.split("_")[0])))
  458. .collect(Collectors.joining(";"));
  459. }
  460. }
  461. }
  462. return coords;
  463. }
  464. public static String coordsSorted2(String coords){
  465. if(StringUtils.isNotEmpty(coords)){
  466. List<String> dataList=Arrays.asList(coords.split(";"));
  467. if(dataList.size()>2){
  468. /*判断分区:根据行列长度*/
  469. List<Integer> row =dataList.stream().map(e->e.split("_")[0]).distinct().map(Integer::parseInt).collect(Collectors.toList());
  470. List<Integer> column=dataList.stream().map(e->e.split("_")[1]).distinct().map(Integer::parseInt).collect(Collectors.toList());
  471. if(row.size()>=column.size()){
  472. /*纵向*/
  473. if(column.size()>1){
  474. List<List<Integer>> consecutiveGroups = IntStream.range(0, column.size())
  475. .boxed()
  476. .collect(Collectors.collectingAndThen(
  477. Collectors.groupingBy(
  478. i -> i - column.get(i),
  479. LinkedHashMap::new,
  480. Collectors.mapping(column::get, Collectors.toList())
  481. ),
  482. map -> new ArrayList<>(map.values())
  483. ));
  484. }
  485. }
  486. /* 确定区内方向:*/
  487. }
  488. }
  489. return coords;
  490. }
  491. /* public static void main(String[] args) {
  492. List<Integer> column = Arrays.asList(1, 2, 3, 5, 6, 7, 9, 11, 17);
  493. List<List<Integer>> consecutiveGroups = IntStream.range(0, column.size())
  494. .boxed()
  495. .collect(Collectors.collectingAndThen(
  496. Collectors.groupingBy(
  497. i -> i - column.get(i),
  498. LinkedHashMap::new,
  499. Collectors.mapping(column::get, Collectors.toList())
  500. ),
  501. map -> new ArrayList<>(map.values())
  502. ));
  503. AtomicInteger i = new AtomicInteger(column.get(0));
  504. List<List<Integer>> consecutiveGroups2= new ArrayList<>(column.stream().collect(Collectors.groupingBy(e -> e - i.getAndSet(e) > 1, LinkedHashMap::new, Collectors.toList())).values());
  505. System.out.println();
  506. }*/
  507. public static List<Object> slice(List<LocalVariable> local, String formula){
  508. int min =0;
  509. List<Object> result = new ArrayList<>();
  510. try {
  511. pretreatment(local,formula);
  512. List<Object> r= local.stream().map(e-> {
  513. /*所有依赖元素的内容必须非空才进行计算,否则返回空值*/
  514. return e.hasEmptyElementValue()?"": Expression.parse(e.getFormula()).calculate(e.getCurrentMap()).toString();
  515. }).collect(Collectors.toList());
  516. if(CollectionUtil.isNotEmpty(r)&&r.stream().anyMatch(StringUtils::isNotEmpty)){
  517. result.addAll(r);
  518. }
  519. }catch (Exception e){
  520. StaticLog.error("公式:{},执行出错",formula);
  521. }
  522. return result;
  523. }
  524. public static void pretreatment(List<LocalVariable> local,String formula){
  525. formula=StringUtils.removeMultiSpace(formula);
  526. if(formula.contains("LIST")){
  527. Matcher m=RegexUtils.matcher("\\(([^)]*)\\)/LIST",formula);
  528. while (m.find()){
  529. List<String> codes=getCodeList(m.group(1).replaceAll("[+-]",","));
  530. local=local.stream().peek(e->{
  531. @SuppressWarnings("unckecked")
  532. Map<String,Object> map = (Map<String, Object>) e.getCurrentMap().getOrDefault("E",new HashMap<>());
  533. int listSize=(int)codes.stream().filter(c->StringUtils.isNotEmpty(map.get(c))).count();
  534. if(listSize<=0||listSize>codes.size()){
  535. listSize=codes.size();
  536. }
  537. map.put("LIST",listSize);
  538. }).collect(Collectors.toList());
  539. }
  540. }
  541. }
  542. /**从方法参数中获取全部code*/
  543. public static List<String> getCodeList(String param){
  544. List<String> list = new ArrayList<>();
  545. if(StringUtils.isNotEmpty(param)){
  546. Arrays.stream(param.split(",")).forEach(s->{
  547. list.add(s.replaceAll("[E\\[\\]']",""));
  548. });
  549. }
  550. return list;
  551. }
  552. /**从时间段中获取最后一个日期*/
  553. static final String RANGE_DATE_REG="^\\[(\\d{4}[年.\\-]\\d{2}[月.\\-]\\d{2}[日]?),\\s+(\\d{4}[年.\\-]\\d{2}[月.\\-]\\d{2}[日]?)]$";
  554. public static String range2end(String t){
  555. if(t!=null&&Pattern.matches(RANGE_DATE_REG,t)){
  556. t=t.replaceAll("^\\[|]$","").split(",")[1].trim();
  557. }
  558. return t;
  559. }
  560. public static FormData createFormDataFast(String name,String code,String values,String coords){
  561. if(StringUtils.isNotEmpty(code,name)){
  562. if(StringUtils.isNotEmpty(coords)) {
  563. /*定位信息存在才合法*/
  564. List<Coords> coordsList = Stream.of(coords).flatMap(s -> Arrays.stream(s.split(";"))).map(s -> {
  565. String[] xy = s.split("_");
  566. return new Coords(xy[1], xy[0]);
  567. }).collect(Collectors.toList());
  568. List<ElementData> eds = new ArrayList<>();
  569. if (StringUtils.isNotEmpty(values)) {
  570. String[] pages = values.split(";;");
  571. for (int index = 0; index < pages.length; index++) {
  572. String pg = pages[index];
  573. if (Func.isNotBlank(pg)) {
  574. String[] val = pg.split("☆");
  575. Map<String, Object> tmpMap = new LinkedHashMap<>();
  576. for (String s : val) {
  577. String[] t = s.split("_\\^_");
  578. String[] c = t[1].split("_");
  579. tmpMap.put(StringUtils.join(code, 0, index, Func.toInt(c[1]), Func.toInt(c[0]), StringPool.AT), t[0]);
  580. }
  581. for (Coords c : coordsList) {
  582. Object data = null;
  583. String key = StringUtils.join(code, 0, index, c.getX(), c.getY(), StringPool.AT);
  584. if (tmpMap.containsKey(key)) {
  585. data = tmpMap.get(key);
  586. }
  587. eds.add(new ElementData(index, 0, data, c.getX(), c.getY()));
  588. }
  589. }
  590. }
  591. } else {
  592. eds = coordsList.stream().map(c -> new ElementData(0, 0, null, c.getX(), c.getY())).collect(Collectors.toList());
  593. }
  594. FormData one = new FormData(code, eds, null, coords);
  595. one.setEName(name);
  596. /*备份原始数据,用于更新比较*/
  597. one.init();
  598. return one;
  599. }
  600. }
  601. return null;
  602. }
  603. public static void mainT(String[] args) throws IOException {
  604. XYSeries series = new XYSeries("Data Series");
  605. series.add(10.2, 1.82);
  606. series.add(11.9, 1.86);
  607. series.add(15.9, 1.87);
  608. series.add(19.3, 1.85);
  609. series.add(20.3, 1.80);
  610. XYSeriesCollection dataset = new XYSeriesCollection();
  611. dataset.addSeries(series);
  612. JFreeChart chart = ChartFactory.createXYLineChart(
  613. "测试散点图", // 标题
  614. "X", // 横轴标题
  615. "Y", // 纵轴标题
  616. dataset, // 数据集
  617. PlotOrientation.VERTICAL, // 图表方向
  618. true, // 是否显示图例
  619. false, // 是否生成工具提示
  620. false // 是否生成URL链接
  621. );
  622. // 设置字体
  623. Font titleFont = new Font("SimSun", Font.PLAIN, 18); // 指定使用宋体字体
  624. Font axisFont = new Font("SimSun", Font.PLAIN, 12); // 指定使用宋体字体
  625. // 设置标题字体
  626. TextTitle title = chart.getTitle();
  627. title.setFont(titleFont);
  628. XYPlot plot = (XYPlot) chart.getPlot();
  629. XYSplineRenderer renderer = new XYSplineRenderer();
  630. plot.setRenderer(renderer);
  631. plot.setBackgroundPaint(Color.WHITE);
  632. // Set the line stroke and shape for the renderer
  633. renderer.setSeriesStroke(0, new BasicStroke(2.0f));
  634. Shape circle = new Ellipse2D.Double(-3, -3, 6, 6);
  635. renderer.setSeriesShape(0, circle);
  636. renderer.setSeriesPaint(0, Color.BLUE);
  637. // 自定义 X 轴刻度
  638. NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
  639. domainAxis.setTickUnit(new NumberTickUnit(5)); // 设置刻度间隔
  640. domainAxis.setRange(0.0, 25); // 设置轴的范围
  641. // 自定义 Y 轴刻度
  642. NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
  643. rangeAxis.setTickUnit(new NumberTickUnit(0.01)); // 设置刻度间隔
  644. rangeAxis.setRange(1.79, 1.90); // 设置轴的范围
  645. // 添加横杠
  646. for(int i=175;i<190;i++){
  647. ValueMarker marker = new ValueMarker((double) i /100);
  648. marker.setPaint(Color.BLUE); // 横杠的颜色
  649. plot.addRangeMarker(marker);
  650. }
  651. ChartPanel chartPanel = new ChartPanel(chart);
  652. chartPanel.setPreferredSize(new Dimension(500, 400));
  653. // 保存图表为图片
  654. int width = 800;
  655. int height = 600;
  656. ChartUtils.saveChartAsPNG(new File("C:/Users/yangyj/Desktop/Swap_space/poi_statistics.png"), chart, width, height);
  657. }
  658. /**字符串sha256映射*/
  659. public static String sha256(String input) {
  660. try {
  661. MessageDigest digest = MessageDigest.getInstance("SHA-256");
  662. byte[] encodedHash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
  663. StringBuilder hexString = new StringBuilder();
  664. for (byte b : encodedHash) {
  665. String hex = Integer.toHexString(0xff & b);
  666. if (hex.length() == 1) {
  667. hexString.append('0');
  668. }
  669. hexString.append(hex);
  670. }
  671. return hexString.toString();
  672. } catch (NoSuchAlgorithmException e) {
  673. e.printStackTrace();
  674. }
  675. return "";
  676. }
  677. /**根据步长获取字符*/
  678. public static String getEveryNthChar(String input, int step) {
  679. StringBuilder result = new StringBuilder();
  680. for (int i = 0; i < input.length(); i += step) {
  681. result.append(input.charAt(i));
  682. }
  683. return result.toString();
  684. }
  685. }