Commit 192bbf6e929bf916e6d3a8058c8b318206541475

Authored by YunaiV
1 parent 2653b614

fix:【bpm 工作流】BpmSequentialMultiInstanceBehavior 兼容 CallActivity、SubProcess 场景

yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/behavior/BpmParallelMultiInstanceBehavior.java
... ... @@ -7,44 +7,35 @@ import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCand
7 7 import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils;
8 8 import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils;
9 9 import lombok.Setter;
10   -import org.flowable.bpmn.model.Activity;
11   -import org.flowable.bpmn.model.CallActivity;
12   -import org.flowable.bpmn.model.FlowElement;
13   -import org.flowable.bpmn.model.UserTask;
  10 +import org.flowable.bpmn.model.*;
14 11 import org.flowable.engine.delegate.DelegateExecution;
15 12 import org.flowable.engine.impl.bpmn.behavior.AbstractBpmnActivityBehavior;
16   -import org.flowable.engine.impl.bpmn.behavior.ParallelMultiInstanceBehavior;
  13 +import org.flowable.engine.impl.bpmn.behavior.SequentialMultiInstanceBehavior;
  14 +import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
17 15  
18 16 import java.util.List;
19 17 import java.util.Set;
20 18  
21 19 /**
22   - * 自定义的【并行】的【多个】流程任务的 assignee 负责人的分配
23   - * 第一步,基于分配规则,计算出分配任务的【多个】候选人们。
24   - * 第二步,将【多个】任务候选人们,设置到 DelegateExecution 的 collectionVariable 变量中,以便 BpmUserTaskActivityBehavior 使用它
  20 + * 自定义的【串行】的【多个】流程任务的 assignee 负责人的分配
25 21 *
26   - * @author kemengkai
27   - * @since 2022-04-21 16:57
  22 + * 本质上,实现和 {@link BpmParallelMultiInstanceBehavior} 一样,只是继承的类不一样
  23 + *
  24 + * @author 芋道源码
28 25 */
29 26 @Setter
30   -public class BpmParallelMultiInstanceBehavior extends ParallelMultiInstanceBehavior {
  27 +public class BpmSequentialMultiInstanceBehavior extends SequentialMultiInstanceBehavior {
31 28  
32 29 private BpmTaskCandidateInvoker taskCandidateInvoker;
33 30  
34   - public BpmParallelMultiInstanceBehavior(Activity activity,
35   - AbstractBpmnActivityBehavior innerActivityBehavior) {
  31 + public BpmSequentialMultiInstanceBehavior(Activity activity, AbstractBpmnActivityBehavior innerActivityBehavior) {
36 32 super(activity, innerActivityBehavior);
37 33 }
38 34  
39 35 /**
40   - * 重写该方法,主要实现两个功能:
41   - * 1. 忽略原有的 collectionVariable、collectionElementVariable 表达式,而是采用自己定义的
42   - * 2. 获得任务的处理人,并设置到 collectionVariable 中,用于 BpmUserTaskActivityBehavior 从中可以获取任务的处理人
43   - *
44   - * 注意,多个任务实例,每个任务实例对应一个处理人,所以返回的数量就是任务处理人的数量
  36 + * 逻辑和 {@link BpmParallelMultiInstanceBehavior#resolveNrOfInstances(DelegateExecution)} 类似
45 37 *
46   - * @param execution 执行任务
47   - * @return 数量
  38 + * 差异的点:是在【第二步】的时候,需要返回 LinkedHashSet 集合!因为它需要有序!
48 39 */
49 40 @Override
50 41 protected int resolveNrOfInstances(DelegateExecution execution) {
... ... @@ -58,8 +49,9 @@ public class BpmParallelMultiInstanceBehavior extends ParallelMultiInstanceBehav
58 49 super.collectionElementVariable = FlowableUtils.formatExecutionCollectionElementVariable(execution.getCurrentActivityId());
59 50  
60 51 // 第二步,获取任务的所有处理人
  52 + // 不使用 execution.getVariable 原因:目前依次审批任务回退后 collectionVariable 变量没有清理, 如果重新进入该任务不会重新分配审批人
61 53 @SuppressWarnings("unchecked")
62   - Set<Long> assigneeUserIds = (Set<Long>) execution.getVariable(super.collectionVariable, Set.class);
  54 + Set<Long> assigneeUserIds = (Set<Long>) execution.getVariableLocal(super.collectionVariable, Set.class);
63 55 if (assigneeUserIds == null) {
64 56 assigneeUserIds = taskCandidateInvoker.calculateUsersByTask(execution);
65 57 if (CollUtil.isEmpty(assigneeUserIds)) {
... ... @@ -88,4 +80,19 @@ public class BpmParallelMultiInstanceBehavior extends ParallelMultiInstanceBehav
88 80 return super.resolveNrOfInstances(execution);
89 81 }
90 82  
  83 + @Override
  84 + protected void executeOriginalBehavior(DelegateExecution execution, ExecutionEntity multiInstanceRootExecution, int loopCounter) {
  85 + // 参见 https://t.zsxq.com/53Meo 情况
  86 + if (execution.getCurrentFlowElement() instanceof CallActivity
  87 + || execution.getCurrentFlowElement() instanceof SubProcess) {
  88 + super.executeOriginalBehavior(execution, multiInstanceRootExecution, loopCounter);
  89 + return;
  90 + }
  91 + // 参见 https://gitee.com/zhijiantianya/yudao-cloud/issues/IC239F
  92 + super.collectionExpression = null;
  93 + super.collectionVariable = FlowableUtils.formatExecutionCollectionVariable(execution.getCurrentActivityId());
  94 + super.collectionElementVariable = FlowableUtils.formatExecutionCollectionElementVariable(execution.getCurrentActivityId());
  95 + super.executeOriginalBehavior(execution, multiInstanceRootExecution, loopCounter);
  96 + }
  97 +
91 98 }
... ...