Преглед на файлове

【代码优化】BPM:1297 fix: 修复simple设计器第一个发起人节点,审批时校验是否存在审批人导致流程异常

YunaiV преди 4 месеца
родител
ревизия
113dfae732

+ 22 - 39
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmProcessInstanceServiceImpl.java

@@ -14,7 +14,6 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
 import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
 import cn.iocoder.yudao.framework.common.util.object.PageUtils;
 import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
-import cn.iocoder.yudao.module.bpm.controller.admin.base.user.UserSimpleBaseVO;
 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.BpmModelMetaInfoVO;
 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO;
 import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.*;
@@ -262,47 +261,31 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
             processVariables.putAll(reqVO.getProcessVariables());
         }
 
-        // 3 获取当前任务节点的信息
-        // 3.1 获取下一个将要执行的节点集合
+        // 3. 获取下一个将要执行的节点集合
         FlowElement flowElement = bpmnModel.getFlowElement(task.getTaskDefinitionKey());
         List<FlowNode> nextFlowNodes = BpmnModelUtils.getNextFlowNodes(flowElement, bpmnModel, processVariables);
-
-        // 2. 收集所有节点的候选用户 ID
-        Set<Long> allCandidateUsers = new HashSet<>();
-        for (FlowNode node : nextFlowNodes) {
-            List<Long> candidateUserIds = getTaskCandidateUserList(bpmnModel, node.getId(),
-                    loginUserId, historicProcessInstance.getProcessDefinitionId(), processVariables);
-            allCandidateUsers.addAll(candidateUserIds);
-        }
-
-        // 3. 批量查询用户和部门信息
-        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(new ArrayList<>(allCandidateUsers));
+        List<ActivityNode> nextActivityNodes = convertList(nextFlowNodes, node -> new ActivityNode().setId(node.getId())
+                .setName(node.getName()).setNodeType(BpmSimpleModelNodeTypeEnum.APPROVE_NODE.getType())
+                .setStatus(BpmTaskStatusEnum.RUNNING.getStatus())
+                .setCandidateStrategy(BpmnModelUtils.parseCandidateStrategy(node))
+                .setCandidateUserIds(getTaskCandidateUserList(bpmnModel, node.getId(),
+                        loginUserId, historicProcessInstance.getProcessDefinitionId(), processVariables)));
+        if (CollUtil.isNotEmpty(nextActivityNodes)) {
+            return nextActivityNodes;
+        }
+
+        // 4. 拼接基础信息
+        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
+                convertSetByFlatMap(nextActivityNodes, ActivityNode::getCandidateUserIds, Collection::stream));
         Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId));
-
-        // 4. 组装节点信息
-        return convertList(nextFlowNodes, node -> {
-            // 4.1 获取当前节点的候选用户 ID
-            List<Long> candidateUserIds = getTaskCandidateUserList(bpmnModel, node.getId(),
-                    loginUserId, historicProcessInstance.getProcessDefinitionId(), processVariables);
-
-            // 4.2 组装候选用户信息
-            List<UserSimpleBaseVO> candidateUsers = convertList(candidateUserIds, userId -> {
-                AdminUserRespDTO user = userMap.get(userId);
-                if (user != null) {
-                    return BpmProcessInstanceConvert.INSTANCE.buildUser(userId, userMap, deptMap);
-                }
-                return null;
-            });
-
-            // 4.3 构建节点信息
-            return new ActivityNode()
-                    .setNodeType(BpmSimpleModelNodeTypeEnum.APPROVE_NODE.getType())
-                    .setId(node.getId())
-                    .setName(node.getName())
-                    .setStatus(BpmTaskStatusEnum.RUNNING.getStatus())
-                    .setCandidateStrategy(BpmnModelUtils.parseCandidateStrategy(node))
-                    .setCandidateUsers(candidateUsers);
-        });
+        nextActivityNodes.forEach(node -> node.setCandidateUsers(convertList(node.getCandidateUserIds(), userId -> {
+            AdminUserRespDTO user = userMap.get(userId);
+            if (user != null) {
+                return BpmProcessInstanceConvert.INSTANCE.buildUser(userId, userMap, deptMap);
+            }
+            return null;
+        })));
+        return nextActivityNodes;
     }
 
     @Override

+ 19 - 17
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java

@@ -6,6 +6,7 @@ import cn.hutool.core.lang.Assert;
 import cn.hutool.core.util.*;
 import cn.hutool.extra.spring.SpringUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
+import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
 import cn.iocoder.yudao.framework.common.util.date.DateUtils;
 import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
 import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
@@ -599,8 +600,8 @@ public class BpmTaskServiceImpl implements BpmTaskService {
      */
     private Map<String, Object> validateAndSetNextAssignees(String taskDefinitionKey, Map<String, Object> variables, BpmnModel bpmnModel,
                                                             Map<String, List<Long>> nextAssignees, ProcessInstance processInstance) {
-        // Simple设计器第一个节点默认为发起人节点,不校验是否存在审批人
-        if (taskDefinitionKey.equals(START_USER_NODE_ID)) {
+        // simple 设计器第一个节点默认为发起人节点,不校验是否存在审批人
+        if (Objects.equals(taskDefinitionKey, START_USER_NODE_ID)) {
             return variables;
         }
         // 1. 获取下一个将要执行的节点集合
@@ -608,15 +609,13 @@ public class BpmTaskServiceImpl implements BpmTaskService {
         List<FlowNode> nextFlowNodes = getNextFlowNodes(flowElement, bpmnModel, variables);
 
         // 2. 校验选择的下一个节点的审批人,是否合法
-        Map<String, List<Long>> processVariables;
         for (FlowNode nextFlowNode : nextFlowNodes) {
             Integer candidateStrategy = parseCandidateStrategy(nextFlowNode);
             // 2.1 情况一:如果节点中的审批人策略为 发起人自选
             if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy())) {
-                // 先从历史中获取审批人,在发起人会把所有的审批人保存到历史中,这里从历史中获取
-                processVariables = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables());
                 // 特殊:如果当前节点已经存在审批人,则不允许覆盖
-                if (processVariables != null && CollUtil.isNotEmpty(processVariables.get(nextFlowNode.getId()))) {
+                Map<String, List<Long>> startUserSelectAssignees = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables());
+                if (startUserSelectAssignees != null && CollUtil.isNotEmpty(startUserSelectAssignees.get(nextFlowNode.getId()))) {
                     continue;
                 }
                 // 如果节点存在,但未配置审批人
@@ -624,28 +623,31 @@ public class BpmTaskServiceImpl implements BpmTaskService {
                 if (CollUtil.isEmpty(assignees)) {
                     throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName());
                 }
+
                 // 设置 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES
-                if (processVariables == null) {
-                    processVariables = new HashMap<>();
+                if (startUserSelectAssignees == null) {
+                    startUserSelectAssignees = new HashMap<>();
                 }
-                processVariables.put(nextFlowNode.getId(), assignees);
-                variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, processVariables);
+                startUserSelectAssignees.put(nextFlowNode.getId(), assignees);
+                variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, startUserSelectAssignees);
+                continue;
             }
+
             // 2.2 情况二:如果节点中的审批人策略为 审批人,在审批时选择下一个节点的审批人,并且该节点的审批人为空
             if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.APPROVE_USER_SELECT.getStrategy())) {
-                // 获取审批人自选的历史变量
-                processVariables = FlowableUtils.getApproveUserSelectAssignees(processInstance.getProcessVariables());
                 // 如果节点存在,但未配置审批人
+                Map<String, List<Long>> approveUserSelectAssignees = FlowableUtils.getApproveUserSelectAssignees(processInstance.getProcessVariables());
                 List<Long> assignees = nextAssignees != null ? nextAssignees.get(nextFlowNode.getId()) : null;
                 if (CollUtil.isEmpty(assignees)) {
                     throw exception(PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName());
                 }
-                if (processVariables == null) {
-                    processVariables = new HashMap<>();
-                }
+
                 // 设置 PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES
-                processVariables.put(nextFlowNode.getId(), assignees);
-                variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES, processVariables);
+                if (approveUserSelectAssignees == null) {
+                    approveUserSelectAssignees = new HashMap<>();
+                }
+                approveUserSelectAssignees.put(nextFlowNode.getId(), assignees);
+                variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES, approveUserSelectAssignees);
             }
         }
         return variables;