package com.iailab.module.bpm.framework.flowable.core.behavior; import cn.hutool.core.collection.CollUtil; import com.iailab.framework.common.util.collection.SetUtils; import com.iailab.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateInvoker; import com.iailab.module.bpm.framework.flowable.core.util.FlowableUtils; import lombok.Setter; import org.flowable.bpmn.model.Activity; import org.flowable.engine.delegate.DelegateExecution; import org.flowable.engine.impl.bpmn.behavior.AbstractBpmnActivityBehavior; import org.flowable.engine.impl.bpmn.behavior.SequentialMultiInstanceBehavior; import java.util.Set; /** * 自定义的ã€ä¸²è¡Œã€‘çš„ã€å¤šä¸ªã€‘æµç¨‹ä»»åŠ¡çš„ assignee è´Ÿè´£äººçš„åˆ†é… * * 本质上,实现和 {@link BpmParallelMultiInstanceBehavior} ä¸€æ ·ï¼Œåªæ˜¯ç»§æ‰¿çš„ç±»ä¸ä¸€æ · * * @author iailab */ @Setter public class BpmSequentialMultiInstanceBehavior extends SequentialMultiInstanceBehavior { private BpmTaskCandidateInvoker taskCandidateInvoker; public BpmSequentialMultiInstanceBehavior(Activity activity, AbstractBpmnActivityBehavior innerActivityBehavior) { super(activity, innerActivityBehavior); } /** * 逻辑和 {@link BpmParallelMultiInstanceBehavior#resolveNrOfInstances(DelegateExecution)} 类似 * * 差异的点:是在ã€ç¬¬äºŒæ¥ã€‘的时候,需è¦è¿”回 LinkedHashSet 集åˆï¼å› ä¸ºå®ƒéœ€è¦æœ‰åºï¼ */ @Override protected int resolveNrOfInstances(DelegateExecution execution) { // 第一æ¥ï¼Œè®¾ç½® collectionVariable å’Œ CollectionVariable // 从 execution.getVariable() è¯»å–æ‰€æœ‰ä»»åС处ç†äººçš„ key super.collectionExpression = null; // collectionExpression å’Œ collectionVariable 是互斥的 super.collectionVariable = FlowableUtils.formatExecutionCollectionVariable(execution.getCurrentActivityId()); // 从 execution.getVariable() 读å–当剿‰€æœ‰ä»»åС处ç†çš„人的 key super.collectionElementVariable = FlowableUtils.formatExecutionCollectionElementVariable(execution.getCurrentActivityId()); // 第二æ¥ï¼ŒèŽ·å–任务的所有处ç†äºº // ä¸ä½¿ç”¨ execution.getVariable åŽŸå› ï¼šç›®å‰ä¾æ¬¡å®¡æ‰¹ä»»åŠ¡å›žé€€åŽ collectionVariable å˜é‡æ²¡æœ‰æ¸…ç†ï¼Œ å¦‚æžœé‡æ–°è¿›å…¥è¯¥ä»»åŠ¡ä¸ä¼šé‡æ–°åˆ†é…审批人 @SuppressWarnings("unchecked") Set<Long> assigneeUserIds = (Set<Long>) execution.getVariableLocal(super.collectionVariable, Set.class); if (assigneeUserIds == null) { assigneeUserIds = taskCandidateInvoker.calculateUsersByTask(execution); if (CollUtil.isEmpty(assigneeUserIds)) { // 特殊:如果没有处ç†äººçš„æƒ…况下,至少有一个 null ç©ºå…ƒç´ ï¼Œé¿å…è‡ªåŠ¨é€šè¿‡ï¼ // è¿™æ ·ï¼Œä¿è¯åœ¨ BpmUserTaskActivityBehavior 至少创建出一个 Task 任务 // 用途:1)审批人为空时;2)审批类型为自动通过ã€è‡ªåŠ¨æ‹’ç»æ—¶ assigneeUserIds = SetUtils.asSet((Long) null); } execution.setVariableLocal(super.collectionVariable, assigneeUserIds); } return assigneeUserIds.size(); } }