Skip to content

Commit 7b69640

Browse files
committed
fix bug
1 parent f1b01ca commit 7b69640

File tree

4 files changed

+172
-3
lines changed

4 files changed

+172
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.codingapi.springboot.flow.service;
2+
3+
class FlowNextNodeService {
4+
5+
6+
}

springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/service/FlowNodeService.java

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import com.codingapi.springboot.flow.domain.FlowWork;
77
import com.codingapi.springboot.flow.domain.Opinion;
88
import com.codingapi.springboot.flow.em.FlowSourceDirection;
9+
import com.codingapi.springboot.flow.event.FlowApprovalEvent;
910
import com.codingapi.springboot.flow.record.FlowRecord;
1011
import com.codingapi.springboot.flow.repository.FlowBindDataRepository;
1112
import com.codingapi.springboot.flow.repository.FlowOperatorRepository;
1213
import com.codingapi.springboot.flow.user.IFlowOperator;
14+
import com.codingapi.springboot.framework.event.EventPusher;
1315
import lombok.Getter;
1416

1517
import java.util.List;
@@ -22,6 +24,7 @@ class FlowNodeService {
2224

2325
private final FlowRecordService2 flowRecordService2;
2426
private final String processId;
27+
private final long preId;
2528

2629
private final FlowRecord flowRecord;
2730
private final FlowWork flowWork;
@@ -60,7 +63,7 @@ public FlowNodeService(FlowOperatorRepository flowOperatorRepository,
6063
this.flowNode = flowRecordService2.getFlowNode();
6164

6265
this.processId = flowRecord.getProcessId();
63-
66+
this.preId = flowRecord.getId();
6467
}
6568

6669

@@ -196,18 +199,27 @@ public void updateFlowRecord() {
196199
}
197200

198201

202+
/**
203+
* 加载子节点的审批记录
204+
* 即加载后续节点的审批记录
205+
*/
199206
public void loadChildrenRecords() {
200207
childrenRecords = flowRecordService2.flowRecordRepository.findFlowRecordByPreId(flowRecord.getId());
201208
}
202209

203210

211+
/**
212+
* 校验是否后续没有审批记录
213+
*/
204214
public void verifyChildrenRecordsIsEmpty() {
205215
if (!childrenRecords.isEmpty()) {
206216
throw new IllegalArgumentException("flow node is done");
207217
}
208218
}
209219

210-
220+
/**
221+
* 校验流程的审批方向
222+
*/
211223
public void verifyFlowSourceDirection() {
212224
if (flowSourceDirection == null) {
213225
throw new IllegalArgumentException("flow source direction is null");
@@ -216,4 +228,95 @@ public void verifyFlowSourceDirection() {
216228
throw new IllegalArgumentException("flow node is start node");
217229
}
218230
}
231+
232+
233+
public void createNextRecord(){
234+
235+
IFlowOperator createOperator = flowOperatorRepository.getFlowOperatorById(flowRecord.getCreateOperatorId());
236+
if(flowSourceDirection == FlowSourceDirection.PASS){
237+
FlowRecordService flowRecordService = new FlowRecordService(flowOperatorRepository, processId, createOperator, currentOperator, snapshot, opinion, flowWork, flowSourceDirection, historyRecords);
238+
FlowNode nextNode = flowRecordService.matcherPassNextNode(flowNode);
239+
if (nextNode == null) {
240+
throw new IllegalArgumentException("next node not found");
241+
}
242+
List<FlowRecord> records = flowRecordService.createRecord(preId, nextNode);
243+
flowRecordService2.flowRecordRepository.save(records);
244+
245+
for (FlowRecord record : records) {
246+
IFlowOperator pushOperator = flowOperatorRepository.getFlowOperatorById(record.getCurrentOperatorId());
247+
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, record, pushOperator,flowWork));
248+
}
249+
}
250+
251+
252+
if(flowSourceDirection == FlowSourceDirection.REJECT){
253+
254+
// 设置了退回流程
255+
if(flowWork.hasBackRelation()){
256+
257+
FlowRecordService flowRecordService = new FlowRecordService(flowOperatorRepository, processId, createOperator, currentOperator, snapshot, opinion, flowWork, flowSourceDirection, historyRecords);
258+
FlowNode nextNode = flowRecordService.matcherBackNextNode(flowNode);
259+
if (nextNode == null) {
260+
throw new IllegalArgumentException("next node not found");
261+
}
262+
263+
IFlowOperator flowOperator = currentOperator;
264+
if (nextNode.isAnyOperatorMatcher()) {
265+
// 如果是任意人员操作时则需要指定为当时审批人员为当前审批人员
266+
FlowRecord preFlowRecord = flowRecordService2.flowRecordRepository.getFlowRecordById(flowRecord.getPreId());
267+
while (preFlowRecord.isTransfer() || !preFlowRecord.getNodeCode().equals(nextNode.getCode())) {
268+
preFlowRecord = flowRecordService2.flowRecordRepository.getFlowRecordById(preFlowRecord.getPreId());
269+
}
270+
flowOperator = flowOperatorRepository.getFlowOperatorById(preFlowRecord.getCurrentOperatorId());
271+
}
272+
flowRecordService.changeCurrentOperator(flowOperator);
273+
List<FlowRecord> records = flowRecordService.createRecord(preId, nextNode);
274+
flowRecordService2.flowRecordRepository.save(records);
275+
276+
for (FlowRecord record : records) {
277+
IFlowOperator pushOperator = flowOperatorRepository.getFlowOperatorById(record.getCurrentOperatorId());
278+
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, record, pushOperator,flowWork));
279+
}
280+
}else{
281+
282+
IFlowOperator flowOperator;
283+
// 拒绝时,默认返回上一个节点
284+
FlowRecord preRecord = flowRecordService2.flowRecordRepository.getFlowRecordById(flowRecord.getPreId());
285+
// 去除所有的转办的记录
286+
while (preRecord.isTransfer()) {
287+
// 继续寻找上一个节点
288+
preRecord = flowRecordService2.flowRecordRepository.getFlowRecordById(preRecord.getPreId());
289+
}
290+
291+
// 获取上一个节点的审批者,继续将审批者设置为当前审批者
292+
flowOperator = flowOperatorRepository.getFlowOperatorById(preRecord.getCurrentOperatorId());
293+
294+
FlowRecordService flowRecordService = new FlowRecordService(flowOperatorRepository, processId, createOperator, flowOperator, snapshot, opinion, flowWork, flowSourceDirection, historyRecords);
295+
FlowNode nextNode = flowWork.getNodeByCode(preRecord.getNodeCode());
296+
if (nextNode == null) {
297+
throw new IllegalArgumentException("next node not found");
298+
}
299+
List<FlowRecord> records = flowRecordService.createRecord(preId, nextNode);
300+
301+
flowRecordService2.flowRecordRepository.save(records);
302+
303+
for (FlowRecord record : records) {
304+
IFlowOperator pushOperator = flowOperatorRepository.getFlowOperatorById(record.getCurrentOperatorId());
305+
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, record, pushOperator,flowWork));
306+
}
307+
308+
}
309+
310+
}
311+
312+
313+
int eventState = flowSourceDirection== FlowSourceDirection.PASS ? FlowApprovalEvent.STATE_PASS : FlowApprovalEvent.STATE_REJECT;
314+
EventPusher.push(new FlowApprovalEvent(eventState, flowRecord, currentOperator,flowWork));
315+
316+
317+
318+
}
319+
320+
321+
219322
}

springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/service/FlowRecordService.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,63 @@ public List<FlowRecord> createRecord(long preId, FlowNode currentNode) {
9393
}
9494
}
9595

96+
/**
97+
* 获取下一个节点
98+
*
99+
* @param currentNode 当前节点
100+
* @return 下一个节点
101+
*/
102+
public FlowNode matcherBackNextNode(FlowNode currentNode) {
103+
List<FlowRelation> relations = flowWork.getRelations().stream()
104+
.filter(relation -> relation.sourceMatcher(currentNode.getCode()) && relation.isBack())
105+
.sorted((o1, o2) -> (o2.getOrder() - o1.getOrder()))
106+
.toList();
107+
if (relations.isEmpty()) {
108+
throw new IllegalArgumentException("relation not found");
109+
}
110+
FlowContent flowContent = new FlowContent(flowWork, currentNode, createOperator, currentOperator, snapshot.toBindData(), opinion, historyRecords);
111+
List<FlowNode> flowNodes = new ArrayList<>();
112+
for (FlowRelation flowRelation : relations) {
113+
FlowNode node = flowRelation.trigger(flowContent);
114+
if (node != null) {
115+
flowNodes.add(node);
116+
}
117+
}
118+
if (flowNodes.isEmpty()) {
119+
throw new IllegalArgumentException("next node not found");
120+
}
121+
return flowNodes.get(0);
122+
}
123+
124+
125+
/**
126+
* 获取下一个节点
127+
*
128+
* @param currentNode 当前节点
129+
* @return 下一个节点
130+
*/
131+
public FlowNode matcherPassNextNode(FlowNode currentNode) {
132+
List<FlowRelation> relations = flowWork.getRelations().stream()
133+
.filter(relation -> relation.sourceMatcher(currentNode.getCode()) && !relation.isBack())
134+
.sorted((o1, o2) -> (o2.getOrder() - o1.getOrder()))
135+
.toList();
136+
if (relations.isEmpty()) {
137+
throw new IllegalArgumentException("relation not found");
138+
}
139+
FlowContent flowContent = new FlowContent(flowWork, currentNode, createOperator, currentOperator, snapshot.toBindData(), opinion, historyRecords);
140+
List<FlowNode> flowNodes = new ArrayList<>();
141+
for (FlowRelation flowRelation : relations) {
142+
FlowNode node = flowRelation.trigger(flowContent);
143+
if (node != null) {
144+
flowNodes.add(node);
145+
}
146+
}
147+
if (flowNodes.isEmpty()) {
148+
throw new IllegalArgumentException("next node not found");
149+
}
150+
return flowNodes.get(0);
151+
}
152+
96153

97154
/**
98155
* 获取下一个节点

springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/service/FlowService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,10 @@ public void submitFlow(long recordId, IFlowOperator currentOperator, IBindData b
375375
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_FINISH, flowRecord, currentOperator,flowWork));
376376
return;
377377
}
378-
this.createNextRecord(flowWork,flowSourceDirection,flowNode,processId,createOperator,currentOperator,snapshot,opinion,flowRecord,historyRecords);
378+
379+
flowNodeService.createNextRecord();
380+
381+
// this.createNextRecord(flowWork,flowSourceDirection,flowNode,processId,createOperator,currentOperator,snapshot,opinion,flowRecord,historyRecords);
379382

380383
}
381384

0 commit comments

Comments
 (0)