ConditionService.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package jnpf.flowable.util;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import com.google.common.collect.ImmutableList;
  4. import jnpf.constant.MsgCode;
  5. import jnpf.exception.WorkFlowException;
  6. import jnpf.flowable.entity.TaskEntity;
  7. import jnpf.flowable.enums.DivideRuleEnum;
  8. import jnpf.flowable.enums.NodeEnum;
  9. import jnpf.flowable.model.flowable.FlowAbleUrl;
  10. import jnpf.flowable.model.flowable.OutgoingFlowsFo;
  11. import jnpf.flowable.model.task.FlowMethod;
  12. import jnpf.flowable.model.templatenode.nodejson.NodeModel;
  13. import jnpf.flowable.model.templatenode.nodejson.ProperCond;
  14. import jnpf.permission.entity.UserEntity;
  15. import jnpf.util.StringUtil;
  16. import jnpf.util.UserProvider;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Component;
  19. import java.util.*;
  20. /**
  21. * 类的描述
  22. *
  23. * @author JNPF@YinMai Info. Co., Ltd
  24. * @version 5.0.x
  25. * @since 2024/4/19 15:14
  26. */
  27. @Component
  28. public class ConditionService {
  29. @Autowired
  30. private ServiceUtil serviceUtil;
  31. @Autowired
  32. private FlowAbleUrl flowAbleUrl;
  33. // 处理选择分支的条件
  34. public Map<String, Boolean> getForBranch(FlowMethod flowMethod, List<String> branchList) throws WorkFlowException {
  35. Map<String, Boolean> resMap = new HashMap<>();
  36. String deploymentId = flowMethod.getDeploymentId();
  37. String nodeCode = flowMethod.getNodeCode();
  38. Map<String, NodeModel> nodes = flowMethod.getNodes();
  39. NodeModel global = nodes.get(NodeEnum.global.getType());
  40. List<String> connectList = global.getConnectList();
  41. List<String> typeList = ImmutableList.of(NodeEnum.trigger.getType());
  42. OutgoingFlowsFo flowsFo = new OutgoingFlowsFo();
  43. flowsFo.setDeploymentId(deploymentId);
  44. flowsFo.setTaskKey(nodeCode);
  45. List<String> outgoingFlows = flowAbleUrl.getOutgoingFlows(flowsFo);
  46. for (String flow : outgoingFlows) {
  47. resMap.put(flow, false);
  48. if (!connectList.contains(flow)) {
  49. // 不存在connectList中,说明是隐藏的线,默认给true
  50. resMap.put(flow, true);
  51. continue;
  52. }
  53. List<String> nodeKey = flowAbleUrl.getTaskKeyAfterFlow(deploymentId, flow);
  54. if (CollectionUtil.isNotEmpty(nodeKey)) {
  55. NodeModel nodeModel = nodes.get(nodeKey.get(0));
  56. if (null != nodeModel && typeList.contains(nodeModel.getType())) {
  57. resMap.put(flow, true);
  58. } else if (branchList.contains(nodeKey.get(0))) {
  59. resMap.put(flow, true);
  60. }
  61. }
  62. }
  63. return resMap;
  64. }
  65. /**
  66. * 处理条件
  67. */
  68. public Map<String, Boolean> handleCondition(FlowMethod flowMethod) throws WorkFlowException {
  69. String deploymentId = flowMethod.getDeploymentId();
  70. String nodeCode = flowMethod.getNodeCode();
  71. // 获取节点的出线
  72. OutgoingFlowsFo flowsFo = new OutgoingFlowsFo();
  73. flowsFo.setDeploymentId(deploymentId);
  74. flowsFo.setTaskKey(nodeCode);
  75. List<String> outgoingFlows = flowAbleUrl.getOutgoingFlows(flowsFo);
  76. Map<String, Boolean> resMap = new HashMap<>();
  77. flowMethod.setResMap(resMap);
  78. flowMethod.setOutgoingFlows(outgoingFlows);
  79. // 判断条件
  80. getConditionResult(flowMethod);
  81. return resMap;
  82. }
  83. /**
  84. * 获取条件结果
  85. */
  86. public void getConditionResult(FlowMethod flowMethod) {
  87. List<String> outgoingFlows = flowMethod.getOutgoingFlows();
  88. Map<String, Boolean> resMap = flowMethod.getResMap();
  89. Map<String, NodeModel> nodes = flowMethod.getNodes();
  90. String nodeCode = flowMethod.getNodeCode();
  91. TaskEntity taskEntity = flowMethod.getTaskEntity();
  92. if (CollectionUtil.isNotEmpty(outgoingFlows)) {
  93. Set<String> userList = new HashSet<>();
  94. userList.add(UserProvider.getLoginUserId());
  95. if (taskEntity != null) {
  96. userList.add(taskEntity.getCreatorUserId());
  97. if (StringUtil.isNotEmpty(taskEntity.getDelegateUserId())) {
  98. userList.add(taskEntity.getDelegateUserId());
  99. }
  100. }
  101. List<UserEntity> userName = serviceUtil.getUserName(new ArrayList<>(userList));
  102. UserEntity createUser = null;
  103. UserEntity delegate = null;
  104. if (taskEntity != null) {
  105. createUser = userName.stream().filter(e -> Objects.equals(e.getId(), taskEntity.getCreatorUserId())).findFirst().orElse(null);
  106. if (StringUtil.isNotEmpty(taskEntity.getDelegateUserId())) {
  107. delegate = userName.stream().filter(e -> Objects.equals(e.getId(), taskEntity.getDelegateUserId())).findFirst().orElse(null);
  108. }
  109. }
  110. // 设置条件判断 所需参数
  111. UserEntity userEntity = userName.stream().filter(e -> Objects.equals(e.getId(), UserProvider.getLoginUserId())).findFirst().orElse(null);
  112. flowMethod.setUserEntity(userEntity);
  113. if (flowMethod.getUserInfo() == null) {
  114. flowMethod.setUserInfo(UserProvider.getUser());
  115. }
  116. flowMethod.setCreateUser(createUser);
  117. flowMethod.setDelegate(delegate);
  118. NodeModel currentNodeModel = nodes.get(nodeCode);
  119. flowMethod.setNodeModel(currentNodeModel);
  120. if (StringUtil.equals(currentNodeModel.getDivideRule(), DivideRuleEnum.PARALLEL.getType())) {
  121. // 并行,全都为true
  122. for (String key : outgoingFlows) {
  123. resMap.put(key, true);
  124. }
  125. }
  126. // else if (StringUtil.equals(currentNodeModel.getDivideRule(), DivideRuleEnum.EXCLUSIVE.getType())) {
  127. // // 排他,获取到第一条为true的结果,其余默认为false
  128. // for (String key : outgoingFlows) {
  129. // // 获取出线节点 判断条件,没有设置条件的默认true
  130. // NodeModel nodeModel = nodes.get(key);
  131. // boolean res = true;
  132. // if (null != nodeModel) {
  133. // List<ProperCond> conditions = nodeModel.getConditions();
  134. // if (CollectionUtil.isNotEmpty(conditions)) {
  135. // flowMethod.setConditions(conditions);
  136. // flowMethod.setMatchLogic(nodeModel.getMatchLogic());
  137. // res = FlowJsonUtil.nodeConditionDecide(flowMethod);
  138. // }
  139. // conditionResMap.put(key, res);
  140. // }
  141. // resMap.put(key, res);
  142. // }
  143. // }
  144. else {
  145. for (String key : outgoingFlows) {
  146. // 获取出线节点 判断条件,没有设置条件的默认true
  147. NodeModel nodeModel = nodes.get(key);
  148. boolean res = true;
  149. if (null != nodeModel) {
  150. List<ProperCond> conditions = nodeModel.getConditions();
  151. if (CollectionUtil.isNotEmpty(conditions)) {
  152. flowMethod.setConditions(conditions);
  153. flowMethod.setMatchLogic(nodeModel.getMatchLogic());
  154. res = FlowJsonUtil.nodeConditionDecide(flowMethod);
  155. }
  156. }
  157. resMap.put(key, res);
  158. }
  159. }
  160. }
  161. }
  162. // 处理条件
  163. public boolean hasCondition(FlowMethod flowMethod) {
  164. TaskEntity taskEntity = flowMethod.getTaskEntity();
  165. UserEntity createUser = null;
  166. UserEntity delegate = null;
  167. if (taskEntity != null) {
  168. createUser = serviceUtil.getUserInfo(taskEntity.getCreatorUserId());
  169. delegate = StringUtil.isNotEmpty(taskEntity.getDelegateUserId()) ? serviceUtil.getUserInfo(taskEntity.getDelegateUserId()) : null;
  170. }
  171. flowMethod.setCreateUser(createUser);
  172. flowMethod.setDelegate(delegate);
  173. UserEntity userEntity = serviceUtil.getUserInfo(UserProvider.getLoginUserId());
  174. flowMethod.setUserEntity(userEntity);
  175. flowMethod.setUserInfo(UserProvider.getUser());
  176. return FlowJsonUtil.nodeConditionDecide(flowMethod);
  177. }
  178. public void checkCondition(Map<String, Boolean> resMap, Map<String, NodeModel> nodes) throws WorkFlowException {
  179. if (CollectionUtil.isEmpty(nodes)) {
  180. return;
  181. }
  182. NodeModel global = nodes.get(NodeEnum.global.getType());
  183. if (null == global) {
  184. throw new WorkFlowException(MsgCode.WF076.get());
  185. }
  186. List<String> connectList = global.getConnectList();
  187. long count = resMap.values().stream().filter(t -> Objects.equals(t, true)).count();
  188. Set<String> set = resMap.keySet();
  189. if (count == resMap.size()) {
  190. return;
  191. }
  192. Map<String, Boolean> defaultResMap = new HashMap<>();
  193. if (CollectionUtil.isNotEmpty(connectList)) {
  194. int c = 0;
  195. for (String key : set) {
  196. if (connectList.contains(key)) {
  197. if (resMap.get(key)) {
  198. c++;
  199. }
  200. NodeModel nodeModel = nodes.get(key);
  201. if (nodeModel != null) {
  202. Boolean isDefault = nodeModel.getIsDefault();
  203. defaultResMap.put(key, isDefault);
  204. }
  205. }
  206. }
  207. if (c == 0) {
  208. long defaultCount = defaultResMap.values().stream().filter(t -> Objects.equals(t, true)).count();
  209. if (defaultCount > 0) {
  210. resMap.putAll(defaultResMap);
  211. c++;
  212. }
  213. }
  214. if (c < 1) {
  215. throw new WorkFlowException(MsgCode.WF075.get());
  216. }
  217. } else {
  218. if (count < 1) {
  219. throw new WorkFlowException(MsgCode.WF075.get());
  220. }
  221. }
  222. }
  223. }