11
11
import com .codingapi .springboot .flow .record .FlowBackup ;
12
12
import com .codingapi .springboot .flow .record .FlowProcess ;
13
13
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 ;
15
18
import com .codingapi .springboot .flow .service .FlowNodeService ;
19
+ import com .codingapi .springboot .flow .service .FlowServiceRepositoryHolder ;
16
20
import com .codingapi .springboot .flow .user .IFlowOperator ;
17
21
import com .codingapi .springboot .framework .event .EventPusher ;
18
- import lombok .AllArgsConstructor ;
19
22
import org .springframework .transaction .annotation .Transactional ;
20
23
21
24
import java .util .ArrayList ;
22
25
import java .util .List ;
23
26
24
27
@ Transactional
25
- @ AllArgsConstructor
26
28
public class FlowStartService {
27
29
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
+ }
35
51
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 () {
45
53
// 检测流程是否存在
46
- FlowWork flowWork = flowWorkRepository .getFlowWorkByCode (workCode );
54
+ FlowWorkRepository flowWorkRepository = flowServiceRepositoryHolder .getFlowWorkRepository ();
55
+ this .flowWork = flowWorkRepository .getFlowWorkByCode (workCode );
47
56
if (flowWork == null ) {
48
57
throw new IllegalArgumentException ("flow work not found" );
49
58
}
50
59
flowWork .verify ();
51
60
flowWork .enableValidate ();
61
+ }
52
62
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 ());
55
66
if (flowBackup == null ) {
56
67
flowBackup = flowBackupRepository .backup (flowWork );
57
68
}
69
+ }
58
70
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
+ }
66
75
67
- // 创建流程id
68
- String processId = flowProcess .getProcessId ();
76
+ private void saveBindDataSnapshot () {
77
+ snapshot = new BindDataSnapshot (bindData );
78
+ flowServiceRepositoryHolder .getFlowBindDataRepository ().save (snapshot );
79
+ }
69
80
70
- // 构建审批意见
71
- Opinion opinion = Opinion .pass (advice );
81
+ private void buildFlowNodeService () {
72
82
73
83
// 获取开始节点
74
84
FlowNode start = flowWork .getStartNode ();
75
85
if (start == null ) {
76
86
throw new IllegalArgumentException ("start node not found" );
77
87
}
88
+
89
+ this .flowNode = start ;
78
90
// 设置开始流程的上一个流程id
79
91
long preId = 0 ;
80
92
93
+ // 创建流程id
94
+ String processId = flowProcess .getProcessId ();
95
+
81
96
List <FlowRecord > historyRecords = new ArrayList <>();
82
97
83
- FlowNodeService flowNodeService = new FlowNodeService (flowOperatorRepository ,
98
+ FlowOperatorRepository flowOperatorRepository = flowServiceRepositoryHolder .getFlowOperatorRepository ();
99
+ FlowRecordRepository flowRecordRepository = flowServiceRepositoryHolder .getFlowRecordRepository ();
100
+
101
+ flowNodeService = new FlowNodeService (flowOperatorRepository ,
84
102
flowRecordRepository ,
85
103
snapshot ,
86
104
opinion ,
@@ -93,6 +111,43 @@ public FlowResult startFlow(String workCode, IFlowOperator operator, IBindData b
93
111
preId );
94
112
95
113
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 ();
96
151
97
152
// 创建待办记录
98
153
List <FlowRecord > records = flowNodeService .createRecord ();
@@ -111,29 +166,23 @@ public FlowResult startFlow(String workCode, IFlowOperator operator, IBindData b
111
166
record .finish ();
112
167
}
113
168
114
- flowRecordRepository . save (records );
169
+ this . saveFlowRecords (records );
115
170
116
171
// 推送事件
117
172
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 );
126
175
}
127
176
return new FlowResult (flowWork , records );
128
177
}
129
178
130
179
// 保存流程记录
131
- flowRecordRepository . save (records );
180
+ this . saveFlowRecords (records );
132
181
133
182
// 推送事件消息
134
183
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 );
137
186
}
138
187
// 当前的审批记录
139
188
return new FlowResult (flowWork , records );
0 commit comments