|
|
@@ -391,7 +391,7 @@ public class FlowEngineServiceImpl implements FlowEngineService {
|
|
|
this.updateTaskStatus(currentTask, TaskStatus.RETURNED.getCode(), comment);
|
|
|
this.approvalTaskHelper.cancelPendingTasks(instance.getId(), LocalDateTime.now());
|
|
|
List<FlowNode> returnTargets = Collections.singletonList(targetNode);
|
|
|
- this.createTasksForNodes(instance, returnTargets, model);
|
|
|
+ this.createTasksForNodes(instance, returnTargets, model, new HashSet<>(), 0, true);
|
|
|
this.updateInstanceNode(instance, returnTargets);
|
|
|
}
|
|
|
|
|
|
@@ -463,14 +463,34 @@ public class FlowEngineServiceImpl implements FlowEngineService {
|
|
|
}
|
|
|
|
|
|
private FlowNode findPreviousNode(FlowModel model, String currentNodeId) {
|
|
|
- List<String> sourceIds = safeEdges(model).stream()
|
|
|
- .filter(e -> Objects.equals(e.getTargetNodeId(), currentNodeId))
|
|
|
- .map(FlowEdge::getSourceNodeId)
|
|
|
- .collect(Collectors.toList());
|
|
|
- return safeNodes(model).stream()
|
|
|
- .filter(n -> sourceIds.contains(n.getId()) && !NodeType.START.getCode().equals(n.getType()))
|
|
|
- .findFirst()
|
|
|
- .orElse(null);
|
|
|
+ // 按直接入边找上一个审批节点;若上游为条件/CC 节点,继续向上追溯
|
|
|
+ Set<String> visited = new HashSet<>();
|
|
|
+ Deque<String> stack = new ArrayDeque<>();
|
|
|
+ stack.push(currentNodeId);
|
|
|
+ while (!stack.isEmpty()) {
|
|
|
+ String nodeId = stack.pop();
|
|
|
+ if (!visited.add(nodeId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ List<String> sourceIds = safeEdges(model).stream()
|
|
|
+ .filter(e -> Objects.equals(e.getTargetNodeId(), nodeId))
|
|
|
+ .map(FlowEdge::getSourceNodeId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ for (String sourceId : sourceIds) {
|
|
|
+ FlowNode sourceNode = safeNodes(model).stream()
|
|
|
+ .filter(n -> n.getId().equals(sourceId))
|
|
|
+ .findFirst()
|
|
|
+ .orElse(null);
|
|
|
+ if (sourceNode == null || NodeType.START.getCode().equals(sourceNode.getType())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (NodeType.APPROVAL.getCode().equals(sourceNode.getType())) {
|
|
|
+ return sourceNode;
|
|
|
+ }
|
|
|
+ stack.push(sourceId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
private boolean isEndNode(List<FlowNode> nodes) {
|
|
|
@@ -486,11 +506,11 @@ public class FlowEngineServiceImpl implements FlowEngineService {
|
|
|
}
|
|
|
|
|
|
private void createTasksForNodes(ProcessInstance instance, List<FlowNode> nodes, FlowModel model) {
|
|
|
- this.createTasksForNodes(instance, nodes, model, new HashSet<>(), 0);
|
|
|
+ this.createTasksForNodes(instance, nodes, model, new HashSet<>(), 0, false);
|
|
|
}
|
|
|
|
|
|
private void createTasksForNodes(ProcessInstance instance, List<FlowNode> nodes, FlowModel model,
|
|
|
- Set<String> visited, int depth) {
|
|
|
+ Set<String> visited, int depth, boolean allowRecreate) {
|
|
|
if (depth > MAX_FLOW_DEPTH) {
|
|
|
throw new BusinessException("流程模型存在循环或层级过深,已超过最大处理深度");
|
|
|
}
|
|
|
@@ -500,8 +520,8 @@ public class FlowEngineServiceImpl implements FlowEngineService {
|
|
|
if (NodeType.START.getCode().equals(node.getType()) || NodeType.END.getCode().equals(node.getType())) {
|
|
|
continue;
|
|
|
}
|
|
|
- // 幂等:已存在处理/跳过/待处理任务则不再重复创建
|
|
|
- if (this.hasExistingTaskForNode(instance.getId(), node.getId())) {
|
|
|
+ // 幂等:已存在处理/跳过/待处理任务则不再重复创建(回退场景允许重建)
|
|
|
+ if (!allowRecreate && this.hasExistingTaskForNode(instance.getId(), node.getId())) {
|
|
|
continue;
|
|
|
}
|
|
|
if (!visited.add(node.getId())) {
|
|
|
@@ -551,7 +571,7 @@ public class FlowEngineServiceImpl implements FlowEngineService {
|
|
|
// 自动推进到CC节点的下游
|
|
|
List<FlowNode> ccNextNodes = this.getNextNodes(model, node.getId(), instance);
|
|
|
if (!ccNextNodes.isEmpty()) {
|
|
|
- this.createTasksForNodes(instance, ccNextNodes, model, visited, depth + 1);
|
|
|
+ this.createTasksForNodes(instance, ccNextNodes, model, visited, depth + 1, allowRecreate);
|
|
|
}
|
|
|
continue;
|
|
|
}
|