package jnpf.flowable.util; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; import com.google.common.collect.ImmutableList; import jnpf.base.UserInfo; import jnpf.emnus.SearchMethodEnum; import jnpf.flowable.entity.RecordEntity; import jnpf.flowable.entity.TaskEntity; import jnpf.flowable.enums.FieldEnum; import jnpf.flowable.enums.NodeEnum; import jnpf.flowable.model.task.FlowMethod; import jnpf.flowable.model.task.FlowModel; import jnpf.flowable.model.templatejson.FlowParamModel; import jnpf.flowable.model.templatenode.nodejson.GroupsModel; import jnpf.flowable.model.templatenode.nodejson.NodeModel; import jnpf.flowable.model.templatenode.nodejson.ProperCond; import jnpf.flowable.model.templatenode.nodejson.TemplateJsonModel; import jnpf.permission.entity.UserEntity; import jnpf.util.JsonUtil; import jnpf.util.StringUtil; import jnpf.util.visiual.JnpfKeyConsts; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringSubstitutor; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.NumberFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 类的描述 * * @author JNPF@YinMai Info. Co., Ltd * @version 5.0.x * @since 2024/4/18 20:22 */ public class FlowJsonUtil { /** * 节点条件判断 **/ public static boolean nodeConditionDecide(FlowMethod flowMethod) { List conditionList = flowMethod.getConditions(); String matchLogic = flowMethod.getMatchLogic(); boolean flag = false; ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("js"); Map map = flowMethod.getFormData(); List expressionAll = new ArrayList<>(); StringBuilder condition = new StringBuilder(); for (int k = 0; k < conditionList.size(); k++) { StringBuilder expression = new StringBuilder(); expression.append("("); ProperCond properCond = conditionList.get(k); String logic = properCond.getLogic(); List groups = properCond.getGroups(); for (int i = 0; i < groups.size(); i++) { GroupsModel groupsModel = groups.get(i); String contain = "!=-1"; String field = groupsModel.getField(); String jnpfKey = groupsModel.getJnpfKey(); int fieldType = groupsModel.getFieldType(); String symbol = groupsModel.getSymbol(); boolean inLike = "like".equals(symbol) || "notLike".equals(symbol); boolean includes = "in".equals(symbol) || "notIn".equals(symbol); Object form = Objects.equals(FieldEnum.Field.getCode(), fieldType) ? formValue(flowMethod, jnpfKey, map.get(field)) : formula(groupsModel, map); Object formValue = form; if ("<>".equals(symbol)) { symbol = "!="; } int fieldValueType = groupsModel.getFieldValueType(); String valueJnpfKey = StringUtil.isNotEmpty(groupsModel.getFieldValueJnpfKey()) ? groupsModel.getFieldValueJnpfKey() : jnpfKey; Object filedData = groupsModel.getFieldValue(); List valueType = ImmutableList.of(FieldEnum.Condition.getCode(), FieldEnum.System.getCode()); Object value = null; List includeValue = new ArrayList<>(); if (valueType.contains(fieldValueType)) { TemplateJsonModel templateJsonModel = new TemplateJsonModel(); templateJsonModel.setField(groupsModel.getField()); templateJsonModel.setSourceType(Objects.equals(FieldEnum.Condition.getCode(), fieldValueType) ? FieldEnum.System.getCode() : FieldEnum.Field.getCode()); templateJsonModel.setMsgTemplateId(groupsModel.getField()); templateJsonModel.setRelationField(String.valueOf(groupsModel.getFieldValue())); MsgUtil msgUtil = new MsgUtil(); TaskEntity taskEntity = flowMethod.getTaskEntity(); Map mapData = new HashMap<>(); if (null != taskEntity) { if (StringUtil.isNotEmpty(taskEntity.getGlobalParameter())) { mapData = JsonUtil.stringToMap(taskEntity.getGlobalParameter()); } } if (CollectionUtil.isEmpty(mapData)) { NodeModel global = flowMethod.getNodes().get(NodeEnum.global.getType()); if (null != global) { List paramModelList = global.getGlobalParameterList(); for (FlowParamModel model : paramModelList) { mapData.put(model.getFieldName(), model.getDefaultValue()); } } } List list = new ArrayList<>(); list.add(templateJsonModel); RecordEntity record = new RecordEntity(); record.setNodeCode(flowMethod.getNodeCode()); UserEntity createUser = flowMethod.getCreateUser(); UserEntity delegate = flowMethod.getDelegate(); FlowModel parameterModel = new FlowModel(); parameterModel.setFormData(mapData); parameterModel.setRecordEntity(record); parameterModel.setTaskEntity(taskEntity); Map resMap = msgUtil.parameterMap(parameterModel, list, createUser, delegate); if (includes) { if (filedData instanceof List) { List filedList = (List) filedData; for (String id : filedList) { String fieldValue = resMap.get(id) != null ? resMap.get(id) : ""; includeValue.add("'" + fieldValue + "'"); } } } else { value = resMap.get(filedData); if (value != null) { value = "'" + value + "'"; } } } else { if (includes) { if (filedData instanceof List) { List filedList = (List) filedData; for (String id : filedList) { if (Objects.equals(FieldEnum.Custom.getCode(), fieldValueType)) { filedValue(flowMethod, id, valueJnpfKey, form, includeValue); } else { filedData(flowMethod, id, valueJnpfKey, form, includeValue); } } } } else { value = Objects.equals(FieldEnum.Custom.getCode(), fieldValueType) ? filedValue(flowMethod, filedData, valueJnpfKey, form, includeValue) : filedData(flowMethod, filedData, valueJnpfKey, form, includeValue); } } Object fieldValue = value; String pression = formValue + symbol + fieldValue; // 比较的处理 if ("<=".equals(symbol) || "<".equals(symbol) || ">".equals(symbol) || ">=".equals(symbol)) { try { String formValueStr = formValue.toString(); if (formValueStr.startsWith("'") && formValueStr.endsWith("'")) { formValueStr = formValueStr.substring(1, formValueStr.length() - 1); } fieldValue = fieldValue == null ? "" : fieldValue; String fieldValueStr = fieldValue.toString(); if (fieldValueStr.startsWith("'") && fieldValueStr.endsWith("'")) { fieldValueStr = fieldValueStr.substring(1, fieldValueStr.length() - 1); } BigDecimal a = new BigDecimal(formValueStr); BigDecimal b = new BigDecimal(fieldValueStr); boolean res = false; if ("<=".equals(symbol)) { res = a.compareTo(b) <= 0; } else if ("<".equals(symbol)) { res = a.compareTo(b) < 0; } else if (">".equals(symbol)) { res = a.compareTo(b) > 0; } else if (">=".equals(symbol)) { res = a.compareTo(b) >= 0; } pression = res + ""; } catch (Exception e) { System.out.println(e.getMessage()); pression = "false"; } } if (inLike) { if ("notLike".equals(symbol)) { contain = "==-1"; } symbol = ".indexOf"; if (!(formValue instanceof CharSequence)) { formValue = "'" + formValue + "'"; } if (!(fieldValue instanceof CharSequence)) { fieldValue = "'" + fieldValue + "'"; } pression = formValue + ".toString()" + symbol + "(" + fieldValue + ")" + contain; } if (includes) { try { boolean isNotIn = "notIn".equals(symbol); String searchModel = isNotIn ? " && " : " || "; symbol = !isNotIn ? " == " : " != "; if (!(formValue instanceof CharSequence)) { formValue = "'" + formValue + "'"; } StringBuilder json = new StringBuilder(); json.append("("); for (int s = 0; s < includeValue.size(); s++) { Object valuse = includeValue.get(s); if (!(valuse instanceof CharSequence)) { valuse = "'" + valuse + "'"; } json.append(formValue + symbol + valuse); if (s != includeValue.size() - 1) { json.append(searchModel); } } json.append(")"); pression = includeValue.isEmpty() ? "false" : json.toString(); } catch (Exception e) { System.out.println(e.getMessage()); pression = "false"; } } if (ObjectUtil.equals(symbol, "null")) { pression = "(" + formValue + " == null || " + formValue + " == '')"; } if (ObjectUtil.equals(symbol, "notNull")) { pression = "(" + formValue + " != null && " + formValue + " != '')"; } expression.append(pression); if (!StringUtils.isEmpty(logic) && i != groups.size() - 1) { expression.append(" " + search(logic) + " "); } } expression.append(")"); expressionAll.add(expression.toString()); } for (int i = 0; i < expressionAll.size(); i++) { String script = expressionAll.get(i); String search = i != expressionAll.size() - 1 ? search(matchLogic) : ""; condition.append(script + " " + search + " "); } try { flag = (Boolean) scriptEngine.eval(condition.toString()); } catch (Exception e) { System.out.println(e.getMessage()); } return flag; } /** * 条件表达式 * * @param logic */ private static String search(String logic) { return SearchMethodEnum.And.getSymbol().equalsIgnoreCase(logic) ? "&&" : "||"; } /** * 条件数据修改 * * @param flowMethod * @param value */ private static Object filedValue(FlowMethod flowMethod, Object value, String jnpfKey, Object form, List includeValue) { UserInfo userInfo = flowMethod.getUserInfo(); if ("currentUser".equals(value)) { value = userInfo.getUserId(); } try { try { List> dataAll = JsonUtil.getJsonToBean(value, List.class); List id = new ArrayList<>(); for (List data : dataAll) { id.addAll(data); } value = String.join(",", id); } catch (Exception e) { try { List id = new ArrayList<>(); List dataAll = JsonUtil.getJsonToList(value, String.class); if (JnpfKeyConsts.CURRORGANIZE.equals(jnpfKey)) { value = dataAll.stream().filter(t -> ("'" + t + "'").equals(form)).findFirst().orElse(null); } else { for (String data : dataAll) { id.add(data); } value = String.join(",", id); } } catch (Exception e1) { } } } catch (Exception e) { } if (value instanceof CharSequence) { value = "'" + value + "'"; } includeValue.add(value); return value; } /** * 条件数据修改 * * @param flowMethod * @param value */ private static Object filedData(FlowMethod flowMethod, Object value, String jnpfKey, Object form, List includeValue) { Map map = flowMethod.getFormData(); value = map.get(value); UserEntity userEntity = flowMethod.getUserEntity(); TaskEntity taskEntity = flowMethod.getTaskEntity(); try { try { List> dataAll = JsonUtil.getJsonToBean(value, List.class); List id = new ArrayList<>(); for (List data : dataAll) { id.addAll(data); } value = String.join(",", id); } catch (Exception e) { try { List id = new ArrayList<>(); List dataAll = JsonUtil.getJsonToList(value, String.class); if (JnpfKeyConsts.CURRORGANIZE.equals(jnpfKey) || JnpfKeyConsts.COMSELECT.equals(jnpfKey)) { value = dataAll.stream().filter(t -> ("'" + t + "'").equals(form)).findFirst().orElse(null); } else { for (String data : dataAll) { id.add(data); } value = String.join(",", id); } } catch (Exception e1) { } } if (JnpfKeyConsts.CREATETIME.equals(jnpfKey)) { Date creatorTime = taskEntity.getCreatorTime(); value = null == creatorTime ? null : creatorTime.getTime(); } else if (JnpfKeyConsts.CREATEUSER.equals(jnpfKey)) { value = taskEntity.getCreatorUserId(); } else if (JnpfKeyConsts.CURRORGANIZE.equals(jnpfKey)) { value = userEntity.getOrganizeId(); } else if (JnpfKeyConsts.CURRPOSITION.equals(jnpfKey)) { value = userEntity.getPositionId(); } else if (JnpfKeyConsts.MODIFYTIME.equals(jnpfKey)) { Date lastModifyTime = taskEntity.getLastModifyTime(); value = null == lastModifyTime ? null : lastModifyTime.getTime(); } else if (JnpfKeyConsts.MODIFYUSER.equals(jnpfKey)) { value = taskEntity.getLastModifyUserId(); } } catch (Exception e) { } if (value instanceof CharSequence) { value = "'" + value + "'"; } includeValue.add(value); return value; } /** * 表单数据修改 * * @param form */ private static Object formValue(FlowMethod flowMethod, String jnpfKey, Object form) { Object result = form; UserEntity userEntity = flowMethod.getUserEntity(); TaskEntity flowTaskEntity = flowMethod.getTaskEntity(); try { try { List> dataAll = JsonUtil.getJsonToBean(form, List.class); List id = new ArrayList<>(); for (List data : dataAll) { id.addAll(data); } result = String.join(",", id); } catch (Exception e) { try { List id = new ArrayList<>(); List dataAll = JsonUtil.getJsonToList(form, String.class); for (String data : dataAll) { id.add(data); } result = String.join(",", id); } catch (Exception e1) { } } if (JnpfKeyConsts.CREATETIME.equals(jnpfKey)) { Date creatorTime = flowTaskEntity.getCreatorTime(); result = null == creatorTime ? null : creatorTime.getTime(); } else if (JnpfKeyConsts.CREATEUSER.equals(jnpfKey)) { result = StringUtil.isNotEmpty(flowTaskEntity.getDelegateUserId()) ? flowTaskEntity.getDelegateUserId() : flowTaskEntity.getCreatorUserId(); } else if (JnpfKeyConsts.CURRORGANIZE.equals(jnpfKey)) { result = userEntity.getOrganizeId(); } else if (JnpfKeyConsts.CURRPOSITION.equals(jnpfKey)) { result = userEntity.getPositionId(); } else if (JnpfKeyConsts.MODIFYTIME.equals(jnpfKey)) { Date lastModifyTime = flowTaskEntity.getLastModifyTime(); result = null == lastModifyTime ? null : lastModifyTime.getTime(); } else if (JnpfKeyConsts.MODIFYUSER.equals(jnpfKey)) { result = flowTaskEntity.getLastModifyUserId(); } } catch (Exception e) { } if (result instanceof CharSequence) { result = "'" + result + "'"; } return result; } /** * 表达式 */ private static Object formula(GroupsModel properCond, Map data) { String result = null; try { StringBuilder builder = new StringBuilder(); builder.append("function getNum(val) {\n" + " return isNaN(val) ? 0 : Number(val)\n" + "};\n" + "// 求和\n" + "function SUM() {\n" + " var value = 0\n" + " for (var i = 0; i < arguments.length; i++) {\n" + " value += getNum(arguments[i])\n" + " }\n" + " return value\n" + "};\n" + "// 求差\n" + "function SUBTRACT(num1, num2) {\n" + " return getNum(num1) - getNum(num2)\n" + "};\n" + "// 相乘\n" + "function PRODUCT() {\n" + " var value = 1\n" + " for (var i = 0; i < arguments.length; i++) {\n" + " value = value * getNum(arguments[i])\n" + " }\n" + " return value\n" + "};\n" + "// 相除\n" + "function DIVIDE(num1, num2) {\n" + " return getNum(num1) / (getNum(num2) === 0 ? 1 : getNum(num2))\n" + "};\n" + "// 获取参数的数量\n" + "function COUNT() {\n" + " var value = 0\n" + " for (var i = 0; i < arguments.length; i++) {\n" + " value ++\n" + " }\n" + " return value\n" + "};\n"); String field = field(properCond.getField(), data, null); ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("js"); String eval = builder + " var result = " + field + ";"; scriptEngine.eval(eval); double d = (double) scriptEngine.get("result"); NumberFormat nf = NumberFormat.getNumberInstance(); nf.setRoundingMode(RoundingMode.UP); result = nf.format(d); } catch (Exception e) { System.out.println(e.getMessage()); } return result; } /** * 替换文本值 * * @param content * @param data * @return */ public static String field(String content, Map data, String type) { String pattern = "[{]([^}]+)[}]"; Pattern patternList = Pattern.compile(pattern); Matcher matcher = patternList.matcher(content); Map> parameterMap = data(matcher, data); Map result = new HashMap<>(); if (StringUtils.isNotEmpty(type)) { Map datas = new HashMap<>(); for (String key : parameterMap.keySet()) { datas.put(key, data.get(key) != null ? String.valueOf(data.get(key)) : ""); } result.putAll(datas); } else { Map dataAll = new HashMap<>(); for (String key : parameterMap.keySet()) { StringJoiner joiner = new StringJoiner(","); List list = parameterMap.get(key); for (String id : list) { joiner.add("'" + id + "'"); } String value = joiner.toString(); if (list.size() > 1) { value = "SUM(" + joiner + ")"; } dataAll.put(key, value); } result.putAll(dataAll); } StringSubstitutor strSubstitutor = new StringSubstitutor(result, "{", "}"); String field = strSubstitutor.replace(content); return field; } /** * 赋值 */ private static Map> data(Matcher matcher, Map dataAll) { Map> map = new HashMap<>(); Map keyAll = new HashMap<>(); while (matcher.find()) { String group = matcher.group().replaceAll("\\{", "").replaceAll("}", ""); keyAll.put(group, group); } for (String id : keyAll.keySet()) { List valueData = new ArrayList<>(); String valueAll[] = id.split("-"); String key = valueAll[0]; Object childDataAll = dataAll.get(key) != null ? dataAll.get(key) : ""; if (valueAll.length > 1) { String data = valueAll[1]; if (childDataAll instanceof List) { List> childData = (List>) childDataAll; for (Map childDatum : childData) { Object childDatas = childDatum.get(data); valueData.add(childDatas + ""); } } } else { valueData.add(childDataAll + ""); } map.put(id, valueData); } return map; } }