Skip to content

Commit 62cc879

Browse files
committed
feat: 加签去重、减签单人校验与合并审批批量能力声明
- 加签拒绝已在当前节点待办队列中的人员(含加签自己) - 减签在当前节点仅剩一名待审批人时拒绝 - FlowRecord 新增 isPendingApproval,统一待审批记录口径 - detail 响应新增 currentNodeOperators(当前节点待审批人,排除自己) - IFlowAction 新增 batchable,减签/自定义动作为 false,并在 detail 动作中附加标记 - 补充加签/减签/detail 相关测试与接口文档
1 parent ec6343f commit 62cc879

12 files changed

Lines changed: 312 additions & 32 deletions

File tree

‎docs/Integration/rest-api.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,15 @@ view / code / viewTitle / nodeName / nodeId / nodeType
185185
title(节点标题) / adviceRequired / signRequired / adviceHidden
186186
form(FlowForm 元数据) / fieldPermissions(字段权限)
187187
todos(当前节点待办 Body 列表) / actions / actionList
188-
mergeable / createOperator / currentOperator / flowState / recordState
188+
mergeable / createOperator / currentOperator / currentNodeOperators / flowState / recordState
189189
histories(审批历史) / nodes(NodeOption 可操作节点) / revoke / urge
190190
resetSubProcess(子流程重置能力标识:当前待办位于开启重置能力、已汇聚完成的子流程的锁定合并节点上时为 true;更深层下游节点为 false)
191191
```
192192

193+
- `currentNodeOperators`:当前记录所属**节点实例**中仍待审批的人员(`id` / `name`,按人去重,排除当前操作人,含顺序审批尚未激活的隐藏记录)。减签等只能作用于当前节点其他待审批人的动作应以此字段作为候选来源,不要使用宿主的全局用户列表。
194+
- 该字段按单条流程记录计算;合并审批(`mergeable = true`)下多条流程实例的待审批人集合可能不同,不能跨流程合并候选人。
195+
- `actions` / `actionList` 中每个动作额外带有 `batchable` 标记:`true` 表示支持合并批量提交,`false` 表示只能单条记录提交(当前为 `REDUCE_AUDIT`、`CUSTOM`)。该标记只在响应中附加,不写入流程设计持久化数据;旧版本服务端无此字段时前端按 `true` 处理。
196+
193197
### `FlowRecordContent`(列表响应,字段摘要)
194198

195199
```

‎flow-engine-framework/src/main/java/com/codingapi/flow/action/IFlowAction.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ public interface IFlowAction extends IMapConvertor, ICopyAbility<IFlowAction> {
4949
*/
5050
boolean show(FlowSession flowSession);
5151

52+
/**
53+
* 是否支持合并批量提交。
54+
*
55+
* <p>合并审批时同一审批人会同时持有多条流程实例的待办,前端按勾选的记录逐条提交。
56+
* 目标按流程实例隔离的动作(如减签,候选人来自各流程自己的当前节点待办人)不能批量,
57+
* 否则会把 A 流程的目标错配到 B 流程。缺省视为支持。
58+
*
59+
* @return true 表示支持合并批量提交
60+
*/
61+
default boolean batchable() {
62+
return true;
63+
}
64+
5265

5366
/**
5467
* 执行动作

‎flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/AddAuditAction.java‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.Objects;
26+
import java.util.Set;
27+
import java.util.stream.Collectors;
2528

2629
/**
2730
* 加签
@@ -102,8 +105,9 @@ public void run(FlowSession flowSession) {
102105
}
103106
}
104107
}
108+
// 已经在本节点待办队列中的人员不允许再次加签,避免同一审批人在同一节点重复产生待办
109+
verifyNotInTodoQueue(flowSession, auditOperators);
105110

106-
// 要延续流程继续的order序号
107111
int maxNodeOrder = 0;
108112
for (FlowRecord record : currentRecords) {
109113
if (record.getNodeOrder() >= maxNodeOrder) {
@@ -136,6 +140,50 @@ public void run(FlowSession flowSession) {
136140
flowEvents.forEach(EventPusher::push);
137141
}
138142

143+
/**
144+
* 校验加签人员不能已经存在于当前节点的待办队列中。
145+
*
146+
* <p>待办队列指当前节点实例中尚未办理的审批记录:包括顺序审批里尚未激活的隐藏记录
147+
* (它们后续会按序号激活为待办),但不包括已办、已撤销以及已结束流程的记录。
148+
*
149+
* @param flowSession 当前会话
150+
* @param auditOperators 本次加签的人员
151+
*/
152+
private void verifyNotInTodoQueue(FlowSession flowSession, List<IFlowOperator> auditOperators) {
153+
if (auditOperators == null || auditOperators.isEmpty()) {
154+
return;
155+
}
156+
Set<Long> pendingOperatorIds = pendingOperatorIds(flowSession);
157+
if (pendingOperatorIds.isEmpty()) {
158+
return;
159+
}
160+
for (IFlowOperator auditOperator : auditOperators) {
161+
if (pendingOperatorIds.contains(auditOperator.getUserId())) {
162+
throw FlowExecutionException.addAuditOperatorAlreadyInTodo(auditOperator.getUserId());
163+
}
164+
}
165+
}
166+
167+
/**
168+
* 当前节点实例中仍有待办的人员id集合。
169+
*
170+
* <p>仓储按 {@code (fromId, nodeId)} 查询节点记录,开始节点({@code fromId = 0})等场景可能
171+
* 命中其他流程实例的记录,必须按 {@code processId} 限定,避免误判其他流程的待办人员。
172+
*/
173+
private Set<Long> pendingOperatorIds(FlowSession flowSession) {
174+
FlowRecord currentRecord = flowSession.getCurrentRecord();
175+
List<FlowRecord> currentNodeRecords = flowSession.getCurrentNodeRecords();
176+
if (currentRecord == null || currentNodeRecords == null) {
177+
return Set.of();
178+
}
179+
return currentNodeRecords.stream()
180+
.filter(Objects::nonNull)
181+
.filter(record -> currentRecord.getProcessId().equals(record.getProcessId()))
182+
.filter(FlowRecord::isPendingApproval)
183+
.map(FlowRecord::getCurrentOperatorId)
184+
.collect(Collectors.toSet());
185+
}
186+
139187
public static AddAuditAction fromMap(Map<String, Object> data) {
140188
AddAuditAction action = BaseAction.fromMap(data, AddAuditAction.class);
141189
String script = (String) data.get("script");

‎flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/CustomAction.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public class CustomAction extends BaseAction {
2626

2727
public static final String DEFAULT_TITLE = "自定义";
2828

29+
/**
30+
* 自定义动作的行为由脚本决定,可能依赖当前记录的表单/选人数据,
31+
* 无法保证多条流程合并提交语义正确,因此不支持批量。
32+
*/
33+
@Override
34+
public boolean batchable() {
35+
return false;
36+
}
37+
2938
/**
3039
* 后端处理脚本
3140
*/

‎flow-engine-framework/src/main/java/com/codingapi/flow/action/actions/ReduceAuditAction.java‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public List<IFlowOperator> operators(FlowSession flowSession) {
7171
return script.execute(flowSession);
7272
}
7373

74+
/**
75+
* 减签不支持合并批量提交:候选人与目标都是「本流程实例当前节点」的待审批人,
76+
* 合并组内各流程的待办人集合不同,批量会把目标错配到其他流程。
77+
*/
78+
@Override
79+
public boolean batchable() {
80+
return false;
81+
}
82+
7483
@Override
7584
public void copy(IFlowAction action) {
7685
super.copy(action);
@@ -120,6 +129,10 @@ public void run(FlowSession flowSession) {
120129
}
121130

122131
List<FlowRecord> currentNodeRecords = currentNodeRecords(flowSession);
132+
// 当前节点只剩一名待审批人时已无其他人可减,直接拒绝
133+
if (pendingOperatorCount(currentNodeRecords) <= 1) {
134+
throw FlowExecutionException.reduceAuditSingleApprover();
135+
}
123136
List<FlowRecord> targetRecords = resolveTargetRecords(targets, currentNodeRecords, currentRecord);
124137

125138
// 撤销后必须仍需要一次真实审批,否则节点条件即使满足也没有触发点,流程会永久卡死
@@ -161,7 +174,7 @@ private List<FlowRecord> resolveTargetRecords(List<IFlowOperator> targets,
161174
throw FlowExecutionException.reduceAuditOwnRecord();
162175
}
163176
List<FlowRecord> matchedRecords = currentNodeRecords.stream()
164-
.filter(this::isPendingApprovalRecord)
177+
.filter(FlowRecord::isPendingApproval)
165178
.filter(record -> record.getCurrentOperatorId() == targetOperatorId)
166179
.toList();
167180
if (matchedRecords.isEmpty()) {
@@ -186,7 +199,7 @@ private List<FlowRecord> pendingRecords(FlowSession flowSession) {
186199
return List.of();
187200
}
188201
return currentNodeRecords(flowSession).stream()
189-
.filter(this::isPendingApprovalRecord)
202+
.filter(FlowRecord::isPendingApproval)
190203
.filter(record -> record.getId() != currentRecord.getId())
191204
.toList();
192205
}
@@ -209,10 +222,18 @@ private List<FlowRecord> currentNodeRecords(FlowSession flowSession) {
209222
.toList();
210223
}
211224

212-
private boolean isPendingApprovalRecord(FlowRecord record) {
213-
return !record.isRevoked()
214-
&& record.getRecordState() == FlowRecord.SATE_RECORD_TODO
215-
&& record.getFlowState() == FlowRecord.SATE_FLOW_RUNNING;
225+
/**
226+
* 当前节点实例中仍有待办的人员数量(按人去重)。
227+
*
228+
* <p>同一审批人可能持有多条待审批记录(例如加签为其生成多条记录),减签按「人」生效,
229+
* 因此只有剩下一名待审批人时才视为「无人可减」。
230+
*/
231+
private long pendingOperatorCount(List<FlowRecord> currentNodeRecords) {
232+
return currentNodeRecords.stream()
233+
.filter(FlowRecord::isPendingApproval)
234+
.map(FlowRecord::getCurrentOperatorId)
235+
.distinct()
236+
.count();
216237
}
217238

218239
private boolean isSupportedMode(IFlowNode node) {

‎flow-engine-framework/src/main/java/com/codingapi/flow/exception/FlowExecutionException.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ public static FlowExecutionException operatorCountExceeded(String actionType, in
9797
String.format("Operator count exceeds the max count %d of the %s action", maxOperatorCount, actionType));
9898
}
9999

100+
/**
101+
* Add audit target is already in the todo list of the current node instance
102+
* <p>
103+
* Adding an approver who already holds a pending record would create duplicated
104+
* todos for the same operator on the same node, so it is rejected.
105+
*
106+
* @param operatorId target operator id
107+
* @return exception
108+
*/
109+
public static FlowExecutionException addAuditOperatorAlreadyInTodo(long operatorId) {
110+
return new FlowExecutionException("execution.addAudit.operatorAlreadyInTodo",
111+
String.format("Operator %d is already a pending approver of the current node", operatorId));
112+
}
113+
100114
/**
101115
* Custom action next not found
102116
*
@@ -232,4 +246,17 @@ public static FlowExecutionException reduceAuditNoRemainingApproval() {
232246
return new FlowExecutionException("execution.reduceAudit.noRemainingApproval",
233247
"Reduce audit rejected: the node still requires at least one pending approval to finish");
234248
}
249+
250+
/**
251+
* Reduce audit is not allowed when the current node has only one pending approver
252+
* <p>
253+
* A reduce audit always removes someone else from the node, so a single pending
254+
* approver means there is nobody left to remove.
255+
*
256+
* @return exception
257+
*/
258+
public static FlowExecutionException reduceAuditSingleApprover() {
259+
return new FlowExecutionException("execution.reduceAudit.singleApprover",
260+
"Reduce audit rejected: the current node has only one pending approver");
261+
}
235262
}

‎flow-engine-framework/src/main/java/com/codingapi/flow/node/BaseAuditNode.java‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public boolean requiresAuditToFinish(List<FlowRecord> records) {
181181
}
182182
MultiOperatorAuditStrategy.Type multiOperatorAuditStrategyType = this.strategyManager().getMultiOperatorAuditStrategyType();
183183
if (multiOperatorAuditStrategyType == MultiOperatorAuditStrategy.Type.SEQUENCE) {
184-
return records.stream().anyMatch(this::isPendingApprovalRecord);
184+
return records.stream().anyMatch(FlowRecord::isPendingApproval);
185185
}
186186
if (multiOperatorAuditStrategyType == MultiOperatorAuditStrategy.Type.MERGE) {
187187
long total = records.stream()
@@ -194,16 +194,6 @@ public boolean requiresAuditToFinish(List<FlowRecord> records) {
194194
return false;
195195
}
196196

197-
/**
198-
* 是否为待审批记录:含顺序审批中尚未激活的隐藏记录。
199-
*/
200-
private boolean isPendingApprovalRecord(FlowRecord record) {
201-
return !record.isRevoked()
202-
&& record.getRecordState() == FlowRecord.SATE_RECORD_TODO
203-
&& record.getFlowState() == FlowRecord.SATE_FLOW_RUNNING;
204-
}
205-
206-
207197
/**
208198
* 生成当前节点的记录
209199
*

‎flow-engine-framework/src/main/java/com/codingapi/flow/pojo/response/FlowContent.java‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import lombok.Data;
1414

1515
import java.util.ArrayList;
16+
import java.util.HashMap;
17+
import java.util.LinkedHashMap;
1618
import java.util.List;
1719
import java.util.Map;
1820

@@ -146,6 +148,14 @@ public class FlowContent {
146148
*/
147149
private FlowOperator currentOperator;
148150

151+
/**
152+
* 当前节点待审批人员(不含当前操作人)
153+
*
154+
* <p>减签等动作的目标必须来自当前节点实例,不能从宿主用户列表随意选取:合并审批
155+
* 场景下每个流程实例的待审批人集合可能不同,候选人按流程实例维度计算。
156+
*/
157+
private List<FlowOperator> currentNodeOperators;
158+
149159
/**
150160
* 流程状态 | 运行中、已完成、异常、删除
151161
*/
@@ -217,13 +227,14 @@ public void pushCurrentNode(FlowSession flowSession) {
217227
if (currentNode != null) {
218228
ActionManager actionManager = currentNode.actionManager();
219229
NodeStrategyManager strategyManager = currentNode.strategyManager();
220-
this.actions = actionManager.filterActions(flowSession).stream().map(IFlowAction::toMap).toList();
221-
this.actionList = actionManager.getActions().stream().map(IFlowAction::toMap).toList();
230+
this.actions = toActionMaps(actionManager.filterActions(flowSession));
231+
this.actionList = toActionMaps(actionManager.getActions());
222232
this.adviceRequired = strategyManager.isAdviceRequired();
223233
this.signRequired = strategyManager.isSignRequired();
224234
this.adviceHidden = strategyManager.isAdviceHidden();
225235
this.nodeId = currentNode.getId();
226236
this.nodeName = currentNode.getName();
237+
this.currentNodeOperators = loadCurrentNodeOperators(flowSession);
227238
this.nodeType = currentNode.getType();
228239
this.fieldPermissions = strategyManager.getFieldPermissions();
229240
Map<String, Object> nodeData = currentNode.toMap();
@@ -233,6 +244,50 @@ public void pushCurrentNode(FlowSession flowSession) {
233244
}
234245
}
235246

247+
/**
248+
* 输出动作配置,并附加运行期能力标记。
249+
*
250+
* <p>{@code batchable} 表示该动作是否支持合并批量提交,由 {@link IFlowAction#batchable()} 声明。
251+
* 该标记只在响应中附加,不写入动作的持久化配置({@link IFlowAction#toMap()})。
252+
*/
253+
private static List<Map<String, Object>> toActionMaps(List<IFlowAction> actions) {
254+
return actions.stream().map(action -> {
255+
Map<String, Object> data = new HashMap<>(action.toMap());
256+
data.put("batchable", action.batchable());
257+
return data;
258+
}).toList();
259+
}
260+
261+
/**
262+
* 计算当前节点实例中的待审批人员(按人去重,排除当前操作人)。
263+
*
264+
* <p>口径与减签目标解析一致:仅取仍待审批的记录(含顺序审批中尚未激活的隐藏记录),
265+
* 且只取同一流程实例的数据,避免仓储按 {@code (fromId, nodeId)} 查询时混入其他流程实例。
266+
*
267+
* <p>合并审批是多条流程实例的待办聚合展示,各自的待审批人集合可能不同,
268+
* 因此候选人员只能按单条流程记录计算,不能跨流程合并。
269+
*/
270+
private List<FlowOperator> loadCurrentNodeOperators(FlowSession flowSession) {
271+
FlowRecord currentRecord = flowSession.getCurrentRecord();
272+
List<FlowRecord> currentNodeRecords = flowSession.getCurrentNodeRecords();
273+
if (currentRecord == null || currentNodeRecords == null) {
274+
return List.of();
275+
}
276+
long currentOperatorId = currentRecord.getCurrentOperatorId();
277+
Map<Long, FlowOperator> operators = new LinkedHashMap<>();
278+
for (FlowRecord record : currentNodeRecords) {
279+
if (record == null || !currentRecord.getProcessId().equals(record.getProcessId())) {
280+
continue;
281+
}
282+
if (!record.isPendingApproval() || record.getCurrentOperatorId() == currentOperatorId) {
283+
continue;
284+
}
285+
operators.putIfAbsent(record.getCurrentOperatorId(),
286+
new FlowOperator(record.getCurrentOperatorId(), record.getCurrentOperatorName()));
287+
}
288+
return List.copyOf(operators.values());
289+
}
290+
236291
public void pushWorkflow(Workflow workflow) {
237292
this.form = workflow.getForm();
238293
this.workCode = workflow.getCode();

‎flow-engine-framework/src/main/java/com/codingapi/flow/record/FlowRecord.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,17 @@ public boolean isDone() {
508508
return recordState == SATE_RECORD_DONE && !hidden && !revoked;
509509
}
510510

511+
/**
512+
* 是否仍待审批。
513+
*
514+
* <p>与 {@link #isTodo()} 的区别是不排除隐藏记录:顺序审批中尚未激活后续审批人的记录
515+
* 虽然当前不可见,但仍属于该节点的待办队列,后续会按序号激活为待办。
516+
* 减签目标解析、加签重复校验、节点待审批人统计都以该口径为准。
517+
*/
518+
public boolean isPendingApproval() {
519+
return recordState == SATE_RECORD_TODO && flowState == SATE_FLOW_RUNNING && !revoked;
520+
}
521+
511522

512523
/**
513524
* 判断是否已完成

0 commit comments

Comments
 (0)