Commit c73d7fe32b150183866ecd2a448c04a4129328ad
1 parent
0c7fd9e1
【代码重构】工作流:BpmTaskCandidateStrategy 拆分成 calculateUsers、calculateUsersByTask、calcu…
…lateUsersByActivity,定位更明确
Showing
40 changed files
with
759 additions
and
345 deletions
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/definition/BpmProcessDefinitionController.java
| ... | ... | @@ -9,7 +9,7 @@ import cn.iocoder.yudao.module.bpm.convert.definition.BpmProcessDefinitionConver |
| 9 | 9 | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmCategoryDO; |
| 10 | 10 | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO; |
| 11 | 11 | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; |
| 12 | -import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.BpmTaskCandidateStartUserSelectStrategy; | |
| 12 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept.BpmTaskCandidateStartUserSelectStrategy; | |
| 13 | 13 | import cn.iocoder.yudao.module.bpm.service.definition.BpmCategoryService; |
| 14 | 14 | import cn.iocoder.yudao.module.bpm.service.definition.BpmFormService; |
| 15 | 15 | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/convert/task/BpmProcessInstanceConvert.java
| ... | ... | @@ -17,7 +17,7 @@ import cn.iocoder.yudao.module.bpm.convert.definition.BpmProcessDefinitionConver |
| 17 | 17 | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmCategoryDO; |
| 18 | 18 | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; |
| 19 | 19 | import cn.iocoder.yudao.module.bpm.event.BpmProcessInstanceStatusEvent; |
| 20 | -import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.BpmTaskCandidateStartUserSelectStrategy; | |
| 20 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept.BpmTaskCandidateStartUserSelectStrategy; | |
| 21 | 21 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils; |
| 22 | 22 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils; |
| 23 | 23 | import cn.iocoder.yudao.module.bpm.service.message.dto.BpmMessageSendWhenProcessInstanceApproveReqDTO; | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/behavior/BpmParallelMultiInstanceBehavior.java
| ... | ... | @@ -53,7 +53,7 @@ public class BpmParallelMultiInstanceBehavior extends ParallelMultiInstanceBehav |
| 53 | 53 | @SuppressWarnings("unchecked") |
| 54 | 54 | Set<Long> assigneeUserIds = (Set<Long>) execution.getVariable(super.collectionVariable, Set.class); |
| 55 | 55 | if (assigneeUserIds == null) { |
| 56 | - assigneeUserIds = taskCandidateInvoker.calculateUsers(execution); | |
| 56 | + assigneeUserIds = taskCandidateInvoker.calculateUsersByTask(execution); | |
| 57 | 57 | execution.setVariable(super.collectionVariable, assigneeUserIds); |
| 58 | 58 | if (CollUtil.isEmpty(assigneeUserIds)) { |
| 59 | 59 | // 特殊:如果没有处理人的情况下,至少有一个 null 空元素,避免自动通过! | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/behavior/BpmSequentialMultiInstanceBehavior.java
| ... | ... | @@ -46,7 +46,7 @@ public class BpmSequentialMultiInstanceBehavior extends SequentialMultiInstanceB |
| 46 | 46 | @SuppressWarnings("unchecked") |
| 47 | 47 | Set<Long> assigneeUserIds = (Set<Long>) execution.getVariable(super.collectionVariable, Set.class); |
| 48 | 48 | if (assigneeUserIds == null) { |
| 49 | - assigneeUserIds = taskCandidateInvoker.calculateUsers(execution); | |
| 49 | + assigneeUserIds = taskCandidateInvoker.calculateUsersByTask(execution); | |
| 50 | 50 | execution.setVariable(super.collectionVariable, assigneeUserIds); |
| 51 | 51 | if (CollUtil.isEmpty(assigneeUserIds)) { |
| 52 | 52 | // 特殊:如果没有处理人的情况下,至少有一个 null 空元素,避免自动通过! | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/behavior/BpmUserTaskActivityBehavior.java
| ... | ... | @@ -57,7 +57,7 @@ public class BpmUserTaskActivityBehavior extends UserTaskActivityBehavior { |
| 57 | 57 | |
| 58 | 58 | // 情况二,如果非多实例的任务,则计算任务处理人 |
| 59 | 59 | // 第一步,先计算可处理该任务的处理人们 |
| 60 | - Set<Long> candidateUserIds = taskCandidateInvoker.calculateUsers(execution); | |
| 60 | + Set<Long> candidateUserIds = taskCandidateInvoker.calculateUsersByTask(execution); | |
| 61 | 61 | if (CollUtil.isEmpty(candidateUserIds)) { |
| 62 | 62 | return null; |
| 63 | 63 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/BpmTaskCandidateInvoker.java
| ... | ... | @@ -18,6 +18,7 @@ import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| 18 | 18 | import com.google.common.annotations.VisibleForTesting; |
| 19 | 19 | import lombok.extern.slf4j.Slf4j; |
| 20 | 20 | import org.flowable.bpmn.model.BpmnModel; |
| 21 | +import org.flowable.bpmn.model.FlowElement; | |
| 21 | 22 | import org.flowable.bpmn.model.UserTask; |
| 22 | 23 | import org.flowable.engine.delegate.DelegateExecution; |
| 23 | 24 | import org.flowable.engine.runtime.ProcessInstance; |
| ... | ... | @@ -89,31 +90,66 @@ public class BpmTaskCandidateInvoker { |
| 89 | 90 | * @return 用户编号集合 |
| 90 | 91 | */ |
| 91 | 92 | @DataPermission(enable = false) // 忽略数据权限,避免因为过滤,导致找不到候选人 |
| 92 | - public Set<Long> calculateUsers(DelegateExecution execution) { | |
| 93 | + public Set<Long> calculateUsersByTask(DelegateExecution execution) { | |
| 93 | 94 | // 审批类型非人工审核时,不进行计算候选人。原因是:后续会自动通过、不通过 |
| 94 | - Integer approveType = BpmnModelUtils.parseApproveType(execution.getCurrentFlowElement()); | |
| 95 | + FlowElement flowElement = execution.getCurrentFlowElement(); | |
| 96 | + Integer approveType = BpmnModelUtils.parseApproveType(flowElement); | |
| 95 | 97 | if (ObjectUtils.equalsAny(approveType, |
| 96 | 98 | BpmUserTaskApproveTypeEnum.AUTO_APPROVE.getType(), |
| 97 | 99 | BpmUserTaskApproveTypeEnum.AUTO_REJECT.getType())) { |
| 98 | 100 | return new HashSet<>(); |
| 99 | 101 | } |
| 100 | 102 | |
| 101 | - Integer strategy = BpmnModelUtils.parseCandidateStrategy(execution.getCurrentFlowElement()); | |
| 102 | - String param = BpmnModelUtils.parseCandidateParam(execution.getCurrentFlowElement()); | |
| 103 | + Integer strategy = BpmnModelUtils.parseCandidateStrategy(flowElement); | |
| 104 | + String param = BpmnModelUtils.parseCandidateParam(flowElement); | |
| 103 | 105 | // 1.1 计算任务的候选人 |
| 104 | - Set<Long> userIds = getCandidateStrategy(strategy).calculateUsers(execution, param); | |
| 105 | - // 1.2 移除被禁用的用户 TODO @芋艿 在 calculateUsers 方法中默认已经移除了被禁用的用户, 这里还需要移除被禁用的用户吗? | |
| 106 | + Set<Long> userIds = getCandidateStrategy(strategy).calculateUsersByTask(execution, param); | |
| 107 | + // 1.2 移除被禁用的用户 | |
| 106 | 108 | removeDisableUsers(userIds); |
| 107 | 109 | |
| 108 | 110 | // 2. 候选人为空时,根据“审批人为空”的配置补充 |
| 109 | 111 | if (CollUtil.isEmpty(userIds)) { |
| 110 | 112 | userIds = getCandidateStrategy(BpmTaskCandidateStrategyEnum.ASSIGN_EMPTY.getStrategy()) |
| 111 | - .calculateUsers(execution, param); | |
| 113 | + .calculateUsersByTask(execution, param); | |
| 112 | 114 | // ASSIGN_EMPTY 策略,不需要移除被禁用的用户。原因是,再移除,可能会出现更没审批人了!!! |
| 113 | 115 | } |
| 114 | 116 | |
| 115 | 117 | // 3. 移除发起人的用户 |
| 116 | - removeStartUserIfSkip(execution, userIds); | |
| 118 | + ProcessInstance processInstance = SpringUtil.getBean(BpmProcessInstanceService.class) | |
| 119 | + .getProcessInstance(execution.getProcessInstanceId()); | |
| 120 | + Assert.notNull(processInstance, "流程实例({}) 不存在", execution.getProcessInstanceId()); | |
| 121 | + removeStartUserIfSkip(userIds, flowElement, Long.valueOf(processInstance.getStartUserId())); | |
| 122 | + return userIds; | |
| 123 | + } | |
| 124 | + | |
| 125 | + public Set<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId, | |
| 126 | + Long startUserId, String processDefinitionId, Map<String, Object> processVariables) { | |
| 127 | + // 审批类型非人工审核时,不进行计算候选人。原因是:后续会自动通过、不通过 | |
| 128 | + FlowElement flowElement = BpmnModelUtils.getFlowElementById(bpmnModel, activityId); | |
| 129 | + Integer approveType = BpmnModelUtils.parseApproveType(flowElement); | |
| 130 | + if (ObjectUtils.equalsAny(approveType, | |
| 131 | + BpmUserTaskApproveTypeEnum.AUTO_APPROVE.getType(), | |
| 132 | + BpmUserTaskApproveTypeEnum.AUTO_REJECT.getType())) { | |
| 133 | + return new HashSet<>(); | |
| 134 | + } | |
| 135 | + | |
| 136 | + Integer strategy = BpmnModelUtils.parseCandidateStrategy(flowElement); | |
| 137 | + String param = BpmnModelUtils.parseCandidateParam(flowElement); | |
| 138 | + // 1.1 计算任务的候选人 | |
| 139 | + Set<Long> userIds = getCandidateStrategy(strategy).calculateUsersByActivity(bpmnModel, activityId, param, | |
| 140 | + startUserId, processDefinitionId, processVariables); | |
| 141 | + // 1.2 移除被禁用的用户 | |
| 142 | + removeDisableUsers(userIds); | |
| 143 | + | |
| 144 | + // 2. 候选人为空时,根据“审批人为空”的配置补充 | |
| 145 | + if (CollUtil.isEmpty(userIds)) { | |
| 146 | + userIds = getCandidateStrategy(BpmTaskCandidateStrategyEnum.ASSIGN_EMPTY.getStrategy()) | |
| 147 | + .calculateUsersByActivity(bpmnModel, activityId, param, startUserId, processDefinitionId, processVariables); | |
| 148 | + // ASSIGN_EMPTY 策略,不需要移除被禁用的用户。原因是,再移除,可能会出现更没审批人了!!! | |
| 149 | + } | |
| 150 | + | |
| 151 | + // 3. 移除发起人的用户 | |
| 152 | + removeStartUserIfSkip(userIds, flowElement, startUserId); | |
| 117 | 153 | return userIds; |
| 118 | 154 | } |
| 119 | 155 | |
| ... | ... | @@ -134,25 +170,23 @@ public class BpmTaskCandidateInvoker { |
| 134 | 170 | * |
| 135 | 171 | * 注意:如果只有一个候选人,则不处理,避免无法审批 |
| 136 | 172 | * |
| 137 | - * @param execution 执行中的任务 | |
| 138 | 173 | * @param assigneeUserIds 当前分配的候选人 |
| 174 | + * @param flowElement 当前节点 | |
| 175 | + * @param startUserId 发起人 | |
| 139 | 176 | */ |
| 140 | 177 | @VisibleForTesting |
| 141 | - void removeStartUserIfSkip(DelegateExecution execution, Set<Long> assigneeUserIds) { | |
| 178 | + void removeStartUserIfSkip(Set<Long> assigneeUserIds, FlowElement flowElement, Long startUserId) { | |
| 142 | 179 | if (CollUtil.size(assigneeUserIds) <= 1) { |
| 143 | 180 | return; |
| 144 | 181 | } |
| 145 | - Integer assignStartUserHandlerType = BpmnModelUtils.parseAssignStartUserHandlerType(execution.getCurrentFlowElement()); | |
| 182 | + Integer assignStartUserHandlerType = BpmnModelUtils.parseAssignStartUserHandlerType(flowElement); | |
| 146 | 183 | if (ObjectUtil.notEqual(assignStartUserHandlerType, BpmUserTaskAssignStartUserHandlerTypeEnum.SKIP.getType())) { |
| 147 | 184 | return; |
| 148 | 185 | } |
| 149 | - ProcessInstance processInstance = SpringUtil.getBean(BpmProcessInstanceService.class) | |
| 150 | - .getProcessInstance(execution.getProcessInstanceId()); | |
| 151 | - Assert.notNull(processInstance, "流程实例({}) 不存在", execution.getProcessInstanceId()); | |
| 152 | - assigneeUserIds.remove(Long.valueOf(processInstance.getStartUserId())); | |
| 186 | + assigneeUserIds.remove(startUserId); | |
| 153 | 187 | } |
| 154 | 188 | |
| 155 | - public BpmTaskCandidateStrategy getCandidateStrategy(Integer strategy) { | |
| 189 | + private BpmTaskCandidateStrategy getCandidateStrategy(Integer strategy) { | |
| 156 | 190 | BpmTaskCandidateStrategyEnum strategyEnum = BpmTaskCandidateStrategyEnum.valueOf(strategy); |
| 157 | 191 | Assert.notNull(strategyEnum, "策略(%s) 不存在", strategy); |
| 158 | 192 | BpmTaskCandidateStrategy strategyObj = strategyMap.get(strategyEnum); | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/BpmTaskCandidateStrategy.java
| 1 | 1 | package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 4 | +import org.flowable.bpmn.model.BpmnModel; | |
| 4 | 5 | import org.flowable.engine.delegate.DelegateExecution; |
| 5 | -import org.flowable.engine.runtime.ProcessInstance; | |
| 6 | 6 | |
| 7 | -import java.util.Collections; | |
| 7 | +import java.util.Map; | |
| 8 | 8 | import java.util.Set; |
| 9 | 9 | |
| 10 | 10 | /** |
| ... | ... | @@ -42,47 +42,44 @@ public interface BpmTaskCandidateStrategy { |
| 42 | 42 | /** |
| 43 | 43 | * 基于候选人参数,获得任务的候选用户们 |
| 44 | 44 | * |
| 45 | + * 注意:实现 calculateUsers 系列方法时,有两种选择: | |
| 46 | + * 1. 只重写 calculateUsers 默认方法 | |
| 47 | + * 2. 都重写 calculateUsersByTask 和 calculateUsersByActivity 两个方法 | |
| 48 | + * | |
| 45 | 49 | * @param param 执行任务 |
| 46 | 50 | * @return 用户编号集合 |
| 47 | 51 | */ |
| 48 | 52 | default Set<Long> calculateUsers(String param) { |
| 49 | - return Collections.emptySet(); | |
| 53 | + throw new UnsupportedOperationException("该分配方法未实现,请检查!"); | |
| 50 | 54 | } |
| 51 | 55 | |
| 52 | 56 | /** |
| 53 | - * 基于执行任务,获得任务的候选用户们 | |
| 57 | + * 基于【执行任务】,获得任务的候选用户们 | |
| 54 | 58 | * |
| 55 | 59 | * @param execution 执行任务 |
| 56 | 60 | * @return 用户编号集合 |
| 57 | 61 | */ |
| 58 | - default Set<Long> calculateUsers(DelegateExecution execution, String param) { | |
| 59 | - Set<Long> users = calculateUsers(param); | |
| 60 | - removeDisableUsers(users); | |
| 61 | - return users; | |
| 62 | + default Set<Long> calculateUsersByTask(DelegateExecution execution, String param) { | |
| 63 | + return calculateUsers(param); | |
| 62 | 64 | } |
| 63 | 65 | |
| 64 | 66 | /** |
| 65 | - * 基于流程实例,获得任务的候选用户们 | |
| 67 | + * 基于【流程活动】,获得任务的候选用户们 | |
| 66 | 68 | * <p> |
| 67 | 69 | * 目的:用于获取未执行节点的候选用户们 |
| 68 | 70 | * |
| 69 | - * @param startUserId 流程发起人编号 | |
| 70 | - * @param processInstance 流程实例编号 | |
| 71 | - * @param activityId 活动 Id (对应 Bpmn XML id) | |
| 71 | + * @param bpmnModel 流程图 | |
| 72 | + * @param activityId 活动 ID (对应 Bpmn XML id) | |
| 72 | 73 | * @param param 节点的参数 |
| 74 | + * @param startUserId 流程发起人编号 | |
| 75 | + * @param processDefinitionId 流程定义编号 | |
| 76 | + * @param processVariables 流程变量 | |
| 73 | 77 | * @return 用户编号集合 |
| 74 | 78 | */ |
| 75 | - default Set<Long> calculateUsers(Long startUserId, ProcessInstance processInstance, String activityId, String param) { | |
| 76 | - Set<Long> users = calculateUsers(param); | |
| 77 | - removeDisableUsers(users); | |
| 78 | - return users; | |
| 79 | + @SuppressWarnings("unused") | |
| 80 | + default Set<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId, String param, | |
| 81 | + Long startUserId, String processDefinitionId, Map<String, Object> processVariables) { | |
| 82 | + return calculateUsers(param); | |
| 79 | 83 | } |
| 80 | 84 | |
| 81 | - /** | |
| 82 | - * 移除被禁用的用户 | |
| 83 | - * | |
| 84 | - * @param users 用户 Ids | |
| 85 | - */ | |
| 86 | - void removeDisableUsers(Set<Long> users); | |
| 87 | - | |
| 88 | 85 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateAbstractStrategy.java deleted
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 2 | - | |
| 3 | -import cn.hutool.core.collection.CollUtil; | |
| 4 | -import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | |
| 5 | -import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; | |
| 6 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 7 | -import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | |
| 8 | - | |
| 9 | -import java.util.Map; | |
| 10 | -import java.util.Set; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * {@link BpmTaskCandidateStrategy} 抽象类 | |
| 14 | - * | |
| 15 | - * @author jason | |
| 16 | - */ | |
| 17 | -public abstract class BpmTaskCandidateAbstractStrategy implements BpmTaskCandidateStrategy { | |
| 18 | - | |
| 19 | - protected AdminUserApi adminUserApi; | |
| 20 | - | |
| 21 | - public BpmTaskCandidateAbstractStrategy(AdminUserApi adminUserApi) { | |
| 22 | - this.adminUserApi = adminUserApi; | |
| 23 | - } | |
| 24 | - | |
| 25 | - @Override | |
| 26 | - public void removeDisableUsers(Set<Long> users) { | |
| 27 | - if (CollUtil.isEmpty(users)) { | |
| 28 | - return; | |
| 29 | - } | |
| 30 | - Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(users); | |
| 31 | - users.removeIf(id -> { | |
| 32 | - AdminUserRespDTO user = userMap.get(id); | |
| 33 | - return user == null || !CommonStatusEnum.ENABLE.getStatus().equals(user.getStatus()); | |
| 34 | - }); | |
| 35 | - } | |
| 36 | - | |
| 37 | -} |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateAbstractDeptLeaderStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/AbstractBpmTaskCandidateDeptLeaderStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.lang.Assert; |
| ... | ... | @@ -6,6 +6,8 @@ import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCand |
| 6 | 6 | import cn.iocoder.yudao.module.system.api.dept.DeptApi; |
| 7 | 7 | import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; |
| 8 | 8 | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| 9 | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | |
| 10 | +import jakarta.annotation.Resource; | |
| 9 | 11 | |
| 10 | 12 | import java.util.*; |
| 11 | 13 | |
| ... | ... | @@ -14,14 +16,12 @@ import java.util.*; |
| 14 | 16 | * |
| 15 | 17 | * @author jason |
| 16 | 18 | */ |
| 17 | -public abstract class BpmTaskCandidateAbstractDeptLeaderStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 19 | +public abstract class AbstractBpmTaskCandidateDeptLeaderStrategy implements BpmTaskCandidateStrategy { | |
| 18 | 20 | |
| 21 | + @Resource | |
| 19 | 22 | protected DeptApi deptApi; |
| 20 | - | |
| 21 | - public BpmTaskCandidateAbstractDeptLeaderStrategy(AdminUserApi adminUserApi, DeptApi deptApi) { | |
| 22 | - super(adminUserApi); | |
| 23 | - this.deptApi = deptApi; | |
| 24 | - } | |
| 23 | + @Resource | |
| 24 | + protected AdminUserApi adminUserApi; | |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * 获得指定层级的部门负责人,只有第 level 的负责人 |
| ... | ... | @@ -75,4 +75,17 @@ public abstract class BpmTaskCandidateAbstractDeptLeaderStrategy extends BpmTas |
| 75 | 75 | return deptLeaderIds; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | + /** | |
| 79 | + * 获取发起人的部门 | |
| 80 | + * | |
| 81 | + * @param startUserId 发起人 Id | |
| 82 | + */ | |
| 83 | + protected DeptRespDTO getStartUserDept(Long startUserId) { | |
| 84 | + AdminUserRespDTO startUser = adminUserApi.getUser(startUserId); | |
| 85 | + if (startUser.getDeptId() == null) { // 找不到部门 | |
| 86 | + return null; | |
| 87 | + } | |
| 88 | + return deptApi.getDept(startUser.getDeptId()); | |
| 89 | + } | |
| 90 | + | |
| 78 | 91 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateDeptLeaderMultiStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateDeptLeaderMultiStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.lang.Assert; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.util.string.StrUtils; |
| 5 | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| 6 | 6 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 7 | -import cn.iocoder.yudao.module.system.api.dept.DeptApi; | |
| 8 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 9 | 7 | import org.springframework.stereotype.Component; |
| 10 | 8 | |
| 9 | +import java.util.List; | |
| 11 | 10 | import java.util.Set; |
| 12 | 11 | |
| 13 | 12 | /** |
| ... | ... | @@ -16,11 +15,7 @@ import java.util.Set; |
| 16 | 15 | * @author jason |
| 17 | 16 | */ |
| 18 | 17 | @Component |
| 19 | -public class BpmTaskCandidateDeptLeaderMultiStrategy extends BpmTaskCandidateAbstractDeptLeaderStrategy { | |
| 20 | - | |
| 21 | - public BpmTaskCandidateDeptLeaderMultiStrategy(AdminUserApi adminUserApi, DeptApi deptApi) { | |
| 22 | - super(adminUserApi, deptApi); | |
| 23 | - } | |
| 18 | +public class BpmTaskCandidateDeptLeaderMultiStrategy extends AbstractBpmTaskCandidateDeptLeaderStrategy { | |
| 24 | 19 | |
| 25 | 20 | @Override |
| 26 | 21 | public BpmTaskCandidateStrategyEnum getStrategy() { |
| ... | ... | @@ -32,14 +27,19 @@ public class BpmTaskCandidateDeptLeaderMultiStrategy extends BpmTaskCandidateAbs |
| 32 | 27 | // 参数格式: | 分隔:1)左边为部门(多个部门用 , 分隔)。2)右边为部门层级 |
| 33 | 28 | String[] params = param.split("\\|"); |
| 34 | 29 | Assert.isTrue(params.length == 2, "参数格式不匹配"); |
| 35 | - deptApi.validateDeptList(StrUtils.splitToLong(params[0], ",")); | |
| 36 | - Assert.isTrue(Integer.parseInt(params[1]) > 0, "部门层级必须大于 0"); | |
| 30 | + List<Long> deptIds = StrUtils.splitToLong(params[0], ","); | |
| 31 | + int level = Integer.parseInt(params[1]); | |
| 32 | + // 校验部门存在 | |
| 33 | + deptApi.validateDeptList(deptIds); | |
| 34 | + Assert.isTrue(level > 0, "部门层级必须大于 0"); | |
| 37 | 35 | } |
| 38 | 36 | |
| 39 | 37 | @Override |
| 40 | 38 | public Set<Long> calculateUsers(String param) { |
| 41 | 39 | String[] params = param.split("\\|"); |
| 42 | - return getMultiLevelDeptLeaderIds(StrUtils.splitToLong(params[0], ","), Integer.valueOf(params[1])); | |
| 40 | + List<Long> deptIds = StrUtils.splitToLong(params[0], ","); | |
| 41 | + int level = Integer.parseInt(params[1]); | |
| 42 | + return super.getMultiLevelDeptLeaderIds(deptIds, level); | |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateDeptLeaderStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateDeptLeaderStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.string.StrUtils; |
| 4 | 4 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| 5 | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 6 | 6 | import cn.iocoder.yudao.module.system.api.dept.DeptApi; |
| 7 | 7 | import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; |
| 8 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 8 | +import jakarta.annotation.Resource; | |
| 9 | 9 | import org.springframework.stereotype.Component; |
| 10 | 10 | |
| 11 | 11 | import java.util.List; |
| ... | ... | @@ -19,14 +19,10 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. |
| 19 | 19 | * @author kyle |
| 20 | 20 | */ |
| 21 | 21 | @Component |
| 22 | -public class BpmTaskCandidateDeptLeaderStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 22 | +public class BpmTaskCandidateDeptLeaderStrategy implements BpmTaskCandidateStrategy { | |
| 23 | 23 | |
| 24 | - private final DeptApi deptApi; | |
| 25 | - | |
| 26 | - public BpmTaskCandidateDeptLeaderStrategy(AdminUserApi adminUserApi, DeptApi deptApi) { | |
| 27 | - super(adminUserApi); | |
| 28 | - this.deptApi = deptApi; | |
| 29 | - } | |
| 24 | + @Resource | |
| 25 | + private DeptApi deptApi; | |
| 30 | 26 | |
| 31 | 27 | @Override |
| 32 | 28 | public BpmTaskCandidateStrategyEnum getStrategy() { | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateDeptMemberStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateDeptMemberStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.string.StrUtils; |
| 4 | 4 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| ... | ... | @@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidat |
| 6 | 6 | import cn.iocoder.yudao.module.system.api.dept.DeptApi; |
| 7 | 7 | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| 8 | 8 | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| 9 | +import jakarta.annotation.Resource; | |
| 9 | 10 | import org.springframework.stereotype.Component; |
| 10 | 11 | |
| 11 | 12 | import java.util.List; |
| ... | ... | @@ -19,14 +20,12 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. |
| 19 | 20 | * @author kyle |
| 20 | 21 | */ |
| 21 | 22 | @Component |
| 22 | -public class BpmTaskCandidateDeptMemberStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 23 | +public class BpmTaskCandidateDeptMemberStrategy implements BpmTaskCandidateStrategy { | |
| 23 | 24 | |
| 24 | - private final DeptApi deptApi; | |
| 25 | - | |
| 26 | - public BpmTaskCandidateDeptMemberStrategy(AdminUserApi adminUserApi, DeptApi deptApi) { | |
| 27 | - super(adminUserApi); | |
| 28 | - this.deptApi = deptApi; | |
| 29 | - } | |
| 25 | + @Resource | |
| 26 | + private DeptApi deptApi; | |
| 27 | + @Resource | |
| 28 | + private AdminUserApi adminUserApi; | |
| 30 | 29 | |
| 31 | 30 | @Override |
| 32 | 31 | public BpmTaskCandidateStrategyEnum getStrategy() { | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateStartUserDeptLeaderMultiStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateStartUserDeptLeaderMultiStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.lang.Assert; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.util.number.NumberUtils; |
| 5 | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| 6 | 6 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 7 | 7 | import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; |
| 8 | -import cn.iocoder.yudao.module.system.api.dept.DeptApi; | |
| 9 | 8 | import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; |
| 10 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 11 | -import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | |
| 12 | 9 | import jakarta.annotation.Resource; |
| 10 | +import org.flowable.bpmn.model.BpmnModel; | |
| 13 | 11 | import org.flowable.engine.delegate.DelegateExecution; |
| 14 | 12 | import org.flowable.engine.runtime.ProcessInstance; |
| 15 | 13 | import org.springframework.context.annotation.Lazy; |
| 16 | 14 | import org.springframework.stereotype.Component; |
| 17 | 15 | |
| 18 | 16 | import java.util.HashSet; |
| 17 | +import java.util.Map; | |
| 19 | 18 | import java.util.Set; |
| 20 | 19 | |
| 21 | 20 | import static cn.hutool.core.collection.ListUtil.toList; |
| ... | ... | @@ -26,16 +25,12 @@ import static cn.hutool.core.collection.ListUtil.toList; |
| 26 | 25 | * @author jason |
| 27 | 26 | */ |
| 28 | 27 | @Component |
| 29 | -public class BpmTaskCandidateStartUserDeptLeaderMultiStrategy extends BpmTaskCandidateAbstractDeptLeaderStrategy { | |
| 28 | +public class BpmTaskCandidateStartUserDeptLeaderMultiStrategy extends AbstractBpmTaskCandidateDeptLeaderStrategy { | |
| 30 | 29 | |
| 31 | 30 | @Resource |
| 32 | 31 | @Lazy |
| 33 | 32 | private BpmProcessInstanceService processInstanceService; |
| 34 | 33 | |
| 35 | - public BpmTaskCandidateStartUserDeptLeaderMultiStrategy(AdminUserApi adminUserApi, DeptApi deptApi) { | |
| 36 | - super(adminUserApi, deptApi); | |
| 37 | - } | |
| 38 | - | |
| 39 | 34 | @Override |
| 40 | 35 | public BpmTaskCandidateStrategyEnum getStrategy() { |
| 41 | 36 | return BpmTaskCandidateStrategyEnum.START_USER_DEPT_LEADER_MULTI; |
| ... | ... | @@ -43,50 +38,33 @@ public class BpmTaskCandidateStartUserDeptLeaderMultiStrategy extends BpmTaskCan |
| 43 | 38 | |
| 44 | 39 | @Override |
| 45 | 40 | public void validateParam(String param) { |
| 46 | - // 参数是部门的层级 | |
| 47 | - Assert.isTrue(Integer.parseInt(param) > 0, "部门的层级必须大于 0"); | |
| 41 | + int level = Integer.parseInt(param); // 参数是部门的层级 | |
| 42 | + Assert.isTrue(level > 0, "部门的层级必须大于 0"); | |
| 48 | 43 | } |
| 49 | 44 | |
| 50 | 45 | @Override |
| 51 | - public Set<Long> calculateUsers(DelegateExecution execution, String param) { | |
| 46 | + public Set<Long> calculateUsersByTask(DelegateExecution execution, String param) { | |
| 47 | + int level = Integer.parseInt(param); // 参数是部门的层级 | |
| 52 | 48 | // 获得流程发起人 |
| 53 | 49 | ProcessInstance processInstance = processInstanceService.getProcessInstance(execution.getProcessInstanceId()); |
| 54 | 50 | Long startUserId = NumberUtils.parseLong(processInstance.getStartUserId()); |
| 55 | 51 | // 获取发起人的 multi 部门负责人 |
| 56 | - DeptRespDTO dept = getStartUserDept(startUserId); | |
| 52 | + DeptRespDTO dept = super.getStartUserDept(startUserId); | |
| 57 | 53 | if (dept == null) { |
| 58 | 54 | return new HashSet<>(); |
| 59 | 55 | } |
| 60 | - Set<Long> users = getMultiLevelDeptLeaderIds(toList(dept.getId()), Integer.valueOf(param)); // 参数是部门的层级 | |
| 61 | - // TODO @jason:这里 removeDisableUsers 的原因是啥呀? | |
| 62 | - // TODO @芋艿 calculateUsers(execution, param) calculateUsers(startUserId, processInstance, activityId, param) 现在这两个方法, 默认都移除了被禁用的用户 | |
| 63 | - // TODO @芋艿 因为被禁用的用户是不能审批任务。 在这里移除是不是好一点。 代码可以重用。 | |
| 64 | - removeDisableUsers(users); | |
| 65 | - return users; | |
| 56 | + return super.getMultiLevelDeptLeaderIds(toList(dept.getId()), level); | |
| 66 | 57 | } |
| 67 | 58 | |
| 68 | 59 | @Override |
| 69 | - public Set<Long> calculateUsers(Long startUserId, ProcessInstance processInstance, String activityId, String param) { | |
| 70 | - DeptRespDTO dept = getStartUserDept(startUserId); | |
| 60 | + public Set<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId, String param, | |
| 61 | + Long startUserId, String processDefinitionId, Map<String, Object> processVariables) { | |
| 62 | + int level = Integer.parseInt(param); // 参数是部门的层级 | |
| 63 | + DeptRespDTO dept = super.getStartUserDept(startUserId); | |
| 71 | 64 | if (dept == null) { |
| 72 | 65 | return new HashSet<>(); |
| 73 | 66 | } |
| 74 | - Set<Long> users = getMultiLevelDeptLeaderIds(toList(dept.getId()), Integer.valueOf(param)); // 参数是部门的层级 | |
| 75 | - removeDisableUsers(users); | |
| 76 | - return users; | |
| 77 | - } | |
| 78 | - | |
| 79 | - /** | |
| 80 | - * 获取发起人的部门 | |
| 81 | - * | |
| 82 | - * @param startUserId 发起人 Id | |
| 83 | - */ | |
| 84 | - protected DeptRespDTO getStartUserDept(Long startUserId) { | |
| 85 | - AdminUserRespDTO startUser = adminUserApi.getUser(startUserId); | |
| 86 | - if (startUser.getDeptId() == null) { // 找不到部门 | |
| 87 | - return null; | |
| 88 | - } | |
| 89 | - return deptApi.getDept(startUser.getDeptId()); | |
| 67 | + return super.getMultiLevelDeptLeaderIds(toList(dept.getId()), level); | |
| 90 | 68 | } |
| 91 | 69 | |
| 92 | 70 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateStartUserDeptLeaderStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateStartUserDeptLeaderStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.lang.Assert; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.util.number.NumberUtils; |
| 5 | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| 6 | 6 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 7 | 7 | import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; |
| 8 | -import cn.iocoder.yudao.module.system.api.dept.DeptApi; | |
| 9 | 8 | import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; |
| 10 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 11 | -import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | |
| 12 | 9 | import jakarta.annotation.Resource; |
| 10 | +import org.flowable.bpmn.model.BpmnModel; | |
| 13 | 11 | import org.flowable.engine.delegate.DelegateExecution; |
| 14 | 12 | import org.flowable.engine.runtime.ProcessInstance; |
| 15 | 13 | import org.springframework.context.annotation.Lazy; |
| 16 | 14 | import org.springframework.stereotype.Component; |
| 17 | 15 | |
| 18 | 16 | import java.util.HashSet; |
| 17 | +import java.util.Map; | |
| 19 | 18 | import java.util.Set; |
| 20 | 19 | |
| 21 | 20 | import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; |
| ... | ... | @@ -26,7 +25,7 @@ import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; |
| 26 | 25 | * @author jason |
| 27 | 26 | */ |
| 28 | 27 | @Component |
| 29 | -public class BpmTaskCandidateStartUserDeptLeaderStrategy extends BpmTaskCandidateAbstractDeptLeaderStrategy { | |
| 28 | +public class BpmTaskCandidateStartUserDeptLeaderStrategy extends AbstractBpmTaskCandidateDeptLeaderStrategy { | |
| 30 | 29 | |
| 31 | 30 | @Resource |
| 32 | 31 | @Lazy // 避免循环依赖 |
| ... | ... | @@ -37,10 +36,6 @@ public class BpmTaskCandidateStartUserDeptLeaderStrategy extends BpmTaskCandidat |
| 37 | 36 | return BpmTaskCandidateStrategyEnum.START_USER_DEPT_LEADER; |
| 38 | 37 | } |
| 39 | 38 | |
| 40 | - public BpmTaskCandidateStartUserDeptLeaderStrategy(AdminUserApi adminUserApi, DeptApi deptApi) { | |
| 41 | - super(adminUserApi, deptApi); | |
| 42 | - } | |
| 43 | - | |
| 44 | 39 | @Override |
| 45 | 40 | public void validateParam(String param) { |
| 46 | 41 | // 参数是部门的层级 |
| ... | ... | @@ -48,44 +43,29 @@ public class BpmTaskCandidateStartUserDeptLeaderStrategy extends BpmTaskCandidat |
| 48 | 43 | } |
| 49 | 44 | |
| 50 | 45 | @Override |
| 51 | - public Set<Long> calculateUsers(DelegateExecution execution, String param) { | |
| 46 | + public Set<Long> calculateUsersByTask(DelegateExecution execution, String param) { | |
| 52 | 47 | // 获得流程发起人 |
| 53 | 48 | ProcessInstance processInstance = processInstanceService.getProcessInstance(execution.getProcessInstanceId()); |
| 54 | 49 | Long startUserId = NumberUtils.parseLong(processInstance.getStartUserId()); |
| 55 | 50 | // 获取发起人的部门负责人 |
| 56 | - Set<Long> users = getStartUserDeptLeader(startUserId, param); | |
| 57 | - removeDisableUsers(users); | |
| 58 | - return users; | |
| 51 | + return getStartUserDeptLeader(startUserId, param); | |
| 59 | 52 | } |
| 60 | 53 | |
| 61 | 54 | @Override |
| 62 | - public Set<Long> calculateUsers(Long startUserId, ProcessInstance processInstance, String activityId, String param) { | |
| 55 | + public Set<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId, String param, | |
| 56 | + Long startUserId, String processDefinitionId, Map<String, Object> processVariables) { | |
| 63 | 57 | // 获取发起人的部门负责人 |
| 64 | - Set<Long> users = getStartUserDeptLeader(startUserId, param); | |
| 65 | - removeDisableUsers(users); | |
| 66 | - return users; | |
| 58 | + return getStartUserDeptLeader(startUserId, param); | |
| 67 | 59 | } |
| 68 | 60 | |
| 69 | 61 | private Set<Long> getStartUserDeptLeader(Long startUserId, String param) { |
| 70 | - DeptRespDTO dept = getStartUserDept(startUserId); | |
| 62 | + int level = Integer.parseInt(param); // 参数是部门的层级 | |
| 63 | + DeptRespDTO dept = super.getStartUserDept(startUserId); | |
| 71 | 64 | if (dept == null) { |
| 72 | 65 | return new HashSet<>(); |
| 73 | 66 | } |
| 74 | - Long deptLeaderId = getAssignLevelDeptLeaderId(dept, Integer.valueOf(param)); // 参数是部门的层级 | |
| 67 | + Long deptLeaderId = super.getAssignLevelDeptLeaderId(dept, level); | |
| 75 | 68 | return deptLeaderId != null ? asSet(deptLeaderId) : new HashSet<>(); |
| 76 | 69 | } |
| 77 | 70 | |
| 78 | - /** | |
| 79 | - * 获取发起人的部门 | |
| 80 | - * | |
| 81 | - * @param startUserId 发起人 Id | |
| 82 | - */ | |
| 83 | - protected DeptRespDTO getStartUserDept(Long startUserId) { | |
| 84 | - AdminUserRespDTO startUser = adminUserApi.getUser(startUserId); | |
| 85 | - if (startUser.getDeptId() == null) { // 找不到部门 | |
| 86 | - return null; | |
| 87 | - } | |
| 88 | - return deptApi.getDept(startUser.getDeptId()); | |
| 89 | - } | |
| 90 | - | |
| 91 | 71 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateStartUserSelectStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateStartUserSelectStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.lang.Assert; |
| 5 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user.BpmTaskCandidateUserStrategy; | |
| 5 | 6 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 6 | 7 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils; |
| 7 | 8 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils; |
| 8 | 9 | import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; |
| 9 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 10 | +import com.google.common.collect.Sets; | |
| 10 | 11 | import jakarta.annotation.Resource; |
| 11 | 12 | import org.flowable.bpmn.model.BpmnModel; |
| 12 | 13 | import org.flowable.bpmn.model.UserTask; |
| ... | ... | @@ -23,16 +24,12 @@ import java.util.*; |
| 23 | 24 | * @author 芋道源码 |
| 24 | 25 | */ |
| 25 | 26 | @Component |
| 26 | -public class BpmTaskCandidateStartUserSelectStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 27 | +public class BpmTaskCandidateStartUserSelectStrategy extends AbstractBpmTaskCandidateDeptLeaderStrategy { | |
| 27 | 28 | |
| 28 | 29 | @Resource |
| 29 | 30 | @Lazy // 延迟加载,避免循环依赖 |
| 30 | 31 | private BpmProcessInstanceService processInstanceService; |
| 31 | 32 | |
| 32 | - public BpmTaskCandidateStartUserSelectStrategy(AdminUserApi adminUserApi) { | |
| 33 | - super(adminUserApi); | |
| 34 | - } | |
| 35 | - | |
| 36 | 33 | @Override |
| 37 | 34 | public BpmTaskCandidateStrategyEnum getStrategy() { |
| 38 | 35 | return BpmTaskCandidateStrategyEnum.START_USER_SELECT; |
| ... | ... | @@ -42,7 +39,12 @@ public class BpmTaskCandidateStartUserSelectStrategy extends BpmTaskCandidateAbs |
| 42 | 39 | public void validateParam(String param) {} |
| 43 | 40 | |
| 44 | 41 | @Override |
| 45 | - public Set<Long> calculateUsers(DelegateExecution execution, String param) { | |
| 42 | + public boolean isParamRequired() { | |
| 43 | + return false; | |
| 44 | + } | |
| 45 | + | |
| 46 | + @Override | |
| 47 | + public LinkedHashSet<Long> calculateUsersByTask(DelegateExecution execution, String param) { | |
| 46 | 48 | ProcessInstance processInstance = processInstanceService.getProcessInstance(execution.getProcessInstanceId()); |
| 47 | 49 | Assert.notNull(processInstance, "流程实例({})不能为空", execution.getProcessInstanceId()); |
| 48 | 50 | Map<String, List<Long>> startUserSelectAssignees = FlowableUtils.getStartUserSelectAssignees(processInstance); |
| ... | ... | @@ -50,28 +52,22 @@ public class BpmTaskCandidateStartUserSelectStrategy extends BpmTaskCandidateAbs |
| 50 | 52 | execution.getProcessInstanceId()); |
| 51 | 53 | // 获得审批人 |
| 52 | 54 | List<Long> assignees = startUserSelectAssignees.get(execution.getCurrentActivityId()); |
| 53 | - Set<Long> users = new LinkedHashSet<>(assignees); | |
| 54 | - removeDisableUsers(users); | |
| 55 | - return users; | |
| 55 | + return new LinkedHashSet<>(assignees); | |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | @Override |
| 59 | - public Set<Long> calculateUsers(Long startUserId, ProcessInstance processInstance, String activityId, String param) { | |
| 60 | - if (processInstance == null) { | |
| 61 | - return Collections.emptySet(); | |
| 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>> startUserSelectAssignees = FlowableUtils.getStartUserSelectAssignees(processVariables); | |
| 65 | + if (startUserSelectAssignees == null) { | |
| 66 | + return Sets.newLinkedHashSet(); | |
| 62 | 67 | } |
| 63 | - Map<String, List<Long>> startUserSelectAssignees = FlowableUtils.getStartUserSelectAssignees(processInstance); | |
| 64 | - Assert.notNull(startUserSelectAssignees, "流程实例({}) 的发起人自选审批人不能为空", processInstance.getId()); | |
| 65 | 68 | // 获得审批人 |
| 66 | 69 | List<Long> assignees = startUserSelectAssignees.get(activityId); |
| 67 | - Set<Long> users = new LinkedHashSet<>(assignees); | |
| 68 | - removeDisableUsers(users); | |
| 69 | - return users; | |
| 70 | - } | |
| 71 | - | |
| 72 | - @Override | |
| 73 | - public boolean isParamRequired() { | |
| 74 | - return false; | |
| 70 | + return new LinkedHashSet<>(assignees); | |
| 75 | 71 | } |
| 76 | 72 | |
| 77 | 73 | /** | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateAssignEmptyStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/other/BpmTaskCandidateAssignEmptyStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.other; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.lang.Assert; |
| 4 | 4 | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; |
| ... | ... | @@ -7,13 +7,15 @@ import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCand |
| 7 | 7 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 8 | 8 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils; |
| 9 | 9 | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; |
| 10 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 11 | 10 | import jakarta.annotation.Resource; |
| 11 | +import org.flowable.bpmn.model.BpmnModel; | |
| 12 | +import org.flowable.bpmn.model.FlowElement; | |
| 12 | 13 | import org.flowable.engine.delegate.DelegateExecution; |
| 13 | 14 | import org.springframework.context.annotation.Lazy; |
| 14 | 15 | import org.springframework.stereotype.Component; |
| 15 | 16 | |
| 16 | 17 | import java.util.HashSet; |
| 18 | +import java.util.Map; | |
| 17 | 19 | import java.util.Objects; |
| 18 | 20 | import java.util.Set; |
| 19 | 21 | |
| ... | ... | @@ -23,16 +25,12 @@ import java.util.Set; |
| 23 | 25 | * @author kyle |
| 24 | 26 | */ |
| 25 | 27 | @Component |
| 26 | -public class BpmTaskCandidateAssignEmptyStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 28 | +public class BpmTaskCandidateAssignEmptyStrategy implements BpmTaskCandidateStrategy { | |
| 27 | 29 | |
| 28 | 30 | @Resource |
| 29 | 31 | @Lazy // 延迟加载,避免循环依赖 |
| 30 | 32 | private BpmProcessDefinitionService processDefinitionService; |
| 31 | 33 | |
| 32 | - public BpmTaskCandidateAssignEmptyStrategy(AdminUserApi adminUserApi) { | |
| 33 | - super(adminUserApi); | |
| 34 | - } | |
| 35 | - | |
| 36 | 34 | @Override |
| 37 | 35 | public BpmTaskCandidateStrategyEnum getStrategy() { |
| 38 | 36 | return BpmTaskCandidateStrategyEnum.ASSIGN_EMPTY; |
| ... | ... | @@ -43,19 +41,28 @@ public class BpmTaskCandidateAssignEmptyStrategy extends BpmTaskCandidateAbstrac |
| 43 | 41 | } |
| 44 | 42 | |
| 45 | 43 | @Override |
| 46 | - public Set<Long> calculateUsers(DelegateExecution execution, String param) { | |
| 44 | + public Set<Long> calculateUsersByTask(DelegateExecution execution, String param) { | |
| 45 | + return getCandidateUsers(execution.getProcessDefinitionId(), execution.getCurrentFlowElement()); | |
| 46 | + } | |
| 47 | + | |
| 48 | + @Override | |
| 49 | + public Set<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId, String param, | |
| 50 | + Long startUserId, String processDefinitionId, Map<String, Object> processVariables) { | |
| 51 | + FlowElement flowElement = BpmnModelUtils.getFlowElementById(bpmnModel, activityId); | |
| 52 | + return getCandidateUsers(processDefinitionId, flowElement); | |
| 53 | + } | |
| 54 | + | |
| 55 | + private Set<Long> getCandidateUsers(String processDefinitionId, FlowElement flowElement) { | |
| 47 | 56 | // 情况一:指定人员审批 |
| 48 | - Integer assignEmptyHandlerType = BpmnModelUtils.parseAssignEmptyHandlerType(execution.getCurrentFlowElement()); | |
| 57 | + Integer assignEmptyHandlerType = BpmnModelUtils.parseAssignEmptyHandlerType(flowElement); | |
| 49 | 58 | if (Objects.equals(assignEmptyHandlerType, BpmUserTaskAssignEmptyHandlerTypeEnum.ASSIGN_USER.getType())) { |
| 50 | - Set<Long> users = new HashSet<>(BpmnModelUtils.parseAssignEmptyHandlerUserIds(execution.getCurrentFlowElement())); | |
| 51 | - removeDisableUsers(users); | |
| 52 | - return users; | |
| 59 | + return new HashSet<>(BpmnModelUtils.parseAssignEmptyHandlerUserIds(flowElement)); | |
| 53 | 60 | } |
| 54 | 61 | |
| 55 | 62 | // 情况二:流程管理员 |
| 56 | 63 | if (Objects.equals(assignEmptyHandlerType, BpmUserTaskAssignEmptyHandlerTypeEnum.ASSIGN_ADMIN.getType())) { |
| 57 | - BpmProcessDefinitionInfoDO processDefinition = processDefinitionService.getProcessDefinitionInfo(execution.getProcessDefinitionId()); | |
| 58 | - Assert.notNull(processDefinition, "流程定义({})不存在", execution.getProcessDefinitionId()); | |
| 64 | + BpmProcessDefinitionInfoDO processDefinition = processDefinitionService.getProcessDefinitionInfo(processDefinitionId); | |
| 65 | + Assert.notNull(processDefinition, "流程定义({})不存在", processDefinitionId); | |
| 59 | 66 | return new HashSet<>(processDefinition.getManagerUserIds()); |
| 60 | 67 | } |
| 61 | 68 | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateExpressionStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/other/BpmTaskCandidateExpressionStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.other; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.convert.Convert; |
| 4 | 4 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| 5 | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 6 | 6 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils; |
| 7 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 7 | +import org.flowable.bpmn.model.BpmnModel; | |
| 8 | 8 | import org.flowable.engine.delegate.DelegateExecution; |
| 9 | 9 | import org.springframework.stereotype.Component; |
| 10 | 10 | |
| 11 | +import java.util.Map; | |
| 11 | 12 | import java.util.Set; |
| 12 | 13 | |
| 13 | 14 | /** |
| ... | ... | @@ -16,11 +17,7 @@ import java.util.Set; |
| 16 | 17 | * @author 芋道源码 |
| 17 | 18 | */ |
| 18 | 19 | @Component |
| 19 | -public class BpmTaskCandidateExpressionStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 20 | - | |
| 21 | - public BpmTaskCandidateExpressionStrategy(AdminUserApi adminUserApi) { | |
| 22 | - super(adminUserApi); | |
| 23 | - } | |
| 20 | +public class BpmTaskCandidateExpressionStrategy implements BpmTaskCandidateStrategy { | |
| 24 | 21 | |
| 25 | 22 | @Override |
| 26 | 23 | public BpmTaskCandidateStrategyEnum getStrategy() { |
| ... | ... | @@ -33,11 +30,16 @@ public class BpmTaskCandidateExpressionStrategy extends BpmTaskCandidateAbstract |
| 33 | 30 | } |
| 34 | 31 | |
| 35 | 32 | @Override |
| 36 | - public Set<Long> calculateUsers(DelegateExecution execution, String param) { | |
| 33 | + public Set<Long> calculateUsersByTask(DelegateExecution execution, String param) { | |
| 37 | 34 | Object result = FlowableUtils.getExpressionValue(execution, param); |
| 38 | - Set<Long> users = Convert.toSet(Long.class, result); | |
| 39 | - removeDisableUsers(users); | |
| 40 | - return users; | |
| 35 | + return Convert.toSet(Long.class, result); | |
| 36 | + } | |
| 37 | + | |
| 38 | + @Override | |
| 39 | + public Set<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId, String param, | |
| 40 | + Long startUserId, String processDefinitionId, Map<String, Object> processVariables) { | |
| 41 | + Object result = FlowableUtils.getExpressionValue(processVariables, param); | |
| 42 | + return Convert.toSet(Long.class, result); | |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | 45 | } |
| 44 | 46 | \ No newline at end of file | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateGroupStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidateGroupStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.string.StrUtils; |
| 4 | 4 | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmUserGroupDO; |
| 5 | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| 6 | 6 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 7 | 7 | import cn.iocoder.yudao.module.bpm.service.definition.BpmUserGroupService; |
| 8 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 8 | +import jakarta.annotation.Resource; | |
| 9 | 9 | import org.springframework.stereotype.Component; |
| 10 | 10 | |
| 11 | 11 | import java.util.Collection; |
| ... | ... | @@ -20,14 +20,10 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. |
| 20 | 20 | * @author kyle |
| 21 | 21 | */ |
| 22 | 22 | @Component |
| 23 | -public class BpmTaskCandidateGroupStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 23 | +public class BpmTaskCandidateGroupStrategy implements BpmTaskCandidateStrategy { | |
| 24 | 24 | |
| 25 | - private final BpmUserGroupService userGroupService; | |
| 26 | - | |
| 27 | - public BpmTaskCandidateGroupStrategy(AdminUserApi adminUserApi, BpmUserGroupService userGroupService) { | |
| 28 | - super(adminUserApi); | |
| 29 | - this.userGroupService = userGroupService; | |
| 30 | - } | |
| 25 | + @Resource | |
| 26 | + private BpmUserGroupService userGroupService; | |
| 31 | 27 | |
| 32 | 28 | @Override |
| 33 | 29 | public BpmTaskCandidateStrategyEnum getStrategy() { |
| ... | ... | @@ -37,7 +33,7 @@ public class BpmTaskCandidateGroupStrategy extends BpmTaskCandidateAbstractStrat |
| 37 | 33 | @Override |
| 38 | 34 | public void validateParam(String param) { |
| 39 | 35 | Set<Long> groupIds = StrUtils.splitToLongSet(param); |
| 40 | - userGroupService.getUserGroupList(groupIds); | |
| 36 | + userGroupService.validUserGroups(groupIds); | |
| 41 | 37 | } |
| 42 | 38 | |
| 43 | 39 | @Override | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidatePostStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidatePostStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.string.StrUtils; |
| 4 | 4 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| ... | ... | @@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidat |
| 6 | 6 | import cn.iocoder.yudao.module.system.api.dept.PostApi; |
| 7 | 7 | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| 8 | 8 | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| 9 | +import jakarta.annotation.Resource; | |
| 9 | 10 | import org.springframework.stereotype.Component; |
| 10 | 11 | |
| 11 | 12 | import java.util.List; |
| ... | ... | @@ -19,14 +20,12 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. |
| 19 | 20 | * @author kyle |
| 20 | 21 | */ |
| 21 | 22 | @Component |
| 22 | -public class BpmTaskCandidatePostStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 23 | +public class BpmTaskCandidatePostStrategy implements BpmTaskCandidateStrategy { | |
| 23 | 24 | |
| 24 | - private final PostApi postApi; | |
| 25 | - | |
| 26 | - public BpmTaskCandidatePostStrategy(AdminUserApi adminUserApi, PostApi postApi) { | |
| 27 | - super(adminUserApi); | |
| 28 | - this.postApi = postApi; | |
| 29 | - } | |
| 25 | + @Resource | |
| 26 | + private PostApi postApi; | |
| 27 | + @Resource | |
| 28 | + private AdminUserApi adminUserApi; | |
| 30 | 29 | |
| 31 | 30 | @Override |
| 32 | 31 | public BpmTaskCandidateStrategyEnum getStrategy() { | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateRoleStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidateRoleStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.string.StrUtils; |
| 4 | 4 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| 5 | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 6 | 6 | import cn.iocoder.yudao.module.system.api.permission.PermissionApi; |
| 7 | 7 | import cn.iocoder.yudao.module.system.api.permission.RoleApi; |
| 8 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 9 | 8 | import jakarta.annotation.Resource; |
| 10 | 9 | import org.springframework.stereotype.Component; |
| 11 | 10 | |
| ... | ... | @@ -17,17 +16,13 @@ import java.util.Set; |
| 17 | 16 | * @author kyle |
| 18 | 17 | */ |
| 19 | 18 | @Component |
| 20 | -public class BpmTaskCandidateRoleStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 19 | +public class BpmTaskCandidateRoleStrategy implements BpmTaskCandidateStrategy { | |
| 21 | 20 | |
| 22 | 21 | @Resource |
| 23 | 22 | private RoleApi roleApi; |
| 24 | 23 | @Resource |
| 25 | 24 | private PermissionApi permissionApi; |
| 26 | 25 | |
| 27 | - public BpmTaskCandidateRoleStrategy(AdminUserApi adminUserApi) { | |
| 28 | - super(adminUserApi); | |
| 29 | - } | |
| 30 | - | |
| 31 | 26 | @Override |
| 32 | 27 | public BpmTaskCandidateStrategyEnum getStrategy() { |
| 33 | 28 | return BpmTaskCandidateStrategyEnum.ROLE; | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateStartUserStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidateStartUserStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.collection.SetUtils; |
| 4 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; | |
| 4 | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 5 | 6 | import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; |
| 6 | -import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 7 | 7 | import jakarta.annotation.Resource; |
| 8 | +import org.flowable.bpmn.model.BpmnModel; | |
| 8 | 9 | import org.flowable.engine.delegate.DelegateExecution; |
| 9 | 10 | import org.flowable.engine.runtime.ProcessInstance; |
| 10 | 11 | import org.springframework.context.annotation.Lazy; |
| 11 | 12 | import org.springframework.stereotype.Component; |
| 12 | 13 | |
| 14 | +import java.util.Map; | |
| 13 | 15 | import java.util.Set; |
| 14 | 16 | |
| 15 | 17 | /** |
| ... | ... | @@ -20,16 +22,12 @@ import java.util.Set; |
| 20 | 22 | * @author jason |
| 21 | 23 | */ |
| 22 | 24 | @Component |
| 23 | -public class BpmTaskCandidateStartUserStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 25 | +public class BpmTaskCandidateStartUserStrategy implements BpmTaskCandidateStrategy { | |
| 24 | 26 | |
| 25 | 27 | @Resource |
| 26 | 28 | @Lazy // 延迟加载,避免循环依赖 |
| 27 | 29 | private BpmProcessInstanceService processInstanceService; |
| 28 | 30 | |
| 29 | - public BpmTaskCandidateStartUserStrategy(AdminUserApi adminUserApi) { | |
| 30 | - super(adminUserApi); | |
| 31 | - } | |
| 32 | - | |
| 33 | 31 | @Override |
| 34 | 32 | public BpmTaskCandidateStrategyEnum getStrategy() { |
| 35 | 33 | return BpmTaskCandidateStrategyEnum.START_USER; |
| ... | ... | @@ -45,18 +43,15 @@ public class BpmTaskCandidateStartUserStrategy extends BpmTaskCandidateAbstractS |
| 45 | 43 | } |
| 46 | 44 | |
| 47 | 45 | @Override |
| 48 | - public Set<Long> calculateUsers(DelegateExecution execution, String param) { | |
| 46 | + public Set<Long> calculateUsersByTask(DelegateExecution execution, String param) { | |
| 49 | 47 | ProcessInstance processInstance = processInstanceService.getProcessInstance(execution.getProcessInstanceId()); |
| 50 | - Set<Long> users = SetUtils.asSet(Long.valueOf(processInstance.getStartUserId())); | |
| 51 | - removeDisableUsers(users); | |
| 52 | - return users; | |
| 48 | + return SetUtils.asSet(Long.valueOf(processInstance.getStartUserId())); | |
| 53 | 49 | } |
| 54 | 50 | |
| 55 | 51 | @Override |
| 56 | - public Set<Long> calculateUsers(Long startUserId, ProcessInstance processInstance, String activityId, String param) { | |
| 57 | - Set<Long> users = SetUtils.asSet(startUserId); | |
| 58 | - removeDisableUsers(users); | |
| 59 | - return users; | |
| 52 | + public Set<Long> calculateUsersByActivity(BpmnModel bpmnModel, String activityId, String param, | |
| 53 | + Long startUserId, String processDefinitionId, Map<String, Object> processVariables) { | |
| 54 | + return SetUtils.asSet(startUserId); | |
| 60 | 55 | } |
| 61 | 56 | |
| 62 | 57 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateUserStrategy.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidateUserStrategy.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.text.StrPool; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.util.string.StrUtils; |
| 5 | 5 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; |
| 6 | 6 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 7 | 7 | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| 8 | +import jakarta.annotation.Resource; | |
| 8 | 9 | import org.springframework.stereotype.Component; |
| 9 | 10 | |
| 10 | 11 | import java.util.LinkedHashSet; |
| 11 | -import java.util.Set; | |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | 14 | * 用户 {@link BpmTaskCandidateStrategy} 实现类 |
| ... | ... | @@ -16,11 +16,10 @@ import java.util.Set; |
| 16 | 16 | * @author kyle |
| 17 | 17 | */ |
| 18 | 18 | @Component |
| 19 | -public class BpmTaskCandidateUserStrategy extends BpmTaskCandidateAbstractStrategy { | |
| 19 | +public class BpmTaskCandidateUserStrategy implements BpmTaskCandidateStrategy { | |
| 20 | 20 | |
| 21 | - public BpmTaskCandidateUserStrategy(AdminUserApi adminUserApi) { | |
| 22 | - super(adminUserApi); | |
| 23 | - } | |
| 21 | + @Resource | |
| 22 | + private AdminUserApi adminUserApi; | |
| 24 | 23 | |
| 25 | 24 | @Override |
| 26 | 25 | public BpmTaskCandidateStrategyEnum getStrategy() { |
| ... | ... | @@ -33,7 +32,7 @@ public class BpmTaskCandidateUserStrategy extends BpmTaskCandidateAbstractStrate |
| 33 | 32 | } |
| 34 | 33 | |
| 35 | 34 | @Override |
| 36 | - public Set<Long> calculateUsers(String param) { | |
| 35 | + public LinkedHashSet<Long> calculateUsers(String param) { | |
| 37 | 36 | return new LinkedHashSet<>(StrUtils.splitToLong(param, StrPool.COMMA)); |
| 38 | 37 | } |
| 39 | 38 | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/listener/BpmCopyTaskDelegate.java
| ... | ... | @@ -34,7 +34,7 @@ public class BpmCopyTaskDelegate implements JavaDelegate { |
| 34 | 34 | @Override |
| 35 | 35 | public void execute(DelegateExecution execution) { |
| 36 | 36 | // 1. 获得抄送人 |
| 37 | - Set<Long> userIds = taskCandidateInvoker.calculateUsers(execution); | |
| 37 | + Set<Long> userIds = taskCandidateInvoker.calculateUsersByTask(execution); | |
| 38 | 38 | if (CollUtil.isEmpty(userIds)) { |
| 39 | 39 | return; |
| 40 | 40 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/BpmnModelUtils.java
| ... | ... | @@ -5,7 +5,6 @@ import cn.hutool.core.map.MapUtil; |
| 5 | 5 | import cn.hutool.core.util.ArrayUtil; |
| 6 | 6 | import cn.hutool.core.util.ObjUtil; |
| 7 | 7 | import cn.hutool.core.util.StrUtil; |
| 8 | -import cn.hutool.extra.spring.SpringUtil; | |
| 9 | 8 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 10 | 9 | import cn.iocoder.yudao.framework.common.util.number.NumberUtils; |
| 11 | 10 | import cn.iocoder.yudao.framework.common.util.string.StrUtils; |
| ... | ... | @@ -23,7 +22,6 @@ import org.flowable.bpmn.model.Process; |
| 23 | 22 | import org.flowable.bpmn.model.*; |
| 24 | 23 | import org.flowable.common.engine.api.FlowableException; |
| 25 | 24 | import org.flowable.common.engine.impl.util.io.BytesStreamSource; |
| 26 | -import org.flowable.engine.ManagementService; | |
| 27 | 25 | |
| 28 | 26 | import java.util.*; |
| 29 | 27 | |
| ... | ... | @@ -773,20 +771,16 @@ public class BpmnModelUtils { |
| 773 | 771 | * @return 是否满足条件 |
| 774 | 772 | */ |
| 775 | 773 | public static boolean evalConditionExpress(Map<String, Object> variables, String express) { |
| 776 | - ManagementService managementService = SpringUtil.getBean(ManagementService.class); | |
| 777 | 774 | if (express == null) { |
| 778 | 775 | return Boolean.FALSE; |
| 779 | 776 | } |
| 780 | - // TODO @jason:疑问,为啥这里要在 managementService 里执行哈? | |
| 781 | - Object result = managementService.executeCommand(context -> { | |
| 782 | - try { | |
| 783 | - return FlowableUtils.getExpressionValue(variables, express); | |
| 784 | - } catch (FlowableException ex) { | |
| 785 | - log.error("[evalConditionExpress][条件表达式({}) 解析报错", express, ex); | |
| 786 | - return Boolean.FALSE; | |
| 787 | - } | |
| 788 | - }); | |
| 789 | - return Boolean.TRUE.equals(result); | |
| 777 | + try { | |
| 778 | + Object result = FlowableUtils.getExpressionValue(variables, express); | |
| 779 | + return Boolean.TRUE.equals(result); | |
| 780 | + } catch (FlowableException ex) { | |
| 781 | + log.error("[evalConditionExpress][条件表达式({}) 变量({}) 解析报错", express, variables, ex); | |
| 782 | + return Boolean.FALSE; | |
| 783 | + } | |
| 790 | 784 | } |
| 791 | 785 | |
| 792 | 786 | @SuppressWarnings("PatternVariableCanBeUsed") | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/FlowableUtils.java
| 1 | 1 | package cn.iocoder.yudao.module.bpm.framework.flowable.core.util; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.ObjectUtil; |
| 4 | +import cn.hutool.extra.spring.SpringUtil; | |
| 4 | 5 | import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; |
| 5 | 6 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; |
| 6 | 7 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants; |
| ... | ... | @@ -9,6 +10,7 @@ import org.flowable.common.engine.api.variable.VariableContainer; |
| 9 | 10 | import org.flowable.common.engine.impl.el.ExpressionManager; |
| 10 | 11 | import org.flowable.common.engine.impl.identity.Authentication; |
| 11 | 12 | import org.flowable.common.engine.impl.variable.MapDelegateVariableContainer; |
| 13 | +import org.flowable.engine.ManagementService; | |
| 12 | 14 | import org.flowable.engine.ProcessEngineConfiguration; |
| 13 | 15 | import org.flowable.engine.history.HistoricProcessInstance; |
| 14 | 16 | import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; |
| ... | ... | @@ -146,9 +148,22 @@ public class FlowableUtils { |
| 146 | 148 | * @param processInstance 流程实例 |
| 147 | 149 | * @return 发起用户选择的审批人 Map |
| 148 | 150 | */ |
| 149 | - @SuppressWarnings("unchecked") | |
| 150 | 151 | public static Map<String, List<Long>> getStartUserSelectAssignees(ProcessInstance processInstance) { |
| 151 | - return (Map<String, List<Long>>) processInstance.getProcessVariables().get( | |
| 152 | + return processInstance != null ? getStartUserSelectAssignees(processInstance.getProcessVariables()) : null; | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * 获得流程实例的发起用户选择的审批人 Map | |
| 157 | + * | |
| 158 | + * @param processVariables 流程变量 | |
| 159 | + * @return 发起用户选择的审批人 Map | |
| 160 | + */ | |
| 161 | + @SuppressWarnings("unchecked") | |
| 162 | + public static Map<String, List<Long>> getStartUserSelectAssignees(Map<String, Object> processVariables) { | |
| 163 | + if (processVariables == null) { | |
| 164 | + return null; | |
| 165 | + } | |
| 166 | + return (Map<String, List<Long>>) processVariables.get( | |
| 152 | 167 | BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES); |
| 153 | 168 | } |
| 154 | 169 | |
| ... | ... | @@ -202,18 +217,29 @@ public class FlowableUtils { |
| 202 | 217 | |
| 203 | 218 | // ========== Expression 相关的工具方法 ========== |
| 204 | 219 | |
| 205 | - public static Object getExpressionValue(VariableContainer variableContainer, String expressionString) { | |
| 206 | - ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(); | |
| 207 | - assert processEngineConfiguration != null; | |
| 220 | + private static Object getExpressionValue(VariableContainer variableContainer, String expressionString, | |
| 221 | + ProcessEngineConfigurationImpl processEngineConfiguration) { | |
| 222 | + assert processEngineConfiguration!= null; | |
| 208 | 223 | ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager(); |
| 209 | - assert expressionManager != null; | |
| 224 | + assert expressionManager!= null; | |
| 210 | 225 | Expression expression = expressionManager.createExpression(expressionString); |
| 211 | 226 | return expression.getValue(variableContainer); |
| 212 | 227 | } |
| 213 | 228 | |
| 229 | + public static Object getExpressionValue(VariableContainer variableContainer, String expressionString) { | |
| 230 | + ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(); | |
| 231 | + if (processEngineConfiguration != null) { | |
| 232 | + return getExpressionValue(variableContainer, expressionString, processEngineConfiguration); | |
| 233 | + } | |
| 234 | + // 如果 ProcessEngineConfigurationImpl 获取不到,则需要通过 ManagementService 来获取 | |
| 235 | + ManagementService managementService = SpringUtil.getBean(ManagementService.class); | |
| 236 | + assert managementService != null; | |
| 237 | + return managementService.executeCommand(context -> | |
| 238 | + getExpressionValue(variableContainer, expressionString, CommandContextUtil.getProcessEngineConfiguration())); | |
| 239 | + } | |
| 240 | + | |
| 214 | 241 | public static Object getExpressionValue(Map<String, Object> variable, String expressionString) { |
| 215 | - VariableContainer variableContainer = new MapDelegateVariableContainer(variable, | |
| 216 | - VariableContainer.empty()); | |
| 242 | + VariableContainer variableContainer = new MapDelegateVariableContainer(variable, VariableContainer.empty()); | |
| 217 | 243 | return getExpressionValue(variableContainer, expressionString); |
| 218 | 244 | } |
| 219 | 245 | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmProcessInstanceServiceImpl.java
| 1 | -package cn.iocoder.yudao.module.bpm.service.task; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.date.DateUtils; 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.definition.vo.model.simple.BpmSimpleModelNodeVO; import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.*; import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.BpmApprovalDetailRespVO.ActivityNodeTask; import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskRespVO; import cn.iocoder.yudao.module.bpm.convert.task.BpmProcessInstanceConvert; import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; import cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants; import cn.iocoder.yudao.module.bpm.enums.definition.BpmModelTypeEnum; import cn.iocoder.yudao.module.bpm.enums.definition.BpmSimpleModelNodeType; import cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum; import cn.iocoder.yudao.module.bpm.enums.task.BpmReasonEnum; import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateInvoker; import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy; import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.BpmTaskCandidateStartUserSelectStrategy; import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants; import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants; import cn.iocoder.yudao.module.bpm.framework.flowable.core.event.BpmProcessInstanceEventPublisher; import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils; import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils; import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.SimpleModelUtils; import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; import cn.iocoder.yudao.module.bpm.service.message.BpmMessageService; import cn.iocoder.yudao.module.system.api.dept.DeptApi; import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; import jakarta.annotation.Resource; import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.flowable.bpmn.constants.BpmnXMLConstants; import org.flowable.bpmn.model.*; import org.flowable.engine.HistoryService; import org.flowable.engine.RuntimeService; import org.flowable.engine.history.HistoricActivityInstance; import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.history.HistoricProcessInstanceQuery; import org.flowable.engine.repository.ProcessDefinition; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.task.api.history.HistoricTaskInstance; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import java.util.*; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; import static cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.BpmApprovalDetailRespVO.ActivityNode; import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*; import static cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants.START_USER_NODE_ID; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static org.flowable.bpmn.constants.BpmnXMLConstants.*; /** * 流程实例 Service 实现类 * <p> * ProcessDefinition & ProcessInstance & Execution & Task 的关系: * 1. <a href="https://blog.csdn.net/bobozai86/article/details/105210414" /> * <p> * HistoricProcessInstance & ProcessInstance 的关系: * 1. <a href=" https://my.oschina.net/843294669/blog/71902" /> * <p> * 简单来说,前者 = 历史 + 运行中的流程实例,后者仅是运行中的流程实例 * * @author 芋道源码 */ @Service @Validated @Slf4j public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService { @Resource private RuntimeService runtimeService; @Resource private HistoryService historyService; @Resource private BpmProcessDefinitionService processDefinitionService; @Resource @Lazy // 避免循环依赖 private BpmTaskService taskService; @Resource private BpmMessageService messageService; @Resource private BpmTaskCandidateInvoker bpmTaskCandidateInvoker; @Resource private AdminUserApi adminUserApi; @Resource private DeptApi deptApi; @Resource private BpmProcessInstanceEventPublisher processInstanceEventPublisher; // ========== Query 查询相关方法 ========== @Override public ProcessInstance getProcessInstance(String id) { return runtimeService.createProcessInstanceQuery() .includeProcessVariables() .processInstanceId(id) .singleResult(); } @Override public List<ProcessInstance> getProcessInstances(Set<String> ids) { return runtimeService.createProcessInstanceQuery().processInstanceIds(ids).list(); } @Override public HistoricProcessInstance getHistoricProcessInstance(String id) { return historyService.createHistoricProcessInstanceQuery().processInstanceId(id).includeProcessVariables().singleResult(); } @Override public List<HistoricProcessInstance> getHistoricProcessInstances(Set<String> ids) { return historyService.createHistoricProcessInstanceQuery().processInstanceIds(ids).list(); } @Override public PageResult<HistoricProcessInstance> getProcessInstancePage(Long userId, BpmProcessInstancePageReqVO pageReqVO) { // 通过 BpmProcessInstanceExtDO 表,先查询到对应的分页 HistoricProcessInstanceQuery processInstanceQuery = historyService.createHistoricProcessInstanceQuery() .includeProcessVariables() .processInstanceTenantId(FlowableUtils.getTenantId()) .orderByProcessInstanceStartTime().desc(); if (userId != null) { // 【我的流程】菜单时,需要传递该字段 processInstanceQuery.startedBy(String.valueOf(userId)); } else if (pageReqVO.getStartUserId() != null) { // 【管理流程】菜单时,才会传递该字段 processInstanceQuery.startedBy(String.valueOf(pageReqVO.getStartUserId())); } if (StrUtil.isNotEmpty(pageReqVO.getName())) { processInstanceQuery.processInstanceNameLike("%" + pageReqVO.getName() + "%"); } if (StrUtil.isNotEmpty(pageReqVO.getProcessDefinitionKey())) { processInstanceQuery.processDefinitionKey(pageReqVO.getProcessDefinitionKey()); } if (StrUtil.isNotEmpty(pageReqVO.getCategory())) { processInstanceQuery.processDefinitionCategory(pageReqVO.getCategory()); } if (pageReqVO.getStatus() != null) { processInstanceQuery.variableValueEquals(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, pageReqVO.getStatus()); } if (ArrayUtil.isNotEmpty(pageReqVO.getCreateTime())) { processInstanceQuery.startedAfter(DateUtils.of(pageReqVO.getCreateTime()[0])); processInstanceQuery.startedBefore(DateUtils.of(pageReqVO.getCreateTime()[1])); } // 查询数量 long processInstanceCount = processInstanceQuery.count(); if (processInstanceCount == 0) { return PageResult.empty(processInstanceCount); } // 查询列表 List<HistoricProcessInstance> processInstanceList = processInstanceQuery.listPage(PageUtils.getStart(pageReqVO), pageReqVO.getPageSize()); return new PageResult<>(processInstanceList, processInstanceCount); } private Map<String, String> getFormFieldsPermission(BpmnModel bpmnModel, String activityId, String taskId) { // 1. 获取流程活动编号。流程活动 Id 为空事,从流程任务中获取流程活动 Id if (StrUtil.isEmpty(activityId) && StrUtil.isNotEmpty(taskId)) { activityId = Optional.ofNullable(taskService.getHistoricTask(taskId)) .map(HistoricTaskInstance::getTaskDefinitionKey).orElse(null); } if (StrUtil.isEmpty(activityId)) { return null; } // 2. 从 BpmnModel 中解析表单字段权限 return BpmnModelUtils.parseFormFieldsPermission(bpmnModel, activityId); } @Override public BpmApprovalDetailRespVO getApprovalDetail(Long loginUserId, BpmApprovalDetailReqVO reqVO) { // 1.1 从 reqVO 中,读取公共变量 Long startUserId = loginUserId; // 流程发起人 HistoricProcessInstance historicProcessInstance = null; // 流程实例 ProcessInstance processInstance = null; // 流程 Integer processInstanceStatus = BpmProcessInstanceStatusEnum.NOT_START.getStatus(); // 流程状态 Map<String, Object> processVariables = reqVO.getProcessVariables(); // 流程变量 // 1.2 如果是流程已发起的场景,则使用流程实例的数据 if (reqVO.getProcessInstanceId() != null) { historicProcessInstance = getHistoricProcessInstance(reqVO.getProcessInstanceId()); if (historicProcessInstance == null) { throw exception(ErrorCodeConstants.PROCESS_INSTANCE_NOT_EXISTS); } processInstance = getProcessInstance(historicProcessInstance.getId()); startUserId = Long.valueOf(historicProcessInstance.getStartUserId()); processInstanceStatus = FlowableUtils.getProcessInstanceStatus(historicProcessInstance); processVariables = historicProcessInstance.getProcessVariables(); } // 1.3 读取其它相关数据 ProcessDefinition processDefinition = processDefinitionService.getProcessDefinition( historicProcessInstance != null ? historicProcessInstance.getProcessDefinitionId() : reqVO.getProcessDefinitionId()); BpmProcessDefinitionInfoDO processDefinitionInfo = processDefinitionService.getProcessDefinitionInfo(processDefinition.getId()); BpmnModel bpmnModel = processDefinitionService.getProcessDefinitionBpmnModel(processDefinition.getId()); // 2.1 已结束 + 进行中的活动节点 List<ActivityNode> endActivityNodes = null; // 已结束的审批信息 List<ActivityNode> runActivityNodes = null; // 进行中的审批信息 List<HistoricActivityInstance> activities = null; // 流程实例列表 if (reqVO.getProcessInstanceId() != null) { activities = taskService.getActivityListByProcessInstanceId(reqVO.getProcessInstanceId()); List<HistoricTaskInstance> tasks = taskService.getTaskListByProcessInstanceId(reqVO.getProcessInstanceId(), true); endActivityNodes = getEndActivityNodeList(startUserId, processDefinitionInfo, historicProcessInstance, processInstanceStatus, activities, tasks); runActivityNodes = getRunApproveNodeList(startUserId, bpmnModel, processInstance, activities, tasks); } // 2.2 流程已经结束,直接 return,无需预测 if (BpmProcessInstanceStatusEnum.isProcessEndStatus(processInstanceStatus)) { return buildApprovalDetail(reqVO, bpmnModel, processDefinition, processDefinitionInfo, historicProcessInstance, processInstanceStatus, endActivityNodes, runActivityNodes, null, null); } // 3.1 计算当前登录用户的待办任务 // TODO @jason:有一个极端情况,如果一个用户有 2 个 task A 和 B,A 已经通过,B 需要审核。这个时,通过 A 进来,todo 拿到 B,会不会表单权限不一致哈。 BpmTaskRespVO todoTask = taskService.getFirstTodoTask(loginUserId, reqVO.getProcessInstanceId()); // 3.2 预测未运行节点的审批信息 List<ActivityNode> simulateActivityNodes = getSimulateApproveNodeList(startUserId, bpmnModel, processDefinitionInfo, processInstance, processVariables, activities); // 4. 拼接最终数据 return buildApprovalDetail(reqVO, bpmnModel, processDefinition, processDefinitionInfo, historicProcessInstance, processInstanceStatus, endActivityNodes, runActivityNodes, simulateActivityNodes, todoTask); } /** * 拼接审批详情的最终数据 * * 主要是,拼接审批人的用户信息、部门信息 */ private BpmApprovalDetailRespVO buildApprovalDetail(BpmApprovalDetailReqVO reqVO, BpmnModel bpmnModel, ProcessDefinition processDefinition, BpmProcessDefinitionInfoDO processDefinitionInfo, HistoricProcessInstance processInstance, Integer processInstanceStatus, List<ActivityNode> endApprovalNodeInfos, List<ActivityNode> runningApprovalNodeInfos, List<ActivityNode> simulateApprovalNodeInfos, BpmTaskRespVO todoTask) { // 1. 获取所有需要读取用户信息的 userIds List<ActivityNode> approveNodes = newArrayList(asList(endApprovalNodeInfos, runningApprovalNodeInfos, simulateApprovalNodeInfos)); Set<Long> userIds = BpmProcessInstanceConvert.INSTANCE.parseUserIds(processInstance, approveNodes, todoTask); Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds); Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); // 2. 表单权限 Map<String, String> formFieldsPermission = getFormFieldsPermission(bpmnModel, reqVO.getActivityId(), reqVO.getTaskId()); // 3. 拼接数据 return BpmProcessInstanceConvert.INSTANCE.buildApprovalDetail(bpmnModel, processDefinition, processDefinitionInfo, processInstance, processInstanceStatus, approveNodes, todoTask, formFieldsPermission, userMap, deptMap); } /** * 获得【已结束】的活动节点们 */ private List<ActivityNode> getEndActivityNodeList(Long startUserId, BpmProcessDefinitionInfoDO processDefinitionInfo, HistoricProcessInstance historicProcessInstance, Integer processInstanceStatus, List<HistoricActivityInstance> activities, List<HistoricTaskInstance> tasks) { // 遍历 tasks 列表,只处理已结束的 UserTask // 为什么不通过 activities 呢?因为,加签场景下,它只存在于 tasks,没有 activities,导致如果遍历 activities 的话,它无法成为一个节点 List<HistoricTaskInstance> endTasks = filterList(tasks, task -> task.getEndTime() != null); List<ActivityNode> approvalNodes = convertList(endTasks, task -> { ActivityNode activityNode = new ActivityNode().setId(task.getTaskDefinitionKey()).setName(task.getName()) .setNodeType(START_USER_NODE_ID.equals(task.getTaskDefinitionKey()) ? BpmSimpleModelNodeType.START_USER_NODE.getType() : BpmSimpleModelNodeType.APPROVE_NODE.getType()) .setStatus(FlowableUtils.getTaskStatus(task)) .setStartTime(DateUtils.of(task.getCreateTime())).setEndTime(DateUtils.of(task.getEndTime())) .setTasks(singletonList(BpmProcessInstanceConvert.INSTANCE.buildApprovalTaskInfo(task))); // 如果是取消状态,则跳过 if (BpmTaskStatusEnum.isCancelStatus(activityNode.getStatus())) { return null; } return activityNode; }); // 遍历 activities,只处理已结束的 StartEvent、EndEvent List<HistoricActivityInstance> endActivities = filterList(activities, activity -> activity.getEndTime() != null && (StrUtil.equalsAny(activity.getActivityType(), ELEMENT_EVENT_START, ELEMENT_EVENT_END))); endActivities.forEach(activity -> { // StartEvent:只处理 BPMN 的场景。因为,SIMPLE 情况下,已经有 START_USER_NODE 节点 if (ELEMENT_EVENT_START.equals(activity.getActivityType()) && BpmModelTypeEnum.BPMN.getType().equals(processDefinitionInfo.getModelType())) { ActivityNodeTask startTask = new ActivityNodeTask().setId(BpmnModelConstants.START_USER_NODE_ID) .setAssignee(startUserId).setStatus(BpmTaskStatusEnum.APPROVE.getStatus()); ActivityNode startNode = new ActivityNode().setId(startTask.getId()) .setName(BpmSimpleModelNodeType.START_USER_NODE.getName()) .setNodeType(BpmSimpleModelNodeType.START_USER_NODE.getType()) .setStatus(startTask.getStatus()).setTasks(ListUtil.of(startTask)) .setStartTime(DateUtils.of(activity.getStartTime())).setEndTime(DateUtils.of(activity.getEndTime())); approvalNodes.add(0, startNode); return; } // EndEvent if (ELEMENT_EVENT_END.equals(activity.getActivityType())) { if (BpmProcessInstanceStatusEnum.isRejectStatus(processInstanceStatus)) { // 拒绝情况下,不需要展示 EndEvent 结束节点。原因是:前端已经展示 x 效果,无需重复展示 return; } ActivityNode endNode = new ActivityNode().setId(activity.getId()) .setName(BpmSimpleModelNodeType.END_NODE.getName()) .setNodeType(BpmSimpleModelNodeType.END_NODE.getType()).setStatus(processInstanceStatus) .setStartTime(DateUtils.of(activity.getStartTime())).setEndTime(DateUtils.of(activity.getEndTime())); String reason = FlowableUtils.getProcessInstanceReason(historicProcessInstance); if (StrUtil.isNotEmpty(reason)) { endNode.setTasks(singletonList(new ActivityNodeTask().setId(endNode.getId()) .setStatus(endNode.getStatus()).setReason(reason))); } approvalNodes.add(endNode); } }); return approvalNodes; } /** * 获得【进行中】的活动节点们 */ private List<ActivityNode> getRunApproveNodeList(Long startUserId, BpmnModel bpmnModel, ProcessInstance processInstance, List<HistoricActivityInstance> activities, List<HistoricTaskInstance> tasks) { // 构建运行中的任务,基于 activityId 分组 List<HistoricActivityInstance> runActivities = filterList(activities, activity -> activity.getEndTime() == null && (StrUtil.equalsAny(activity.getActivityType(), ELEMENT_TASK_USER))); Map<String, List<HistoricActivityInstance>> runningTaskMap = convertMultiMap(runActivities, HistoricActivityInstance::getActivityId); // 按照 activityId 分组,构建 ApprovalNodeInfo 节点 Map<String, HistoricTaskInstance> taskMap = convertMap(tasks, HistoricTaskInstance::getId); return convertList(runningTaskMap.entrySet(), entry -> { String activityId = entry.getKey(); List<HistoricActivityInstance> taskActivities = entry.getValue(); // 构建活动节点 HistoricActivityInstance firstActivity = CollUtil.getFirst(taskActivities); // 取第一个任务,会签/或签的任务,开始时间相同 ActivityNode activityNode = new ActivityNode().setId(firstActivity.getActivityId()).setName(firstActivity.getActivityName()) .setNodeType(BpmSimpleModelNodeType.APPROVE_NODE.getType()).setStatus(BpmTaskStatusEnum.RUNNING.getStatus()) .setTasks(new ArrayList<>()); // 处理每个任务的 tasks 属性 for (HistoricActivityInstance activity : taskActivities) { HistoricTaskInstance task = taskMap.get(activity.getTaskId()); activityNode.getTasks().add(BpmProcessInstanceConvert.INSTANCE.buildApprovalTaskInfo(task)); // 加签子任务 List<HistoricTaskInstance> childrenTasks = taskService.getAllChildrenTaskListByParentTaskId(activity.getTaskId(), tasks); if (CollUtil.isNotEmpty(childrenTasks)) { activityNode.getTasks().addAll(convertList(childrenTasks, BpmProcessInstanceConvert.INSTANCE::buildApprovalTaskInfo)); } } // 处理每个任务的 candidateUsers 属性:如果是依次审批,需要预测它的后续审批人。因为 Task 是审批完一个,创建一个新的 Task FlowElement flowNode = BpmnModelUtils.getFlowElementById(bpmnModel, activityId); if (BpmnModelUtils.isSequentialUserTask(flowNode)) { List<Long> candidateUserIds = getTaskCandidateUserList(startUserId, processInstance, flowNode.getId(), BpmnModelUtils.parseCandidateStrategy(flowNode), BpmnModelUtils.parseCandidateParam(flowNode)); // 截取当前审批人位置后面的候选人,不包含当前审批人 ActivityNodeTask approvalTaskInfo = CollUtil.getFirst(activityNode.getTasks()); Assert.notNull(approvalTaskInfo, "任务不能为空"); int index = CollUtil.indexOf(candidateUserIds, userId -> userId.equals(approvalTaskInfo.getAssignee())); activityNode.setCandidateUserIds(CollUtil.sub(candidateUserIds, index + 1, candidateUserIds.size())); } return activityNode; }); } /** * 获得【预测(未来)】的活动节点们 */ private List<ActivityNode> getSimulateApproveNodeList(Long startUserId, BpmnModel bpmnModel, BpmProcessDefinitionInfoDO processDefinitionInfo, ProcessInstance processInstance, Map<String, Object> processVariable, List<HistoricActivityInstance> activities) { // TODO @芋艿:【可优化】在驳回场景下,未来的预测准确性不高。原因是,驳回后,HistoricActivityInstance 包括了历史的操作,不是只有 startEvent 到当前节点的记录 Set<String> runActivityIds = convertSet(activities, HistoricActivityInstance::getActivityId); // 情况一:BPMN 设计器 if (Objects.equals(BpmModelTypeEnum.BPMN.getType(), processDefinitionInfo.getModelType())) { List<FlowElement> flowElements = BpmnModelUtils.simulateProcess(bpmnModel, processVariable); return convertList(flowElements, flowElement -> buildNotRunApproveNodeForBpmn(startUserId, processInstance, flowElement, runActivityIds)); } // 情况二:SIMPLE 设计器 if (Objects.equals(BpmModelTypeEnum.SIMPLE.getType(), processDefinitionInfo.getModelType())) { BpmSimpleModelNodeVO simpleModel = JsonUtils.parseObject(processDefinitionInfo.getSimpleModel(), BpmSimpleModelNodeVO.class); List<BpmSimpleModelNodeVO> simpleNodes = SimpleModelUtils.simulateProcess(simpleModel, processVariable); return convertList(simpleNodes, simpleNode -> buildNotRunApproveNodeForSimple(startUserId, processInstance, simpleNode, runActivityIds)); } throw new IllegalArgumentException("未知设计器类型:" + processDefinitionInfo.getModelType()); } private ActivityNode buildNotRunApproveNodeForSimple(Long startUserId, ProcessInstance processInstance, BpmSimpleModelNodeVO node, Set<String> runActivityIds) { // TODO @芋艿:【可优化】在驳回场景下,未来的预测准确性不高。原因是,驳回后,HistoricActivityInstance 包括了历史的操作,不是只有 startEvent 到当前节点的记录 if (runActivityIds.contains(node.getId())) { return null; } ActivityNode activityNode = new ActivityNode().setId(node.getId()).setName(node.getName()) .setNodeType(node.getType()).setStatus(BpmTaskStatusEnum.NOT_START.getStatus()); // 1. 开始节点/审批节点 if (ObjectUtils.equalsAny(node.getType(), BpmSimpleModelNodeType.START_USER_NODE.getType(), BpmSimpleModelNodeType.APPROVE_NODE.getType())) { Integer candidateStrategy = BpmSimpleModelNodeType.START_USER_NODE.getType().equals(node.getType()) ? BpmTaskCandidateStrategyEnum.START_USER.getStrategy() : node.getCandidateStrategy(); if (candidateStrategy != null) { activityNode.setCandidateUserIds(getTaskCandidateUserList(startUserId, processInstance, node.getId(), candidateStrategy, node.getCandidateParam())); } return activityNode; } // 2. 结束节点 if (BpmSimpleModelNodeType.END_NODE.getType().equals(node.getType())) { return activityNode; } // 3. 抄送节点 TODO @jason:抄送节点 return null; } private ActivityNode buildNotRunApproveNodeForBpmn(Long startUserId, ProcessInstance processInstance, FlowElement node, Set<String> runActivityIds) { if (runActivityIds.contains(node.getId())) { return null; } ActivityNode activityNode = new ActivityNode().setId(node.getId()).setStatus(BpmTaskStatusEnum.NOT_START.getStatus()); // 1. 开始节点 if (node instanceof StartEvent) { return activityNode.setName(BpmSimpleModelNodeType.START_USER_NODE.getName()) .setNodeType(BpmSimpleModelNodeType.START_USER_NODE.getType()); } // 2. 审批节点 if (node instanceof UserTask) { return activityNode.setName(node.getName()).setNodeType(BpmSimpleModelNodeType.APPROVE_NODE.getType()) .setCandidateUserIds(getTaskCandidateUserList(startUserId, processInstance, node.getId(), BpmnModelUtils.parseCandidateStrategy(node), BpmnModelUtils.parseCandidateParam(node))); } // 3. 结束节点 if (node instanceof EndEvent) { return activityNode.setName(BpmSimpleModelNodeType.END_NODE.getName()) .setNodeType(BpmSimpleModelNodeType.END_NODE.getType()); } return null; } private List<Long> getTaskCandidateUserList(Long startUserId, ProcessInstance processInstance, String activityId, Integer candidateStrategy, String candidateParam) { BpmTaskCandidateStrategy taskCandidateStrategy = bpmTaskCandidateInvoker.getCandidateStrategy(candidateStrategy); return new ArrayList<>(taskCandidateStrategy.calculateUsers(startUserId, processInstance, activityId, candidateParam)); } @Override public BpmProcessInstanceBpmnModelViewRespVO getProcessInstanceBpmnModelView(String id) { // 1.1 获得流程实例 HistoricProcessInstance processInstance = getHistoricProcessInstance(id); if (processInstance == null) { return null; } // 1.2 获得流程定义 BpmnModel bpmnModel = processDefinitionService.getProcessDefinitionBpmnModel(processInstance.getProcessDefinitionId()); if (bpmnModel == null) { return null; } // 1.3 获得流程实例对应的活动实例列表 + 任务列表 List<HistoricActivityInstance> activities = taskService.getActivityListByProcessInstanceId(id); List<HistoricTaskInstance> tasks = taskService.getTaskListByProcessInstanceId(id, true); // 2.1 拼接进度信息 Set<String> unfinishedTaskActivityIds = convertSet(activities, HistoricActivityInstance::getActivityId, activityInstance -> activityInstance.getEndTime() == null); Set<String> finishedTaskActivityIds = convertSet(activities, HistoricActivityInstance::getActivityId, activityInstance -> activityInstance.getEndTime() != null && ObjectUtil.notEqual(activityInstance.getActivityType(), BpmnXMLConstants.ELEMENT_SEQUENCE_FLOW)); Set<String> finishedSequenceFlowActivityIds = convertSet(activities, HistoricActivityInstance::getActivityId, activityInstance -> activityInstance.getEndTime() != null && ObjectUtil.equals(activityInstance.getActivityType(), BpmnXMLConstants.ELEMENT_SEQUENCE_FLOW)); // 特殊:会签情况下,会有部分已完成(审批)、部分未完成(待审批),此时需要 finishedTaskActivityIds 移除掉 unfinishedTaskActivityIds.removeAll(finishedTaskActivityIds); // 特殊:如果流程实例被拒绝,则需要计算是哪个活动节点。 // 注意,只取最后一个。因为会存在多次拒绝的情况,拒绝驳回到指定节点 Set<String> rejectTaskActivityIds = CollUtil.newHashSet(); if (BpmProcessInstanceStatusEnum.isRejectStatus(FlowableUtils.getProcessInstanceStatus(processInstance))) { tasks.stream() .filter(task -> BpmTaskStatusEnum.isRejectStatus(FlowableUtils.getTaskStatus(task))) .max(Comparator.comparing(HistoricTaskInstance::getEndTime)) .ifPresent(reject -> rejectTaskActivityIds.add(reject.getTaskDefinitionKey())); finishedTaskActivityIds.removeAll(rejectTaskActivityIds); } // 2.2 拼接基础信息 Set<Long> userIds = BpmProcessInstanceConvert.INSTANCE.parseUserIds02(processInstance, tasks); Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds); Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); return BpmProcessInstanceConvert.INSTANCE.buildProcessInstanceBpmnModelView(processInstance, tasks, bpmnModel, unfinishedTaskActivityIds, finishedTaskActivityIds, finishedSequenceFlowActivityIds, rejectTaskActivityIds, userMap, deptMap); } // ========== Update 写入相关方法 ========== @Override @Transactional(rollbackFor = Exception.class) public String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqVO createReqVO) { // 获得流程定义 ProcessDefinition definition = processDefinitionService.getProcessDefinition(createReqVO.getProcessDefinitionId()); // 发起流程 return createProcessInstance0(userId, definition, createReqVO.getVariables(), null, createReqVO.getStartUserSelectAssignees()); } @Override public String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqDTO createReqDTO) { // 获得流程定义 ProcessDefinition definition = processDefinitionService.getActiveProcessDefinition(createReqDTO.getProcessDefinitionKey()); // 发起流程 return createProcessInstance0(userId, definition, createReqDTO.getVariables(), createReqDTO.getBusinessKey(), createReqDTO.getStartUserSelectAssignees()); } private String createProcessInstance0(Long userId, ProcessDefinition definition, Map<String, Object> variables, String businessKey, Map<String, List<Long>> startUserSelectAssignees) { // 1.1 校验流程定义 if (definition == null) { throw exception(PROCESS_DEFINITION_NOT_EXISTS); } if (definition.isSuspended()) { throw exception(PROCESS_DEFINITION_IS_SUSPENDED); } BpmProcessDefinitionInfoDO processDefinitionInfo = processDefinitionService.getProcessDefinitionInfo(definition.getId()); if (processDefinitionInfo == null) { throw exception(PROCESS_DEFINITION_NOT_EXISTS); } // 1.2 校验是否能够发起 if (!processDefinitionService.canUserStartProcessDefinition(processDefinitionInfo, userId)) { throw exception(PROCESS_INSTANCE_START_USER_CAN_START); } // 1.3 校验发起人自选审批人 validateStartUserSelectAssignees(definition, startUserSelectAssignees); // 2. 创建流程实例 if (variables == null) { variables = new HashMap<>(); } FlowableUtils.filterProcessInstanceFormVariable(variables); // 过滤一下,避免 ProcessInstance 系统级的变量被占用 variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, // 流程实例状态:审批中 BpmProcessInstanceStatusEnum.RUNNING.getStatus()); if (CollUtil.isNotEmpty(startUserSelectAssignees)) { variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, startUserSelectAssignees); } ProcessInstance instance = runtimeService.createProcessInstanceBuilder() .processDefinitionId(definition.getId()) .businessKey(businessKey) .name(definition.getName().trim()) .variables(variables) .start(); return instance.getId(); } private void validateStartUserSelectAssignees(ProcessDefinition definition, Map<String, List<Long>> startUserSelectAssignees) { // 1. 获得发起人自选审批人的 UserTask 列表 BpmnModel bpmnModel = processDefinitionService.getProcessDefinitionBpmnModel(definition.getId()); List<UserTask> userTaskList = BpmTaskCandidateStartUserSelectStrategy.getStartUserSelectUserTaskList(bpmnModel); if (CollUtil.isEmpty(userTaskList)) { return; } // 2. 校验发起人自选审批人的 UserTask 是否都配置了 userTaskList.forEach(userTask -> { List<Long> assignees = startUserSelectAssignees != null ? startUserSelectAssignees.get(userTask.getId()) : null; if (CollUtil.isEmpty(assignees)) { throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_CONFIG, userTask.getName()); } Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(assignees); assignees.forEach(assignee -> { if (userMap.get(assignee) == null) { throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_EXISTS, userTask.getName(), assignee); } }); }); } @Override public void cancelProcessInstanceByStartUser(Long userId, @Valid BpmProcessInstanceCancelReqVO cancelReqVO) { // 1.1 校验流程实例存在 ProcessInstance instance = getProcessInstance(cancelReqVO.getId()); if (instance == null) { throw exception(PROCESS_INSTANCE_CANCEL_FAIL_NOT_EXISTS); } // 1.2 只能取消自己的 if (!Objects.equals(instance.getStartUserId(), String.valueOf(userId))) { throw exception(PROCESS_INSTANCE_CANCEL_FAIL_NOT_SELF); } // 2. 取消流程 updateProcessInstanceCancel(cancelReqVO.getId(), BpmReasonEnum.CANCEL_PROCESS_INSTANCE_BY_START_USER.format(cancelReqVO.getReason())); } @Override public void cancelProcessInstanceByAdmin(Long userId, BpmProcessInstanceCancelReqVO cancelReqVO) { // 1.1 校验流程实例存在 ProcessInstance instance = getProcessInstance(cancelReqVO.getId()); if (instance == null) { throw exception(PROCESS_INSTANCE_CANCEL_FAIL_NOT_EXISTS); } // 2. 取消流程 AdminUserRespDTO user = adminUserApi.getUser(userId); updateProcessInstanceCancel(cancelReqVO.getId(), BpmReasonEnum.CANCEL_PROCESS_INSTANCE_BY_ADMIN.format(user.getNickname(), cancelReqVO.getReason())); } private void updateProcessInstanceCancel(String id, String reason) { // 1. 更新流程实例 status runtimeService.setVariable(id, BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, BpmProcessInstanceStatusEnum.CANCEL.getStatus()); runtimeService.setVariable(id, BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON, reason); // 2. 结束流程 taskService.moveTaskToEnd(id); } @Override public void updateProcessInstanceReject(ProcessInstance processInstance, String reason) { runtimeService.setVariable(processInstance.getProcessInstanceId(), BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, BpmProcessInstanceStatusEnum.REJECT.getStatus()); runtimeService.setVariable(processInstance.getProcessInstanceId(), BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON, BpmReasonEnum.REJECT_TASK.format(reason)); } // ========== Event 事件相关方法 ========== @Override public void processProcessInstanceCompleted(ProcessInstance instance) { // 注意:需要基于 instance 设置租户编号,避免 Flowable 内部异步时,丢失租户编号 FlowableUtils.execute(instance.getTenantId(), () -> { // 1.1 获取当前状态 Integer status = (Integer) instance.getProcessVariables().get(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS); String reason = (String) instance.getProcessVariables().get(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON); // 1.2 当流程状态还是审批状态中,说明审批通过了,则变更下它的状态 // 为什么这么处理?因为流程完成,并且完成了,说明审批通过了 if (Objects.equals(status, BpmProcessInstanceStatusEnum.RUNNING.getStatus())) { status = BpmProcessInstanceStatusEnum.APPROVE.getStatus(); runtimeService.setVariable(instance.getId(), BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, status); } // 2. 发送对应的消息通知 if (Objects.equals(status, BpmProcessInstanceStatusEnum.APPROVE.getStatus())) { messageService.sendMessageWhenProcessInstanceApprove(BpmProcessInstanceConvert.INSTANCE.buildProcessInstanceApproveMessage(instance)); } else if (Objects.equals(status, BpmProcessInstanceStatusEnum.REJECT.getStatus())) { messageService.sendMessageWhenProcessInstanceReject( BpmProcessInstanceConvert.INSTANCE.buildProcessInstanceRejectMessage(instance, reason)); } // 3. 发送流程实例的状态事件 processInstanceEventPublisher.sendProcessInstanceResultEvent( BpmProcessInstanceConvert.INSTANCE.buildProcessInstanceStatusEvent(this, instance, status)); }); } } | |
| 2 | 1 | \ No newline at end of file |
| 2 | +package cn.iocoder.yudao.module.bpm.service.task; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.date.DateUtils; 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.definition.vo.model.simple.BpmSimpleModelNodeVO; import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.*; import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.BpmApprovalDetailRespVO.ActivityNodeTask; import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskRespVO; import cn.iocoder.yudao.module.bpm.convert.task.BpmProcessInstanceConvert; import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; import cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants; import cn.iocoder.yudao.module.bpm.enums.definition.BpmModelTypeEnum; import cn.iocoder.yudao.module.bpm.enums.definition.BpmSimpleModelNodeType; import cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum; import cn.iocoder.yudao.module.bpm.enums.task.BpmReasonEnum; import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateInvoker; import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept.BpmTaskCandidateStartUserSelectStrategy; import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants; import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants; import cn.iocoder.yudao.module.bpm.framework.flowable.core.event.BpmProcessInstanceEventPublisher; import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils; import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils; import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.SimpleModelUtils; import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; import cn.iocoder.yudao.module.bpm.service.message.BpmMessageService; import cn.iocoder.yudao.module.system.api.dept.DeptApi; import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; import jakarta.annotation.Resource; import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.flowable.bpmn.constants.BpmnXMLConstants; import org.flowable.bpmn.model.*; import org.flowable.engine.HistoryService; import org.flowable.engine.RuntimeService; import org.flowable.engine.history.HistoricActivityInstance; import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.history.HistoricProcessInstanceQuery; import org.flowable.engine.repository.ProcessDefinition; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.task.api.history.HistoricTaskInstance; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import java.util.*; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; import static cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.BpmApprovalDetailRespVO.ActivityNode; import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*; import static cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants.START_USER_NODE_ID; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static org.flowable.bpmn.constants.BpmnXMLConstants.*; /** * 流程实例 Service 实现类 * <p> * ProcessDefinition & ProcessInstance & Execution & Task 的关系: * 1. <a href="https://blog.csdn.net/bobozai86/article/details/105210414" /> * <p> * HistoricProcessInstance & ProcessInstance 的关系: * 1. <a href=" https://my.oschina.net/843294669/blog/71902" /> * <p> * 简单来说,前者 = 历史 + 运行中的流程实例,后者仅是运行中的流程实例 * * @author 芋道源码 */ @Service @Validated @Slf4j public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService { @Resource private RuntimeService runtimeService; @Resource private HistoryService historyService; @Resource private BpmProcessDefinitionService processDefinitionService; @Resource @Lazy // 避免循环依赖 private BpmTaskService taskService; @Resource private BpmMessageService messageService; @Resource private AdminUserApi adminUserApi; @Resource private DeptApi deptApi; @Resource private BpmProcessInstanceEventPublisher processInstanceEventPublisher; @Resource private BpmTaskCandidateInvoker taskCandidateInvoker; // ========== Query 查询相关方法 ========== @Override public ProcessInstance getProcessInstance(String id) { return runtimeService.createProcessInstanceQuery() .includeProcessVariables() .processInstanceId(id) .singleResult(); } @Override public List<ProcessInstance> getProcessInstances(Set<String> ids) { return runtimeService.createProcessInstanceQuery().processInstanceIds(ids).list(); } @Override public HistoricProcessInstance getHistoricProcessInstance(String id) { return historyService.createHistoricProcessInstanceQuery().processInstanceId(id).includeProcessVariables().singleResult(); } @Override public List<HistoricProcessInstance> getHistoricProcessInstances(Set<String> ids) { return historyService.createHistoricProcessInstanceQuery().processInstanceIds(ids).list(); } @Override public PageResult<HistoricProcessInstance> getProcessInstancePage(Long userId, BpmProcessInstancePageReqVO pageReqVO) { // 通过 BpmProcessInstanceExtDO 表,先查询到对应的分页 HistoricProcessInstanceQuery processInstanceQuery = historyService.createHistoricProcessInstanceQuery() .includeProcessVariables() .processInstanceTenantId(FlowableUtils.getTenantId()) .orderByProcessInstanceStartTime().desc(); if (userId != null) { // 【我的流程】菜单时,需要传递该字段 processInstanceQuery.startedBy(String.valueOf(userId)); } else if (pageReqVO.getStartUserId() != null) { // 【管理流程】菜单时,才会传递该字段 processInstanceQuery.startedBy(String.valueOf(pageReqVO.getStartUserId())); } if (StrUtil.isNotEmpty(pageReqVO.getName())) { processInstanceQuery.processInstanceNameLike("%" + pageReqVO.getName() + "%"); } if (StrUtil.isNotEmpty(pageReqVO.getProcessDefinitionKey())) { processInstanceQuery.processDefinitionKey(pageReqVO.getProcessDefinitionKey()); } if (StrUtil.isNotEmpty(pageReqVO.getCategory())) { processInstanceQuery.processDefinitionCategory(pageReqVO.getCategory()); } if (pageReqVO.getStatus() != null) { processInstanceQuery.variableValueEquals(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, pageReqVO.getStatus()); } if (ArrayUtil.isNotEmpty(pageReqVO.getCreateTime())) { processInstanceQuery.startedAfter(DateUtils.of(pageReqVO.getCreateTime()[0])); processInstanceQuery.startedBefore(DateUtils.of(pageReqVO.getCreateTime()[1])); } // 查询数量 long processInstanceCount = processInstanceQuery.count(); if (processInstanceCount == 0) { return PageResult.empty(processInstanceCount); } // 查询列表 List<HistoricProcessInstance> processInstanceList = processInstanceQuery.listPage(PageUtils.getStart(pageReqVO), pageReqVO.getPageSize()); return new PageResult<>(processInstanceList, processInstanceCount); } private Map<String, String> getFormFieldsPermission(BpmnModel bpmnModel, String activityId, String taskId) { // 1. 获取流程活动编号。流程活动 Id 为空事,从流程任务中获取流程活动 Id if (StrUtil.isEmpty(activityId) && StrUtil.isNotEmpty(taskId)) { activityId = Optional.ofNullable(taskService.getHistoricTask(taskId)) .map(HistoricTaskInstance::getTaskDefinitionKey).orElse(null); } if (StrUtil.isEmpty(activityId)) { return null; } // 2. 从 BpmnModel 中解析表单字段权限 return BpmnModelUtils.parseFormFieldsPermission(bpmnModel, activityId); } @Override public BpmApprovalDetailRespVO getApprovalDetail(Long loginUserId, BpmApprovalDetailReqVO reqVO) { // 1.1 从 reqVO 中,读取公共变量 Long startUserId = loginUserId; // 流程发起人 HistoricProcessInstance historicProcessInstance = null; // 流程实例 Integer processInstanceStatus = BpmProcessInstanceStatusEnum.NOT_START.getStatus(); // 流程状态 Map<String, Object> processVariables = reqVO.getProcessVariables(); // 流程变量 // 1.2 如果是流程已发起的场景,则使用流程实例的数据 if (reqVO.getProcessInstanceId() != null) { historicProcessInstance = getHistoricProcessInstance(reqVO.getProcessInstanceId()); if (historicProcessInstance == null) { throw exception(ErrorCodeConstants.PROCESS_INSTANCE_NOT_EXISTS); } startUserId = Long.valueOf(historicProcessInstance.getStartUserId()); processInstanceStatus = FlowableUtils.getProcessInstanceStatus(historicProcessInstance); processVariables = historicProcessInstance.getProcessVariables(); } // 1.3 读取其它相关数据 ProcessDefinition processDefinition = processDefinitionService.getProcessDefinition( historicProcessInstance != null ? historicProcessInstance.getProcessDefinitionId() : reqVO.getProcessDefinitionId()); BpmProcessDefinitionInfoDO processDefinitionInfo = processDefinitionService.getProcessDefinitionInfo(processDefinition.getId()); BpmnModel bpmnModel = processDefinitionService.getProcessDefinitionBpmnModel(processDefinition.getId()); // 2.1 已结束 + 进行中的活动节点 List<ActivityNode> endActivityNodes = null; // 已结束的审批信息 List<ActivityNode> runActivityNodes = null; // 进行中的审批信息 List<HistoricActivityInstance> activities = null; // 流程实例列表 if (reqVO.getProcessInstanceId() != null) { activities = taskService.getActivityListByProcessInstanceId(reqVO.getProcessInstanceId()); List<HistoricTaskInstance> tasks = taskService.getTaskListByProcessInstanceId(reqVO.getProcessInstanceId(), true); endActivityNodes = getEndActivityNodeList(startUserId, processDefinitionInfo, historicProcessInstance, processInstanceStatus, activities, tasks); runActivityNodes = getRunApproveNodeList(startUserId, bpmnModel, processDefinition, processVariables, activities, tasks); } // 2.2 流程已经结束,直接 return,无需预测 if (BpmProcessInstanceStatusEnum.isProcessEndStatus(processInstanceStatus)) { return buildApprovalDetail(reqVO, bpmnModel, processDefinition, processDefinitionInfo, historicProcessInstance, processInstanceStatus, endActivityNodes, runActivityNodes, null, null); } // 3.1 计算当前登录用户的待办任务 // TODO @jason:有一个极端情况,如果一个用户有 2 个 task A 和 B,A 已经通过,B 需要审核。这个时,通过 A 进来,todo 拿到 B,会不会表单权限不一致哈。 BpmTaskRespVO todoTask = taskService.getFirstTodoTask(loginUserId, reqVO.getProcessInstanceId()); // 3.2 预测未运行节点的审批信息 List<ActivityNode> simulateActivityNodes = getSimulateApproveNodeList(startUserId, bpmnModel, processDefinitionInfo, processVariables, activities); // 4. 拼接最终数据 return buildApprovalDetail(reqVO, bpmnModel, processDefinition, processDefinitionInfo, historicProcessInstance, processInstanceStatus, endActivityNodes, runActivityNodes, simulateActivityNodes, todoTask); } /** * 拼接审批详情的最终数据 * * 主要是,拼接审批人的用户信息、部门信息 */ private BpmApprovalDetailRespVO buildApprovalDetail(BpmApprovalDetailReqVO reqVO, BpmnModel bpmnModel, ProcessDefinition processDefinition, BpmProcessDefinitionInfoDO processDefinitionInfo, HistoricProcessInstance processInstance, Integer processInstanceStatus, List<ActivityNode> endApprovalNodeInfos, List<ActivityNode> runningApprovalNodeInfos, List<ActivityNode> simulateApprovalNodeInfos, BpmTaskRespVO todoTask) { // 1. 获取所有需要读取用户信息的 userIds List<ActivityNode> approveNodes = newArrayList(asList(endApprovalNodeInfos, runningApprovalNodeInfos, simulateApprovalNodeInfos)); Set<Long> userIds = BpmProcessInstanceConvert.INSTANCE.parseUserIds(processInstance, approveNodes, todoTask); Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds); Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); // 2. 表单权限 Map<String, String> formFieldsPermission = getFormFieldsPermission(bpmnModel, reqVO.getActivityId(), reqVO.getTaskId()); // 3. 拼接数据 return BpmProcessInstanceConvert.INSTANCE.buildApprovalDetail(bpmnModel, processDefinition, processDefinitionInfo, processInstance, processInstanceStatus, approveNodes, todoTask, formFieldsPermission, userMap, deptMap); } /** * 获得【已结束】的活动节点们 */ private List<ActivityNode> getEndActivityNodeList(Long startUserId, BpmProcessDefinitionInfoDO processDefinitionInfo, HistoricProcessInstance historicProcessInstance, Integer processInstanceStatus, List<HistoricActivityInstance> activities, List<HistoricTaskInstance> tasks) { // 遍历 tasks 列表,只处理已结束的 UserTask // 为什么不通过 activities 呢?因为,加签场景下,它只存在于 tasks,没有 activities,导致如果遍历 activities 的话,它无法成为一个节点 List<HistoricTaskInstance> endTasks = filterList(tasks, task -> task.getEndTime() != null); List<ActivityNode> approvalNodes = convertList(endTasks, task -> { ActivityNode activityNode = new ActivityNode().setId(task.getTaskDefinitionKey()).setName(task.getName()) .setNodeType(START_USER_NODE_ID.equals(task.getTaskDefinitionKey()) ? BpmSimpleModelNodeType.START_USER_NODE.getType() : BpmSimpleModelNodeType.APPROVE_NODE.getType()) .setStatus(FlowableUtils.getTaskStatus(task)) .setStartTime(DateUtils.of(task.getCreateTime())).setEndTime(DateUtils.of(task.getEndTime())) .setTasks(singletonList(BpmProcessInstanceConvert.INSTANCE.buildApprovalTaskInfo(task))); // 如果是取消状态,则跳过 if (BpmTaskStatusEnum.isCancelStatus(activityNode.getStatus())) { return null; } return activityNode; }); // 遍历 activities,只处理已结束的 StartEvent、EndEvent List<HistoricActivityInstance> endActivities = filterList(activities, activity -> activity.getEndTime() != null && (StrUtil.equalsAny(activity.getActivityType(), ELEMENT_EVENT_START, ELEMENT_EVENT_END))); endActivities.forEach(activity -> { // StartEvent:只处理 BPMN 的场景。因为,SIMPLE 情况下,已经有 START_USER_NODE 节点 if (ELEMENT_EVENT_START.equals(activity.getActivityType()) && BpmModelTypeEnum.BPMN.getType().equals(processDefinitionInfo.getModelType())) { ActivityNodeTask startTask = new ActivityNodeTask().setId(BpmnModelConstants.START_USER_NODE_ID) .setAssignee(startUserId).setStatus(BpmTaskStatusEnum.APPROVE.getStatus()); ActivityNode startNode = new ActivityNode().setId(startTask.getId()) .setName(BpmSimpleModelNodeType.START_USER_NODE.getName()) .setNodeType(BpmSimpleModelNodeType.START_USER_NODE.getType()) .setStatus(startTask.getStatus()).setTasks(ListUtil.of(startTask)) .setStartTime(DateUtils.of(activity.getStartTime())).setEndTime(DateUtils.of(activity.getEndTime())); approvalNodes.add(0, startNode); return; } // EndEvent if (ELEMENT_EVENT_END.equals(activity.getActivityType())) { if (BpmProcessInstanceStatusEnum.isRejectStatus(processInstanceStatus)) { // 拒绝情况下,不需要展示 EndEvent 结束节点。原因是:前端已经展示 x 效果,无需重复展示 return; } ActivityNode endNode = new ActivityNode().setId(activity.getId()) .setName(BpmSimpleModelNodeType.END_NODE.getName()) .setNodeType(BpmSimpleModelNodeType.END_NODE.getType()).setStatus(processInstanceStatus) .setStartTime(DateUtils.of(activity.getStartTime())).setEndTime(DateUtils.of(activity.getEndTime())); String reason = FlowableUtils.getProcessInstanceReason(historicProcessInstance); if (StrUtil.isNotEmpty(reason)) { endNode.setTasks(singletonList(new ActivityNodeTask().setId(endNode.getId()) .setStatus(endNode.getStatus()).setReason(reason))); } approvalNodes.add(endNode); } }); return approvalNodes; } /** * 获得【进行中】的活动节点们 */ private List<ActivityNode> getRunApproveNodeList(Long startUserId, BpmnModel bpmnModel, ProcessDefinition processDefinition, Map<String, Object> processVariables, List<HistoricActivityInstance> activities, List<HistoricTaskInstance> tasks) { // 构建运行中的任务,基于 activityId 分组 List<HistoricActivityInstance> runActivities = filterList(activities, activity -> activity.getEndTime() == null && (StrUtil.equalsAny(activity.getActivityType(), ELEMENT_TASK_USER))); Map<String, List<HistoricActivityInstance>> runningTaskMap = convertMultiMap(runActivities, HistoricActivityInstance::getActivityId); // 按照 activityId 分组,构建 ApprovalNodeInfo 节点 Map<String, HistoricTaskInstance> taskMap = convertMap(tasks, HistoricTaskInstance::getId); return convertList(runningTaskMap.entrySet(), entry -> { String activityId = entry.getKey(); List<HistoricActivityInstance> taskActivities = entry.getValue(); // 构建活动节点 HistoricActivityInstance firstActivity = CollUtil.getFirst(taskActivities); // 取第一个任务,会签/或签的任务,开始时间相同 ActivityNode activityNode = new ActivityNode().setId(firstActivity.getActivityId()).setName(firstActivity.getActivityName()) .setNodeType(BpmSimpleModelNodeType.APPROVE_NODE.getType()).setStatus(BpmTaskStatusEnum.RUNNING.getStatus()) .setTasks(new ArrayList<>()); // 处理每个任务的 tasks 属性 for (HistoricActivityInstance activity : taskActivities) { HistoricTaskInstance task = taskMap.get(activity.getTaskId()); activityNode.getTasks().add(BpmProcessInstanceConvert.INSTANCE.buildApprovalTaskInfo(task)); // 加签子任务 List<HistoricTaskInstance> childrenTasks = taskService.getAllChildrenTaskListByParentTaskId(activity.getTaskId(), tasks); if (CollUtil.isNotEmpty(childrenTasks)) { activityNode.getTasks().addAll(convertList(childrenTasks, BpmProcessInstanceConvert.INSTANCE::buildApprovalTaskInfo)); } } // 处理每个任务的 candidateUsers 属性:如果是依次审批,需要预测它的后续审批人。因为 Task 是审批完一个,创建一个新的 Task FlowElement flowNode = BpmnModelUtils.getFlowElementById(bpmnModel, activityId); if (BpmnModelUtils.isSequentialUserTask(flowNode)) { List<Long> candidateUserIds = getTaskCandidateUserList(bpmnModel, flowNode.getId(), startUserId, processDefinition.getId(), processVariables); // 截取当前审批人位置后面的候选人,不包含当前审批人 ActivityNodeTask approvalTaskInfo = CollUtil.getFirst(activityNode.getTasks()); Assert.notNull(approvalTaskInfo, "任务不能为空"); int index = CollUtil.indexOf(candidateUserIds, userId -> userId.equals(approvalTaskInfo.getAssignee())); activityNode.setCandidateUserIds(CollUtil.sub(candidateUserIds, index + 1, candidateUserIds.size())); } return activityNode; }); } /** * 获得【预测(未来)】的活动节点们 */ private List<ActivityNode> getSimulateApproveNodeList(Long startUserId, BpmnModel bpmnModel, BpmProcessDefinitionInfoDO processDefinitionInfo, Map<String, Object> processVariables, List<HistoricActivityInstance> activities) { // TODO @芋艿:【可优化】在驳回场景下,未来的预测准确性不高。原因是,驳回后,HistoricActivityInstance 包括了历史的操作,不是只有 startEvent 到当前节点的记录 Set<String> runActivityIds = convertSet(activities, HistoricActivityInstance::getActivityId); // 情况一:BPMN 设计器 if (Objects.equals(BpmModelTypeEnum.BPMN.getType(), processDefinitionInfo.getModelType())) { List<FlowElement> flowElements = BpmnModelUtils.simulateProcess(bpmnModel, processVariables); return convertList(flowElements, flowElement -> buildNotRunApproveNodeForBpmn(startUserId, bpmnModel, processDefinitionInfo, processVariables, flowElement, runActivityIds)); } // 情况二:SIMPLE 设计器 if (Objects.equals(BpmModelTypeEnum.SIMPLE.getType(), processDefinitionInfo.getModelType())) { BpmSimpleModelNodeVO simpleModel = JsonUtils.parseObject(processDefinitionInfo.getSimpleModel(), BpmSimpleModelNodeVO.class); List<BpmSimpleModelNodeVO> simpleNodes = SimpleModelUtils.simulateProcess(simpleModel, processVariables); return convertList(simpleNodes, simpleNode -> buildNotRunApproveNodeForSimple(startUserId, bpmnModel, processDefinitionInfo, processVariables, simpleNode, runActivityIds)); } throw new IllegalArgumentException("未知设计器类型:" + processDefinitionInfo.getModelType()); } private ActivityNode buildNotRunApproveNodeForSimple(Long startUserId, BpmnModel bpmnModel, BpmProcessDefinitionInfoDO processDefinitionInfo, Map<String, Object> processVariables, BpmSimpleModelNodeVO node, Set<String> runActivityIds) { // TODO @芋艿:【可优化】在驳回场景下,未来的预测准确性不高。原因是,驳回后,HistoricActivityInstance 包括了历史的操作,不是只有 startEvent 到当前节点的记录 if (runActivityIds.contains(node.getId())) { return null; } ActivityNode activityNode = new ActivityNode().setId(node.getId()).setName(node.getName()) .setNodeType(node.getType()).setStatus(BpmTaskStatusEnum.NOT_START.getStatus()); // 1. 开始节点/审批节点 if (ObjectUtils.equalsAny(node.getType(), BpmSimpleModelNodeType.START_USER_NODE.getType(), BpmSimpleModelNodeType.APPROVE_NODE.getType())) { List<Long> candidateUserIds = getTaskCandidateUserList(bpmnModel, node.getId(), startUserId, processDefinitionInfo.getProcessDefinitionId(), processVariables); activityNode.setCandidateUserIds(candidateUserIds); return activityNode; } // 2. 结束节点 if (BpmSimpleModelNodeType.END_NODE.getType().equals(node.getType())) { return activityNode; } // 3. 抄送节点 TODO @jason:抄送节点 return null; } private ActivityNode buildNotRunApproveNodeForBpmn(Long startUserId, BpmnModel bpmnModel, BpmProcessDefinitionInfoDO processDefinitionInfo, Map<String, Object> processVariables, FlowElement node, Set<String> runActivityIds) { if (runActivityIds.contains(node.getId())) { return null; } ActivityNode activityNode = new ActivityNode().setId(node.getId()).setStatus(BpmTaskStatusEnum.NOT_START.getStatus()); // 1. 开始节点 if (node instanceof StartEvent) { return activityNode.setName(BpmSimpleModelNodeType.START_USER_NODE.getName()) .setNodeType(BpmSimpleModelNodeType.START_USER_NODE.getType()); } // 2. 审批节点 if (node instanceof UserTask) { List<Long> candidateUserIds = getTaskCandidateUserList(bpmnModel, node.getId(), startUserId, processDefinitionInfo.getProcessDefinitionId(), processVariables); return activityNode.setName(node.getName()).setNodeType(BpmSimpleModelNodeType.APPROVE_NODE.getType()) .setCandidateUserIds(candidateUserIds); } // 3. 结束节点 if (node instanceof EndEvent) { return activityNode.setName(BpmSimpleModelNodeType.END_NODE.getName()) .setNodeType(BpmSimpleModelNodeType.END_NODE.getType()); } return null; } private List<Long> getTaskCandidateUserList(BpmnModel bpmnModel, String activityId, Long startUserId, String processDefinitionId, Map<String, Object> processVariables) { Set<Long> userIds = taskCandidateInvoker.calculateUsersByActivity(bpmnModel, activityId, startUserId, processDefinitionId, processVariables); return new ArrayList<>(userIds); } @Override public BpmProcessInstanceBpmnModelViewRespVO getProcessInstanceBpmnModelView(String id) { // 1.1 获得流程实例 HistoricProcessInstance processInstance = getHistoricProcessInstance(id); if (processInstance == null) { return null; } // 1.2 获得流程定义 BpmnModel bpmnModel = processDefinitionService.getProcessDefinitionBpmnModel(processInstance.getProcessDefinitionId()); if (bpmnModel == null) { return null; } // 1.3 获得流程实例对应的活动实例列表 + 任务列表 List<HistoricActivityInstance> activities = taskService.getActivityListByProcessInstanceId(id); List<HistoricTaskInstance> tasks = taskService.getTaskListByProcessInstanceId(id, true); // 2.1 拼接进度信息 Set<String> unfinishedTaskActivityIds = convertSet(activities, HistoricActivityInstance::getActivityId, activityInstance -> activityInstance.getEndTime() == null); Set<String> finishedTaskActivityIds = convertSet(activities, HistoricActivityInstance::getActivityId, activityInstance -> activityInstance.getEndTime() != null && ObjectUtil.notEqual(activityInstance.getActivityType(), BpmnXMLConstants.ELEMENT_SEQUENCE_FLOW)); Set<String> finishedSequenceFlowActivityIds = convertSet(activities, HistoricActivityInstance::getActivityId, activityInstance -> activityInstance.getEndTime() != null && ObjectUtil.equals(activityInstance.getActivityType(), BpmnXMLConstants.ELEMENT_SEQUENCE_FLOW)); // 特殊:会签情况下,会有部分已完成(审批)、部分未完成(待审批),此时需要 finishedTaskActivityIds 移除掉 unfinishedTaskActivityIds.removeAll(finishedTaskActivityIds); // 特殊:如果流程实例被拒绝,则需要计算是哪个活动节点。 // 注意,只取最后一个。因为会存在多次拒绝的情况,拒绝驳回到指定节点 Set<String> rejectTaskActivityIds = CollUtil.newHashSet(); if (BpmProcessInstanceStatusEnum.isRejectStatus(FlowableUtils.getProcessInstanceStatus(processInstance))) { tasks.stream() .filter(task -> BpmTaskStatusEnum.isRejectStatus(FlowableUtils.getTaskStatus(task))) .max(Comparator.comparing(HistoricTaskInstance::getEndTime)) .ifPresent(reject -> rejectTaskActivityIds.add(reject.getTaskDefinitionKey())); finishedTaskActivityIds.removeAll(rejectTaskActivityIds); } // 2.2 拼接基础信息 Set<Long> userIds = BpmProcessInstanceConvert.INSTANCE.parseUserIds02(processInstance, tasks); Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds); Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(convertSet(userMap.values(), AdminUserRespDTO::getDeptId)); return BpmProcessInstanceConvert.INSTANCE.buildProcessInstanceBpmnModelView(processInstance, tasks, bpmnModel, unfinishedTaskActivityIds, finishedTaskActivityIds, finishedSequenceFlowActivityIds, rejectTaskActivityIds, userMap, deptMap); } // ========== Update 写入相关方法 ========== @Override @Transactional(rollbackFor = Exception.class) public String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqVO createReqVO) { // 获得流程定义 ProcessDefinition definition = processDefinitionService.getProcessDefinition(createReqVO.getProcessDefinitionId()); // 发起流程 return createProcessInstance0(userId, definition, createReqVO.getVariables(), null, createReqVO.getStartUserSelectAssignees()); } @Override public String createProcessInstance(Long userId, @Valid BpmProcessInstanceCreateReqDTO createReqDTO) { // 获得流程定义 ProcessDefinition definition = processDefinitionService.getActiveProcessDefinition(createReqDTO.getProcessDefinitionKey()); // 发起流程 return createProcessInstance0(userId, definition, createReqDTO.getVariables(), createReqDTO.getBusinessKey(), createReqDTO.getStartUserSelectAssignees()); } private String createProcessInstance0(Long userId, ProcessDefinition definition, Map<String, Object> variables, String businessKey, Map<String, List<Long>> startUserSelectAssignees) { // 1.1 校验流程定义 if (definition == null) { throw exception(PROCESS_DEFINITION_NOT_EXISTS); } if (definition.isSuspended()) { throw exception(PROCESS_DEFINITION_IS_SUSPENDED); } BpmProcessDefinitionInfoDO processDefinitionInfo = processDefinitionService.getProcessDefinitionInfo(definition.getId()); if (processDefinitionInfo == null) { throw exception(PROCESS_DEFINITION_NOT_EXISTS); } // 1.2 校验是否能够发起 if (!processDefinitionService.canUserStartProcessDefinition(processDefinitionInfo, userId)) { throw exception(PROCESS_INSTANCE_START_USER_CAN_START); } // 1.3 校验发起人自选审批人 validateStartUserSelectAssignees(definition, startUserSelectAssignees); // 2. 创建流程实例 if (variables == null) { variables = new HashMap<>(); } FlowableUtils.filterProcessInstanceFormVariable(variables); // 过滤一下,避免 ProcessInstance 系统级的变量被占用 variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, // 流程实例状态:审批中 BpmProcessInstanceStatusEnum.RUNNING.getStatus()); if (CollUtil.isNotEmpty(startUserSelectAssignees)) { variables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, startUserSelectAssignees); } ProcessInstance instance = runtimeService.createProcessInstanceBuilder() .processDefinitionId(definition.getId()) .businessKey(businessKey) .name(definition.getName().trim()) .variables(variables) .start(); return instance.getId(); } private void validateStartUserSelectAssignees(ProcessDefinition definition, Map<String, List<Long>> startUserSelectAssignees) { // 1. 获得发起人自选审批人的 UserTask 列表 BpmnModel bpmnModel = processDefinitionService.getProcessDefinitionBpmnModel(definition.getId()); List<UserTask> userTaskList = BpmTaskCandidateStartUserSelectStrategy.getStartUserSelectUserTaskList(bpmnModel); if (CollUtil.isEmpty(userTaskList)) { return; } // 2. 校验发起人自选审批人的 UserTask 是否都配置了 userTaskList.forEach(userTask -> { List<Long> assignees = startUserSelectAssignees != null ? startUserSelectAssignees.get(userTask.getId()) : null; if (CollUtil.isEmpty(assignees)) { throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_CONFIG, userTask.getName()); } Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(assignees); assignees.forEach(assignee -> { if (userMap.get(assignee) == null) { throw exception(PROCESS_INSTANCE_START_USER_SELECT_ASSIGNEES_NOT_EXISTS, userTask.getName(), assignee); } }); }); } @Override public void cancelProcessInstanceByStartUser(Long userId, @Valid BpmProcessInstanceCancelReqVO cancelReqVO) { // 1.1 校验流程实例存在 ProcessInstance instance = getProcessInstance(cancelReqVO.getId()); if (instance == null) { throw exception(PROCESS_INSTANCE_CANCEL_FAIL_NOT_EXISTS); } // 1.2 只能取消自己的 if (!Objects.equals(instance.getStartUserId(), String.valueOf(userId))) { throw exception(PROCESS_INSTANCE_CANCEL_FAIL_NOT_SELF); } // 2. 取消流程 updateProcessInstanceCancel(cancelReqVO.getId(), BpmReasonEnum.CANCEL_PROCESS_INSTANCE_BY_START_USER.format(cancelReqVO.getReason())); } @Override public void cancelProcessInstanceByAdmin(Long userId, BpmProcessInstanceCancelReqVO cancelReqVO) { // 1.1 校验流程实例存在 ProcessInstance instance = getProcessInstance(cancelReqVO.getId()); if (instance == null) { throw exception(PROCESS_INSTANCE_CANCEL_FAIL_NOT_EXISTS); } // 2. 取消流程 AdminUserRespDTO user = adminUserApi.getUser(userId); updateProcessInstanceCancel(cancelReqVO.getId(), BpmReasonEnum.CANCEL_PROCESS_INSTANCE_BY_ADMIN.format(user.getNickname(), cancelReqVO.getReason())); } private void updateProcessInstanceCancel(String id, String reason) { // 1. 更新流程实例 status runtimeService.setVariable(id, BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, BpmProcessInstanceStatusEnum.CANCEL.getStatus()); runtimeService.setVariable(id, BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON, reason); // 2. 结束流程 taskService.moveTaskToEnd(id); } @Override public void updateProcessInstanceReject(ProcessInstance processInstance, String reason) { runtimeService.setVariable(processInstance.getProcessInstanceId(), BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, BpmProcessInstanceStatusEnum.REJECT.getStatus()); runtimeService.setVariable(processInstance.getProcessInstanceId(), BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON, BpmReasonEnum.REJECT_TASK.format(reason)); } // ========== Event 事件相关方法 ========== @Override public void processProcessInstanceCompleted(ProcessInstance instance) { // 注意:需要基于 instance 设置租户编号,避免 Flowable 内部异步时,丢失租户编号 FlowableUtils.execute(instance.getTenantId(), () -> { // 1.1 获取当前状态 Integer status = (Integer) instance.getProcessVariables().get(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS); String reason = (String) instance.getProcessVariables().get(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON); // 1.2 当流程状态还是审批状态中,说明审批通过了,则变更下它的状态 // 为什么这么处理?因为流程完成,并且完成了,说明审批通过了 if (Objects.equals(status, BpmProcessInstanceStatusEnum.RUNNING.getStatus())) { status = BpmProcessInstanceStatusEnum.APPROVE.getStatus(); runtimeService.setVariable(instance.getId(), BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS, status); } // 2. 发送对应的消息通知 if (Objects.equals(status, BpmProcessInstanceStatusEnum.APPROVE.getStatus())) { messageService.sendMessageWhenProcessInstanceApprove(BpmProcessInstanceConvert.INSTANCE.buildProcessInstanceApproveMessage(instance)); } else if (Objects.equals(status, BpmProcessInstanceStatusEnum.REJECT.getStatus())) { messageService.sendMessageWhenProcessInstanceReject( BpmProcessInstanceConvert.INSTANCE.buildProcessInstanceRejectMessage(instance, reason)); } // 3. 发送流程实例的状态事件 processInstanceEventPublisher.sendProcessInstanceResultEvent( BpmProcessInstanceConvert.INSTANCE.buildProcessInstanceStatusEvent(this, instance, status)); }); } } | |
| 3 | 3 | \ No newline at end of file | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/BpmTaskCandidateInvokerTest.java
| ... | ... | @@ -3,7 +3,7 @@ package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate; |
| 3 | 3 | import cn.hutool.core.map.MapUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | 5 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 6 | -import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.BpmTaskCandidateUserStrategy; | |
| 6 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user.BpmTaskCandidateUserStrategy; | |
| 7 | 7 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
| 8 | 8 | import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants; |
| 9 | 9 | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| ... | ... | @@ -47,13 +47,13 @@ public class BpmTaskCandidateInvokerTest extends BaseMockitoUnitTest { |
| 47 | 47 | |
| 48 | 48 | @BeforeEach |
| 49 | 49 | public void setUp() { |
| 50 | - strategy = new BpmTaskCandidateUserStrategy(adminUserApi); // 创建strategy实例 | |
| 50 | + strategy = new BpmTaskCandidateUserStrategy(); // 创建strategy实例 | |
| 51 | 51 | strategyList = Collections.singletonList(strategy); // 创建strategyList |
| 52 | 52 | taskCandidateInvoker = new BpmTaskCandidateInvoker(strategyList, adminUserApi); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | @Test |
| 56 | - public void testCalculateUsers() { | |
| 56 | + public void testCalculateUsersByTask() { | |
| 57 | 57 | // 准备参数 |
| 58 | 58 | String param = "1,2"; |
| 59 | 59 | DelegateExecution execution = mock(DelegateExecution.class); |
| ... | ... | @@ -74,7 +74,7 @@ public class BpmTaskCandidateInvokerTest extends BaseMockitoUnitTest { |
| 74 | 74 | when(adminUserApi.getUserMap(eq(asSet(1L, 2L)))).thenReturn(userMap); |
| 75 | 75 | |
| 76 | 76 | // 调用 |
| 77 | - Set<Long> results = taskCandidateInvoker.calculateUsers(execution); | |
| 77 | + Set<Long> results = taskCandidateInvoker.calculateUsersByTask(execution); | |
| 78 | 78 | // 断言 |
| 79 | 79 | assertEquals(asSet(1L, 2L), results); |
| 80 | 80 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateDeptLeaderMultiStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | |
| 4 | +import cn.iocoder.yudao.module.system.api.dept.DeptApi; | |
| 5 | +import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; | |
| 6 | +import org.assertj.core.util.Sets; | |
| 7 | +import org.junit.jupiter.api.Test; | |
| 8 | +import org.mockito.InjectMocks; | |
| 9 | +import org.mockito.Mock; | |
| 10 | +import org.mockito.stubbing.Answer; | |
| 11 | + | |
| 12 | +import java.util.Set; | |
| 13 | + | |
| 14 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 16 | +import static org.mockito.ArgumentMatchers.any; | |
| 17 | +import static org.mockito.Mockito.when; | |
| 18 | + | |
| 19 | +public class BpmTaskCandidateDeptLeaderMultiStrategyTest extends BaseMockitoUnitTest { | |
| 20 | + | |
| 21 | + @InjectMocks | |
| 22 | + private BpmTaskCandidateDeptLeaderMultiStrategy strategy; | |
| 23 | + | |
| 24 | + @Mock | |
| 25 | + private DeptApi deptApi; | |
| 26 | + | |
| 27 | + @Test | |
| 28 | + public void testCalculateUsers() { | |
| 29 | + // 准备参数 | |
| 30 | + String param = "10,20|2"; | |
| 31 | + // mock 方法 | |
| 32 | + when(deptApi.getDept(any())).thenAnswer((Answer<DeptRespDTO>) invocationOnMock -> { | |
| 33 | + Long deptId = invocationOnMock.getArgument(0); | |
| 34 | + return randomPojo(DeptRespDTO.class, o -> o.setId(deptId).setParentId(deptId * 100).setLeaderUserId(deptId + 1)); | |
| 35 | + }); | |
| 36 | + | |
| 37 | + // 调用 | |
| 38 | + Set<Long> userIds = strategy.calculateUsers(param); | |
| 39 | + // 断言结果 | |
| 40 | + assertEquals(Sets.newLinkedHashSet(11L, 1001L, 21L, 2001L), userIds); | |
| 41 | + } | |
| 42 | + | |
| 43 | +} | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateDeptLeaderStrategyTest.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateDeptLeaderStrategyTest.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.common.util.collection.SetUtils; | |
| 3 | 4 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 4 | 5 | import cn.iocoder.yudao.module.system.api.dept.DeptApi; |
| 5 | 6 | import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; |
| 7 | +import org.assertj.core.util.Sets; | |
| 6 | 8 | import org.junit.jupiter.api.Test; |
| 7 | 9 | import org.mockito.InjectMocks; |
| 8 | 10 | import org.mockito.Mock; |
| 9 | 11 | |
| 10 | 12 | import java.util.Set; |
| 11 | 13 | |
| 12 | -import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; | |
| 13 | 14 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 14 | 15 | import static java.util.Arrays.asList; |
| 15 | 16 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| ... | ... | @@ -27,16 +28,16 @@ public class BpmTaskCandidateDeptLeaderStrategyTest extends BaseMockitoUnitTest |
| 27 | 28 | @Test |
| 28 | 29 | public void testCalculateUsers() { |
| 29 | 30 | // 准备参数 |
| 30 | - String param = "1,2"; | |
| 31 | + String param = "10,20"; | |
| 31 | 32 | // mock 方法 |
| 32 | - DeptRespDTO dept1 = randomPojo(DeptRespDTO.class, o -> o.setLeaderUserId(11L)); | |
| 33 | - DeptRespDTO dept2 = randomPojo(DeptRespDTO.class, o -> o.setLeaderUserId(22L)); | |
| 34 | - when(deptApi.getDeptList(eq(asSet(1L, 2L)))).thenReturn(asList(dept1, dept2)); | |
| 33 | + when(deptApi.getDeptList(eq(SetUtils.asSet(10L, 20L)))).thenReturn(asList( | |
| 34 | + randomPojo(DeptRespDTO.class, o -> o.setId(10L).setParentId(10L).setLeaderUserId(11L)), | |
| 35 | + randomPojo(DeptRespDTO.class, o -> o.setId(20L).setParentId(20L).setLeaderUserId(21L)))); | |
| 35 | 36 | |
| 36 | 37 | // 调用 |
| 37 | - Set<Long> results = strategy.calculateUsers(null, param); | |
| 38 | - // 断言 | |
| 39 | - assertEquals(asSet(11L, 22L), results); | |
| 38 | + Set<Long> userIds = strategy.calculateUsers(param); | |
| 39 | + // 断言结果 | |
| 40 | + assertEquals(Sets.newLinkedHashSet(11L, 21L), userIds); | |
| 40 | 41 | } |
| 41 | 42 | |
| 42 | 43 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateDeptMemberStrategyTest.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateDeptMemberStrategyTest.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.common.util.collection.SetUtils; | |
| 3 | 4 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 5 | +import cn.iocoder.yudao.module.system.api.dept.DeptApi; | |
| 4 | 6 | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| 5 | 7 | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| 8 | +import org.assertj.core.util.Sets; | |
| 6 | 9 | import org.junit.jupiter.api.Test; |
| 7 | 10 | import org.mockito.InjectMocks; |
| 8 | 11 | import org.mockito.Mock; |
| 9 | 12 | |
| 10 | -import java.util.List; | |
| 11 | 13 | import java.util.Set; |
| 12 | 14 | |
| 13 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | |
| 14 | -import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; | |
| 15 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 16 | +import static java.util.Arrays.asList; | |
| 15 | 17 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | 18 | import static org.mockito.ArgumentMatchers.eq; |
| 17 | 19 | import static org.mockito.Mockito.when; |
| ... | ... | @@ -22,21 +24,23 @@ public class BpmTaskCandidateDeptMemberStrategyTest extends BaseMockitoUnitTest |
| 22 | 24 | private BpmTaskCandidateDeptMemberStrategy strategy; |
| 23 | 25 | |
| 24 | 26 | @Mock |
| 27 | + private DeptApi deptApi; | |
| 28 | + @Mock | |
| 25 | 29 | private AdminUserApi adminUserApi; |
| 26 | 30 | |
| 27 | 31 | @Test |
| 28 | 32 | public void testCalculateUsers() { |
| 29 | 33 | // 准备参数 |
| 30 | - String param = "11,22"; | |
| 34 | + String param = "10,20"; | |
| 31 | 35 | // mock 方法 |
| 32 | - List<AdminUserRespDTO> users = convertList(asSet(11L, 22L), | |
| 33 | - id -> new AdminUserRespDTO().setId(id)); | |
| 34 | - when(adminUserApi.getUserListByDeptIds(eq(asSet(11L, 22L)))).thenReturn(users); | |
| 36 | + when(adminUserApi.getUserListByDeptIds(eq(SetUtils.asSet(10L, 20L)))).thenReturn(asList( | |
| 37 | + randomPojo(AdminUserRespDTO.class, o -> o.setId(11L)), | |
| 38 | + randomPojo(AdminUserRespDTO.class, o -> o.setId(21L)))); | |
| 35 | 39 | |
| 36 | 40 | // 调用 |
| 37 | - Set<Long> results = strategy.calculateUsers(null, param); | |
| 38 | - // 断言 | |
| 39 | - assertEquals(asSet(11L, 22L), results); | |
| 41 | + Set<Long> userIds = strategy.calculateUsers(param); | |
| 42 | + // 断言结果 | |
| 43 | + assertEquals(Sets.newLinkedHashSet(11L, 21L), userIds); | |
| 40 | 44 | } |
| 41 | 45 | |
| 42 | 46 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateStartUserDeptLeaderMultiStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | |
| 4 | +import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; | |
| 5 | +import cn.iocoder.yudao.module.system.api.dept.DeptApi; | |
| 6 | +import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; | |
| 7 | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 8 | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | |
| 9 | +import org.assertj.core.util.Sets; | |
| 10 | +import org.flowable.engine.delegate.DelegateExecution; | |
| 11 | +import org.flowable.engine.runtime.ProcessInstance; | |
| 12 | +import org.junit.jupiter.api.Test; | |
| 13 | +import org.mockito.InjectMocks; | |
| 14 | +import org.mockito.Mock; | |
| 15 | +import org.mockito.stubbing.Answer; | |
| 16 | + | |
| 17 | +import java.util.Set; | |
| 18 | + | |
| 19 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 21 | +import static org.mockito.ArgumentMatchers.any; | |
| 22 | +import static org.mockito.ArgumentMatchers.eq; | |
| 23 | +import static org.mockito.Mockito.mock; | |
| 24 | +import static org.mockito.Mockito.when; | |
| 25 | + | |
| 26 | +public class BpmTaskCandidateStartUserDeptLeaderMultiStrategyTest extends BaseMockitoUnitTest { | |
| 27 | + | |
| 28 | + @InjectMocks | |
| 29 | + private BpmTaskCandidateStartUserDeptLeaderMultiStrategy strategy; | |
| 30 | + | |
| 31 | + @Mock | |
| 32 | + private BpmProcessInstanceService processInstanceService; | |
| 33 | + | |
| 34 | + @Mock | |
| 35 | + private AdminUserApi adminUserApi; | |
| 36 | + @Mock | |
| 37 | + private DeptApi deptApi; | |
| 38 | + | |
| 39 | + @Test | |
| 40 | + public void testCalculateUsersByTask() { | |
| 41 | + // 准备参数 | |
| 42 | + String param = "2"; | |
| 43 | + // mock 方法(获得流程发起人) | |
| 44 | + Long startUserId = 1L; | |
| 45 | + ProcessInstance processInstance = mock(ProcessInstance.class); | |
| 46 | + DelegateExecution execution = mock(DelegateExecution.class); | |
| 47 | + when(processInstanceService.getProcessInstance(eq(execution.getProcessInstanceId()))).thenReturn(processInstance); | |
| 48 | + when(processInstance.getStartUserId()).thenReturn(startUserId.toString()); | |
| 49 | + // mock 方法(获取发起人的 multi 部门负责人) | |
| 50 | + mockGetStartUserDept(startUserId); | |
| 51 | + | |
| 52 | + // 调用 | |
| 53 | + Set<Long> userIds = strategy.calculateUsersByTask(execution, param); | |
| 54 | + // 断言 | |
| 55 | + assertEquals(Sets.newLinkedHashSet(11L, 1001L), userIds); | |
| 56 | + } | |
| 57 | + | |
| 58 | + @Test | |
| 59 | + public void testCalculateUsersByActivity() { | |
| 60 | + // 准备参数 | |
| 61 | + String param = "2"; | |
| 62 | + // mock 方法 | |
| 63 | + Long startUserId = 1L; | |
| 64 | + mockGetStartUserDept(startUserId); | |
| 65 | + | |
| 66 | + // 调用 | |
| 67 | + Set<Long> userIds = strategy.calculateUsersByActivity(null, null, param, | |
| 68 | + startUserId, null, null); | |
| 69 | + // 断言 | |
| 70 | + assertEquals(Sets.newLinkedHashSet(11L, 1001L), userIds); | |
| 71 | + } | |
| 72 | + | |
| 73 | + private void mockGetStartUserDept(Long startUserId) { | |
| 74 | + when(adminUserApi.getUser(eq(startUserId))).thenReturn( | |
| 75 | + randomPojo(AdminUserRespDTO.class, o -> o.setId(startUserId).setDeptId(10L))); | |
| 76 | + when(deptApi.getDept(any())).thenAnswer((Answer<DeptRespDTO>) invocationOnMock -> { | |
| 77 | + Long deptId = invocationOnMock.getArgument(0); | |
| 78 | + return randomPojo(DeptRespDTO.class, o -> o.setId(deptId).setParentId(deptId * 100).setLeaderUserId(deptId + 1)); | |
| 79 | + }); | |
| 80 | + } | |
| 81 | + | |
| 82 | +} | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateStartUserDeptLeaderStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | |
| 4 | +import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; | |
| 5 | +import cn.iocoder.yudao.module.system.api.dept.DeptApi; | |
| 6 | +import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; | |
| 7 | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 8 | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | |
| 9 | +import org.assertj.core.util.Sets; | |
| 10 | +import org.flowable.engine.delegate.DelegateExecution; | |
| 11 | +import org.flowable.engine.runtime.ProcessInstance; | |
| 12 | +import org.junit.jupiter.api.Test; | |
| 13 | +import org.mockito.InjectMocks; | |
| 14 | +import org.mockito.Mock; | |
| 15 | +import org.mockito.stubbing.Answer; | |
| 16 | + | |
| 17 | +import java.util.Set; | |
| 18 | + | |
| 19 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 21 | +import static org.mockito.ArgumentMatchers.any; | |
| 22 | +import static org.mockito.ArgumentMatchers.eq; | |
| 23 | +import static org.mockito.Mockito.mock; | |
| 24 | +import static org.mockito.Mockito.when; | |
| 25 | + | |
| 26 | +public class BpmTaskCandidateStartUserDeptLeaderStrategyTest extends BaseMockitoUnitTest { | |
| 27 | + | |
| 28 | + @InjectMocks | |
| 29 | + private BpmTaskCandidateStartUserDeptLeaderStrategy strategy; | |
| 30 | + | |
| 31 | + @Mock | |
| 32 | + private BpmProcessInstanceService processInstanceService; | |
| 33 | + | |
| 34 | + @Mock | |
| 35 | + private AdminUserApi adminUserApi; | |
| 36 | + @Mock | |
| 37 | + private DeptApi deptApi; | |
| 38 | + | |
| 39 | + @Test | |
| 40 | + public void testCalculateUsersByTask() { | |
| 41 | + // 准备参数 | |
| 42 | + String param = "2"; | |
| 43 | + // mock 方法(获得流程发起人) | |
| 44 | + Long startUserId = 1L; | |
| 45 | + ProcessInstance processInstance = mock(ProcessInstance.class); | |
| 46 | + DelegateExecution execution = mock(DelegateExecution.class); | |
| 47 | + when(processInstanceService.getProcessInstance(eq(execution.getProcessInstanceId()))).thenReturn(processInstance); | |
| 48 | + when(processInstance.getStartUserId()).thenReturn(startUserId.toString()); | |
| 49 | + // mock 方法(获取发起人的部门负责人) | |
| 50 | + mockGetStartUserDeptLeader(startUserId); | |
| 51 | + | |
| 52 | + // 调用 | |
| 53 | + Set<Long> userIds = strategy.calculateUsersByTask(execution, param); | |
| 54 | + // 断言 | |
| 55 | + assertEquals(Sets.newLinkedHashSet(1001L), userIds); | |
| 56 | + } | |
| 57 | + | |
| 58 | + @Test | |
| 59 | + public void testGetStartUserDeptLeader() { | |
| 60 | + // 准备参数 | |
| 61 | + String param = "2"; | |
| 62 | + // mock 方法 | |
| 63 | + Long startUserId = 1L; | |
| 64 | + mockGetStartUserDeptLeader(startUserId); | |
| 65 | + | |
| 66 | + // 调用 | |
| 67 | + Set<Long> userIds = strategy.calculateUsersByActivity(null, null, param, | |
| 68 | + startUserId, null, null); | |
| 69 | + // 断言 | |
| 70 | + assertEquals(Sets.newLinkedHashSet(1001L), userIds); | |
| 71 | + } | |
| 72 | + | |
| 73 | + private void mockGetStartUserDeptLeader(Long startUserId) { | |
| 74 | + when(adminUserApi.getUser(eq(startUserId))).thenReturn( | |
| 75 | + randomPojo(AdminUserRespDTO.class, o -> o.setId(startUserId).setDeptId(10L))); | |
| 76 | + when(deptApi.getDept(any())).thenAnswer((Answer<DeptRespDTO>) invocationOnMock -> { | |
| 77 | + Long deptId = invocationOnMock.getArgument(0); | |
| 78 | + return randomPojo(DeptRespDTO.class, o -> o.setId(deptId).setParentId(deptId * 100).setLeaderUserId(deptId + 1)); | |
| 79 | + }); | |
| 80 | + } | |
| 81 | + | |
| 82 | +} | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/dept/BpmTaskCandidateStartUserSelectStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.dept; | |
| 2 | + | |
| 3 | +import cn.hutool.core.map.MapUtil; | |
| 4 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | |
| 5 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnVariableConstants; | |
| 6 | +import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; | |
| 7 | +import org.assertj.core.util.Sets; | |
| 8 | +import org.flowable.engine.delegate.DelegateExecution; | |
| 9 | +import org.flowable.engine.runtime.ProcessInstance; | |
| 10 | +import org.junit.jupiter.api.Test; | |
| 11 | +import org.mockito.InjectMocks; | |
| 12 | +import org.mockito.Mock; | |
| 13 | + | |
| 14 | +import java.util.HashMap; | |
| 15 | +import java.util.List; | |
| 16 | +import java.util.Map; | |
| 17 | +import java.util.Set; | |
| 18 | + | |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 20 | +import static org.mockito.ArgumentMatchers.eq; | |
| 21 | +import static org.mockito.Mockito.mock; | |
| 22 | +import static org.mockito.Mockito.when; | |
| 23 | + | |
| 24 | +public class BpmTaskCandidateStartUserSelectStrategyTest extends BaseMockitoUnitTest { | |
| 25 | + | |
| 26 | + @InjectMocks | |
| 27 | + private BpmTaskCandidateStartUserSelectStrategy strategy; | |
| 28 | + | |
| 29 | + @Mock | |
| 30 | + private BpmProcessInstanceService processInstanceService; | |
| 31 | + | |
| 32 | + @Test | |
| 33 | + public void testCalculateUsersByTask() { | |
| 34 | + // 准备参数 | |
| 35 | + String param = "2"; | |
| 36 | + // mock 方法(获得流程发起人) | |
| 37 | + ProcessInstance processInstance = mock(ProcessInstance.class); | |
| 38 | + DelegateExecution execution = mock(DelegateExecution.class); | |
| 39 | + when(processInstanceService.getProcessInstance(eq(execution.getProcessInstanceId()))).thenReturn(processInstance); | |
| 40 | + when(execution.getCurrentActivityId()).thenReturn("activity_001"); | |
| 41 | + // mock 方法(FlowableUtils) | |
| 42 | + Map<String, Object> processVariables = new HashMap<>(); | |
| 43 | + processVariables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, | |
| 44 | + MapUtil.of("activity_001", List.of(1L, 2L))); | |
| 45 | + when(processInstance.getProcessVariables()).thenReturn(processVariables); | |
| 46 | + | |
| 47 | + // 调用 | |
| 48 | + Set<Long> userIds = strategy.calculateUsersByTask(execution, param); | |
| 49 | + // 断言 | |
| 50 | + assertEquals(Sets.newLinkedHashSet(1L, 2L), userIds); | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Test | |
| 54 | + public void testCalculateUsersByActivity() { | |
| 55 | + // 准备参数 | |
| 56 | + String activityId = "activity_001"; | |
| 57 | + Map<String, Object> processVariables = new HashMap<>(); | |
| 58 | + processVariables.put(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES, | |
| 59 | + MapUtil.of("activity_001", List.of(1L, 2L))); | |
| 60 | + | |
| 61 | + // 调用 | |
| 62 | + Set<Long> userIds = strategy.calculateUsersByActivity(null, activityId, null, | |
| 63 | + null, null, processVariables); | |
| 64 | + // 断言 | |
| 65 | + assertEquals(Sets.newLinkedHashSet(1L, 2L), userIds); | |
| 66 | + } | |
| 67 | + | |
| 68 | +} | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/other/BpmTaskCandidateAssignEmptyStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.other; | |
| 2 | + | |
| 3 | +import cn.hutool.core.collection.ListUtil; | |
| 4 | +import cn.iocoder.yudao.framework.common.util.collection.SetUtils; | |
| 5 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | |
| 6 | +import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO; | |
| 7 | +import cn.iocoder.yudao.module.bpm.enums.definition.BpmUserTaskAssignEmptyHandlerTypeEnum; | |
| 8 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils; | |
| 9 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils; | |
| 10 | +import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; | |
| 11 | +import org.flowable.bpmn.model.BpmnModel; | |
| 12 | +import org.flowable.bpmn.model.FlowElement; | |
| 13 | +import org.flowable.engine.delegate.DelegateExecution; | |
| 14 | +import org.junit.jupiter.api.Test; | |
| 15 | +import org.mockito.InjectMocks; | |
| 16 | +import org.mockito.Mock; | |
| 17 | +import org.mockito.MockedStatic; | |
| 18 | + | |
| 19 | +import java.util.Set; | |
| 20 | + | |
| 21 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 22 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; | |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 24 | +import static org.mockito.Mockito.*; | |
| 25 | + | |
| 26 | +public class BpmTaskCandidateAssignEmptyStrategyTest extends BaseMockitoUnitTest { | |
| 27 | + | |
| 28 | + @InjectMocks | |
| 29 | + private BpmTaskCandidateAssignEmptyStrategy strategy; | |
| 30 | + | |
| 31 | + @Mock | |
| 32 | + private BpmProcessDefinitionService processDefinitionService; | |
| 33 | + | |
| 34 | + @Test | |
| 35 | + public void testCalculateUsersByTask() { | |
| 36 | + try (MockedStatic<FlowableUtils> flowableUtilMockedStatic = mockStatic(FlowableUtils.class); | |
| 37 | + MockedStatic<BpmnModelUtils> bpmnModelUtilsMockedStatic = mockStatic(BpmnModelUtils.class)) { | |
| 38 | + // 准备参数 | |
| 39 | + DelegateExecution execution = mock(DelegateExecution.class); | |
| 40 | + String param = randomString(); | |
| 41 | + // mock 方法(execution) | |
| 42 | + String processDefinitionId = randomString(); | |
| 43 | + when(execution.getProcessDefinitionId()).thenReturn(processDefinitionId); | |
| 44 | + FlowElement flowElement = mock(FlowElement.class); | |
| 45 | + when(execution.getCurrentFlowElement()).thenReturn(flowElement); | |
| 46 | + // mock 方法(parseAssignEmptyHandlerType) | |
| 47 | + bpmnModelUtilsMockedStatic.when(() -> BpmnModelUtils.parseAssignEmptyHandlerType(same(flowElement))) | |
| 48 | + .thenReturn(BpmUserTaskAssignEmptyHandlerTypeEnum.ASSIGN_USER.getType()); | |
| 49 | + bpmnModelUtilsMockedStatic.when(() -> BpmnModelUtils.parseAssignEmptyHandlerUserIds(same(flowElement))) | |
| 50 | + .thenReturn(ListUtil.of(1L, 2L)); | |
| 51 | + | |
| 52 | + // 调用 | |
| 53 | + Set<Long> userIds = strategy.calculateUsersByTask(execution, param); | |
| 54 | + // 断言 | |
| 55 | + assertEquals(SetUtils.asSet(1L, 2L), userIds); | |
| 56 | + } | |
| 57 | + | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Test | |
| 61 | + public void testCalculateUsersByActivity() { | |
| 62 | + try (MockedStatic<BpmnModelUtils> bpmnModelUtilsMockedStatic = mockStatic(BpmnModelUtils.class)) { | |
| 63 | + // 准备参数 | |
| 64 | + String processDefinitionId = randomString(); | |
| 65 | + String activityId = randomString(); | |
| 66 | + String param = randomString(); | |
| 67 | + // mock 方法(getFlowElementById) | |
| 68 | + FlowElement flowElement = mock(FlowElement.class); | |
| 69 | + BpmnModel bpmnModel = mock(BpmnModel.class); | |
| 70 | + bpmnModelUtilsMockedStatic.when(() -> BpmnModelUtils.getFlowElementById(same(bpmnModel), eq(activityId))) | |
| 71 | + .thenReturn(flowElement); | |
| 72 | + // mock 方法(parseAssignEmptyHandlerType) | |
| 73 | + bpmnModelUtilsMockedStatic.when(() -> BpmnModelUtils.parseAssignEmptyHandlerType(same(flowElement))) | |
| 74 | + .thenReturn(BpmUserTaskAssignEmptyHandlerTypeEnum.ASSIGN_ADMIN.getType()); | |
| 75 | + // mock 方法(getProcessDefinitionInfo) | |
| 76 | + BpmProcessDefinitionInfoDO processDefinition = randomPojo(BpmProcessDefinitionInfoDO.class, | |
| 77 | + o -> o.setManagerUserIds(ListUtil.of(1L, 2L))); | |
| 78 | + when(processDefinitionService.getProcessDefinitionInfo(eq(processDefinitionId))).thenReturn(processDefinition); | |
| 79 | + | |
| 80 | + // 调用 | |
| 81 | + Set<Long> userIds = strategy.calculateUsersByActivity(bpmnModel, activityId, param, | |
| 82 | + null, processDefinitionId, null); | |
| 83 | + // 断言 | |
| 84 | + assertEquals(SetUtils.asSet(1L, 2L), userIds); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 88 | +} | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateExpressionStrategyTest.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/other/BpmTaskCandidateExpressionStrategyTest.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.other; | |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils; | |
| 4 | 3 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 4 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils; | |
| 5 | 5 | import org.flowable.engine.delegate.DelegateExecution; |
| 6 | 6 | import org.junit.jupiter.api.Test; |
| 7 | 7 | import org.mockito.InjectMocks; |
| 8 | 8 | import org.mockito.MockedStatic; |
| 9 | 9 | |
| 10 | +import java.util.HashMap; | |
| 11 | +import java.util.Map; | |
| 10 | 12 | import java.util.Set; |
| 11 | 13 | |
| 12 | 14 | import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; |
| ... | ... | @@ -20,7 +22,7 @@ public class BpmTaskCandidateExpressionStrategyTest extends BaseMockitoUnitTest |
| 20 | 22 | private BpmTaskCandidateExpressionStrategy strategy; |
| 21 | 23 | |
| 22 | 24 | @Test |
| 23 | - public void testCalculateUsers() { | |
| 25 | + public void testCalculateUsersByTask() { | |
| 24 | 26 | try (MockedStatic<FlowableUtils> flowableUtilMockedStatic = mockStatic(FlowableUtils.class)) { |
| 25 | 27 | // 准备参数 |
| 26 | 28 | String param = "1,2"; |
| ... | ... | @@ -30,7 +32,25 @@ public class BpmTaskCandidateExpressionStrategyTest extends BaseMockitoUnitTest |
| 30 | 32 | .thenReturn(asSet(1L, 2L)); |
| 31 | 33 | |
| 32 | 34 | // 调用 |
| 33 | - Set<Long> results = strategy.calculateUsers(execution, param); | |
| 35 | + Set<Long> results = strategy.calculateUsersByTask(execution, param); | |
| 36 | + // 断言 | |
| 37 | + assertEquals(asSet(1L, 2L), results); | |
| 38 | + } | |
| 39 | + } | |
| 40 | + | |
| 41 | + @Test | |
| 42 | + public void testCalculateUsersByActivity() { | |
| 43 | + try (MockedStatic<FlowableUtils> flowableUtilMockedStatic = mockStatic(FlowableUtils.class)) { | |
| 44 | + // 准备参数 | |
| 45 | + String param = "1,2"; | |
| 46 | + Map<String, Object> processVariables = new HashMap<>(); | |
| 47 | + // mock 方法 | |
| 48 | + flowableUtilMockedStatic.when(() -> FlowableUtils.getExpressionValue(same(processVariables), eq(param))) | |
| 49 | + .thenReturn(asSet(1L, 2L)); | |
| 50 | + | |
| 51 | + // 调用 | |
| 52 | + Set<Long> results = strategy.calculateUsersByActivity(null, null, param, | |
| 53 | + null, null, processVariables); | |
| 34 | 54 | // 断言 |
| 35 | 55 | assertEquals(asSet(1L, 2L), results); |
| 36 | 56 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateGroupStrategyTest.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidateGroupStrategyTest.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 4 | 4 | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmUserGroupDO; |
| ... | ... | @@ -34,9 +34,9 @@ public class BpmTaskCandidateGroupStrategyTest extends BaseMockitoUnitTest { |
| 34 | 34 | when(userGroupService.getUserGroupList(eq(asSet(1L, 2L)))).thenReturn(Arrays.asList(userGroup1, userGroup2)); |
| 35 | 35 | |
| 36 | 36 | // 调用 |
| 37 | - Set<Long> results = strategy.calculateUsers(null, param); | |
| 37 | + Set<Long> userIds = strategy.calculateUsersByTask(null, param); | |
| 38 | 38 | // 断言 |
| 39 | - assertEquals(asSet(11L, 12L, 21L, 22L), results); | |
| 39 | + assertEquals(asSet(11L, 12L, 21L, 22L), userIds); | |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidatePostStrategyTest.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidatePostStrategyTest.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 4 | 4 | import cn.iocoder.yudao.module.system.api.dept.PostApi; |
| ... | ... | @@ -37,9 +37,9 @@ public class BpmTaskCandidatePostStrategyTest extends BaseMockitoUnitTest { |
| 37 | 37 | when(adminUserApi.getUserListByPostIds(eq(asSet(1L, 2L)))).thenReturn(users); |
| 38 | 38 | |
| 39 | 39 | // 调用 |
| 40 | - Set<Long> results = strategy.calculateUsers(null, param); | |
| 40 | + Set<Long> userIds = strategy.calculateUsersByTask(null, param); | |
| 41 | 41 | // 断言 |
| 42 | - assertEquals(asSet(11L, 22L), results); | |
| 42 | + assertEquals(asSet(11L, 22L), userIds); | |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateRoleStrategyTest.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidateRoleStrategyTest.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 4 | 4 | import cn.iocoder.yudao.module.system.api.permission.PermissionApi; |
| ... | ... | @@ -33,9 +33,9 @@ public class BpmTaskCandidateRoleStrategyTest extends BaseMockitoUnitTest { |
| 33 | 33 | .thenReturn(asSet(11L, 22L)); |
| 34 | 34 | |
| 35 | 35 | // 调用 |
| 36 | - Set<Long> results = strategy.calculateUsers(null, param); | |
| 36 | + Set<Long> userIds = strategy.calculateUsersByTask(null, param); | |
| 37 | 37 | // 断言 |
| 38 | - assertEquals(asSet(11L, 22L), results); | |
| 38 | + assertEquals(asSet(11L, 22L), userIds); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidateStartUserStrategyTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | |
| 4 | +import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; | |
| 5 | +import org.assertj.core.util.Sets; | |
| 6 | +import org.flowable.engine.delegate.DelegateExecution; | |
| 7 | +import org.flowable.engine.runtime.ProcessInstance; | |
| 8 | +import org.junit.jupiter.api.Test; | |
| 9 | +import org.mockito.InjectMocks; | |
| 10 | +import org.mockito.Mock; | |
| 11 | + | |
| 12 | +import java.util.Set; | |
| 13 | + | |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 15 | +import static org.mockito.ArgumentMatchers.eq; | |
| 16 | +import static org.mockito.Mockito.mock; | |
| 17 | +import static org.mockito.Mockito.when; | |
| 18 | + | |
| 19 | +public class BpmTaskCandidateStartUserStrategyTest extends BaseMockitoUnitTest { | |
| 20 | + | |
| 21 | + @InjectMocks | |
| 22 | + private BpmTaskCandidateStartUserStrategy strategy; | |
| 23 | + | |
| 24 | + @Mock | |
| 25 | + private BpmProcessInstanceService processInstanceService; | |
| 26 | + | |
| 27 | + @Test | |
| 28 | + public void testCalculateUsersByTask() { | |
| 29 | + // 准备参数 | |
| 30 | + String param = "2"; | |
| 31 | + // mock 方法(获得流程发起人) | |
| 32 | + Long startUserId = 1L; | |
| 33 | + ProcessInstance processInstance = mock(ProcessInstance.class); | |
| 34 | + DelegateExecution execution = mock(DelegateExecution.class); | |
| 35 | + when(processInstanceService.getProcessInstance(eq(execution.getProcessInstanceId()))).thenReturn(processInstance); | |
| 36 | + when(processInstance.getStartUserId()).thenReturn(startUserId.toString()); | |
| 37 | + | |
| 38 | + // 调用 | |
| 39 | + Set<Long> userIds = strategy.calculateUsersByTask(execution, param); | |
| 40 | + // 断言 | |
| 41 | + assertEquals(Sets.newLinkedHashSet(startUserId), userIds); | |
| 42 | + } | |
| 43 | + | |
| 44 | + @Test | |
| 45 | + public void testCalculateUsersByActivity() { | |
| 46 | + // 准备参数 | |
| 47 | + Long startUserId = 1L; | |
| 48 | + | |
| 49 | + // 调用 | |
| 50 | + Set<Long> userIds = strategy.calculateUsersByActivity(null, null, null, | |
| 51 | + startUserId, null, null); | |
| 52 | + // 断言 | |
| 53 | + assertEquals(Sets.newLinkedHashSet(startUserId), userIds); | |
| 54 | + } | |
| 55 | + | |
| 56 | +} | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/BpmTaskCandidateUserStrategyTest.java renamed to yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidateUserStrategyTest.java
| 1 | -package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy; | |
| 1 | +package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 4 | 4 | import org.junit.jupiter.api.Test; |
| ... | ... | @@ -15,14 +15,15 @@ public class BpmTaskCandidateUserStrategyTest extends BaseMockitoUnitTest { |
| 15 | 15 | private BpmTaskCandidateUserStrategy strategy; |
| 16 | 16 | |
| 17 | 17 | @Test |
| 18 | - public void testCalculateUsers() { | |
| 18 | + public void test() { | |
| 19 | 19 | // 准备参数 |
| 20 | 20 | String param = "1,2"; |
| 21 | 21 | |
| 22 | 22 | // 调用 |
| 23 | - Set<Long> results = strategy.calculateUsers(null, param); | |
| 23 | + Set<Long> userIds = strategy.calculateUsersByTask(null, param); | |
| 24 | 24 | // 断言 |
| 25 | - assertEquals(asSet(1L, 2L), results); | |
| 25 | + assertEquals(asSet(1L, 2L), userIds); | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | + | |
| 28 | 29 | } | ... | ... |