Commit 113dfae73255e38b686f0ee846b8d97a83edb7aa
1 parent
26c1a10b
【代码优化】BPM:1297 fix: 修复simple设计器第一个发起人节点,审批时校验是否存在审批人导致流程异常
Showing
2 changed files
with
41 additions
and
56 deletions
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; | @@ -14,7 +14,6 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | ||
| 14 | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | 14 | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| 15 | import cn.iocoder.yudao.framework.common.util.object.PageUtils; | 15 | import cn.iocoder.yudao.framework.common.util.object.PageUtils; |
| 16 | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; | 16 | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| 17 | -import cn.iocoder.yudao.module.bpm.controller.admin.base.user.UserSimpleBaseVO; | ||
| 18 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.BpmModelMetaInfoVO; | 17 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.BpmModelMetaInfoVO; |
| 19 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO; | 18 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO; |
| 20 | import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.*; | 19 | import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.*; |
| @@ -262,47 +261,31 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService | @@ -262,47 +261,31 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService | ||
| 262 | processVariables.putAll(reqVO.getProcessVariables()); | 261 | processVariables.putAll(reqVO.getProcessVariables()); |
| 263 | } | 262 | } |
| 264 | 263 | ||
| 265 | - // 3 获取当前任务节点的信息 | ||
| 266 | - // 3.1 获取下一个将要执行的节点集合 | 264 | + // 3. 获取下一个将要执行的节点集合 |
| 267 | FlowElement flowElement = bpmnModel.getFlowElement(task.getTaskDefinitionKey()); | 265 | FlowElement flowElement = bpmnModel.getFlowElement(task.getTaskDefinitionKey()); |
| 268 | List<FlowNode> nextFlowNodes = BpmnModelUtils.getNextFlowNodes(flowElement, bpmnModel, processVariables); | 266 | List<FlowNode> nextFlowNodes = BpmnModelUtils.getNextFlowNodes(flowElement, bpmnModel, processVariables); |
| 269 | - | ||
| 270 | - // 2. 收集所有节点的候选用户 ID | ||
| 271 | - Set<Long> allCandidateUsers = new HashSet<>(); | ||
| 272 | - for (FlowNode node : nextFlowNodes) { | ||
| 273 | - List<Long> candidateUserIds = getTaskCandidateUserList(bpmnModel, node.getId(), | ||
| 274 | - loginUserId, historicProcessInstance.getProcessDefinitionId(), processVariables); | ||
| 275 | - allCandidateUsers.addAll(candidateUserIds); | ||
| 276 | - } | ||
| 277 | - | ||
| 278 | - // 3. 批量查询用户和部门信息 | ||
| 279 | - Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(new ArrayList<>(allCandidateUsers)); | 267 | + List<ActivityNode> nextActivityNodes = convertList(nextFlowNodes, node -> new ActivityNode().setId(node.getId()) |
| 268 | + .setName(node.getName()).setNodeType(BpmSimpleModelNodeTypeEnum.APPROVE_NODE.getType()) | ||
| 269 | + .setStatus(BpmTaskStatusEnum.RUNNING.getStatus()) | ||
| 270 | + .setCandidateStrategy(BpmnModelUtils.parseCandidateStrategy(node)) | ||
| 271 | + .setCandidateUserIds(getTaskCandidateUserList(bpmnModel, node.getId(), | ||
| 272 | + loginUserId, historicProcessInstance.getProcessDefinitionId(), processVariables))); | ||
| 273 | + if (CollUtil.isNotEmpty(nextActivityNodes)) { | ||
| 274 | + return nextActivityNodes; | ||
| 275 | + } | ||
| 276 | + | ||
| 277 | + // 4. 拼接基础信息 | ||
| 278 | + Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap( | ||
| 279 | + convertSetByFlatMap(nextActivityNodes, ActivityNode::getCandidateUserIds, Collection::stream)); | ||
| 280 | Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); | 280 | Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); |
| 281 | - | ||
| 282 | - // 4. 组装节点信息 | ||
| 283 | - return convertList(nextFlowNodes, node -> { | ||
| 284 | - // 4.1 获取当前节点的候选用户 ID | ||
| 285 | - List<Long> candidateUserIds = getTaskCandidateUserList(bpmnModel, node.getId(), | ||
| 286 | - loginUserId, historicProcessInstance.getProcessDefinitionId(), processVariables); | ||
| 287 | - | ||
| 288 | - // 4.2 组装候选用户信息 | ||
| 289 | - List<UserSimpleBaseVO> candidateUsers = convertList(candidateUserIds, userId -> { | ||
| 290 | - AdminUserRespDTO user = userMap.get(userId); | ||
| 291 | - if (user != null) { | ||
| 292 | - return BpmProcessInstanceConvert.INSTANCE.buildUser(userId, userMap, deptMap); | ||
| 293 | - } | ||
| 294 | - return null; | ||
| 295 | - }); | ||
| 296 | - | ||
| 297 | - // 4.3 构建节点信息 | ||
| 298 | - return new ActivityNode() | ||
| 299 | - .setNodeType(BpmSimpleModelNodeTypeEnum.APPROVE_NODE.getType()) | ||
| 300 | - .setId(node.getId()) | ||
| 301 | - .setName(node.getName()) | ||
| 302 | - .setStatus(BpmTaskStatusEnum.RUNNING.getStatus()) | ||
| 303 | - .setCandidateStrategy(BpmnModelUtils.parseCandidateStrategy(node)) | ||
| 304 | - .setCandidateUsers(candidateUsers); | ||
| 305 | - }); | 281 | + nextActivityNodes.forEach(node -> node.setCandidateUsers(convertList(node.getCandidateUserIds(), userId -> { |
| 282 | + AdminUserRespDTO user = userMap.get(userId); | ||
| 283 | + if (user != null) { | ||
| 284 | + return BpmProcessInstanceConvert.INSTANCE.buildUser(userId, userMap, deptMap); | ||
| 285 | + } | ||
| 286 | + return null; | ||
| 287 | + }))); | ||
| 288 | + return nextActivityNodes; | ||
| 306 | } | 289 | } |
| 307 | 290 | ||
| 308 | @Override | 291 | @Override |
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; | @@ -6,6 +6,7 @@ import cn.hutool.core.lang.Assert; | ||
| 6 | import cn.hutool.core.util.*; | 6 | import cn.hutool.core.util.*; |
| 7 | import cn.hutool.extra.spring.SpringUtil; | 7 | import cn.hutool.extra.spring.SpringUtil; |
| 8 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 8 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 9 | +import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | ||
| 9 | import cn.iocoder.yudao.framework.common.util.date.DateUtils; | 10 | import cn.iocoder.yudao.framework.common.util.date.DateUtils; |
| 10 | import cn.iocoder.yudao.framework.common.util.number.NumberUtils; | 11 | import cn.iocoder.yudao.framework.common.util.number.NumberUtils; |
| 11 | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | 12 | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| @@ -599,8 +600,8 @@ public class BpmTaskServiceImpl implements BpmTaskService { | @@ -599,8 +600,8 @@ public class BpmTaskServiceImpl implements BpmTaskService { | ||
| 599 | */ | 600 | */ |
| 600 | private Map<String, Object> validateAndSetNextAssignees(String taskDefinitionKey, Map<String, Object> variables, BpmnModel bpmnModel, | 601 | private Map<String, Object> validateAndSetNextAssignees(String taskDefinitionKey, Map<String, Object> variables, BpmnModel bpmnModel, |
| 601 | Map<String, List<Long>> nextAssignees, ProcessInstance processInstance) { | 602 | Map<String, List<Long>> nextAssignees, ProcessInstance processInstance) { |
| 602 | - // Simple设计器第一个节点默认为发起人节点,不校验是否存在审批人 | ||
| 603 | - if (taskDefinitionKey.equals(START_USER_NODE_ID)) { | 603 | + // simple 设计器第一个节点默认为发起人节点,不校验是否存在审批人 |
| 604 | + if (Objects.equals(taskDefinitionKey, START_USER_NODE_ID)) { | ||
| 604 | return variables; | 605 | return variables; |
| 605 | } | 606 | } |
| 606 | // 1. 获取下一个将要执行的节点集合 | 607 | // 1. 获取下一个将要执行的节点集合 |
| @@ -608,15 +609,13 @@ public class BpmTaskServiceImpl implements BpmTaskService { | @@ -608,15 +609,13 @@ public class BpmTaskServiceImpl implements BpmTaskService { | ||
| 608 | List<FlowNode> nextFlowNodes = getNextFlowNodes(flowElement, bpmnModel, variables); | 609 | List<FlowNode> nextFlowNodes = getNextFlowNodes(flowElement, bpmnModel, variables); |
| 609 | 610 | ||
| 610 | // 2. 校验选择的下一个节点的审批人,是否合法 | 611 | // 2. 校验选择的下一个节点的审批人,是否合法 |
| 611 | - Map<String, List<Long>> processVariables; | ||
| 612 | for (FlowNode nextFlowNode : nextFlowNodes) { | 612 | for (FlowNode nextFlowNode : nextFlowNodes) { |
| 613 | Integer candidateStrategy = parseCandidateStrategy(nextFlowNode); | 613 | Integer candidateStrategy = parseCandidateStrategy(nextFlowNode); |
| 614 | // 2.1 情况一:如果节点中的审批人策略为 发起人自选 | 614 | // 2.1 情况一:如果节点中的审批人策略为 发起人自选 |
| 615 | if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy())) { | 615 | if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy())) { |
| 616 | - // 先从历史中获取审批人,在发起人会把所有的审批人保存到历史中,这里从历史中获取 | ||
| 617 | - processVariables = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables()); | ||
| 618 | // 特殊:如果当前节点已经存在审批人,则不允许覆盖 | 616 | // 特殊:如果当前节点已经存在审批人,则不允许覆盖 |
| 619 | - if (processVariables != null && CollUtil.isNotEmpty(processVariables.get(nextFlowNode.getId()))) { | 617 | + Map<String, List<Long>> startUserSelectAssignees = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables()); |
| 618 | + if (startUserSelectAssignees != null && CollUtil.isNotEmpty(startUserSelectAssignees.get(nextFlowNode.getId()))) { | ||
| 620 | continue; | 619 | continue; |
| 621 | } | 620 | } |
| 622 | // 如果节点存在,但未配置审批人 | 621 | // 如果节点存在,但未配置审批人 |
| @@ -624,28 +623,31 @@ public class BpmTaskServiceImpl implements BpmTaskService { | @@ -624,28 +623,31 @@ public class BpmTaskServiceImpl implements BpmTaskService { | ||
| 624 | if (CollUtil.isEmpty(assignees)) { | 623 | if (CollUtil.isEmpty(assignees)) { |
| 625 | throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName()); | 624 | throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName()); |
| 626 | } | 625 | } |
| 626 | + | ||
| 627 | // 设置 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES | 627 | // 设置 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES |
| 628 | - if (processVariables == null) { | ||
| 629 | - processVariables = new HashMap<>(); | 628 | + if (startUserSelectAssignees == null) { |
| 629 | + startUserSelectAssignees = new HashMap<>(); | ||
| 630 | } | 630 | } |
| 631 | - processVariables.put(nextFlowNode.getId(), assignees); | ||
| 632 | - variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, processVariables); | 631 | + startUserSelectAssignees.put(nextFlowNode.getId(), assignees); |
| 632 | + variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, startUserSelectAssignees); | ||
| 633 | + continue; | ||
| 633 | } | 634 | } |
| 635 | + | ||
| 634 | // 2.2 情况二:如果节点中的审批人策略为 审批人,在审批时选择下一个节点的审批人,并且该节点的审批人为空 | 636 | // 2.2 情况二:如果节点中的审批人策略为 审批人,在审批时选择下一个节点的审批人,并且该节点的审批人为空 |
| 635 | if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.APPROVE_USER_SELECT.getStrategy())) { | 637 | if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.APPROVE_USER_SELECT.getStrategy())) { |
| 636 | - // 获取审批人自选的历史变量 | ||
| 637 | - processVariables = FlowableUtils.getApproveUserSelectAssignees(processInstance.getProcessVariables()); | ||
| 638 | // 如果节点存在,但未配置审批人 | 638 | // 如果节点存在,但未配置审批人 |
| 639 | + Map<String, List<Long>> approveUserSelectAssignees = FlowableUtils.getApproveUserSelectAssignees(processInstance.getProcessVariables()); | ||
| 639 | List<Long> assignees = nextAssignees != null ? nextAssignees.get(nextFlowNode.getId()) : null; | 640 | List<Long> assignees = nextAssignees != null ? nextAssignees.get(nextFlowNode.getId()) : null; |
| 640 | if (CollUtil.isEmpty(assignees)) { | 641 | if (CollUtil.isEmpty(assignees)) { |
| 641 | throw exception(PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName()); | 642 | throw exception(PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName()); |
| 642 | } | 643 | } |
| 643 | - if (processVariables == null) { | ||
| 644 | - processVariables = new HashMap<>(); | ||
| 645 | - } | 644 | + |
| 646 | // 设置 PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES | 645 | // 设置 PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES |
| 647 | - processVariables.put(nextFlowNode.getId(), assignees); | ||
| 648 | - variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES, processVariables); | 646 | + if (approveUserSelectAssignees == null) { |
| 647 | + approveUserSelectAssignees = new HashMap<>(); | ||
| 648 | + } | ||
| 649 | + approveUserSelectAssignees.put(nextFlowNode.getId(), assignees); | ||
| 650 | + variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES, approveUserSelectAssignees); | ||
| 649 | } | 651 | } |
| 650 | } | 652 | } |
| 651 | return variables; | 653 | return variables; |