Skip to content

Commit 48f0fd3

Browse files
committed
fix start
1 parent 63ce4f3 commit 48f0fd3

File tree

5 files changed

+106
-49
lines changed

5 files changed

+106
-49
lines changed

example/example-infra-flow/src/main/java/com/codingapi/example/convert/FlowWorkConvertor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static FlowWorkEntity convert(FlowWork flowWork) {
1919
entity.setCreateTime(flowWork.getCreateTime());
2020
entity.setUpdateTime(flowWork.getUpdateTime());
2121
entity.setEnable(flowWork.isEnable());
22+
entity.setSkipIfSameApprover(flowWork.isSkipIfSameApprover());
2223
entity.setPostponedMax(flowWork.getPostponedMax());
2324
entity.setSchema(flowWork.getSchema());
2425
return entity;

example/example-infra-flow/src/main/java/com/codingapi/example/entity/FlowWorkEntity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public class FlowWorkEntity {
2828

2929
private Boolean enable;
3030

31+
/**
32+
* 是否跳过相同审批人,默认为false
33+
*/
34+
private Boolean skipIfSameApprover;
35+
36+
3137
private Integer postponedMax;
3238

3339
@Lob

example/example-infra-flow/src/main/java/com/codingapi/example/repository/FlowWorkRepositoryImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public FlowWork getFlowWorkById(long id) {
4949
entity.getCreateTime(),
5050
entity.getUpdateTime(),
5151
entity.getEnable(),
52+
entity.getSkipIfSameApprover(),
5253
entity.getPostponedMax(),
5354
flowNodes,
5455
flowRelations,
@@ -81,6 +82,7 @@ public FlowWork getFlowWorkByCode(String code) {
8182
entity.getCreateTime(),
8283
entity.getUpdateTime(),
8384
entity.getEnable(),
85+
entity.getSkipIfSameApprover(),
8486
entity.getPostponedMax(),
8587
flowNodes,
8688
flowRelations,

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
public class FlowService {
2121

2222
private final FlowDetailService flowDetailService;
23-
private final FlowStartService flowStartService;
2423
private final FlowCustomEventService flowCustomEventService;
2524
private final FlowRecallService flowRecallService;
2625
private final FlowTrySubmitService flowTrySubmitService;
@@ -40,7 +39,6 @@ public FlowService(FlowWorkRepository flowWorkRepository,
4039
FlowBackupRepository flowBackupRepository) {
4140
this.flowServiceRepositoryHolder = new FlowServiceRepositoryHolder(flowWorkRepository, flowRecordRepository, flowBindDataRepository, flowOperatorRepository, flowProcessRepository, flowBackupRepository);
4241
this.flowDetailService = new FlowDetailService(flowWorkRepository, flowRecordRepository, flowBindDataRepository, flowOperatorRepository, flowProcessRepository);
43-
this.flowStartService = new FlowStartService(flowWorkRepository, flowRecordRepository, flowBindDataRepository, flowOperatorRepository, flowProcessRepository, flowBackupRepository);
4442
this.flowCustomEventService = new FlowCustomEventService(flowRecordRepository, flowProcessRepository);
4543
this.flowRecallService = new FlowRecallService(flowRecordRepository, flowProcessRepository);
4644
this.flowTrySubmitService = new FlowTrySubmitService(flowRecordRepository, flowBindDataRepository, flowOperatorRepository, flowProcessRepository, flowWorkRepository, flowBackupRepository);
@@ -171,7 +169,8 @@ public void save(long recordId, IFlowOperator currentOperator, IBindData bindDat
171169
* @param advice 审批意见
172170
*/
173171
public FlowResult startFlow(String workCode, IFlowOperator operator, IBindData bindData, String advice) {
174-
return flowStartService.startFlow(workCode, operator, bindData, advice);
172+
FlowStartService flowStartService = new FlowStartService(workCode, operator, bindData, advice, flowServiceRepositoryHolder);
173+
return flowStartService.startFlow();
175174
}
176175

177176

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

Lines changed: 95 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,94 @@
1111
import com.codingapi.springboot.flow.record.FlowBackup;
1212
import com.codingapi.springboot.flow.record.FlowProcess;
1313
import com.codingapi.springboot.flow.record.FlowRecord;
14-
import com.codingapi.springboot.flow.repository.*;
14+
import com.codingapi.springboot.flow.repository.FlowBackupRepository;
15+
import com.codingapi.springboot.flow.repository.FlowOperatorRepository;
16+
import com.codingapi.springboot.flow.repository.FlowRecordRepository;
17+
import com.codingapi.springboot.flow.repository.FlowWorkRepository;
1518
import com.codingapi.springboot.flow.service.FlowNodeService;
19+
import com.codingapi.springboot.flow.service.FlowServiceRepositoryHolder;
1620
import com.codingapi.springboot.flow.user.IFlowOperator;
1721
import com.codingapi.springboot.framework.event.EventPusher;
18-
import lombok.AllArgsConstructor;
1922
import org.springframework.transaction.annotation.Transactional;
2023

2124
import java.util.ArrayList;
2225
import java.util.List;
2326

2427
@Transactional
25-
@AllArgsConstructor
2628
public class FlowStartService {
2729

28-
private final FlowWorkRepository flowWorkRepository;
29-
private final FlowRecordRepository flowRecordRepository;
30-
private final FlowBindDataRepository flowBindDataRepository;
31-
private final FlowOperatorRepository flowOperatorRepository;
32-
private final FlowProcessRepository flowProcessRepository;
33-
private final FlowBackupRepository flowBackupRepository;
34-
30+
private final String workCode;
31+
private final IFlowOperator operator;
32+
private final IBindData bindData;
33+
private final Opinion opinion;
34+
private final FlowServiceRepositoryHolder flowServiceRepositoryHolder;
35+
36+
37+
private FlowWork flowWork;
38+
private FlowNode flowNode;
39+
private FlowBackup flowBackup;
40+
private FlowProcess flowProcess;
41+
private BindDataSnapshot snapshot;
42+
private FlowNodeService flowNodeService;
43+
44+
public FlowStartService(String workCode, IFlowOperator operator, IBindData bindData, String advice, FlowServiceRepositoryHolder flowServiceRepositoryHolder) {
45+
this.workCode = workCode;
46+
this.operator = operator;
47+
this.bindData = bindData;
48+
this.opinion = Opinion.pass(advice);
49+
this.flowServiceRepositoryHolder = flowServiceRepositoryHolder;
50+
}
3551

36-
/**
37-
* 发起流程 (不自动提交到下一节点)
38-
*
39-
* @param workCode 流程编码
40-
* @param operator 操作者
41-
* @param bindData 绑定数据
42-
* @param advice 审批意见
43-
*/
44-
public FlowResult startFlow(String workCode, IFlowOperator operator, IBindData bindData, String advice) {
52+
private void loadFlowWork() {
4553
// 检测流程是否存在
46-
FlowWork flowWork = flowWorkRepository.getFlowWorkByCode(workCode);
54+
FlowWorkRepository flowWorkRepository = flowServiceRepositoryHolder.getFlowWorkRepository();
55+
this.flowWork = flowWorkRepository.getFlowWorkByCode(workCode);
4756
if (flowWork == null) {
4857
throw new IllegalArgumentException("flow work not found");
4958
}
5059
flowWork.verify();
5160
flowWork.enableValidate();
61+
}
5262

53-
// 流程数据备份
54-
FlowBackup flowBackup = flowBackupRepository.getFlowBackupByWorkIdAndVersion(flowWork.getId(), flowWork.getUpdateTime());
63+
private void loadFlowBackup() {
64+
FlowBackupRepository flowBackupRepository = flowServiceRepositoryHolder.getFlowBackupRepository();
65+
this.flowBackup = flowBackupRepository.getFlowBackupByWorkIdAndVersion(flowWork.getId(), flowWork.getUpdateTime());
5566
if (flowBackup == null) {
5667
flowBackup = flowBackupRepository.backup(flowWork);
5768
}
69+
}
5870

59-
// 保存流程
60-
FlowProcess flowProcess = new FlowProcess(flowBackup.getId(), operator);
61-
flowProcessRepository.save(flowProcess);
62-
63-
// 保存绑定数据
64-
BindDataSnapshot snapshot = new BindDataSnapshot(bindData);
65-
flowBindDataRepository.save(snapshot);
71+
private void saveFlowProcess() {
72+
this.flowProcess = new FlowProcess(flowBackup.getId(), operator);
73+
flowServiceRepositoryHolder.getFlowProcessRepository().save(flowProcess);
74+
}
6675

67-
// 创建流程id
68-
String processId = flowProcess.getProcessId();
76+
private void saveBindDataSnapshot() {
77+
snapshot = new BindDataSnapshot(bindData);
78+
flowServiceRepositoryHolder.getFlowBindDataRepository().save(snapshot);
79+
}
6980

70-
// 构建审批意见
71-
Opinion opinion = Opinion.pass(advice);
81+
private void buildFlowNodeService() {
7282

7383
// 获取开始节点
7484
FlowNode start = flowWork.getStartNode();
7585
if (start == null) {
7686
throw new IllegalArgumentException("start node not found");
7787
}
88+
89+
this.flowNode = start;
7890
// 设置开始流程的上一个流程id
7991
long preId = 0;
8092

93+
// 创建流程id
94+
String processId = flowProcess.getProcessId();
95+
8196
List<FlowRecord> historyRecords = new ArrayList<>();
8297

83-
FlowNodeService flowNodeService = new FlowNodeService(flowOperatorRepository,
98+
FlowOperatorRepository flowOperatorRepository = flowServiceRepositoryHolder.getFlowOperatorRepository();
99+
FlowRecordRepository flowRecordRepository = flowServiceRepositoryHolder.getFlowRecordRepository();
100+
101+
flowNodeService = new FlowNodeService(flowOperatorRepository,
84102
flowRecordRepository,
85103
snapshot,
86104
opinion,
@@ -93,6 +111,43 @@ public FlowResult startFlow(String workCode, IFlowOperator operator, IBindData b
93111
preId);
94112

95113
flowNodeService.setNextNode(start);
114+
}
115+
116+
117+
private void pushEvent(int flowApprovalEventState, FlowRecord flowRecord) {
118+
EventPusher.push(new FlowApprovalEvent(flowApprovalEventState,
119+
flowRecord,
120+
flowRecord.getCurrentOperator(),
121+
flowWork,
122+
snapshot.toBindData()),
123+
true);
124+
}
125+
126+
127+
private void saveFlowRecords(List<FlowRecord> flowRecords) {
128+
FlowRecordRepository flowRecordRepository = flowServiceRepositoryHolder.getFlowRecordRepository();
129+
flowRecordRepository.save(flowRecords);
130+
}
131+
132+
133+
/**
134+
* 发起流程 (不自动提交到下一节点)
135+
*/
136+
public FlowResult startFlow() {
137+
// 检测流程是否存在
138+
this.loadFlowWork();
139+
140+
// 流程数据备份
141+
this.loadFlowBackup();
142+
143+
// 保存流程
144+
this.saveFlowProcess();
145+
146+
// 保存绑定数据
147+
this.saveBindDataSnapshot();
148+
149+
// 构建流程节点服务
150+
this.buildFlowNodeService();
96151

97152
// 创建待办记录
98153
List<FlowRecord> records = flowNodeService.createRecord();
@@ -111,29 +166,23 @@ public FlowResult startFlow(String workCode, IFlowOperator operator, IBindData b
111166
record.finish();
112167
}
113168

114-
flowRecordRepository.save(records);
169+
this.saveFlowRecords(records);
115170

116171
// 推送事件
117172
for (FlowRecord record : records) {
118-
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_CREATE, record, operator, flowWork, snapshot.toBindData()), true);
119-
120-
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_FINISH,
121-
record,
122-
operator,
123-
flowWork,
124-
snapshot.toBindData()),
125-
true);
173+
this.pushEvent(FlowApprovalEvent.STATE_CREATE, record);
174+
this.pushEvent(FlowApprovalEvent.STATE_FINISH, record);
126175
}
127176
return new FlowResult(flowWork, records);
128177
}
129178

130179
// 保存流程记录
131-
flowRecordRepository.save(records);
180+
this.saveFlowRecords(records);
132181

133182
// 推送事件消息
134183
for (FlowRecord record : records) {
135-
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_CREATE, record, operator, flowWork, snapshot.toBindData()), true);
136-
EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, record, operator, flowWork, snapshot.toBindData()), true);
184+
this.pushEvent(FlowApprovalEvent.STATE_CREATE, record);
185+
this.pushEvent(FlowApprovalEvent.STATE_TODO, record);
137186
}
138187
// 当前的审批记录
139188
return new FlowResult(flowWork, records);

0 commit comments

Comments
 (0)