Commit 9dfed5365b2b3216406e53603ffd9cab397003c2
1 parent
4d35a272
feat:
新增:审批人自选标识, 新增:审批人自选策略, 新增:获得流程实例的审批用户选择的下一个节点的审批人 新增:错误码1_009_004_007,下一个节点审批人未配置
Showing
5 changed files
with
128 additions
and
22 deletions
yudao-module-bpm/yudao-module-bpm-api/src/main/java/cn/iocoder/yudao/module/bpm/enums/ErrorCodeConstants.java
| @@ -40,7 +40,7 @@ public interface ErrorCodeConstants { | @@ -40,7 +40,7 @@ public interface ErrorCodeConstants { | ||
| 40 | ErrorCode PROCESS_INSTANCE_START_USER_CAN_START = new ErrorCode(1_009_004_005, "发起流程失败,你没有权限发起该流程"); | 40 | ErrorCode PROCESS_INSTANCE_START_USER_CAN_START = new ErrorCode(1_009_004_005, "发起流程失败,你没有权限发起该流程"); |
| 41 | ErrorCode PROCESS_INSTANCE_CANCEL_FAIL_NOT_ALLOW = new ErrorCode(1_009_004_005, "流程取消失败,该流程不允许取消"); | 41 | ErrorCode PROCESS_INSTANCE_CANCEL_FAIL_NOT_ALLOW = new ErrorCode(1_009_004_005, "流程取消失败,该流程不允许取消"); |
| 42 | ErrorCode PROCESS_INSTANCE_HTTP_TRIGGER_CALL_ERROR = new ErrorCode(1_009_004_006, "流程 Http 触发器请求调用失败"); | 42 | ErrorCode PROCESS_INSTANCE_HTTP_TRIGGER_CALL_ERROR = new ErrorCode(1_009_004_006, "流程 Http 触发器请求调用失败"); |
| 43 | - ErrorCode PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG = new ErrorCode(1_009_004_007, "任务({})的下一个节点审批人未配置"); | 43 | + ErrorCode PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG = new ErrorCode(1_009_004_007, "任务({})的下一个执行节点审批人未配置"); |
| 44 | 44 | ||
| 45 | // ========== 流程任务 1-009-005-000 ========== | 45 | // ========== 流程任务 1-009-005-000 ========== |
| 46 | ErrorCode TASK_OPERATE_FAIL_ASSIGN_NOT_SELF = new ErrorCode(1_009_005_001, "操作失败,原因:该任务的审批人不是你"); | 46 | ErrorCode TASK_OPERATE_FAIL_ASSIGN_NOT_SELF = new ErrorCode(1_009_005_001, "操作失败,原因:该任务的审批人不是你"); |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateApproveUserSelectStrategy.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 4 | +import cn.hutool.core.lang.Assert; | ||
| 5 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user.BpmTaskCandidateUserStrategy; | ||
| 6 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; | ||
| 7 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils; | ||
| 8 | +import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; | ||
| 9 | +import com.google.common.collect.Sets; | ||
| 10 | +import jakarta.annotation.Resource; | ||
| 11 | +import org.flowable.bpmn.model.BpmnModel; | ||
| 12 | +import org.flowable.engine.delegate.DelegateExecution; | ||
| 13 | +import org.flowable.engine.runtime.ProcessInstance; | ||
| 14 | +import org.springframework.context.annotation.Lazy; | ||
| 15 | +import org.springframework.stereotype.Component; | ||
| 16 | + | ||
| 17 | +import java.util.LinkedHashSet; | ||
| 18 | +import java.util.List; | ||
| 19 | +import java.util.Map; | ||
| 20 | + | ||
| 21 | +/** | ||
| 22 | + * 审批人自选 {@link BpmTaskCandidateUserStrategy} 实现类 | ||
| 23 | + * 审批人在审批时选择下一个节点的审批人 | ||
| 24 | + * @author smallNorthLee | ||
| 25 | + */ | ||
| 26 | +@Component | ||
| 27 | +public class BpmTaskCandidateApproveUserSelectStrategy extends AbstractBpmTaskCandidateDeptLeaderStrategy { | ||
| 28 | + | ||
| 29 | + @Resource | ||
| 30 | + @Lazy // 延迟加载,避免循环依赖 | ||
| 31 | + private BpmProcessInstanceService processInstanceService; | ||
| 32 | + | ||
| 33 | + @Override | ||
| 34 | + public BpmTaskCandidateStrategyEnum getStrategy() { | ||
| 35 | + return BpmTaskCandidateStrategyEnum.APPROVE_USER_SELECT; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + @Override | ||
| 39 | + public void validateParam(String param) {} | ||
| 40 | + | ||
| 41 | + @Override | ||
| 42 | + public boolean isParamRequired() { | ||
| 43 | + return false; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + @Override | ||
| 47 | + public LinkedHashSet<Long> calculateUsersByTask(DelegateExecution execution, String param) { | ||
| 48 | + ProcessInstance processInstance = processInstanceService.getProcessInstance(execution.getProcessInstanceId()); | ||
| 49 | + Assert.notNull(processInstance, "流程实例({})不能为空", execution.getProcessInstanceId()); | ||
| 50 | + Map<String, List<Long>> approveUserSelectAssignees = FlowableUtils.getApproveUserSelectAssignees(processInstance); | ||
| 51 | + Assert.notNull(approveUserSelectAssignees, "流程实例({}) 的下一个执行节点审批人不能为空", | ||
| 52 | + execution.getProcessInstanceId()); | ||
| 53 | + // 获得审批人 | ||
| 54 | + List<Long> assignees = approveUserSelectAssignees.get(execution.getCurrentActivityId()); | ||
| 55 | + return CollUtil.isNotEmpty(assignees) ? new LinkedHashSet<>(assignees) : Sets.newLinkedHashSet(); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + @Override | ||
| 59 | + public LinkedHashSet<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId, String param, | ||
| 60 | + Long startUserId, String processDefinitionId, Map<String, Object> processVariables) { | ||
| 61 | + if (processVariables == null) { | ||
| 62 | + return Sets.newLinkedHashSet(); | ||
| 63 | + } | ||
| 64 | + Map<String, List<Long>> approveUserSelectAssignees = FlowableUtils.getApproveUserSelectAssignees(processVariables); | ||
| 65 | + if (approveUserSelectAssignees == null) { | ||
| 66 | + return Sets.newLinkedHashSet(); | ||
| 67 | + } | ||
| 68 | + // 获得审批人 | ||
| 69 | + List<Long> assignees = approveUserSelectAssignees.get(activityId); | ||
| 70 | + return CollUtil.isNotEmpty(assignees) ? new LinkedHashSet<>(assignees) : Sets.newLinkedHashSet(); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | +} |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/enums/BpmTaskCandidateStrategyEnum.java
| @@ -24,6 +24,7 @@ public enum BpmTaskCandidateStrategyEnum implements ArrayValuable<Integer> { | @@ -24,6 +24,7 @@ public enum BpmTaskCandidateStrategyEnum implements ArrayValuable<Integer> { | ||
| 24 | MULTI_DEPT_LEADER_MULTI(23, "连续多级部门的负责人"), | 24 | MULTI_DEPT_LEADER_MULTI(23, "连续多级部门的负责人"), |
| 25 | POST(22, "岗位"), | 25 | POST(22, "岗位"), |
| 26 | USER(30, "用户"), | 26 | USER(30, "用户"), |
| 27 | + APPROVE_USER_SELECT(34, "审批人自选"), // 审批人在审批时选择下一个节点的审批人 | ||
| 27 | START_USER_SELECT(35, "发起人自选"), // 申请人自己,可在提交申请时选择此节点的审批人 | 28 | START_USER_SELECT(35, "发起人自选"), // 申请人自己,可在提交申请时选择此节点的审批人 |
| 28 | START_USER(36, "发起人自己"), // 申请人自己, 一般紧挨开始节点,常用于发起人信息审核场景 | 29 | START_USER(36, "发起人自己"), // 申请人自己, 一般紧挨开始节点,常用于发起人信息审核场景 |
| 29 | START_USER_DEPT_LEADER(37, "发起人部门负责人"), | 30 | START_USER_DEPT_LEADER(37, "发起人部门负责人"), |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/FlowableUtils.java
| @@ -201,6 +201,31 @@ public class FlowableUtils { | @@ -201,6 +201,31 @@ public class FlowableUtils { | ||
| 201 | } | 201 | } |
| 202 | 202 | ||
| 203 | /** | 203 | /** |
| 204 | + * 获得流程实例的审批用户选择的下一个节点的审批人 Map | ||
| 205 | + * | ||
| 206 | + * @param processInstance 流程实例 | ||
| 207 | + * @return 审批用户选择的下一个节点的审批人Map | ||
| 208 | + */ | ||
| 209 | + public static Map<String, List<Long>> getApproveUserSelectAssignees(ProcessInstance processInstance) { | ||
| 210 | + return processInstance != null ? getApproveUserSelectAssignees(processInstance.getProcessVariables()) : null; | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + /** | ||
| 214 | + * 获得流程实例的审批用户选择的下一个节点的审批人 Map | ||
| 215 | + * | ||
| 216 | + * @param processVariables 流程变量 | ||
| 217 | + * @return 审批用户选择的下一个节点的审批人Map Map | ||
| 218 | + */ | ||
| 219 | + @SuppressWarnings("unchecked") | ||
| 220 | + public static Map<String, List<Long>> getApproveUserSelectAssignees(Map<String, Object> processVariables) { | ||
| 221 | + if (processVariables == null) { | ||
| 222 | + return null; | ||
| 223 | + } | ||
| 224 | + return (Map<String, List<Long>>) processVariables.get( | ||
| 225 | + BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES); | ||
| 226 | + } | ||
| 227 | + | ||
| 228 | + /** | ||
| 204 | * 获得流程实例的摘要 | 229 | * 获得流程实例的摘要 |
| 205 | * | 230 | * |
| 206 | * 仅有 {@link BpmModelFormTypeEnum#getType()} 表单,才有摘要。 | 231 | * 仅有 {@link BpmModelFormTypeEnum#getType()} 表单,才有摘要。 |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java
| @@ -528,22 +528,9 @@ public class BpmTaskServiceImpl implements BpmTaskService { | @@ -528,22 +528,9 @@ public class BpmTaskServiceImpl implements BpmTaskService { | ||
| 528 | // 2.3 调用 BPM complete 去完成任务 | 528 | // 2.3 调用 BPM complete 去完成任务 |
| 529 | // 其中,variables 是存储动态表单到 local 任务级别。过滤一下,避免 ProcessInstance 系统级的变量被占用 | 529 | // 其中,variables 是存储动态表单到 local 任务级别。过滤一下,避免 ProcessInstance 系统级的变量被占用 |
| 530 | if (CollUtil.isNotEmpty(reqVO.getVariables())) { | 530 | if (CollUtil.isNotEmpty(reqVO.getVariables())) { |
| 531 | - Map<String, Object> variables = FlowableUtils.filterTaskFormVariable(reqVO.getVariables()); | ||
| 532 | // 校验传递的参数中是否为下一个将要执行的任务节点 | 531 | // 校验传递的参数中是否为下一个将要执行的任务节点 |
| 533 | - validateAndSetNextAssignees(task.getTaskDefinitionKey(), reqVO.getVariables(), bpmnModel, reqVO.getNextAssignees(), instance); | ||
| 534 | - // 如果有下一个审批人,则设置到流程变量中 | ||
| 535 | - if (CollUtil.isNotEmpty(reqVO.getNextAssignees())) { | ||
| 536 | - // 获取实例中的全部节点数据,避免后续节点的审批人被覆盖 | ||
| 537 | - // TODO @小北:这里有个需要讨论的点,微信哈; | ||
| 538 | - // TODO 因为 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES 定位是发起人,那么审批人选择的,放在 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES。目前想到两个方案: | ||
| 539 | - // TODO 方案一:增加一个 PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES,然后设置到这里面。然后,BpmTaskCandidateStartUserSelectStrategy 也从这里读 | ||
| 540 | - // TODO 方案二:也是增加一个 PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES,根据节点审批人类型,放到 PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES、PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES | ||
| 541 | - // TODO 方案三:融合成 PROCESS_INSTANCE_VARIABLE_USER_SELECT_ASSIGNEES,不再区分是发起人选择、还是审批人选择。 | ||
| 542 | - Map<String, List<Long>> hisProcessVariables = FlowableUtils.getStartUserSelectAssignees(instance.getProcessVariables()); | ||
| 543 | - hisProcessVariables.putAll(reqVO.getNextAssignees()); | ||
| 544 | - variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, hisProcessVariables); | ||
| 545 | - } | ||
| 546 | - runtimeService.setVariables(task.getProcessInstanceId(), variables); | 532 | + Map<String, Object> variables = validateAndSetNextAssignees(task.getTaskDefinitionKey(), reqVO.getVariables(), |
| 533 | + bpmnModel, reqVO.getNextAssignees(), instance); | ||
| 547 | taskService.complete(task.getId(), variables, true); | 534 | taskService.complete(task.getId(), variables, true); |
| 548 | } else { | 535 | } else { |
| 549 | taskService.complete(task.getId()); | 536 | taskService.complete(task.getId()); |
| @@ -566,21 +553,29 @@ public class BpmTaskServiceImpl implements BpmTaskService { | @@ -566,21 +553,29 @@ public class BpmTaskServiceImpl implements BpmTaskService { | ||
| 566 | * @param nextAssignees 下一个节点审批人集合(参数) | 553 | * @param nextAssignees 下一个节点审批人集合(参数) |
| 567 | * @param processInstance 流程实例 | 554 | * @param processInstance 流程实例 |
| 568 | */ | 555 | */ |
| 569 | - private void validateAndSetNextAssignees(String taskDefinitionKey, Map<String, Object> variables, BpmnModel bpmnModel, | 556 | + private Map<String, Object> validateAndSetNextAssignees(String taskDefinitionKey, Map<String, Object> variables, BpmnModel bpmnModel, |
| 570 | Map<String, List<Long>> nextAssignees, ProcessInstance processInstance) { | 557 | Map<String, List<Long>> nextAssignees, ProcessInstance processInstance) { |
| 571 | // 1. 获取当前任务节点的信息 | 558 | // 1. 获取当前任务节点的信息 |
| 572 | FlowElement flowElement = bpmnModel.getFlowElement(taskDefinitionKey); | 559 | FlowElement flowElement = bpmnModel.getFlowElement(taskDefinitionKey); |
| 573 | // 2. 获取下一个将要执行的节点集合 | 560 | // 2. 获取下一个将要执行的节点集合 |
| 574 | List<FlowNode> nextFlowNodes = getNextFlowNodes(flowElement, bpmnModel, variables); | 561 | List<FlowNode> nextFlowNodes = getNextFlowNodes(flowElement, bpmnModel, variables); |
| 562 | + // 获取流程实例中的审批人自选审批人 | ||
| 563 | + Map<String, List<Long>> hisProcessVariables = FlowableUtils.getApproveUserSelectAssignees(processInstance.getProcessVariables()); | ||
| 564 | + if (CollUtil.isEmpty(hisProcessVariables)){ | ||
| 565 | + hisProcessVariables = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables()); | ||
| 566 | + } | ||
| 567 | + // 校验通过的全部节点和审批人 | ||
| 568 | + Map<String, List<Long>> allNextAssignees = new HashMap<>(); | ||
| 575 | // 3. 循环下一个将要执行的节点集合 | 569 | // 3. 循环下一个将要执行的节点集合 |
| 576 | for (FlowNode nextFlowNode : nextFlowNodes) { | 570 | for (FlowNode nextFlowNode : nextFlowNodes) { |
| 577 | // 3.1 获取下一个将要执行节点中的审批人策略 | 571 | // 3.1 获取下一个将要执行节点中的审批人策略 |
| 578 | Integer candidateStrategy = parseCandidateStrategy(nextFlowNode); | 572 | Integer candidateStrategy = parseCandidateStrategy(nextFlowNode); |
| 579 | - // 3.2 获取流程实例中的发起人自选审批人 | ||
| 580 | - Map<String, List<Long>> startUserSelectAssignees = FlowableUtils.getStartUserSelectAssignees(processInstance.getProcessVariables()); | ||
| 581 | - List<Long> startUserSelectAssignee = startUserSelectAssignees.get(nextFlowNode.getId()); | ||
| 582 | - // 3.3 如果节点中的审批人策略为 发起人自选,并且该节点的审批人为空 | ||
| 583 | - if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy()) && CollUtil.isEmpty(startUserSelectAssignee)) { | 573 | + // 3.2 获取流程实例中的审批人自选审批人 |
| 574 | + List<Long> approveUserSelectAssignee = hisProcessVariables.get(nextFlowNode.getId()); | ||
| 575 | + // 3.3 如果节点中的审批人策略为 发起人自选或者审批人自选,并且该节点的审批人为空 | ||
| 576 | + if (ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.START_USER_SELECT.getStrategy()) | ||
| 577 | + || ObjUtil.equals(candidateStrategy, BpmTaskCandidateStrategyEnum.APPROVE_USER_SELECT.getStrategy()) | ||
| 578 | + && CollUtil.isEmpty(approveUserSelectAssignee)) { | ||
| 584 | // 判断节点是否为执行节点,仅校验节点 | 579 | // 判断节点是否为执行节点,仅校验节点 |
| 585 | if (!nextAssignees.containsKey(nextFlowNode.getId())) { | 580 | if (!nextAssignees.containsKey(nextFlowNode.getId())) { |
| 586 | throw exception(TASK_TARGET_NODE_NOT_EXISTS, nextFlowNode.getName()); | 581 | throw exception(TASK_TARGET_NODE_NOT_EXISTS, nextFlowNode.getName()); |
| @@ -589,9 +584,21 @@ public class BpmTaskServiceImpl implements BpmTaskService { | @@ -589,9 +584,21 @@ public class BpmTaskServiceImpl implements BpmTaskService { | ||
| 589 | if (CollUtil.isEmpty(nextAssignees.get(nextFlowNode.getId()))) { | 584 | if (CollUtil.isEmpty(nextAssignees.get(nextFlowNode.getId()))) { |
| 590 | throw exception(PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName()); | 585 | throw exception(PROCESS_INSTANCE_APPROVE_USER_SELECT_ASSIGNEES_NOT_CONFIG, nextFlowNode.getName()); |
| 591 | } | 586 | } |
| 587 | + // 4.3.3 校验通过的全部节点和审批人 | ||
| 588 | + allNextAssignees.put(nextFlowNode.getId(), nextAssignees.get(nextFlowNode.getId())); | ||
| 592 | } | 589 | } |
| 593 | // TODO @小北:加一个“审批人选择”的校验; | 590 | // TODO @小北:加一个“审批人选择”的校验; |
| 594 | } | 591 | } |
| 592 | + // variables 是存储动态表单到 local 任务级别。过滤一下,避免 ProcessInstance 系统级的变量被占用 | ||
| 593 | + Map<String, Object> newVariables = FlowableUtils.filterTaskFormVariable(variables); | ||
| 594 | + // 4. 如果下个节点审批参数不为空,则设置流程变量 | ||
| 595 | + if (CollUtil.isNotEmpty(allNextAssignees)) { | ||
| 596 | + // 获取实例中的全部节点数据,避免后续节点的审批人被覆盖 | ||
| 597 | + hisProcessVariables.putAll(allNextAssignees); | ||
| 598 | + newVariables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_APPROVE_USER_SELECT_ASSIGNEES, hisProcessVariables); | ||
| 599 | + runtimeService.setVariables(processInstance.getProcessInstanceId(), variables); | ||
| 600 | + } | ||
| 601 | + return newVariables; | ||
| 595 | } | 602 | } |
| 596 | 603 | ||
| 597 | /** | 604 | /** |