66import com .codingapi .springboot .flow .domain .FlowWork ;
77import com .codingapi .springboot .flow .domain .Opinion ;
88import com .codingapi .springboot .flow .em .FlowSourceDirection ;
9+ import com .codingapi .springboot .flow .event .FlowApprovalEvent ;
910import com .codingapi .springboot .flow .record .FlowRecord ;
1011import com .codingapi .springboot .flow .repository .FlowBindDataRepository ;
1112import com .codingapi .springboot .flow .repository .FlowOperatorRepository ;
1213import com .codingapi .springboot .flow .user .IFlowOperator ;
14+ import com .codingapi .springboot .framework .event .EventPusher ;
1315import lombok .Getter ;
1416
1517import java .util .List ;
@@ -22,6 +24,7 @@ class FlowNodeService {
2224
2325 private final FlowRecordService2 flowRecordService2 ;
2426 private final String processId ;
27+ private final long preId ;
2528
2629 private final FlowRecord flowRecord ;
2730 private final FlowWork flowWork ;
@@ -60,7 +63,7 @@ public FlowNodeService(FlowOperatorRepository flowOperatorRepository,
6063 this .flowNode = flowRecordService2 .getFlowNode ();
6164
6265 this .processId = flowRecord .getProcessId ();
63-
66+ this . preId = flowRecord . getId ();
6467 }
6568
6669
@@ -196,18 +199,27 @@ public void updateFlowRecord() {
196199 }
197200
198201
202+ /**
203+ * 加载子节点的审批记录
204+ * 即加载后续节点的审批记录
205+ */
199206 public void loadChildrenRecords () {
200207 childrenRecords = flowRecordService2 .flowRecordRepository .findFlowRecordByPreId (flowRecord .getId ());
201208 }
202209
203210
211+ /**
212+ * 校验是否后续没有审批记录
213+ */
204214 public void verifyChildrenRecordsIsEmpty () {
205215 if (!childrenRecords .isEmpty ()) {
206216 throw new IllegalArgumentException ("flow node is done" );
207217 }
208218 }
209219
210-
220+ /**
221+ * 校验流程的审批方向
222+ */
211223 public void verifyFlowSourceDirection () {
212224 if (flowSourceDirection == null ) {
213225 throw new IllegalArgumentException ("flow source direction is null" );
@@ -216,4 +228,95 @@ public void verifyFlowSourceDirection() {
216228 throw new IllegalArgumentException ("flow node is start node" );
217229 }
218230 }
231+
232+
233+ public void createNextRecord (){
234+
235+ IFlowOperator createOperator = flowOperatorRepository .getFlowOperatorById (flowRecord .getCreateOperatorId ());
236+ if (flowSourceDirection == FlowSourceDirection .PASS ){
237+ FlowRecordService flowRecordService = new FlowRecordService (flowOperatorRepository , processId , createOperator , currentOperator , snapshot , opinion , flowWork , flowSourceDirection , historyRecords );
238+ FlowNode nextNode = flowRecordService .matcherPassNextNode (flowNode );
239+ if (nextNode == null ) {
240+ throw new IllegalArgumentException ("next node not found" );
241+ }
242+ List <FlowRecord > records = flowRecordService .createRecord (preId , nextNode );
243+ flowRecordService2 .flowRecordRepository .save (records );
244+
245+ for (FlowRecord record : records ) {
246+ IFlowOperator pushOperator = flowOperatorRepository .getFlowOperatorById (record .getCurrentOperatorId ());
247+ EventPusher .push (new FlowApprovalEvent (FlowApprovalEvent .STATE_TODO , record , pushOperator ,flowWork ));
248+ }
249+ }
250+
251+
252+ if (flowSourceDirection == FlowSourceDirection .REJECT ){
253+
254+ // 设置了退回流程
255+ if (flowWork .hasBackRelation ()){
256+
257+ FlowRecordService flowRecordService = new FlowRecordService (flowOperatorRepository , processId , createOperator , currentOperator , snapshot , opinion , flowWork , flowSourceDirection , historyRecords );
258+ FlowNode nextNode = flowRecordService .matcherBackNextNode (flowNode );
259+ if (nextNode == null ) {
260+ throw new IllegalArgumentException ("next node not found" );
261+ }
262+
263+ IFlowOperator flowOperator = currentOperator ;
264+ if (nextNode .isAnyOperatorMatcher ()) {
265+ // 如果是任意人员操作时则需要指定为当时审批人员为当前审批人员
266+ FlowRecord preFlowRecord = flowRecordService2 .flowRecordRepository .getFlowRecordById (flowRecord .getPreId ());
267+ while (preFlowRecord .isTransfer () || !preFlowRecord .getNodeCode ().equals (nextNode .getCode ())) {
268+ preFlowRecord = flowRecordService2 .flowRecordRepository .getFlowRecordById (preFlowRecord .getPreId ());
269+ }
270+ flowOperator = flowOperatorRepository .getFlowOperatorById (preFlowRecord .getCurrentOperatorId ());
271+ }
272+ flowRecordService .changeCurrentOperator (flowOperator );
273+ List <FlowRecord > records = flowRecordService .createRecord (preId , nextNode );
274+ flowRecordService2 .flowRecordRepository .save (records );
275+
276+ for (FlowRecord record : records ) {
277+ IFlowOperator pushOperator = flowOperatorRepository .getFlowOperatorById (record .getCurrentOperatorId ());
278+ EventPusher .push (new FlowApprovalEvent (FlowApprovalEvent .STATE_TODO , record , pushOperator ,flowWork ));
279+ }
280+ }else {
281+
282+ IFlowOperator flowOperator ;
283+ // 拒绝时,默认返回上一个节点
284+ FlowRecord preRecord = flowRecordService2 .flowRecordRepository .getFlowRecordById (flowRecord .getPreId ());
285+ // 去除所有的转办的记录
286+ while (preRecord .isTransfer ()) {
287+ // 继续寻找上一个节点
288+ preRecord = flowRecordService2 .flowRecordRepository .getFlowRecordById (preRecord .getPreId ());
289+ }
290+
291+ // 获取上一个节点的审批者,继续将审批者设置为当前审批者
292+ flowOperator = flowOperatorRepository .getFlowOperatorById (preRecord .getCurrentOperatorId ());
293+
294+ FlowRecordService flowRecordService = new FlowRecordService (flowOperatorRepository , processId , createOperator , flowOperator , snapshot , opinion , flowWork , flowSourceDirection , historyRecords );
295+ FlowNode nextNode = flowWork .getNodeByCode (preRecord .getNodeCode ());
296+ if (nextNode == null ) {
297+ throw new IllegalArgumentException ("next node not found" );
298+ }
299+ List <FlowRecord > records = flowRecordService .createRecord (preId , nextNode );
300+
301+ flowRecordService2 .flowRecordRepository .save (records );
302+
303+ for (FlowRecord record : records ) {
304+ IFlowOperator pushOperator = flowOperatorRepository .getFlowOperatorById (record .getCurrentOperatorId ());
305+ EventPusher .push (new FlowApprovalEvent (FlowApprovalEvent .STATE_TODO , record , pushOperator ,flowWork ));
306+ }
307+
308+ }
309+
310+ }
311+
312+
313+ int eventState = flowSourceDirection == FlowSourceDirection .PASS ? FlowApprovalEvent .STATE_PASS : FlowApprovalEvent .STATE_REJECT ;
314+ EventPusher .push (new FlowApprovalEvent (eventState , flowRecord , currentOperator ,flowWork ));
315+
316+
317+
318+ }
319+
320+
321+
219322}
0 commit comments