1212import com .codingapi .springboot .flow .service .FlowNodeService ;
1313import com .codingapi .springboot .flow .service .FlowServiceRepositoryHolder ;
1414import com .codingapi .springboot .flow .user .IFlowOperator ;
15+ import com .codingapi .springboot .framework .utils .RandomGenerator ;
1516
1617import java .util .ArrayList ;
18+ import java .util .Comparator ;
1719import java .util .List ;
20+ import java .util .stream .Collectors ;
1821
1922public class FlowStepService {
2023 private final FlowWork flowWork ;
24+ private final long recordId ;
25+ private final List <FlowRecord > flowRecords ;
2126
2227 private final IFlowOperator currentOperator ;
2328 private final IBindData bindData ;
2429 private final FlowServiceRepositoryHolder flowServiceRepositoryHolder ;
2530
2631 private FlowNodeService flowNodeService ;
2732 private FlowNode flowNode ;
33+ private FlowRecord currentFlowRecord ;
2834
29- public FlowStepService (String workCode , IFlowOperator currentOperator , IBindData bindData , FlowServiceRepositoryHolder flowServiceRepositoryHolder ) {
35+ public FlowStepService (long recordId ,String workCode , IFlowOperator currentOperator , IBindData bindData , FlowServiceRepositoryHolder flowServiceRepositoryHolder ) {
36+ this .recordId = recordId ;
3037 this .currentOperator = currentOperator ;
3138 this .bindData = bindData ;
3239 this .flowServiceRepositoryHolder = flowServiceRepositoryHolder ;
33- this .flowWork = flowServiceRepositoryHolder .getFlowWorkRepository ().getFlowWorkByCode (workCode );
40+ if (this .recordId >0 ) {
41+ this .currentFlowRecord = flowServiceRepositoryHolder .getFlowRecordRepository ().getFlowRecordById (recordId );
42+ this .flowRecords = flowServiceRepositoryHolder .getFlowRecordRepository ().findFlowRecordByProcessId (currentFlowRecord .getProcessId ()).stream ().sorted (Comparator .comparingLong (FlowRecord ::getId )).collect (Collectors .toList ());
43+ this .flowWork = flowServiceRepositoryHolder .getFlowWorkRepository ().getFlowWorkByCode (currentFlowRecord .getWorkCode ());
44+ }else {
45+ this .currentFlowRecord = null ;
46+ this .flowRecords = new ArrayList <>();
47+ this .flowWork = flowServiceRepositoryHolder .getFlowWorkRepository ().getFlowWorkByCode (workCode );
48+ }
3449 }
3550
3651
3752 public FlowStepResult getFlowStep () {
3853 FlowStepResult flowStepResult = new FlowStepResult ();
39- // 获取开始节点
40- FlowNode start = flowWork .getStartNode ();
41- if (start == null ) {
42- throw new IllegalArgumentException ("start node not found" );
43- }
4454
45- this .flowNode = start ;
4655 // 设置开始流程的上一个流程id
4756 long preId = 0 ;
57+ if (currentFlowRecord ==null ) {
58+ // 获取开始节点
59+ FlowNode start = flowWork .getStartNode ();
60+ if (start == null ) {
61+ throw new IllegalArgumentException ("start node not found" );
62+ }
63+ preId = 0 ;
64+ this .flowNode = start ;
65+ }else {
66+ for (FlowRecord flowRecord : flowRecords ) {
67+ FlowNode flowNode = this .flowWork .getNodeByCode (flowRecord .getNodeCode ());
68+ List <IFlowOperator > operators = new ArrayList <>();
69+ if (flowRecord .getCurrentOperator ()!=null ) {
70+ operators .add (flowRecord .getCurrentOperator ());
71+ }
72+ boolean isDone =flowRecord .isDone () || flowRecord .getOpinion ().isCirculate ();
73+ flowStepResult .addFlowNode (flowNode ,isDone , operators );
74+ }
75+ FlowRecord lastRecord = this .flowRecords .get (this .flowRecords .size ()-1 );
76+ this .flowNode = this .flowWork .getNodeByCode (lastRecord .getNodeCode ());
77+ preId = lastRecord .getId ();
78+ }
4879
4980 // 创建流程id
50- String processId = "flow_" + System .currentTimeMillis ();
51-
52- List <FlowRecord > historyRecords = new ArrayList <>();
81+ String processId = "flow_" + RandomGenerator .generateUUID ();
5382
5483 FlowOperatorRepository flowOperatorRepository = flowServiceRepositoryHolder .getFlowOperatorRepository ();
5584 FlowRecordRepository flowRecordRepository = flowServiceRepositoryHolder .getFlowRecordRepository ();
56-
85+ List < FlowRecord > historyRecords = new ArrayList <>();
5786 BindDataSnapshot snapshot = new BindDataSnapshot (bindData );
5887 flowNodeService = new FlowNodeService (flowOperatorRepository ,
5988 flowRecordRepository ,
@@ -67,15 +96,19 @@ public FlowStepResult getFlowStep() {
6796 processId ,
6897 preId );
6998
70- flowNodeService .setNextNode (start );
99+ flowNodeService .setNextNode (this . flowNode );
71100
72- this .flowNode = start ;
73- flowStepResult .addFlowNode (this .flowNode , this .flowNodeService .loadNextNodeOperators ());
101+ if (currentFlowRecord ==null ) {
102+ flowStepResult .addFlowNode (this .flowNode , false , this .flowNodeService .loadNextNodeOperators ());
103+ }
74104
75105 do {
76106 flowNodeService .loadNextPassNode (this .flowNode );
77107 this .flowNode = flowNodeService .getNextNode ();
78- flowStepResult .addFlowNode (this .flowNode , this .flowNodeService .loadNextNodeOperators ());
108+
109+ boolean isFinish = currentFlowRecord != null && currentFlowRecord .isFinish ();
110+
111+ flowStepResult .addFlowNode (this .flowNode ,isFinish , this .flowNodeService .loadNextNodeOperators ());
79112 } while (!flowNode .isOverNode ());
80113
81114 return flowStepResult ;
0 commit comments