1111import com .codingapi .springboot .flow .record .FlowBackup ;
1212import com .codingapi .springboot .flow .record .FlowProcess ;
1313import 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 ;
1518import com .codingapi .springboot .flow .service .FlowNodeService ;
19+ import com .codingapi .springboot .flow .service .FlowServiceRepositoryHolder ;
1620import com .codingapi .springboot .flow .user .IFlowOperator ;
1721import com .codingapi .springboot .framework .event .EventPusher ;
18- import lombok .AllArgsConstructor ;
1922import org .springframework .transaction .annotation .Transactional ;
2023
2124import java .util .ArrayList ;
2225import java .util .List ;
2326
2427@ Transactional
25- @ AllArgsConstructor
2628public 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