From 159e0c8a189e01b6aabc0084365a138e0649734f Mon Sep 17 00:00:00 2001 From: hubin Date: Sun, 1 Sep 2024 20:07:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=89=A7=E8=A1=8C=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E4=B8=AD=E5=8A=A8=E6=80=81=E6=8C=87=E5=AE=9A=E5=90=8E?= =?UTF-8?q?=E7=BB=AD=E8=8A=82=E7=82=B9=E5=A4=84=E7=90=86=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/aizuda/bpm/engine/RuntimeService.java | 9 ++++ .../com/aizuda/bpm/engine/core/Execution.java | 14 +++++++ .../bpm/engine/impl/RuntimeServiceImpl.java | 41 +++++++++---------- .../aizuda/bpm/engine/model/ModelHelper.java | 31 ++++++++++++++ .../java/test/mysql/TestCcToCondition.java | 23 +++++++++-- 5 files changed, 92 insertions(+), 26 deletions(-) diff --git a/flowlong-core/src/main/java/com/aizuda/bpm/engine/RuntimeService.java b/flowlong-core/src/main/java/com/aizuda/bpm/engine/RuntimeService.java index 2976f12c..2b8fe39d 100644 --- a/flowlong-core/src/main/java/com/aizuda/bpm/engine/RuntimeService.java +++ b/flowlong-core/src/main/java/com/aizuda/bpm/engine/RuntimeService.java @@ -120,6 +120,15 @@ default void timeout(Long instanceId) { */ void updateInstance(FlwInstance flwInstance); + /** + * 根据 流程实例ID 更新流程实例模型内容 + * + * @param id 流程实例ID + * @param processModel 流程模型 + * @return true 成功 false 失败 + */ + boolean updateInstanceModelById(Long id, ProcessModel processModel); + /** * 级联删除指定流程实例的所有数据 * diff --git a/flowlong-core/src/main/java/com/aizuda/bpm/engine/core/Execution.java b/flowlong-core/src/main/java/com/aizuda/bpm/engine/core/Execution.java index bae55a8e..bd1f8e86 100644 --- a/flowlong-core/src/main/java/com/aizuda/bpm/engine/core/Execution.java +++ b/flowlong-core/src/main/java/com/aizuda/bpm/engine/core/Execution.java @@ -10,6 +10,7 @@ import com.aizuda.bpm.engine.entity.FlwInstance; import com.aizuda.bpm.engine.entity.FlwTask; import com.aizuda.bpm.engine.entity.FlwTaskActor; +import com.aizuda.bpm.engine.model.ModelHelper; import com.aizuda.bpm.engine.model.NodeModel; import com.aizuda.bpm.engine.model.ProcessModel; import lombok.Getter; @@ -143,6 +144,19 @@ public Execution createSubExecution(Execution execution, ProcessModel processMod public boolean executeNodeModel(FlowLongContext flowLongContext, String nodeKey) { ProcessModel processModel = this.getProcessModel(); Assert.isNull(processModel, "Process model content cannot be empty"); + + // 重新加载流程模型内容 + ModelHelper.reloadProcessModel(processModel, t -> { + + // 更新流程模型 + boolean ok = flowLongContext.getRuntimeService().updateInstanceModelById(flwInstance.getId(), t); + Assert.isFalse(ok, "Failed to update process model content"); + + // 重新构建父节点 + t.buildParentNode(processModel.getNodeConfig()); + }); + + // 获取节点模型 NodeModel nodeModel = processModel.getNode(nodeKey); Assert.isNull(nodeModel, "Not found in the process model, process nodeKey=" + nodeKey); diff --git a/flowlong-core/src/main/java/com/aizuda/bpm/engine/impl/RuntimeServiceImpl.java b/flowlong-core/src/main/java/com/aizuda/bpm/engine/impl/RuntimeServiceImpl.java index f74f3bbb..6d7842d6 100644 --- a/flowlong-core/src/main/java/com/aizuda/bpm/engine/impl/RuntimeServiceImpl.java +++ b/flowlong-core/src/main/java/com/aizuda/bpm/engine/impl/RuntimeServiceImpl.java @@ -3,7 +3,10 @@ */ package com.aizuda.bpm.engine.impl; -import com.aizuda.bpm.engine.*; +import com.aizuda.bpm.engine.FlowConstants; +import com.aizuda.bpm.engine.QueryService; +import com.aizuda.bpm.engine.RuntimeService; +import com.aizuda.bpm.engine.TaskService; import com.aizuda.bpm.engine.assist.Assert; import com.aizuda.bpm.engine.assist.DateUtils; import com.aizuda.bpm.engine.assist.ObjectUtils; @@ -18,7 +21,7 @@ import com.aizuda.bpm.engine.entity.*; import com.aizuda.bpm.engine.listener.InstanceListener; import com.aizuda.bpm.engine.model.ConditionNode; -import com.aizuda.bpm.engine.model.DynamicAssignee; +import com.aizuda.bpm.engine.model.ModelHelper; import com.aizuda.bpm.engine.model.NodeModel; import com.aizuda.bpm.engine.model.ProcessModel; @@ -77,26 +80,8 @@ public FlwInstance createInstance(FlwProcess flwProcess, FlowCreator flowCreator flwInstance.setProcessId(flwProcess.getId()); flwInstance.setMapVariable(args); - /* - * 处理追加模型逻辑 - */ - Map modelData = FlowDataTransfer.get(FlowConstants.processDynamicAssignee); - if (ObjectUtils.isNotEmpty(modelData)) { - ProcessModel processModel = flwProcess.model(); - modelData.forEach((key, value) -> { - if (value instanceof DynamicAssignee) { - NodeModel thisNodeModel = processModel.getNode(key); - if (null != thisNodeModel) { - DynamicAssignee dynamicAssignee = (DynamicAssignee) value; - thisNodeModel.setNodeAssigneeList(dynamicAssignee.getAssigneeList()); - } - } - }); - // 清理父节点 - processModel.cleanParentNode(processModel.getNodeConfig()); - // 更新模型 - flwProcess.setModelContent2Json(processModel); - } + // 重新加载流程模型 + ModelHelper.reloadProcessModel(flwProcess.model(), flwProcess::setModelContent2Json); // 保存实例 this.saveInstance(flwInstance, flwProcess, flowCreator); @@ -291,6 +276,18 @@ public void updateInstance(FlwInstance flwInstance) { instanceDao.updateById(flwInstance); } + @Override + public boolean updateInstanceModelById(Long id, ProcessModel processModel) { + // 使缓存失效 + FlowLongContext.invalidateProcessModel(FlowConstants.processInstanceCacheKey + id); + + // 更新流程实例模型 + FlwExtInstance extInstance = new FlwExtInstance(); + extInstance.setId(id); + extInstance.setModelContent(FlowLongContext.toJson(processModel)); + return extInstanceDao.updateById(extInstance); + } + /** * 级联删除指定流程实例的所有数据 *

diff --git a/flowlong-core/src/main/java/com/aizuda/bpm/engine/model/ModelHelper.java b/flowlong-core/src/main/java/com/aizuda/bpm/engine/model/ModelHelper.java index c26fd5fb..04f49e42 100644 --- a/flowlong-core/src/main/java/com/aizuda/bpm/engine/model/ModelHelper.java +++ b/flowlong-core/src/main/java/com/aizuda/bpm/engine/model/ModelHelper.java @@ -3,11 +3,14 @@ */ package com.aizuda.bpm.engine.model; +import com.aizuda.bpm.engine.FlowConstants; +import com.aizuda.bpm.engine.FlowDataTransfer; import com.aizuda.bpm.engine.assist.ObjectUtils; import com.aizuda.bpm.engine.core.enums.NodeSetType; import com.aizuda.bpm.engine.core.enums.TaskType; import java.util.*; +import java.util.function.Consumer; import java.util.stream.Collectors; /** @@ -335,4 +338,32 @@ public static NodeModel getNodeModel(String nodeKey, NodeModel rootNodeModel) { .orElse(null); } + /** + * 重新加载流程模型 + * + * @param processModel 流程模型 + * @param consumer 流程模型消费 + */ + public static void reloadProcessModel(ProcessModel processModel, Consumer consumer) { + Map modelData = FlowDataTransfer.get(FlowConstants.processDynamicAssignee); + if (ObjectUtils.isNotEmpty(modelData)) { + + // 追加动态分配处理人员 + modelData.forEach((key, value) -> { + if (value instanceof DynamicAssignee) { + NodeModel thisNodeModel = processModel.getNode(key); + if (null != thisNodeModel) { + DynamicAssignee dynamicAssignee = (DynamicAssignee) value; + thisNodeModel.setNodeAssigneeList(dynamicAssignee.getAssigneeList()); + } + } + }); + + // 清理父节点 + processModel.cleanParentNode(processModel.getNodeConfig()); + + // 更新模型 + consumer.accept(processModel); + } + } } diff --git a/flowlong-spring-boot-starter/src/test/java/test/mysql/TestCcToCondition.java b/flowlong-spring-boot-starter/src/test/java/test/mysql/TestCcToCondition.java index d9ebe79d..0b918863 100644 --- a/flowlong-spring-boot-starter/src/test/java/test/mysql/TestCcToCondition.java +++ b/flowlong-spring-boot-starter/src/test/java/test/mysql/TestCcToCondition.java @@ -10,10 +10,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * 测试抄送节点跟条件分支 @@ -53,4 +50,22 @@ public void test() { }); } + + @Test + public void test2() { + // 执行认领审批分支 + Map args = new HashMap<>(); + args.put("day", 8); + flowLongEngine.startInstanceById(processId, testCreator, args).ifPresent(instance -> { + + // 认领审批并动态执行下一个审批节点处理人员为 test003 + FlowDataTransfer.dynamicAssignee(new HashMap() {{ + put("k008", DynamicAssignee.assigneeUserList(Collections.singletonList(NodeAssignee.ofFlowCreator(test3Creator)))); + }}); + executeActiveTasks(instance.getId(), test3Creator); + + // 动态处理人员 test003 办理流程结束 + executeActiveTasks(instance.getId(), test3Creator); + }); + } }