ソースを参照

流程重发起cmd

caixiaofeng 5 ヶ月 前
コミット
87f61e7e3f

+ 53 - 0
flow-common/flow-common-flowable-starter/src/main/java/com/flow/flowable/cmd/ReCreateProcessInstanceCmd.java

@@ -0,0 +1,53 @@
+package com.flow.flowable.cmd;
+
+import org.flowable.common.engine.api.FlowableException;
+import org.flowable.common.engine.impl.identity.Authentication;
+import org.flowable.common.engine.impl.interceptor.Command;
+import org.flowable.common.engine.impl.interceptor.CommandContext;
+import org.flowable.engine.HistoryService;
+import org.flowable.engine.RuntimeService;
+import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
+import org.flowable.engine.impl.util.CommandContextUtil;
+import org.flowable.engine.runtime.ProcessInstance;
+
+import java.util.Map;
+import java.util.Objects;
+
+public class ReCreateProcessInstanceCmd implements Command<ProcessInstance> {
+    protected String processInstanceId;
+
+    public ReCreateProcessInstanceCmd(String processInstanceId) {
+        this.processInstanceId = processInstanceId;
+    }
+
+    @Override
+    public ProcessInstance execute(CommandContext commandContext) {
+        ProcessEngineConfigurationImpl processEngineConf = CommandContextUtil.getProcessEngineConfiguration(commandContext);
+        RuntimeService runtimeService = processEngineConf.getRuntimeService();
+        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(this.processInstanceId).singleResult();
+        if (Objects.isNull(processInstance)) {
+            throw new FlowableException("流程实例不存在或已结束");
+        }
+        // 不是流程发起人不能撤回流程
+        String authenticatedUserId = Authentication.getAuthenticatedUserId();
+        if (!processInstance.getStartUserId().equals(authenticatedUserId)) {
+            throw new FlowableException("非流程发起人不能撤销流程");
+        }
+        // 查询流程变量
+        Map<String, Object> variables = runtimeService.getVariables(this.processInstanceId);
+        // 删除流程实例
+        runtimeService.deleteProcessInstance(this.processInstanceId, "流程撤销删除流程");
+        // 删除历史流程实例
+        HistoryService historyService = processEngineConf.getHistoryService();
+        historyService.deleteHistoricProcessInstance(this.processInstanceId);
+        // 重新发起
+        return runtimeService.createProcessInstanceBuilder()
+                .processDefinitionId(processInstance.getProcessDefinitionId())
+                .processDefinitionKey(processInstance.getProcessDefinitionKey())
+                .predefineProcessInstanceId(this.processInstanceId)
+                .businessKey(processInstance.getBusinessKey())
+                .variables(variables)
+                .tenantId(processInstance.getTenantId())
+                .start();
+    }
+}