FlowJsonUtil.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. package jnpf.flowable.util;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.google.common.collect.ImmutableList;
  5. import jnpf.base.UserInfo;
  6. import jnpf.emnus.SearchMethodEnum;
  7. import jnpf.flowable.entity.RecordEntity;
  8. import jnpf.flowable.entity.TaskEntity;
  9. import jnpf.flowable.enums.FieldEnum;
  10. import jnpf.flowable.enums.NodeEnum;
  11. import jnpf.flowable.model.task.FlowMethod;
  12. import jnpf.flowable.model.task.FlowModel;
  13. import jnpf.flowable.model.templatejson.FlowParamModel;
  14. import jnpf.flowable.model.templatenode.nodejson.GroupsModel;
  15. import jnpf.flowable.model.templatenode.nodejson.NodeModel;
  16. import jnpf.flowable.model.templatenode.nodejson.ProperCond;
  17. import jnpf.flowable.model.templatenode.nodejson.TemplateJsonModel;
  18. import jnpf.permission.entity.UserEntity;
  19. import jnpf.util.JsonUtil;
  20. import jnpf.util.StringUtil;
  21. import jnpf.util.visiual.JnpfKeyConsts;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.apache.commons.text.StringSubstitutor;
  24. import javax.script.ScriptEngine;
  25. import javax.script.ScriptEngineManager;
  26. import java.math.BigDecimal;
  27. import java.math.RoundingMode;
  28. import java.text.NumberFormat;
  29. import java.util.*;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;
  32. /**
  33. * 类的描述
  34. *
  35. * @author JNPF@YinMai Info. Co., Ltd
  36. * @version 5.0.x
  37. * @since 2024/4/18 20:22
  38. */
  39. public class FlowJsonUtil {
  40. /**
  41. * 节点条件判断
  42. **/
  43. public static boolean nodeConditionDecide(FlowMethod flowMethod) {
  44. List<ProperCond> conditionList = flowMethod.getConditions();
  45. String matchLogic = flowMethod.getMatchLogic();
  46. boolean flag = false;
  47. ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
  48. ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("js");
  49. Map<String, Object> map = flowMethod.getFormData();
  50. List<String> expressionAll = new ArrayList<>();
  51. StringBuilder condition = new StringBuilder();
  52. for (int k = 0; k < conditionList.size(); k++) {
  53. StringBuilder expression = new StringBuilder();
  54. expression.append("(");
  55. ProperCond properCond = conditionList.get(k);
  56. String logic = properCond.getLogic();
  57. List<GroupsModel> groups = properCond.getGroups();
  58. for (int i = 0; i < groups.size(); i++) {
  59. GroupsModel groupsModel = groups.get(i);
  60. String contain = "!=-1";
  61. String field = groupsModel.getField();
  62. String jnpfKey = groupsModel.getJnpfKey();
  63. int fieldType = groupsModel.getFieldType();
  64. String symbol = groupsModel.getSymbol();
  65. boolean inLike = "like".equals(symbol) || "notLike".equals(symbol);
  66. boolean includes = "in".equals(symbol) || "notIn".equals(symbol);
  67. Object form = Objects.equals(FieldEnum.Field.getCode(), fieldType) ? formValue(flowMethod, jnpfKey, map.get(field)) : formula(groupsModel, map);
  68. Object formValue = form;
  69. if ("<>".equals(symbol)) {
  70. symbol = "!=";
  71. }
  72. int fieldValueType = groupsModel.getFieldValueType();
  73. String valueJnpfKey = StringUtil.isNotEmpty(groupsModel.getFieldValueJnpfKey()) ? groupsModel.getFieldValueJnpfKey() : jnpfKey;
  74. Object filedData = groupsModel.getFieldValue();
  75. List<Integer> valueType = ImmutableList.of(FieldEnum.Condition.getCode(), FieldEnum.System.getCode());
  76. Object value = null;
  77. List<Object> includeValue = new ArrayList<>();
  78. if (valueType.contains(fieldValueType)) {
  79. TemplateJsonModel templateJsonModel = new TemplateJsonModel();
  80. templateJsonModel.setField(groupsModel.getField());
  81. templateJsonModel.setSourceType(Objects.equals(FieldEnum.Condition.getCode(), fieldValueType) ? FieldEnum.System.getCode() : FieldEnum.Field.getCode());
  82. templateJsonModel.setMsgTemplateId(groupsModel.getField());
  83. templateJsonModel.setRelationField(String.valueOf(groupsModel.getFieldValue()));
  84. MsgUtil msgUtil = new MsgUtil();
  85. TaskEntity taskEntity = flowMethod.getTaskEntity();
  86. Map<String, Object> mapData = new HashMap<>();
  87. if (null != taskEntity) {
  88. if (StringUtil.isNotEmpty(taskEntity.getGlobalParameter())) {
  89. mapData = JsonUtil.stringToMap(taskEntity.getGlobalParameter());
  90. }
  91. }
  92. if (CollectionUtil.isEmpty(mapData)) {
  93. NodeModel global = flowMethod.getNodes().get(NodeEnum.global.getType());
  94. if (null != global) {
  95. List<FlowParamModel> paramModelList = global.getGlobalParameterList();
  96. for (FlowParamModel model : paramModelList) {
  97. mapData.put(model.getFieldName(), model.getDefaultValue());
  98. }
  99. }
  100. }
  101. List<TemplateJsonModel> list = new ArrayList<>();
  102. list.add(templateJsonModel);
  103. RecordEntity record = new RecordEntity();
  104. record.setNodeCode(flowMethod.getNodeCode());
  105. UserEntity createUser = flowMethod.getCreateUser();
  106. UserEntity delegate = flowMethod.getDelegate();
  107. FlowModel parameterModel = new FlowModel();
  108. parameterModel.setFormData(mapData);
  109. parameterModel.setRecordEntity(record);
  110. parameterModel.setTaskEntity(taskEntity);
  111. Map<String, String> resMap = msgUtil.parameterMap(parameterModel, list, createUser, delegate);
  112. if (includes) {
  113. if (filedData instanceof List) {
  114. List<String> filedList = (List<String>) filedData;
  115. for (String id : filedList) {
  116. String fieldValue = resMap.get(id) != null ? resMap.get(id) : "";
  117. includeValue.add("'" + fieldValue + "'");
  118. }
  119. }
  120. } else {
  121. value = resMap.get(filedData);
  122. if (value != null) {
  123. value = "'" + value + "'";
  124. }
  125. }
  126. } else {
  127. if (includes) {
  128. if (filedData instanceof List) {
  129. List<String> filedList = (List<String>) filedData;
  130. for (String id : filedList) {
  131. if (Objects.equals(FieldEnum.Custom.getCode(), fieldValueType)) {
  132. filedValue(flowMethod, id, valueJnpfKey, form, includeValue);
  133. } else {
  134. filedData(flowMethod, id, valueJnpfKey, form, includeValue);
  135. }
  136. }
  137. }
  138. } else {
  139. value = Objects.equals(FieldEnum.Custom.getCode(), fieldValueType) ? filedValue(flowMethod, filedData, valueJnpfKey, form, includeValue) : filedData(flowMethod, filedData, valueJnpfKey, form, includeValue);
  140. }
  141. }
  142. Object fieldValue = value;
  143. String pression = formValue + symbol + fieldValue;
  144. // 比较的处理
  145. if ("<=".equals(symbol) || "<".equals(symbol) || ">".equals(symbol) || ">=".equals(symbol)) {
  146. try {
  147. String formValueStr = formValue.toString();
  148. if (formValueStr.startsWith("'") && formValueStr.endsWith("'")) {
  149. formValueStr = formValueStr.substring(1, formValueStr.length() - 1);
  150. }
  151. fieldValue = fieldValue == null ? "" : fieldValue;
  152. String fieldValueStr = fieldValue.toString();
  153. if (fieldValueStr.startsWith("'") && fieldValueStr.endsWith("'")) {
  154. fieldValueStr = fieldValueStr.substring(1, fieldValueStr.length() - 1);
  155. }
  156. BigDecimal a = new BigDecimal(formValueStr);
  157. BigDecimal b = new BigDecimal(fieldValueStr);
  158. boolean res = false;
  159. if ("<=".equals(symbol)) {
  160. res = a.compareTo(b) <= 0;
  161. } else if ("<".equals(symbol)) {
  162. res = a.compareTo(b) < 0;
  163. } else if (">".equals(symbol)) {
  164. res = a.compareTo(b) > 0;
  165. } else if (">=".equals(symbol)) {
  166. res = a.compareTo(b) >= 0;
  167. }
  168. pression = res + "";
  169. } catch (Exception e) {
  170. System.out.println(e.getMessage());
  171. pression = "false";
  172. }
  173. }
  174. if (inLike) {
  175. if ("notLike".equals(symbol)) {
  176. contain = "==-1";
  177. }
  178. symbol = ".indexOf";
  179. if (!(formValue instanceof CharSequence)) {
  180. formValue = "'" + formValue + "'";
  181. }
  182. if (!(fieldValue instanceof CharSequence)) {
  183. fieldValue = "'" + fieldValue + "'";
  184. }
  185. pression = formValue + ".toString()" + symbol + "(" + fieldValue + ")" + contain;
  186. }
  187. if (includes) {
  188. try {
  189. boolean isNotIn = "notIn".equals(symbol);
  190. String searchModel = isNotIn ? " && " : " || ";
  191. symbol = !isNotIn ? " == " : " != ";
  192. if (!(formValue instanceof CharSequence)) {
  193. formValue = "'" + formValue + "'";
  194. }
  195. StringBuilder json = new StringBuilder();
  196. json.append("(");
  197. for (int s = 0; s < includeValue.size(); s++) {
  198. Object valuse = includeValue.get(s);
  199. if (!(valuse instanceof CharSequence)) {
  200. valuse = "'" + valuse + "'";
  201. }
  202. json.append(formValue + symbol + valuse);
  203. if (s != includeValue.size() - 1) {
  204. json.append(searchModel);
  205. }
  206. }
  207. json.append(")");
  208. pression = includeValue.isEmpty() ? "false" : json.toString();
  209. } catch (Exception e) {
  210. System.out.println(e.getMessage());
  211. pression = "false";
  212. }
  213. }
  214. if (ObjectUtil.equals(symbol, "null")) {
  215. pression = "(" + formValue + " == null || " + formValue + " == '')";
  216. }
  217. if (ObjectUtil.equals(symbol, "notNull")) {
  218. pression = "(" + formValue + " != null && " + formValue + " != '')";
  219. }
  220. expression.append(pression);
  221. if (!StringUtils.isEmpty(logic) && i != groups.size() - 1) {
  222. expression.append(" " + search(logic) + " ");
  223. }
  224. }
  225. expression.append(")");
  226. expressionAll.add(expression.toString());
  227. }
  228. for (int i = 0; i < expressionAll.size(); i++) {
  229. String script = expressionAll.get(i);
  230. String search = i != expressionAll.size() - 1 ? search(matchLogic) : "";
  231. condition.append(script + " " + search + " ");
  232. }
  233. try {
  234. flag = (Boolean) scriptEngine.eval(condition.toString());
  235. } catch (Exception e) {
  236. System.out.println(e.getMessage());
  237. }
  238. return flag;
  239. }
  240. /**
  241. * 条件表达式
  242. *
  243. * @param logic
  244. */
  245. private static String search(String logic) {
  246. return SearchMethodEnum.And.getSymbol().equalsIgnoreCase(logic) ? "&&" : "||";
  247. }
  248. /**
  249. * 条件数据修改
  250. *
  251. * @param flowMethod
  252. * @param value
  253. */
  254. private static Object filedValue(FlowMethod flowMethod, Object value, String jnpfKey, Object form, List<Object> includeValue) {
  255. UserInfo userInfo = flowMethod.getUserInfo();
  256. if ("currentUser".equals(value)) {
  257. value = userInfo.getUserId();
  258. }
  259. try {
  260. try {
  261. List<List<String>> dataAll = JsonUtil.getJsonToBean(value, List.class);
  262. List<String> id = new ArrayList<>();
  263. for (List<String> data : dataAll) {
  264. id.addAll(data);
  265. }
  266. value = String.join(",", id);
  267. } catch (Exception e) {
  268. try {
  269. List<String> id = new ArrayList<>();
  270. List<String> dataAll = JsonUtil.getJsonToList(value, String.class);
  271. if (JnpfKeyConsts.CURRORGANIZE.equals(jnpfKey)) {
  272. value = dataAll.stream().filter(t -> ("'" + t + "'").equals(form)).findFirst().orElse(null);
  273. } else {
  274. for (String data : dataAll) {
  275. id.add(data);
  276. }
  277. value = String.join(",", id);
  278. }
  279. } catch (Exception e1) {
  280. }
  281. }
  282. } catch (Exception e) {
  283. }
  284. if (value instanceof CharSequence) {
  285. value = "'" + value + "'";
  286. }
  287. includeValue.add(value);
  288. return value;
  289. }
  290. /**
  291. * 条件数据修改
  292. *
  293. * @param flowMethod
  294. * @param value
  295. */
  296. private static Object filedData(FlowMethod flowMethod, Object value, String jnpfKey, Object form, List<Object> includeValue) {
  297. Map<String, Object> map = flowMethod.getFormData();
  298. value = map.get(value);
  299. UserEntity userEntity = flowMethod.getUserEntity();
  300. TaskEntity taskEntity = flowMethod.getTaskEntity();
  301. try {
  302. try {
  303. List<List<String>> dataAll = JsonUtil.getJsonToBean(value, List.class);
  304. List<String> id = new ArrayList<>();
  305. for (List<String> data : dataAll) {
  306. id.addAll(data);
  307. }
  308. value = String.join(",", id);
  309. } catch (Exception e) {
  310. try {
  311. List<String> id = new ArrayList<>();
  312. List<String> dataAll = JsonUtil.getJsonToList(value, String.class);
  313. if (JnpfKeyConsts.CURRORGANIZE.equals(jnpfKey) || JnpfKeyConsts.COMSELECT.equals(jnpfKey)) {
  314. value = dataAll.stream().filter(t -> ("'" + t + "'").equals(form)).findFirst().orElse(null);
  315. } else {
  316. for (String data : dataAll) {
  317. id.add(data);
  318. }
  319. value = String.join(",", id);
  320. }
  321. } catch (Exception e1) {
  322. }
  323. }
  324. if (JnpfKeyConsts.CREATETIME.equals(jnpfKey)) {
  325. Date creatorTime = taskEntity.getCreatorTime();
  326. value = null == creatorTime ? null : creatorTime.getTime();
  327. } else if (JnpfKeyConsts.CREATEUSER.equals(jnpfKey)) {
  328. value = taskEntity.getCreatorUserId();
  329. } else if (JnpfKeyConsts.CURRORGANIZE.equals(jnpfKey)) {
  330. value = userEntity.getOrganizeId();
  331. } else if (JnpfKeyConsts.CURRPOSITION.equals(jnpfKey)) {
  332. value = userEntity.getPositionId();
  333. } else if (JnpfKeyConsts.MODIFYTIME.equals(jnpfKey)) {
  334. Date lastModifyTime = taskEntity.getLastModifyTime();
  335. value = null == lastModifyTime ? null : lastModifyTime.getTime();
  336. } else if (JnpfKeyConsts.MODIFYUSER.equals(jnpfKey)) {
  337. value = taskEntity.getLastModifyUserId();
  338. }
  339. } catch (Exception e) {
  340. }
  341. if (value instanceof CharSequence) {
  342. value = "'" + value + "'";
  343. }
  344. includeValue.add(value);
  345. return value;
  346. }
  347. /**
  348. * 表单数据修改
  349. *
  350. * @param form
  351. */
  352. private static Object formValue(FlowMethod flowMethod, String jnpfKey, Object form) {
  353. Object result = form;
  354. UserEntity userEntity = flowMethod.getUserEntity();
  355. TaskEntity flowTaskEntity = flowMethod.getTaskEntity();
  356. try {
  357. try {
  358. List<List<String>> dataAll = JsonUtil.getJsonToBean(form, List.class);
  359. List<String> id = new ArrayList<>();
  360. for (List<String> data : dataAll) {
  361. id.addAll(data);
  362. }
  363. result = String.join(",", id);
  364. } catch (Exception e) {
  365. try {
  366. List<String> id = new ArrayList<>();
  367. List<String> dataAll = JsonUtil.getJsonToList(form, String.class);
  368. for (String data : dataAll) {
  369. id.add(data);
  370. }
  371. result = String.join(",", id);
  372. } catch (Exception e1) {
  373. }
  374. }
  375. if (JnpfKeyConsts.CREATETIME.equals(jnpfKey)) {
  376. Date creatorTime = flowTaskEntity.getCreatorTime();
  377. result = null == creatorTime ? null : creatorTime.getTime();
  378. } else if (JnpfKeyConsts.CREATEUSER.equals(jnpfKey)) {
  379. result = StringUtil.isNotEmpty(flowTaskEntity.getDelegateUserId()) ? flowTaskEntity.getDelegateUserId() : flowTaskEntity.getCreatorUserId();
  380. } else if (JnpfKeyConsts.CURRORGANIZE.equals(jnpfKey)) {
  381. result = userEntity.getOrganizeId();
  382. } else if (JnpfKeyConsts.CURRPOSITION.equals(jnpfKey)) {
  383. result = userEntity.getPositionId();
  384. } else if (JnpfKeyConsts.MODIFYTIME.equals(jnpfKey)) {
  385. Date lastModifyTime = flowTaskEntity.getLastModifyTime();
  386. result = null == lastModifyTime ? null : lastModifyTime.getTime();
  387. } else if (JnpfKeyConsts.MODIFYUSER.equals(jnpfKey)) {
  388. result = flowTaskEntity.getLastModifyUserId();
  389. }
  390. } catch (Exception e) {
  391. }
  392. if (result instanceof CharSequence) {
  393. result = "'" + result + "'";
  394. }
  395. return result;
  396. }
  397. /**
  398. * 表达式
  399. */
  400. private static Object formula(GroupsModel properCond, Map<String, Object> data) {
  401. String result = null;
  402. try {
  403. StringBuilder builder = new StringBuilder();
  404. builder.append("function getNum(val) {\n" +
  405. " return isNaN(val) ? 0 : Number(val)\n" +
  406. "};\n" +
  407. "// 求和\n" +
  408. "function SUM() {\n" +
  409. " var value = 0\n" +
  410. " for (var i = 0; i < arguments.length; i++) {\n" +
  411. " value += getNum(arguments[i])\n" +
  412. " }\n" +
  413. " return value\n" +
  414. "};\n" +
  415. "// 求差\n" +
  416. "function SUBTRACT(num1, num2) {\n" +
  417. " return getNum(num1) - getNum(num2)\n" +
  418. "};\n" +
  419. "// 相乘\n" +
  420. "function PRODUCT() {\n" +
  421. " var value = 1\n" +
  422. " for (var i = 0; i < arguments.length; i++) {\n" +
  423. " value = value * getNum(arguments[i])\n" +
  424. " }\n" +
  425. " return value\n" +
  426. "};\n" +
  427. "// 相除\n" +
  428. "function DIVIDE(num1, num2) {\n" +
  429. " return getNum(num1) / (getNum(num2) === 0 ? 1 : getNum(num2))\n" +
  430. "};\n" +
  431. "// 获取参数的数量\n" +
  432. "function COUNT() {\n" +
  433. " var value = 0\n" +
  434. " for (var i = 0; i < arguments.length; i++) {\n" +
  435. " value ++\n" +
  436. " }\n" +
  437. " return value\n" +
  438. "};\n");
  439. String field = field(properCond.getField(), data, null);
  440. ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
  441. ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("js");
  442. String eval = builder + " var result = " + field + ";";
  443. scriptEngine.eval(eval);
  444. double d = (double) scriptEngine.get("result");
  445. NumberFormat nf = NumberFormat.getNumberInstance();
  446. nf.setRoundingMode(RoundingMode.UP);
  447. result = nf.format(d);
  448. } catch (Exception e) {
  449. System.out.println(e.getMessage());
  450. }
  451. return result;
  452. }
  453. /**
  454. * 替换文本值
  455. *
  456. * @param content
  457. * @param data
  458. * @return
  459. */
  460. public static String field(String content, Map<String, Object> data, String type) {
  461. String pattern = "[{]([^}]+)[}]";
  462. Pattern patternList = Pattern.compile(pattern);
  463. Matcher matcher = patternList.matcher(content);
  464. Map<String, List<String>> parameterMap = data(matcher, data);
  465. Map<String, Object> result = new HashMap<>();
  466. if (StringUtils.isNotEmpty(type)) {
  467. Map<String, String> datas = new HashMap<>();
  468. for (String key : parameterMap.keySet()) {
  469. datas.put(key, data.get(key) != null ? String.valueOf(data.get(key)) : "");
  470. }
  471. result.putAll(datas);
  472. } else {
  473. Map<String, Object> dataAll = new HashMap<>();
  474. for (String key : parameterMap.keySet()) {
  475. StringJoiner joiner = new StringJoiner(",");
  476. List<String> list = parameterMap.get(key);
  477. for (String id : list) {
  478. joiner.add("'" + id + "'");
  479. }
  480. String value = joiner.toString();
  481. if (list.size() > 1) {
  482. value = "SUM(" + joiner + ")";
  483. }
  484. dataAll.put(key, value);
  485. }
  486. result.putAll(dataAll);
  487. }
  488. StringSubstitutor strSubstitutor = new StringSubstitutor(result, "{", "}");
  489. String field = strSubstitutor.replace(content);
  490. return field;
  491. }
  492. /**
  493. * 赋值
  494. */
  495. private static Map<String, List<String>> data(Matcher matcher, Map<String, Object> dataAll) {
  496. Map<String, List<String>> map = new HashMap<>();
  497. Map<String, String> keyAll = new HashMap<>();
  498. while (matcher.find()) {
  499. String group = matcher.group().replaceAll("\\{", "").replaceAll("}", "");
  500. keyAll.put(group, group);
  501. }
  502. for (String id : keyAll.keySet()) {
  503. List<String> valueData = new ArrayList<>();
  504. String valueAll[] = id.split("-");
  505. String key = valueAll[0];
  506. Object childDataAll = dataAll.get(key) != null ? dataAll.get(key) : "";
  507. if (valueAll.length > 1) {
  508. String data = valueAll[1];
  509. if (childDataAll instanceof List) {
  510. List<Map<String, Object>> childData = (List<Map<String, Object>>) childDataAll;
  511. for (Map<String, Object> childDatum : childData) {
  512. Object childDatas = childDatum.get(data);
  513. valueData.add(childDatas + "");
  514. }
  515. }
  516. } else {
  517. valueData.add(childDataAll + "");
  518. }
  519. map.put(id, valueData);
  520. }
  521. return map;
  522. }
  523. }