FormulaUtils.java 33 KB

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