55import com .codingapi .flow .gateway .FlowOperatorGateway ;
66import com .codingapi .flow .operator .IFlowOperator ;
77import com .codingapi .flow .record .FlowRecord ;
8+ import com .codingapi .flow .record .FlowTodoRecord ;
9+ import com .codingapi .flow .record .FlowTodoMerge ;
810import com .codingapi .flow .repository .*;
911import com .codingapi .flow .service .FlowService ;
1012import com .codingapi .flow .service .impl .FlowActionService ;
1113import com .codingapi .flow .service .impl .FlowDelayTriggerService ;
1214import com .codingapi .flow .session .FlowSession ;
1315import lombok .Getter ;
1416
17+ import java .util .ArrayList ;
1518import java .util .List ;
1619
1720public class RepositoryHolderContext {
@@ -29,6 +32,10 @@ private RepositoryHolderContext() {
2932 @ Getter
3033 private FlowRecordRepository flowRecordRepository ;
3134 @ Getter
35+ private FlowTodoRecordRepository flowTodoRecordRepository ;
36+ @ Getter
37+ private FlowTodoMergeRepository flowTodoMergeRepository ;
38+ @ Getter
3239 private FlowOperatorGateway flowOperatorGateway ;
3340 @ Getter
3441 private ParallelBranchRepository parallelBranchRepository ;
@@ -45,6 +52,8 @@ public boolean isRegistered() {
4552 && delayTaskRepository != null
4653 && workflowBackupRepository != null
4754 && flowRecordRepository != null
55+ && flowTodoRecordRepository != null
56+ && flowTodoMergeRepository != null
4857 && flowOperatorGateway != null
4958 && workflowRepository != null
5059 && urgeIntervalRepository != null ;
@@ -60,13 +69,17 @@ public void verify() {
6069 public void register (WorkflowRepository workflowRepository ,
6170 WorkflowBackupRepository workflowBackupRepository ,
6271 FlowRecordRepository flowRecordRepository ,
72+ FlowTodoRecordRepository flowTodoRecordRepository ,
73+ FlowTodoMergeRepository flowTodoMergeRepository ,
6374 FlowOperatorGateway flowOperatorGateway ,
6475 ParallelBranchRepository parallelBranchRepository ,
6576 DelayTaskRepository delayTaskRepository ,
6677 UrgeIntervalRepository urgeIntervalRepository ) {
6778 this .workflowRepository = workflowRepository ;
6879 this .workflowBackupRepository = workflowBackupRepository ;
6980 this .flowRecordRepository = flowRecordRepository ;
81+ this .flowTodoRecordRepository = flowTodoRecordRepository ;
82+ this .flowTodoMergeRepository = flowTodoMergeRepository ;
7083 this .flowOperatorGateway = flowOperatorGateway ;
7184 this .parallelBranchRepository = parallelBranchRepository ;
7285 this .delayTaskRepository = delayTaskRepository ;
@@ -97,10 +110,7 @@ public FlowDelayTriggerService createDelayTriggerService(DelayTask task) {
97110 */
98111 public FlowActionService createFlowActionService (FlowSession flowSession ) {
99112 this .verify ();
100- return new FlowActionService (flowSession .toActionRequest (),
101- flowOperatorGateway ,
102- flowRecordRepository ,
103- workflowBackupRepository );
113+ return new FlowActionService (flowSession .toActionRequest ());
104114 }
105115
106116
@@ -114,6 +124,8 @@ public FlowService createFlowService() {
114124 return new FlowService (workflowRepository ,
115125 flowOperatorGateway ,
116126 flowRecordRepository ,
127+ flowTodoRecordRepository ,
128+ flowTodoMergeRepository ,
117129 workflowBackupRepository ,
118130 parallelBranchRepository ,
119131 delayTaskRepository ,
@@ -142,12 +154,15 @@ public void deleteDelayTask(DelayTask delayTask) {
142154 delayTaskRepository .delete (delayTask );
143155 }
144156
157+
145158 public void saveRecords (List <FlowRecord > flowRecords ) {
146- flowRecordRepository .saveAll (flowRecords );
159+ FlowRecordRepositoryService flowRecordRepositoryService = new FlowRecordRepositoryService (flowRecords );
160+ flowRecordRepositoryService .saveAll ();
147161 }
148162
149163 public void saveRecord (FlowRecord flowRecord ) {
150- flowRecordRepository .save (flowRecord );
164+ FlowRecordRepositoryService flowRecordRepositoryService = new FlowRecordRepositoryService (flowRecord );
165+ flowRecordRepositoryService .saveAll ();
151166 }
152167
153168 public List <FlowRecord > findCurrentNodeRecords (long fromId , String nodeId ) {
@@ -179,4 +194,109 @@ public List<DelayTask> findDelayTasks() {
179194 }
180195
181196
197+ private static class FlowRecordRepositoryService {
198+
199+ private final List <FlowRecord > flowRecords ;
200+ private final FlowTodoRecordRepository flowTodoRecordRepository ;
201+ private final FlowTodoMergeRepository flowTodoMergeRepository ;
202+ private final FlowRecordRepository flowRecordRepository ;
203+
204+
205+ public FlowRecordRepositoryService (List <FlowRecord > flowRecords ) {
206+ this .flowTodoRecordRepository = RepositoryHolderContext .getInstance ().getFlowTodoRecordRepository ();
207+ this .flowTodoMergeRepository = RepositoryHolderContext .getInstance ().getFlowTodoMergeRepository ();
208+ this .flowRecordRepository = RepositoryHolderContext .getInstance ().getFlowRecordRepository ();
209+ this .flowRecords = flowRecords ;
210+ }
211+
212+ public FlowRecordRepositoryService (FlowRecord flowRecord ) {
213+ this .flowTodoRecordRepository = RepositoryHolderContext .getInstance ().getFlowTodoRecordRepository ();
214+ this .flowTodoMergeRepository = RepositoryHolderContext .getInstance ().getFlowTodoMergeRepository ();
215+ this .flowRecordRepository = RepositoryHolderContext .getInstance ().getFlowRecordRepository ();
216+ this .flowRecords = new ArrayList <>();
217+ this .flowRecords .add (flowRecord );
218+ }
219+
220+
221+ private void saveTodoMargeRecords () {
222+ List <FlowTodoRecord > flowTodoRecords = new ArrayList <>();
223+ for (FlowRecord flowRecord : flowRecords ) {
224+ if (flowRecord .isTodo ()) {
225+ FlowTodoRecord todoMargeRecord = null ;
226+ if (flowRecord .isMergeable ()) {
227+ todoMargeRecord = flowTodoRecordRepository .getByMergeKey (flowRecord .getMergeKey ());
228+ if (todoMargeRecord == null ) {
229+ todoMargeRecord = new FlowTodoRecord (flowRecord );
230+ } else {
231+ todoMargeRecord .update (flowRecord );
232+ todoMargeRecord .addMargeCount ();
233+ }
234+ } else {
235+ todoMargeRecord = new FlowTodoRecord (flowRecord );
236+ }
237+ flowTodoRecords .add (todoMargeRecord );
238+ }
239+ }
240+ if (!flowTodoRecords .isEmpty ()) {
241+ flowTodoRecordRepository .saveAll (flowTodoRecords );
242+ }
243+
244+ if (!flowTodoRecords .isEmpty ()) {
245+ List <FlowTodoMerge > relationList = new ArrayList <>();
246+ for (FlowTodoRecord margeRecord : flowTodoRecords ) {
247+ if (margeRecord .isMergeable ()) {
248+ relationList .add (new FlowTodoMerge (margeRecord ));
249+ }
250+ }
251+ flowTodoMergeRepository .saveAll (relationList );
252+ }
253+ }
254+
255+ private void saveRecords () {
256+ if (!flowRecords .isEmpty ()) {
257+ flowRecordRepository .saveAll (flowRecords );
258+ }
259+ }
260+
261+
262+ private void removeTodoMargeRecords () {
263+ for (FlowRecord flowRecord : flowRecords ) {
264+ if (flowRecord .isDone ()) {
265+ if (flowRecord .isMergeable ()) {
266+ FlowTodoRecord todoMargeRecord = flowTodoRecordRepository .getByMergeKey (flowRecord .getMergeKey ());
267+ if (todoMargeRecord !=null ) {
268+ List <FlowTodoMerge > margeRelations = flowTodoMergeRepository .findByTodoId (todoMargeRecord .getId ());
269+ if (margeRelations !=null && !margeRelations .isEmpty ()) {
270+ for (FlowTodoMerge margeRelation : margeRelations ) {
271+ if (margeRelation .isRecord (flowRecord .getId ())) {
272+ flowTodoMergeRepository .remove (margeRelation );
273+ todoMargeRecord .divMargeCount ();
274+ if (todoMargeRecord .hasMargeCount ()) {
275+ flowTodoRecordRepository .save (todoMargeRecord );
276+ } else {
277+ flowTodoRecordRepository .remove (todoMargeRecord );
278+ }
279+ }
280+ }
281+ }
282+ }
283+ } else {
284+ FlowTodoRecord todoMargeRecord = flowTodoRecordRepository .getByMergeKey (flowRecord .getMergeKey ());
285+ if (todoMargeRecord != null ) {
286+ flowTodoRecordRepository .remove (todoMargeRecord );
287+ }
288+ }
289+ }
290+ }
291+ }
292+
293+ public void saveAll () {
294+ this .saveRecords ();
295+ this .saveTodoMargeRecords ();
296+ this .removeTodoMargeRecords ();
297+ }
298+
299+
300+ }
301+
182302}
0 commit comments