Browse Source

优化加签/减签

caixiaofeng 7 months ago
parent
commit
dc40694783

+ 38 - 0
flow-workflow/flow-workflow-biz/src/main/java/com/flow/cmd/GetLoopCharacteristics.java

@@ -0,0 +1,38 @@
+package com.flow.cmd;
+
+import com.flow.common.core.exception.BaseException;
+import lombok.AllArgsConstructor;
+import org.flowable.bpmn.model.BpmnModel;
+import org.flowable.bpmn.model.FlowElement;
+import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
+import org.flowable.bpmn.model.UserTask;
+import org.flowable.common.engine.impl.interceptor.Command;
+import org.flowable.common.engine.impl.interceptor.CommandContext;
+import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
+import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
+import org.flowable.engine.impl.util.CommandContextUtil;
+import org.flowable.engine.impl.util.ProcessDefinitionUtil;
+
+import java.util.Objects;
+
+@AllArgsConstructor
+public class GetLoopCharacteristics implements Command<MultiInstanceLoopCharacteristics> {
+    private String processInstanceId;
+    private String taskDefinitionKey;
+
+    @Override
+    public MultiInstanceLoopCharacteristics execute(CommandContext commandContext) {
+        ExecutionEntityManager executionManager = CommandContextUtil.getExecutionEntityManager(commandContext);
+        ExecutionEntity processInstance = executionManager.findById(processInstanceId);
+        if (Objects.isNull(processInstance)) {
+            throw new BaseException("流程不存在");
+        }
+        BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processInstance.getProcessDefinitionId());
+        FlowElement flowElement = bpmnModel.getFlowElement(taskDefinitionKey);
+        MultiInstanceLoopCharacteristics loopCharacteristics = ((UserTask) flowElement).getLoopCharacteristics();
+        if (Objects.isNull(loopCharacteristics)) {
+            throw new BaseException("该节点不是多实例任务");
+        }
+        return loopCharacteristics;
+    }
+}

+ 37 - 0
flow-workflow/flow-workflow-biz/src/main/java/com/flow/cmd/GetMultiInstanceRootExecution.java

@@ -0,0 +1,37 @@
+package com.flow.cmd;
+
+import lombok.AllArgsConstructor;
+import org.flowable.common.engine.impl.interceptor.Command;
+import org.flowable.common.engine.impl.interceptor.CommandContext;
+import org.flowable.engine.RuntimeService;
+import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
+import org.flowable.engine.impl.util.CommandContextUtil;
+import org.flowable.engine.runtime.Execution;
+
+import java.util.List;
+
+@AllArgsConstructor
+public class GetMultiInstanceRootExecution implements Command<Execution> {
+    private String processInstanceId;
+    private String taskDefinitionKey;
+
+    @Override
+    public Execution execute(CommandContext commandContext) {
+        RuntimeService runtimeService = CommandContextUtil.getProcessEngineConfiguration(commandContext).getRuntimeService();
+        List<Execution> executions = runtimeService.createExecutionQuery()
+                .processInstanceId(processInstanceId)
+                .activityId(taskDefinitionKey)
+                .list();
+        Execution multiInstanceRootExecution = null;
+        Execution currentExecution = executions.get(0);
+        while (currentExecution != null && multiInstanceRootExecution == null && currentExecution.getParentId() != null) {
+            if (((ExecutionEntity) currentExecution).isMultiInstanceRoot()) {
+                multiInstanceRootExecution = currentExecution;
+            } else {
+                currentExecution = runtimeService.createExecutionQuery()
+                        .executionId(currentExecution.getParentId()).singleResult();
+            }
+        }
+        return multiInstanceRootExecution;
+    }
+}

+ 6 - 37
flow-workflow/flow-workflow-biz/src/main/java/com/flow/service/impl/FlowTaskServiceImpl.java

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.flow.cmd.GetLoopCharacteristics;
+import com.flow.cmd.GetMultiInstanceRootExecution;
 import com.flow.cmd.JumpActivityCmd;
 import com.flow.common.core.exception.BaseException;
 import com.flow.common.core.model.PageResult;
@@ -32,10 +34,8 @@ import org.flowable.bpmn.model.BpmnModel;
 import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
 import org.flowable.bpmn.model.UserTask;
 import org.flowable.engine.*;
-import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
 import org.flowable.engine.runtime.ActivityInstance;
 import org.flowable.engine.runtime.Execution;
-import org.flowable.engine.runtime.ProcessInstance;
 import org.flowable.task.api.DelegationState;
 import org.flowable.task.api.Task;
 import org.flowable.task.api.TaskInfo;
@@ -298,10 +298,10 @@ public class FlowTaskServiceImpl extends BaseServiceImpl<FlowTaskDao, FlowTask>
             assigneeList.add(taskForm.getAssignee());
         }
         runtimeService.setVariable(taskInfo.getProcessInstanceId(), collectionName, assigneeList);
-        Execution miExecution = getMultiInstanceRootExecution(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey());
+        Execution miExecution = managementService.executeCommand(new GetMultiInstanceRootExecution(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey()));
         Integer nrOfInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfInstances", Integer.class);
         runtimeService.setVariableLocal(miExecution.getId(), "nrOfInstances", nrOfInstances + 1);
-        MultiInstanceLoopCharacteristics loopCharacteristics = getLoopCharacteristics(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey());
+        MultiInstanceLoopCharacteristics loopCharacteristics = managementService.executeCommand(new GetLoopCharacteristics(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey()));
         if (!loopCharacteristics.isSequential()) {
             Integer nrOfActiveInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfActiveInstances", Integer.class);
             runtimeService.setVariableLocal(miExecution.getId(), "nrOfActiveInstances", nrOfActiveInstances + 1);
@@ -318,10 +318,10 @@ public class FlowTaskServiceImpl extends BaseServiceImpl<FlowTaskDao, FlowTask>
     public void removeSign(AddSignTaskForm taskForm) {
         TaskInfo taskInfo = this.getTask(taskForm.getTaskId());
         String collectionName = String.format("%sCollection", taskInfo.getTaskDefinitionKey());
-        Execution miExecution = getMultiInstanceRootExecution(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey());
+        Execution miExecution = managementService.executeCommand(new GetMultiInstanceRootExecution(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey()));
         Integer nrOfInstances = runtimeService.getVariableLocal(miExecution.getId(), "nrOfInstances", Integer.class);
         runtimeService.setVariableLocal(miExecution.getId(), "nrOfInstances", nrOfInstances - 1);
-        MultiInstanceLoopCharacteristics loopCharacteristics = getLoopCharacteristics(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey());
+        MultiInstanceLoopCharacteristics loopCharacteristics = managementService.executeCommand(new GetLoopCharacteristics(taskInfo.getProcessInstanceId(), taskInfo.getTaskDefinitionKey()));
         List<String> assigneeList = runtimeService.getVariable(taskInfo.getProcessInstanceId(), collectionName, List.class);
         if (loopCharacteristics.isSequential()) {
             Integer loopCounter = runtimeService.getVariableLocal(taskInfo.getExecutionId(), "loopCounter", Integer.class);
@@ -357,37 +357,6 @@ public class FlowTaskServiceImpl extends BaseServiceImpl<FlowTaskDao, FlowTask>
                 .remove();
     }
 
-    private MultiInstanceLoopCharacteristics getLoopCharacteristics(String processInstanceId, String taskDefinitionKey) {
-        ProcessInstance procInst = runtimeService.createProcessInstanceQuery()
-                .processInstanceId(processInstanceId).singleResult();
-        if (Objects.isNull(procInst)) {
-            throw new BaseException("流程不存在");
-        }
-        BpmnModel bpmnModel = repositoryService.getBpmnModel(procInst.getProcessDefinitionId());
-        MultiInstanceLoopCharacteristics loopCharacteristics = ((UserTask) bpmnModel.getFlowElement(taskDefinitionKey)).getLoopCharacteristics();
-        if (Objects.isNull(loopCharacteristics)) {
-            throw new BaseException("该节点不是多实例任务");
-        }
-        return loopCharacteristics;
-    }
-
-    private Execution getMultiInstanceRootExecution(String procInstId, String taskDefinitionKey) {
-        List<Execution> executions = runtimeService.createExecutionQuery()
-                .processInstanceId(procInstId).activityId(taskDefinitionKey).list();
-        Execution multiInstanceRootExecution = null;
-        Execution currentExecution = executions.get(0);
-        while (currentExecution != null && multiInstanceRootExecution == null && currentExecution.getParentId() != null) {
-            if (((ExecutionEntity) currentExecution).isMultiInstanceRoot()) {
-                multiInstanceRootExecution = currentExecution;
-            } else {
-                currentExecution = runtimeService.createExecutionQuery()
-                        .executionId(currentExecution.getParentId()).singleResult();
-            }
-        }
-        return multiInstanceRootExecution;
-    }
-
-
     @Transactional(rollbackFor = Exception.class)
     @Override
     public void delegate(TransferTaskForm taskForm) {