Skip to content

Commit 3d94667

Browse files
committed
fix #123
1 parent 94f7d2d commit 3d94667

File tree

14 files changed

+757
-41
lines changed

14 files changed

+757
-41
lines changed

springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/em/FlowType.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public enum FlowType {
2424
/**
2525
* 等待执行
2626
*/
27-
WAITING;
27+
WAITING,
28+
/**
29+
* 删除
30+
*/
31+
DELETE;
2832

2933

3034
public static FlowType parser(String type){

springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/event/FlowApprovalEvent.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public class FlowApprovalEvent implements ISyncEvent {
3838
public static final int STATE_SAVE = 10;
3939
// 删除
4040
public static final int STATE_DELETE = 11;
41+
// 退回
42+
public static final int STATE_BACK = 12;
43+
// 作废
44+
public static final int STATE_VOIDED = 13;
4145

4246

4347
private final int state;
@@ -110,4 +114,16 @@ public boolean isRecall() {
110114
public boolean isFinish() {
111115
return state == STATE_FINISH;
112116
}
117+
118+
public boolean isDelete() {
119+
return state == STATE_DELETE;
120+
}
121+
122+
public boolean isVoided() {
123+
return state == STATE_VOIDED;
124+
}
125+
126+
public boolean isBack() {
127+
return state == STATE_BACK;
128+
}
113129
}

springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/record/FlowProcess.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ public class FlowProcess {
3333
private long createOperatorId;
3434

3535

36+
/**
37+
* 是否作废
38+
*/
39+
private boolean voided;
40+
41+
/**
42+
* 作废流程
43+
*/
44+
public void voided(){
45+
this.voided = true;
46+
}
47+
48+
3649
public FlowProcess(long backupId, IFlowOperator createOperator) {
3750
this.processId = RandomGenerator.generateUUID();
3851
this.createTime = System.currentTimeMillis();
3952
this.backupId = backupId;
4053
this.createOperatorId = createOperator.getUserId();
54+
this.voided = false;
4155
}
4256
}

springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/record/FlowRecord.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,13 @@ public void recall() {
395395
this.updateTime = System.currentTimeMillis();
396396
}
397397

398+
/**
399+
* 删除流程
400+
*/
401+
public void delete() {
402+
this.flowType = FlowType.DELETE;
403+
this.updateTime = System.currentTimeMillis();
404+
}
398405

399406
/**
400407
* 复制流程记录
@@ -430,6 +437,10 @@ public FlowRecord copy() {
430437
return record;
431438
}
432439

440+
public boolean isDelete() {
441+
return this.flowType == FlowType.DELETE;
442+
}
443+
433444

434445
/**
435446
* 是否超时

springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/repository/FlowProcessRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public interface FlowProcessRepository {
1010

1111
void save(FlowProcess flowProcess);
1212

13-
1413
FlowWork getFlowWorkByProcessId(String processId);
1514

15+
FlowProcess getFlowProcessByProcessId(String processId);
1616

1717
void deleteByProcessId(String processId);
1818

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class FlowService {
2828
private final FlowTransferService flowTransferService;
2929
private final FlowPostponedService flowPostponedService;
3030
private final FlowUrgeService flowUrgeService;
31+
private final FlowVoidedService flowVoidedService;
32+
private final FlowBackService flowBackService;
3133

3234
private final FlowServiceRepositoryHolder flowServiceRepositoryHolder;
3335

@@ -47,6 +49,8 @@ public FlowService(FlowWorkRepository flowWorkRepository,
4749
this.flowTransferService = new FlowTransferService(flowWorkRepository, flowRecordRepository, flowBindDataRepository, flowProcessRepository);
4850
this.flowPostponedService = new FlowPostponedService(flowWorkRepository, flowRecordRepository, flowProcessRepository);
4951
this.flowUrgeService = new FlowUrgeService(flowWorkRepository, flowRecordRepository, flowProcessRepository);
52+
this.flowVoidedService = new FlowVoidedService(flowWorkRepository, flowRecordRepository, flowProcessRepository, flowBindDataRepository);
53+
this.flowBackService = new FlowBackService(flowWorkRepository, flowRecordRepository, flowProcessRepository, flowBindDataRepository);
5054
}
5155

5256
/**
@@ -287,4 +291,27 @@ public void recall(long recordId, IFlowOperator currentOperator) {
287291
public void remove(long recordId, IFlowOperator currentOperator) {
288292
flowRemoveService.remove(recordId, currentOperator);
289293
}
294+
295+
296+
/**
297+
* 作废流程
298+
*
299+
* @param processId 流程processId
300+
* @param currentOperator 当前操作者
301+
*/
302+
public void voided(String processId, IFlowOperator currentOperator) {
303+
flowVoidedService.voided(processId, currentOperator);
304+
}
305+
306+
307+
/**
308+
* 退回流程
309+
*
310+
* @param processId 流程processId
311+
* @param backNodeCode 退回节点编码
312+
* @param currentOperator 当前操作者
313+
*/
314+
public void back(String processId, String backNodeCode, IFlowOperator currentOperator) {
315+
flowBackService.back(processId, backNodeCode, currentOperator);
316+
}
290317
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.codingapi.springboot.flow.service.impl;
2+
3+
import com.codingapi.springboot.flow.bind.IBindData;
4+
import com.codingapi.springboot.flow.domain.FlowWork;
5+
import com.codingapi.springboot.flow.event.FlowApprovalEvent;
6+
import com.codingapi.springboot.flow.record.FlowProcess;
7+
import com.codingapi.springboot.flow.record.FlowRecord;
8+
import com.codingapi.springboot.flow.repository.FlowBindDataRepository;
9+
import com.codingapi.springboot.flow.repository.FlowProcessRepository;
10+
import com.codingapi.springboot.flow.repository.FlowRecordRepository;
11+
import com.codingapi.springboot.flow.repository.FlowWorkRepository;
12+
import com.codingapi.springboot.flow.user.IFlowOperator;
13+
import com.codingapi.springboot.framework.event.EventPusher;
14+
import lombok.AllArgsConstructor;
15+
import org.springframework.transaction.annotation.Transactional;
16+
17+
import java.util.Comparator;
18+
import java.util.List;
19+
import java.util.stream.Collectors;
20+
21+
/**
22+
* 流程退回服务(流程管理员操作)
23+
*/
24+
@Transactional
25+
@AllArgsConstructor
26+
public class FlowBackService {
27+
28+
private final FlowWorkRepository flowWorkRepository;
29+
private final FlowRecordRepository flowRecordRepository;
30+
private final FlowProcessRepository flowProcessRepository;
31+
private final FlowBindDataRepository flowBindDataRepository;
32+
33+
34+
/**
35+
* 退回流程
36+
*
37+
* @param processId 流程processId
38+
* @param backNodeCode 退回节点编码
39+
* @param currentOperator 当前操作者
40+
*/
41+
public void back(String processId, String backNodeCode, IFlowOperator currentOperator) {
42+
if (!currentOperator.isFlowManager()) {
43+
throw new IllegalArgumentException("current operator not flow manager");
44+
}
45+
46+
FlowProcess flowProcess = flowProcessRepository.getFlowProcessByProcessId(processId);
47+
if (flowProcess.isVoided()) {
48+
throw new IllegalArgumentException("flow process already voided");
49+
}
50+
51+
List<FlowRecord> historyRecords = flowRecordRepository
52+
.findFlowRecordByProcessId(processId)
53+
.stream()
54+
.sorted(Comparator.comparingLong(FlowRecord::getId))
55+
.collect(Collectors.toList());
56+
57+
for (FlowRecord flowRecord : historyRecords) {
58+
if (flowRecord.isFinish()) {
59+
throw new IllegalArgumentException("flow record already finish");
60+
}
61+
}
62+
63+
if (historyRecords.isEmpty()) {
64+
throw new IllegalArgumentException("flow record not found");
65+
}
66+
67+
List<String> historyNodeCodes = historyRecords.stream()
68+
.map(FlowRecord::getNodeCode)
69+
.distinct()
70+
.collect(Collectors.toList());
71+
72+
if (!historyNodeCodes.contains(backNodeCode)) {
73+
throw new IllegalArgumentException("flow node code not found");
74+
}
75+
76+
FlowRecord beginRecord = historyRecords.get(0);
77+
for (FlowRecord flowRecord : historyRecords) {
78+
if(flowRecord.getNodeCode().equals(backNodeCode)){
79+
beginRecord = flowRecord;
80+
}
81+
}
82+
83+
for (FlowRecord flowRecord : historyRecords) {
84+
if(flowRecord.getId()> beginRecord.getId() ){
85+
flowRecord.delete();
86+
}else {
87+
if(flowRecord.getNodeCode().equals(beginRecord.getNodeCode())){
88+
flowRecord.recall();
89+
}
90+
}
91+
}
92+
93+
flowRecordRepository.save(historyRecords);
94+
95+
FlowWork flowWork = flowWorkRepository.getFlowWorkByCode(beginRecord.getWorkCode());
96+
IBindData bindData = flowBindDataRepository.getBindDataSnapshotById(beginRecord.getSnapshotId()).toBindData();
97+
98+
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_BACK, beginRecord, currentOperator, flowWork, bindData), true);
99+
100+
}
101+
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import lombok.AllArgsConstructor;
1616
import org.springframework.transaction.annotation.Transactional;
1717

18-
import java.util.Collections;
18+
import java.util.ArrayList;
1919
import java.util.List;
2020

2121
@Transactional
@@ -53,14 +53,17 @@ public void recall(long recordId, IFlowOperator currentOperator) {
5353
List<FlowRecord> childrenRecords = flowRecordRepository.findFlowRecordByPreId(recordId);
5454

5555
BindDataSnapshot bindDataSnapshot = flowBindDataRepository.getBindDataSnapshotById(flowRecord.getSnapshotId());
56+
5657
// 下一流程均为办理且未读
5758

5859
// 如果是在开始节点撤销,则直接删除
5960
if (flowRecord.isStartRecord() && flowRecord.isTodo()) {
6061
if (!childrenRecords.isEmpty()) {
6162
throw new IllegalArgumentException("flow record not recall");
6263
}
63-
flowRecordRepository.delete(Collections.singletonList(flowRecord));
64+
List<FlowRecord> flowRecords = new ArrayList<>();
65+
flowRecords.add(flowRecord);
66+
flowRecordRepository.delete(flowRecords);
6467
} else {
6568
// 如果是在中间节点撤销,则需要判断是否所有的子流程都是未读状态
6669
if (childrenRecords.isEmpty()) {
@@ -74,9 +77,11 @@ public void recall(long recordId, IFlowOperator currentOperator) {
7477
flowRecord.recall();
7578
flowRecordRepository.update(flowRecord);
7679

77-
flowRecordRepository.delete(childrenRecords);
80+
for(FlowRecord childrenRecord : childrenRecords) {
81+
childrenRecord.delete();
82+
}
83+
flowRecordRepository.save(childrenRecords);
7884
}
79-
8085
IBindData bindData = bindDataSnapshot.toBindData();
8186

8287
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_RECALL, flowRecord, currentOperator, flowWork, bindData), true);

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.codingapi.springboot.flow.domain.FlowNode;
66
import com.codingapi.springboot.flow.domain.FlowWork;
77
import com.codingapi.springboot.flow.event.FlowApprovalEvent;
8+
import com.codingapi.springboot.flow.record.FlowProcess;
89
import com.codingapi.springboot.flow.record.FlowRecord;
910
import com.codingapi.springboot.flow.repository.FlowBindDataRepository;
1011
import com.codingapi.springboot.flow.repository.FlowProcessRepository;
@@ -16,7 +17,6 @@
1617
import lombok.AllArgsConstructor;
1718
import org.springframework.transaction.annotation.Transactional;
1819

19-
import java.util.Collections;
2020
import java.util.List;
2121

2222
@Transactional
@@ -54,12 +54,18 @@ public void remove(long recordId, IFlowOperator currentOperator) {
5454
if(!flowNode.isStartNode()){
5555
throw new IllegalArgumentException("flow record not remove");
5656
}
57-
5857
BindDataSnapshot bindDataSnapshot = flowBindDataRepository.getBindDataSnapshotById(flowRecord.getSnapshotId());
5958
IBindData bindData = bindDataSnapshot.toBindData();
6059

61-
flowProcessRepository.deleteByProcessId(flowRecord.getProcessId());
62-
flowRecordRepository.deleteByProcessId(flowRecord.getProcessId());
60+
FlowProcess flowProcess = flowProcessRepository.getFlowProcessByProcessId(flowRecord.getProcessId());
61+
flowProcess.voided();
62+
flowProcessRepository.save(flowProcess);
63+
64+
List<FlowRecord> historyRecords = flowRecordRepository.findFlowRecordByProcessId(flowRecord.getProcessId());
65+
for (FlowRecord record : historyRecords) {
66+
record.delete();
67+
}
68+
flowRecordRepository.save(historyRecords);
6369

6470
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_DELETE, flowRecord, currentOperator, flowWork, bindData), true);
6571
}

0 commit comments

Comments
 (0)