| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- 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<ProperCond> conditionList = flowMethod.getConditions();
- String matchLogic = flowMethod.getMatchLogic();
- boolean flag = false;
- ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
- ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("js");
- Map<String, Object> map = flowMethod.getFormData();
- List<String> 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<GroupsModel> 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<Integer> valueType = ImmutableList.of(FieldEnum.Condition.getCode(), FieldEnum.System.getCode());
- Object value = null;
- List<Object> 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<String, Object> 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<FlowParamModel> paramModelList = global.getGlobalParameterList();
- for (FlowParamModel model : paramModelList) {
- mapData.put(model.getFieldName(), model.getDefaultValue());
- }
- }
- }
- List<TemplateJsonModel> 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<String, String> resMap = msgUtil.parameterMap(parameterModel, list, createUser, delegate);
- if (includes) {
- if (filedData instanceof List) {
- List<String> filedList = (List<String>) 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<String> filedList = (List<String>) 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<Object> includeValue) {
- UserInfo userInfo = flowMethod.getUserInfo();
- if ("currentUser".equals(value)) {
- value = userInfo.getUserId();
- }
- try {
- try {
- List<List<String>> dataAll = JsonUtil.getJsonToBean(value, List.class);
- List<String> id = new ArrayList<>();
- for (List<String> data : dataAll) {
- id.addAll(data);
- }
- value = String.join(",", id);
- } catch (Exception e) {
- try {
- List<String> id = new ArrayList<>();
- List<String> 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<Object> includeValue) {
- Map<String, Object> map = flowMethod.getFormData();
- value = map.get(value);
- UserEntity userEntity = flowMethod.getUserEntity();
- TaskEntity taskEntity = flowMethod.getTaskEntity();
- try {
- try {
- List<List<String>> dataAll = JsonUtil.getJsonToBean(value, List.class);
- List<String> id = new ArrayList<>();
- for (List<String> data : dataAll) {
- id.addAll(data);
- }
- value = String.join(",", id);
- } catch (Exception e) {
- try {
- List<String> id = new ArrayList<>();
- List<String> 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<List<String>> dataAll = JsonUtil.getJsonToBean(form, List.class);
- List<String> id = new ArrayList<>();
- for (List<String> data : dataAll) {
- id.addAll(data);
- }
- result = String.join(",", id);
- } catch (Exception e) {
- try {
- List<String> id = new ArrayList<>();
- List<String> 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<String, Object> 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<String, Object> data, String type) {
- String pattern = "[{]([^}]+)[}]";
- Pattern patternList = Pattern.compile(pattern);
- Matcher matcher = patternList.matcher(content);
- Map<String, List<String>> parameterMap = data(matcher, data);
- Map<String, Object> result = new HashMap<>();
- if (StringUtils.isNotEmpty(type)) {
- Map<String, String> 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<String, Object> dataAll = new HashMap<>();
- for (String key : parameterMap.keySet()) {
- StringJoiner joiner = new StringJoiner(",");
- List<String> 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<String, List<String>> data(Matcher matcher, Map<String, Object> dataAll) {
- Map<String, List<String>> map = new HashMap<>();
- Map<String, String> keyAll = new HashMap<>();
- while (matcher.find()) {
- String group = matcher.group().replaceAll("\\{", "").replaceAll("}", "");
- keyAll.put(group, group);
- }
- for (String id : keyAll.keySet()) {
- List<String> 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<Map<String, Object>> childData = (List<Map<String, Object>>) childDataAll;
- for (Map<String, Object> childDatum : childData) {
- Object childDatas = childDatum.get(data);
- valueData.add(childDatas + "");
- }
- }
- } else {
- valueData.add(childDataAll + "");
- }
- map.put(id, valueData);
- }
- return map;
- }
- }
|