FormulaUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package com.mixsmart.utils;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.springblade.core.tool.utils.Func;
  5. import org.springblade.manager.bean.TableInfo;
  6. import org.springblade.manager.dto.Coords;
  7. import org.springblade.manager.dto.ElementData;
  8. import org.springblade.manager.dto.FormData;
  9. import org.springblade.manager.entity.Formula;
  10. import java.math.BigDecimal;
  11. import java.util.*;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. import java.util.stream.Collectors;
  15. /**
  16. * @author yangyj
  17. * @Date 2022/7/14 15:55
  18. * @description TODO
  19. */
  20. public class FormulaUtils {
  21. public static Map<String,Object> triangleSquare(Object ranges){
  22. Map<String,Object> map =new HashMap<>();
  23. if(StringUtils.isEmpty(ranges)){
  24. //z的默认取值范围
  25. ranges="(0,15)";
  26. }
  27. Matcher m = RegexUtils.matcher("(\\-?\\d+)(\\D)(\\+?\\d+)",ranges.toString());
  28. if(m.find()) {
  29. System.out.println();
  30. int min = StringUtils.handObj2Integer(m.group(1));
  31. int max = StringUtils.handObj2Integer(m.group(3));
  32. Integer[] r = pythagorean(min, max);
  33. map.put("X", String.valueOf(r[0]));
  34. map.put("Y", String.valueOf(r[1]));
  35. map.put("Z", String.valueOf(r[2]));
  36. }
  37. return map;
  38. }
  39. /**
  40. * result[0]^2+result[1]^2=result[2]^2 result[] 元素均为正整数
  41. */
  42. public static Integer[] pythagorean(Integer min,Integer max){
  43. Integer[] result = null;
  44. List<Integer[]> list = new ArrayList<>();
  45. for(int i=1;i<=max;i++){
  46. for(int j=1;j<=max;j++){
  47. double tmp = Math.sqrt(Math.pow(i,2)+Math.pow(j,2));
  48. int z= (int) Math.round(tmp);
  49. if(min<z&&z<=max){
  50. Integer[] arr = new Integer[]{ i,j,z};
  51. list.add(arr);
  52. }
  53. }
  54. }
  55. if(ListUtils.isNotEmpty(list)){
  56. Random rm = new Random();
  57. result = list.get(rm.nextInt(list.size()));
  58. }
  59. return result;
  60. }
  61. public static void write(FormData fd, Object data,Boolean nullOrBlank ){
  62. if(Func.isEmpty(fd.getValues())){
  63. /*无定位信息不写入*/
  64. return;
  65. }
  66. /*写入前清空内容*/
  67. fd.getValues().forEach(t->t.setValue(null));
  68. if(data instanceof List){
  69. List<Object> values = (List<Object>) data;
  70. if(!nullOrBlank){
  71. values=values.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
  72. }
  73. if(values.size()>fd.getValues().size()){
  74. /*当生成的数据超过实际容量的时候,会自动追加页数*/
  75. if(fd.getCoordsList().size()==1){
  76. if(values.stream().filter(CustomFunction::containsZH).anyMatch(e->e.toString().contains("\n"))){
  77. fd.getValues().get(0).setValue(values.stream().filter(Objects::nonNull).map(Object::toString).collect(Collectors.joining()));
  78. }else{
  79. fd.getValues().get(0).setValue(values.stream().map(StringUtils::handleNull).collect(Collectors.joining("、")));
  80. }
  81. }else{
  82. // copy(fd,values);
  83. for(int n=0;n<fd.getValues().size();n++){
  84. fd.getValues().get(n).setValue(values.get(n));
  85. }
  86. List<Object> overList=values.stream().skip(fd.getValues().size()).collect(Collectors.toList());
  87. List<Coords> coordsList = fd.getCoordsList();
  88. int addPage=(int)Math.ceil((double)overList.size()/(double)coordsList.size());
  89. fd.setAddPages(addPage);
  90. ElementData last =fd.getValues().get(fd.getValues().size()-1);
  91. int indexBase=last.getIndex()+1;
  92. List<ElementData> addList= new ArrayList<>();
  93. for(int i=0;i<addPage;i++){
  94. for(int j=0;j<coordsList.size();j++){
  95. /*超页就尽管写进去,格式化阶段再加表*/
  96. Coords coords = coordsList.get(j);
  97. Object v=null;
  98. int st=i*coordsList.size()+j;
  99. if(st<overList.size()){
  100. v= overList.get(st);
  101. }
  102. addList.add(new ElementData(indexBase+i,last.getGroupId(),v,coords.getX(),coords.getY()));
  103. }
  104. }
  105. fd.getValues().addAll(addList);
  106. }
  107. }else{
  108. for(int n=0;n<values.size();n++){
  109. fd.getValues().get(n).setValue(values.get(n));
  110. }
  111. }
  112. }else{
  113. if(Formula.FULL.equals(fd.getFormula().getOutm())){
  114. /*填充策略*/
  115. fd.getValues().forEach(e->e.setValue(data));
  116. }else{
  117. fd.getValues().get(0).setValue(data);
  118. }
  119. }
  120. }
  121. public static List<TableInfo> getTableInfoList(JSONArray dataArray) {
  122. if (dataArray != null && !dataArray.isEmpty()) {
  123. List<TableInfo> result = new ArrayList<>();
  124. for (int m = 0; m < dataArray.size(); m++) {
  125. TableInfo tableInfo = new TableInfo();
  126. JSONObject dataInfo2 = dataArray.getJSONObject(m);
  127. //
  128. tableInfo.setContractId(dataInfo2.getString("contractId"));
  129. tableInfo.setPkeyId(dataInfo2.getString("pkeyId"));
  130. tableInfo.setProjectId(dataInfo2.getString("projectId"));
  131. //huangjn 填报的类型,施工或监理
  132. tableInfo.setClassify(dataInfo2.getString("classify"));
  133. //设置首件信息
  134. setFirstData(dataInfo2, tableInfo);
  135. // //设置日志信息
  136. setTheLogData(dataInfo2, tableInfo);
  137. dataInfo2.fluentRemove("contractId")
  138. .fluentRemove("pkeyId")
  139. .fluentRemove("p_key_id")
  140. .fluentRemove("projectId")
  141. .fluentRemove("classify")
  142. .fluentRemove("pickerKey")
  143. .fluentRemove("id")
  144. .fluentRemove("isFirst")
  145. .fluentRemove("firstNodeId")
  146. .fluentRemove("isTheLog")
  147. .fluentRemove("theLogId")
  148. .fluentRemove("linkTabIds")
  149. .fluentRemove("recordTime")
  150. .fluentRemove("businessId")
  151. .fluentRemove("sourceUrl")
  152. .fluentRemove("pdfUrl")
  153. .fluentRemove("firstFileName")
  154. .fluentRemove("");
  155. // 计算数据
  156. LinkedHashMap<String, List<String>> dataMap = dataInfo2.keySet().stream().filter(e -> e.contains("__")).collect(Collectors.groupingBy(e -> e.split("__")[0], LinkedHashMap<String, List<String>>::new, Collectors.toList()));
  157. LinkedHashMap<String, String> dataMap2 = new LinkedHashMap<>();
  158. // 字段组合
  159. for (String k : dataMap.keySet()) {
  160. if (dataMap.get(k).size() > 1 && !dataMap.get(k).contains("000Z")) {
  161. String[] ziduan = dataMap.get(k).toArray(new String[]{});
  162. String temp = "";
  163. for (int i = 0; i < ziduan.length - 1; i++) {
  164. for (int j = 0; j < ziduan.length - i - 1; j++) {
  165. Integer tr = Integer.parseInt((ziduan[j].split("__")[1]).split("_")[0]);
  166. Integer td = Integer.parseInt(ziduan[j].split("__")[1].split("_")[1]);
  167. Integer tr_1 = Integer.parseInt(ziduan[j + 1].split("__")[1].split("_")[0]);
  168. Integer td_1 = Integer.parseInt(ziduan[j + 1].split("__")[1].split("_")[1]);
  169. if (tr > tr_1 && td == td_1) { //纵向排序
  170. temp = ziduan[j];
  171. ziduan[j] = ziduan[j + 1];
  172. ziduan[j + 1] = temp;
  173. }
  174. }
  175. }
  176. String lastStr = dataInfo2.getString(ziduan[0]) + "_^_" + ziduan[0].split("__")[1];
  177. for (int i = 1; i < ziduan.length; i++) {
  178. String keyData = dataInfo2.getString(ziduan[i]);
  179. if (!keyData.equals("")) {
  180. lastStr += "☆" + dataInfo2.getString(ziduan[i]) + "_^_" + ziduan[i].split("__")[1];
  181. }
  182. }
  183. dataMap2.put(k, lastStr);
  184. } else {
  185. String dataVal = dataInfo2.getString(dataMap.get(k).get(0));
  186. dataMap2.put(k, dataVal + "_^_" + dataMap.get(k).get(0).split("__")[1]);
  187. }
  188. }
  189. dataMap2.put("p_key_id", tableInfo.getPkeyId());
  190. tableInfo.setDataMap(dataMap2);
  191. result.add(tableInfo);
  192. }
  193. return result;
  194. }
  195. return null;
  196. }
  197. public static void setFirstData(JSONObject dataInfo2, TableInfo tableInfo) {
  198. //huangjn 判断是否是首件
  199. if (dataInfo2.containsKey("isFirst")) {
  200. tableInfo.setIsFirst(dataInfo2.getString("isFirst"));
  201. }
  202. //huangjn 判断是否是首件
  203. //首件资料绑定的节点
  204. if (dataInfo2.containsKey("firstNodeId")) {
  205. tableInfo.setFirstNodeId(dataInfo2.getString("firstNodeId"));
  206. }
  207. //首件ID(编辑时有值,新增时为空)
  208. if (dataInfo2.containsKey("firstId")) {
  209. tableInfo.setFirstId(dataInfo2.getString("firstId"));
  210. }
  211. //源文件
  212. if (dataInfo2.containsKey("sourceUrl")) {
  213. tableInfo.setSourceUrl(dataInfo2.getString("sourceUrl"));
  214. }
  215. //pdfUrl
  216. if (dataInfo2.containsKey("pdfUrl")) {
  217. tableInfo.setPdfUrl(dataInfo2.getString("pdfUrl"));
  218. }
  219. //文件名称
  220. if (dataInfo2.containsKey("firstFileName")) {
  221. tableInfo.setFirstFileName(dataInfo2.getString("firstFileName"));
  222. }
  223. //关联的信息
  224. if (dataInfo2.containsKey("linkProcessList")) {
  225. tableInfo.setLinkProcessList(dataInfo2.getJSONArray("linkProcessList"));
  226. }
  227. }
  228. /**
  229. * 设置日志信息
  230. */
  231. public static void setTheLogData(JSONObject dataInfo2, TableInfo tableInfo) {
  232. //huangjn 判断是否是日志
  233. if (dataInfo2.containsKey("isTheLog")) {
  234. tableInfo.setIsTheLog(dataInfo2.getString("isTheLog"));
  235. }
  236. //huangjn 判断是否是日志
  237. //huangjn 日志ID
  238. if (dataInfo2.containsKey("theLogId")) {
  239. tableInfo.setTheLogId(dataInfo2.getString("theLogId"));
  240. }
  241. //huangjn 日志ID
  242. //huangjn 日志勾选的工序
  243. if (dataInfo2.containsKey("linkTabIds")) {
  244. tableInfo.setLinkTabIds(dataInfo2.getJSONArray("linkTabIds"));
  245. }
  246. //huangjn 日志勾选的工序
  247. //huangjn 日志所选时间
  248. if (dataInfo2.containsKey("recordTime")) {
  249. tableInfo.setRecordTime(dataInfo2.getString("recordTime"));
  250. }
  251. //huangjn 日志所选时间
  252. //huangjn 每份填报数据的id,目前日志专用
  253. if (dataInfo2.containsKey("id")) {
  254. tableInfo.setBusinessId(dataInfo2.getString("id"));
  255. }
  256. //huangjn 每份填报数据的id,目前日志专用
  257. }
  258. /*从元素名称中解析项目名称*/
  259. public static String parseItemName(String eName){
  260. String[] candidate= StringUtils.handleNull(eName).replaceAll("^[^\\u4e00-\\u9fa5]+","").replaceAll("\\s+","").split("[((].+[))]|_");
  261. if(candidate.length>0){
  262. return Arrays.stream(candidate).filter(e->!e.contains("实测项目")&&!e.contains("△")).findFirst().orElse(eName);
  263. }
  264. return eName;
  265. }
  266. // public static void main(String[] args) {
  267. // System.out.println(parseItemName("实 测 项 目_3△_内轮廓高度(mm)_不小于设计值_实测值或实测偏差值"));
  268. // }
  269. }