From 38607f0a3e91ef401f49d489a4289fee31477452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=84=95=E8=B6=85?= <14155701+wu-huanchao@user.noreply.gitee.com> Date: Fri, 23 May 2025 16:01:51 +0800 Subject: [PATCH] =?UTF-8?q?fix(bpm):=E8=A7=A3=E5=86=B3=E7=94=B1=E4=BA=8E?= =?UTF-8?q?=E9=80=80=E5=9B=9E=E7=9A=84=E4=BB=BB=E5=8A=A1=E4=B8=AD=E5=B7=B2?= =?UTF-8?q?=E6=9C=89=E8=87=AA=E9=80=89=E5=AE=A1=E6=89=B9=E4=BA=BA=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=95=B0=E6=8D=AE=EF=BC=8C=E5=AF=BC=E8=87=B4=E9=80=80?= =?UTF-8?q?=E5=9B=9E=E5=90=8E=E9=87=8D=E6=96=B0=E8=87=AA=E9=80=89=E5=AE=A1?= =?UTF-8?q?=E6=89=B9=E4=BA=BA=E7=9B=B8=E5=85=B3=E6=95=B0=E6=8D=AE=E8=A2=AB?= =?UTF-8?q?=E6=97=A7=E6=95=B0=E6=8D=AE=E5=BD=B1=E5=93=8D=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BpmParallelMultiInstanceBehavior.java | 2 +- .../flowable/core/util/BpmnModelUtils.java | 42 +++++++++++++++++++ .../bpm/service/task/BpmTaskServiceImpl.java | 30 +++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/behavior/BpmParallelMultiInstanceBehavior.java b/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/behavior/BpmParallelMultiInstanceBehavior.java index 57f4d393f3..5ba75c0524 100644 --- a/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/behavior/BpmParallelMultiInstanceBehavior.java +++ b/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/behavior/BpmParallelMultiInstanceBehavior.java @@ -59,7 +59,7 @@ public class BpmParallelMultiInstanceBehavior extends ParallelMultiInstanceBehav // 第二步,获取任务的所有处理人 @SuppressWarnings("unchecked") - Set assigneeUserIds = (Set) execution.getVariable(super.collectionVariable, Set.class); + Set assigneeUserIds = (Set) execution.getVariableLocal(super.collectionVariable, Set.class); if (assigneeUserIds == null) { assigneeUserIds = taskCandidateInvoker.calculateUsersByTask(execution); if (CollUtil.isEmpty(assigneeUserIds)) { diff --git a/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/BpmnModelUtils.java b/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/BpmnModelUtils.java index 1cccf18f04..adc5741c50 100644 --- a/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/BpmnModelUtils.java +++ b/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/BpmnModelUtils.java @@ -755,6 +755,48 @@ public class BpmnModelUtils { return userTaskList; } + /** + * 获取当前元素后可能执行到的所有userTask + * + * @param startElement 起始元素 + * @param bpmnModel 流程模型 + * @return 当前元素后可能执行到的所有userTask,不包含自身 + */ + public static Set findReachableUserTasks(FlowElement startElement, BpmnModel bpmnModel) { + Set userTasks = new HashSet<>(); + Set visited = new HashSet<>(); + + if (startElement instanceof FlowNode) { + List outgoing = ((FlowNode) startElement).getOutgoingFlows(); + for (SequenceFlow flow : outgoing) { + FlowElement target = bpmnModel.getMainProcess().getFlowElement(flow.getTargetRef()); + dfs(target, bpmnModel, userTasks, visited); + } + } + return userTasks; + } + + private static void dfs(FlowElement current, BpmnModel model, Set userTasks, Set visited) { + if (current == null || visited.contains(current.getId())) { + return; + } + + visited.add(current.getId()); + + if (current instanceof UserTask) { + userTasks.add((UserTask) current); + } + + if (current instanceof FlowNode) { + List outgoingFlows = ((FlowNode) current).getOutgoingFlows(); + for (SequenceFlow flow : outgoingFlows) { + String targetRef = flow.getTargetRef(); + FlowElement targetElement = model.getMainProcess().getFlowElement(targetRef); + dfs(targetElement, model, userTasks, visited); + } + } + + } // ========== BPMN 流程预测相关的方法 ========== /** diff --git a/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java b/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java index a02c945fcc..163d10bfa2 100644 --- a/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java +++ b/yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java @@ -925,6 +925,36 @@ public class BpmTaskServiceImpl implements BpmTaskService { .processInstanceId(currentTask.getProcessInstanceId()) .moveExecutionToActivityId(mainExecutionId, reqVO.getTargetTaskDefinitionKey()) .changeState(); + // 5. 清除'审批人自选'策略相关数据 + // 移除退回后不会自动通过的节点的审批人数据 + // 根据自动去重设置执行相关操作 + BpmProcessDefinitionInfoDO processDefinitionInfo = bpmProcessDefinitionService.getProcessDefinitionInfo(currentTask.getProcessDefinitionId()); + BpmnModel bpmnModel = bpmProcessDefinitionService.getProcessDefinitionBpmnModel(currentTask.getProcessDefinitionId()); + ProcessInstance processInstance = processInstanceService.getProcessInstance(currentTask.getProcessInstanceId()); + if (BpmAutoApproveTypeEnum.NONE.getType().equals(processDefinitionInfo.getAutoApprovalType())) { + // 不自动通过,清除 targetElement 后所有节点的审批人数据 + Map> approveUserSelectAssignees = FlowableUtils.getApproveUserSelectAssignees(processInstance); + if (ObjectUtil.isNotEmpty(approveUserSelectAssignees)) { + Set reachableUserTasks = findReachableUserTasks(targetElement, bpmnModel); + for (UserTask reachableUserTask : reachableUserTasks) { + approveUserSelectAssignees.remove(reachableUserTask.getId()); + } + runtimeService.setVariable(processInstance.getProcessInstanceId(),BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES,approveUserSelectAssignees); + } + } else if (BpmAutoApproveTypeEnum.APPROVE_SEQUENT.getType().equals(processDefinitionInfo.getAutoApprovalType())) { + // TODO 优化:去重规则为连续审批的节点自动通过,看退回到目标节点后会自动通过走到哪里,将走到的位置之后的节点自选审批人数据清空 + // 暂且清除 targetElement 后所有节点的审批人数据 + // 自动通过时若候选人策略为'审批人自选',会因为缺失自选审批人报错,需要重新选择审批人手动审批 + Map> approveUserSelectAssignees = FlowableUtils.getApproveUserSelectAssignees(processInstance); + if (ObjectUtil.isNotEmpty(approveUserSelectAssignees)) { + Set reachableUserTasks = findReachableUserTasks(targetElement, bpmnModel); + for (UserTask reachableUserTask : reachableUserTasks) { + approveUserSelectAssignees.remove(reachableUserTask.getId()); + } + runtimeService.setVariable(processInstance.getProcessInstanceId(),BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES,approveUserSelectAssignees); + } + } + // 若为自动通过,还会走到当前节点,不清除节点自选审批人数据 } @Override