| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- package jnpf.flowable.util;
- import cn.hutool.core.collection.CollectionUtil;
- import com.google.common.collect.ImmutableList;
- import jnpf.constant.MsgCode;
- import jnpf.exception.WorkFlowException;
- import jnpf.flowable.entity.TaskEntity;
- import jnpf.flowable.enums.DivideRuleEnum;
- import jnpf.flowable.enums.NodeEnum;
- import jnpf.flowable.model.flowable.FlowAbleUrl;
- import jnpf.flowable.model.flowable.OutgoingFlowsFo;
- import jnpf.flowable.model.task.FlowMethod;
- import jnpf.flowable.model.templatenode.nodejson.NodeModel;
- import jnpf.flowable.model.templatenode.nodejson.ProperCond;
- import jnpf.permission.entity.UserEntity;
- import jnpf.util.StringUtil;
- import jnpf.util.UserProvider;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.util.*;
- /**
- * 类的描述
- *
- * @author JNPF@YinMai Info. Co., Ltd
- * @version 5.0.x
- * @since 2024/4/19 15:14
- */
- @Component
- public class ConditionService {
- @Autowired
- private ServiceUtil serviceUtil;
- @Autowired
- private FlowAbleUrl flowAbleUrl;
- // 处理选择分支的条件
- public Map<String, Boolean> getForBranch(FlowMethod flowMethod, List<String> branchList) throws WorkFlowException {
- Map<String, Boolean> resMap = new HashMap<>();
- String deploymentId = flowMethod.getDeploymentId();
- String nodeCode = flowMethod.getNodeCode();
- Map<String, NodeModel> nodes = flowMethod.getNodes();
- NodeModel global = nodes.get(NodeEnum.global.getType());
- List<String> connectList = global.getConnectList();
- List<String> typeList = ImmutableList.of(NodeEnum.trigger.getType());
- OutgoingFlowsFo flowsFo = new OutgoingFlowsFo();
- flowsFo.setDeploymentId(deploymentId);
- flowsFo.setTaskKey(nodeCode);
- List<String> outgoingFlows = flowAbleUrl.getOutgoingFlows(flowsFo);
- for (String flow : outgoingFlows) {
- resMap.put(flow, false);
- if (!connectList.contains(flow)) {
- // 不存在connectList中,说明是隐藏的线,默认给true
- resMap.put(flow, true);
- continue;
- }
- List<String> nodeKey = flowAbleUrl.getTaskKeyAfterFlow(deploymentId, flow);
- if (CollectionUtil.isNotEmpty(nodeKey)) {
- NodeModel nodeModel = nodes.get(nodeKey.get(0));
- if (null != nodeModel && typeList.contains(nodeModel.getType())) {
- resMap.put(flow, true);
- } else if (branchList.contains(nodeKey.get(0))) {
- resMap.put(flow, true);
- }
- }
- }
- return resMap;
- }
- /**
- * 处理条件
- */
- public Map<String, Boolean> handleCondition(FlowMethod flowMethod) throws WorkFlowException {
- String deploymentId = flowMethod.getDeploymentId();
- String nodeCode = flowMethod.getNodeCode();
- // 获取节点的出线
- OutgoingFlowsFo flowsFo = new OutgoingFlowsFo();
- flowsFo.setDeploymentId(deploymentId);
- flowsFo.setTaskKey(nodeCode);
- List<String> outgoingFlows = flowAbleUrl.getOutgoingFlows(flowsFo);
- Map<String, Boolean> resMap = new HashMap<>();
- flowMethod.setResMap(resMap);
- flowMethod.setOutgoingFlows(outgoingFlows);
- // 判断条件
- getConditionResult(flowMethod);
- return resMap;
- }
- /**
- * 获取条件结果
- */
- public void getConditionResult(FlowMethod flowMethod) {
- List<String> outgoingFlows = flowMethod.getOutgoingFlows();
- Map<String, Boolean> resMap = flowMethod.getResMap();
- Map<String, NodeModel> nodes = flowMethod.getNodes();
- String nodeCode = flowMethod.getNodeCode();
- TaskEntity taskEntity = flowMethod.getTaskEntity();
- if (CollectionUtil.isNotEmpty(outgoingFlows)) {
- Set<String> userList = new HashSet<>();
- userList.add(UserProvider.getLoginUserId());
- if (taskEntity != null) {
- userList.add(taskEntity.getCreatorUserId());
- if (StringUtil.isNotEmpty(taskEntity.getDelegateUserId())) {
- userList.add(taskEntity.getDelegateUserId());
- }
- }
- List<UserEntity> userName = serviceUtil.getUserName(new ArrayList<>(userList));
- UserEntity createUser = null;
- UserEntity delegate = null;
- if (taskEntity != null) {
- createUser = userName.stream().filter(e -> Objects.equals(e.getId(), taskEntity.getCreatorUserId())).findFirst().orElse(null);
- if (StringUtil.isNotEmpty(taskEntity.getDelegateUserId())) {
- delegate = userName.stream().filter(e -> Objects.equals(e.getId(), taskEntity.getDelegateUserId())).findFirst().orElse(null);
- }
- }
- // 设置条件判断 所需参数
- UserEntity userEntity = userName.stream().filter(e -> Objects.equals(e.getId(), UserProvider.getLoginUserId())).findFirst().orElse(null);
- flowMethod.setUserEntity(userEntity);
- if (flowMethod.getUserInfo() == null) {
- flowMethod.setUserInfo(UserProvider.getUser());
- }
- flowMethod.setCreateUser(createUser);
- flowMethod.setDelegate(delegate);
- NodeModel currentNodeModel = nodes.get(nodeCode);
- flowMethod.setNodeModel(currentNodeModel);
- if (StringUtil.equals(currentNodeModel.getDivideRule(), DivideRuleEnum.PARALLEL.getType())) {
- // 并行,全都为true
- for (String key : outgoingFlows) {
- resMap.put(key, true);
- }
- }
- // else if (StringUtil.equals(currentNodeModel.getDivideRule(), DivideRuleEnum.EXCLUSIVE.getType())) {
- // // 排他,获取到第一条为true的结果,其余默认为false
- // for (String key : outgoingFlows) {
- // // 获取出线节点 判断条件,没有设置条件的默认true
- // NodeModel nodeModel = nodes.get(key);
- // boolean res = true;
- // if (null != nodeModel) {
- // List<ProperCond> conditions = nodeModel.getConditions();
- // if (CollectionUtil.isNotEmpty(conditions)) {
- // flowMethod.setConditions(conditions);
- // flowMethod.setMatchLogic(nodeModel.getMatchLogic());
- // res = FlowJsonUtil.nodeConditionDecide(flowMethod);
- // }
- // conditionResMap.put(key, res);
- // }
- // resMap.put(key, res);
- // }
- // }
- else {
- for (String key : outgoingFlows) {
- // 获取出线节点 判断条件,没有设置条件的默认true
- NodeModel nodeModel = nodes.get(key);
- boolean res = true;
- if (null != nodeModel) {
- List<ProperCond> conditions = nodeModel.getConditions();
- if (CollectionUtil.isNotEmpty(conditions)) {
- flowMethod.setConditions(conditions);
- flowMethod.setMatchLogic(nodeModel.getMatchLogic());
- res = FlowJsonUtil.nodeConditionDecide(flowMethod);
- }
- }
- resMap.put(key, res);
- }
- }
- }
- }
- // 处理条件
- public boolean hasCondition(FlowMethod flowMethod) {
- TaskEntity taskEntity = flowMethod.getTaskEntity();
- UserEntity createUser = null;
- UserEntity delegate = null;
- if (taskEntity != null) {
- createUser = serviceUtil.getUserInfo(taskEntity.getCreatorUserId());
- delegate = StringUtil.isNotEmpty(taskEntity.getDelegateUserId()) ? serviceUtil.getUserInfo(taskEntity.getDelegateUserId()) : null;
- }
- flowMethod.setCreateUser(createUser);
- flowMethod.setDelegate(delegate);
- UserEntity userEntity = serviceUtil.getUserInfo(UserProvider.getLoginUserId());
- flowMethod.setUserEntity(userEntity);
- flowMethod.setUserInfo(UserProvider.getUser());
- return FlowJsonUtil.nodeConditionDecide(flowMethod);
- }
- public void checkCondition(Map<String, Boolean> resMap, Map<String, NodeModel> nodes) throws WorkFlowException {
- if (CollectionUtil.isEmpty(nodes)) {
- return;
- }
- NodeModel global = nodes.get(NodeEnum.global.getType());
- if (null == global) {
- throw new WorkFlowException(MsgCode.WF076.get());
- }
- List<String> connectList = global.getConnectList();
- long count = resMap.values().stream().filter(t -> Objects.equals(t, true)).count();
- Set<String> set = resMap.keySet();
- if (count == resMap.size()) {
- return;
- }
- Map<String, Boolean> defaultResMap = new HashMap<>();
- if (CollectionUtil.isNotEmpty(connectList)) {
- int c = 0;
- for (String key : set) {
- if (connectList.contains(key)) {
- if (resMap.get(key)) {
- c++;
- }
- NodeModel nodeModel = nodes.get(key);
- if (nodeModel != null) {
- Boolean isDefault = nodeModel.getIsDefault();
- defaultResMap.put(key, isDefault);
- }
- }
- }
- if (c == 0) {
- long defaultCount = defaultResMap.values().stream().filter(t -> Objects.equals(t, true)).count();
- if (defaultCount > 0) {
- resMap.putAll(defaultResMap);
- c++;
- }
- }
- if (c < 1) {
- throw new WorkFlowException(MsgCode.WF075.get());
- }
- } else {
- if (count < 1) {
- throw new WorkFlowException(MsgCode.WF075.get());
- }
- }
- }
- }
|