1313import com .codingapi .flow .utils .RandomUtils ;
1414import com .codingapi .springboot .framework .event .EventPusher ;
1515
16+ import java .util .ArrayList ;
17+ import java .util .HashMap ;
1618import java .util .List ;
1719import java .util .Map ;
20+ import java .util .concurrent .Executors ;
21+ import java .util .concurrent .ScheduledExecutorService ;
1822
1923/**
2024 * 结束节点
@@ -24,6 +28,8 @@ public class EndNode extends BaseFlowNode implements IDisplayNode {
2428 public static final String NODE_TYPE = NodeType .END .name ();
2529 public static final String DEFAULT_NAME = "结束节点" ;
2630
31+ private final ScheduledExecutorService threadPools = Executors .newScheduledThreadPool (1 );
32+
2733 @ Override
2834 public String getType () {
2935 return NODE_TYPE ;
@@ -47,17 +53,30 @@ public void fillNewRecord(FlowSession session, FlowRecord flowRecord) {
4753 IFlowAction currentAction = session .getCurrentAction ();
4854 // 标记当前流程结束
4955 FlowRecord latestRecord = session .getCurrentRecord ();
56+
5057 // 添加历史记录到记录中
5158 List <FlowRecord > historyRecords = RepositoryHolderContext .getInstance ().findProcessRecords (latestRecord .getProcessId ());
59+
60+ // 同步当前处理的FlowRecord的数据
61+ List <FlowRecord > recordList = new ArrayList <>();
62+ for (FlowRecord historyRecord :historyRecords ){
63+ if (historyRecord .getId ()== latestRecord .getId ()){
64+ recordList .add (latestRecord );
65+ }else {
66+ recordList .add (historyRecord );
67+ }
68+ }
69+
5270 // 设置状态为完成
53- historyRecords .forEach (item -> {
71+ recordList .forEach (item -> {
5472 item .finish (currentAction instanceof PassAction );
5573 });
56- RepositoryHolderContext .getInstance ().saveRecords (historyRecords );
74+ RepositoryHolderContext .getInstance ().saveRecords (recordList );
5775 // 流程是否正常结束
5876 EventPusher .push (new FlowRecordFinishEvent (latestRecord ));
5977 }
6078
79+
6180 @ Override
6281 public boolean handle (FlowSession session ) {
6382 return false ;
@@ -75,6 +94,46 @@ public static EndNode formMap(Map<String, Object> map) {
7594 return BaseFlowNode .fromMap (map , EndNode .class );
7695 }
7796
97+
98+ private static class RecordMergeHelper {
99+
100+ private final List <FlowRecord > currentRecords ;
101+ private final List <FlowRecord > historyRecords ;
102+
103+ private final Map <Long ,FlowRecord > currentRecordCache ;
104+
105+ private final List <FlowRecord > recordList ;
106+
107+ public RecordMergeHelper (List <FlowRecord > historyRecords , List <FlowRecord > currentRecords ) {
108+ this .historyRecords = historyRecords ;
109+ this .currentRecords = currentRecords ;
110+ this .currentRecordCache = new HashMap <>();
111+ this .recordList = new ArrayList <>();
112+ this .initCurrentRecordCache ();
113+ }
114+
115+ private void initCurrentRecordCache (){
116+ for (FlowRecord currentRecord :this .currentRecords ){
117+ this .currentRecordCache .put (currentRecord .getId (),currentRecord );
118+ }
119+ }
120+
121+ public List <FlowRecord > getUpdateRecordList () {
122+ for (FlowRecord historyRecord :historyRecords ){
123+ FlowRecord currentRecord = this .currentRecordCache .get (historyRecord .getId ());
124+ if (currentRecord !=null ){
125+ this .recordList .add (currentRecord );
126+ }else {
127+ this .recordList .add (historyRecord );
128+ }
129+ }
130+ return this .recordList ;
131+ }
132+
133+
134+
135+ }
136+
78137 public static Builder builder () {
79138 return new Builder ();
80139 }
0 commit comments