22
33import com .codingapi .springboot .flow .bind .BindDataSnapshot ;
44import com .codingapi .springboot .flow .bind .IBindData ;
5+ import com .codingapi .springboot .flow .content .FlowContent ;
56import com .codingapi .springboot .flow .domain .FlowNode ;
7+ import com .codingapi .springboot .flow .domain .FlowRelation ;
68import com .codingapi .springboot .flow .domain .FlowWork ;
79import com .codingapi .springboot .flow .domain .Opinion ;
810import com .codingapi .springboot .flow .em .FlowSourceDirection ;
9- import com .codingapi .springboot .flow .event .FlowApprovalEvent ;
11+ import com .codingapi .springboot .flow .error .ErrorResult ;
12+ import com .codingapi .springboot .flow .error .NodeResult ;
13+ import com .codingapi .springboot .flow .error .OperatorResult ;
1014import com .codingapi .springboot .flow .record .FlowRecord ;
1115import com .codingapi .springboot .flow .repository .FlowBindDataRepository ;
1216import com .codingapi .springboot .flow .repository .FlowOperatorRepository ;
1317import com .codingapi .springboot .flow .user .IFlowOperator ;
14- import com .codingapi .springboot .framework .event .EventPusher ;
1518import lombok .Getter ;
1619
20+ import java .util .ArrayList ;
1721import java .util .List ;
1822
1923class FlowNodeService {
@@ -40,6 +44,8 @@ class FlowNodeService {
4044 private List <FlowRecord > childrenRecords ;
4145 @ Getter
4246 private List <FlowRecord > historyRecords ;
47+ @ Getter
48+ private IFlowOperator createOperator ;
4349
4450 @ Getter
4551 private BindDataSnapshot snapshot ;
@@ -131,7 +137,7 @@ public boolean hasCurrentFlowIsFinish() {
131137
132138
133139 /**
134- * 完成所有流程
140+ * 完成所有流程
135141 */
136142 public void finishFlow () {
137143 flowRecord .finish ();
@@ -209,7 +215,7 @@ public void loadChildrenRecords() {
209215
210216
211217 /**
212- * 校验是否后续没有审批记录
218+ * 校验是否后续没有审批记录
213219 */
214220 public void verifyChildrenRecordsIsEmpty () {
215221 if (!childrenRecords .isEmpty ()) {
@@ -218,7 +224,7 @@ public void verifyChildrenRecordsIsEmpty() {
218224 }
219225
220226 /**
221- * 校验流程的审批方向
227+ * 校验流程的审批方向
222228 */
223229 public void verifyFlowSourceDirection () {
224230 if (flowSourceDirection == null ) {
@@ -230,36 +236,139 @@ public void verifyFlowSourceDirection() {
230236 }
231237
232238
233- public void createNextRecord (){
239+ /**
240+ * 加载流程发起者
241+ */
242+ public void loadCreateOperator () {
243+ createOperator = flowOperatorRepository .getFlowOperatorById (flowRecord .getCreateOperatorId ());
244+ }
234245
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" );
246+
247+ /**
248+ * 获取下一个节点
249+ *
250+ * @return 下一个节点
251+ */
252+ public FlowNode matcherNextNode (boolean back ) {
253+ List <FlowRelation > relations = flowWork .getRelations ().stream ()
254+ .filter (relation -> relation .sourceMatcher (flowNode .getCode ()))
255+ .filter (relation -> relation .isBack () == back )
256+ .sorted ((o1 , o2 ) -> (o2 .getOrder () - o1 .getOrder ()))
257+ .toList ();
258+ if (relations .isEmpty ()) {
259+ throw new IllegalArgumentException ("relation not found" );
260+ }
261+ FlowContent flowContent = new FlowContent (flowWork , flowNode , createOperator , currentOperator , snapshot .toBindData (), opinion , historyRecords );
262+ List <FlowNode > flowNodes = new ArrayList <>();
263+ for (FlowRelation flowRelation : relations ) {
264+ FlowNode node = flowRelation .trigger (flowContent );
265+ if (node != null ) {
266+ flowNodes .add (node );
241267 }
242- List <FlowRecord > records = flowRecordService .createRecord (preId , nextNode );
243- flowRecordService2 .flowRecordRepository .save (records );
268+ }
269+ if (flowNodes .isEmpty ()) {
270+ throw new IllegalArgumentException ("next node not found" );
271+ }
272+ return flowNodes .get (0 );
273+ }
244274
245- for (FlowRecord record : records ) {
246- IFlowOperator pushOperator = flowOperatorRepository .getFlowOperatorById (record .getCurrentOperatorId ());
247- EventPusher .push (new FlowApprovalEvent (FlowApprovalEvent .STATE_TODO , record , pushOperator ,flowWork ));
275+ /**
276+ * 异常匹配
277+ *
278+ * @param currentNode 当前节点
279+ * @param currentOperator 当前操作者
280+ * @return 流程记录
281+ */
282+ private List <FlowRecord > errMatcher (FlowNode currentNode , IFlowOperator currentOperator ) {
283+ if (currentNode .hasErrTrigger ()) {
284+ FlowContent flowContent = new FlowContent (flowWork , currentNode , createOperator , currentOperator , snapshot .toBindData (), opinion , historyRecords );
285+ ErrorResult errorResult = currentNode .errMatcher (flowContent );
286+ if (errorResult == null ) {
287+ throw new IllegalArgumentException ("errMatcher match error." );
288+ }
289+
290+ // 匹配操作者
291+ if (errorResult .isOperator ()) {
292+ List <FlowRecord > recordList = new ArrayList <>();
293+ List <Long > operatorIds = ((OperatorResult ) errorResult ).getOperatorIds ();
294+ List <? extends IFlowOperator > operators = flowOperatorRepository .findByIds (operatorIds );
295+ for (IFlowOperator operator : operators ) {
296+ FlowContent content = new FlowContent (flowWork , currentNode , createOperator , operator , snapshot .toBindData (), opinion , historyRecords );
297+ String recordTitle = currentNode .generateTitle (content );
298+ FlowRecord record = currentNode .createRecord (flowWork .getId (), processId , preId , recordTitle , createOperator , operator , snapshot );
299+ recordList .add (record );
300+ }
301+ return recordList ;
302+ }
303+ // 匹配节点
304+ if (errorResult .isNode ()) {
305+ String nodeCode = ((NodeResult ) errorResult ).getNode ();
306+ FlowNode node = flowWork .getNodeByCode (nodeCode );
307+ if (node == null ) {
308+ throw new IllegalArgumentException ("node not found." );
309+ }
310+ List <FlowRecord > recordList = new ArrayList <>();
311+ FlowContent content = new FlowContent (flowWork , node , createOperator , currentOperator , snapshot .toBindData (), opinion , historyRecords );
312+ List <? extends IFlowOperator > matcherOperators = node .loadFlowNodeOperator (content , flowOperatorRepository );
313+ if (!matcherOperators .isEmpty ()) {
314+ for (IFlowOperator matcherOperator : matcherOperators ) {
315+ String recordTitle = node .generateTitle (content );
316+ FlowRecord record = node .createRecord (flowWork .getId (), processId , preId , recordTitle , createOperator , matcherOperator , snapshot );
317+ recordList .add (record );
318+ }
319+ }
320+ return recordList ;
321+ }
322+ throw new IllegalArgumentException ("errMatcher not match." );
323+ }
324+ throw new IllegalArgumentException ("operator not match." );
325+ }
326+
327+
328+ /**
329+ * 创建流程记录
330+ *
331+ * @param currentNode 当前节点
332+ * @return 流程记录
333+ */
334+ public List <FlowRecord > createRecord (FlowNode currentNode , IFlowOperator currentOperator ) {
335+ FlowContent flowContent = new FlowContent (flowWork , currentNode , createOperator , currentOperator , snapshot .toBindData (), opinion , historyRecords );
336+ long workId = flowWork .getId ();
337+ List <? extends IFlowOperator > operators = currentNode .loadFlowNodeOperator (flowContent , flowOperatorRepository );
338+ if (operators .isEmpty ()) {
339+ List <FlowRecord > errorRecordList = this .errMatcher (currentNode , currentOperator );
340+ if (errorRecordList .isEmpty ()) {
341+ throw new IllegalArgumentException ("operator not match." );
248342 }
343+ return errorRecordList ;
344+ } else {
345+ String recordTitle = currentNode .generateTitle (flowContent );
346+ List <FlowRecord > recordList = new ArrayList <>();
347+ for (IFlowOperator operator : operators ) {
348+ FlowRecord record = currentNode .createRecord (workId , processId , preId , recordTitle , createOperator , operator , snapshot );
349+ recordList .add (record );
350+ }
351+ return recordList ;
249352 }
353+ }
250354
251355
252- if (flowSourceDirection == FlowSourceDirection .REJECT ){
356+ private List <FlowRecord > createPassRecord () {
357+ if (flowSourceDirection == FlowSourceDirection .PASS ) {
358+ FlowNode nextNode = this .matcherNextNode (false );
359+ return this .createRecord (nextNode , currentOperator );
360+ }
361+ return null ;
362+ }
253363
364+ private List <FlowRecord > createCustomBackRecord () {
365+ if (flowSourceDirection == FlowSourceDirection .REJECT ) {
254366 // 设置了退回流程
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 );
367+ if (flowWork .hasBackRelation ()) {
368+ FlowNode nextNode = this .matcherNextNode (true );
259369 if (nextNode == null ) {
260370 throw new IllegalArgumentException ("next node not found" );
261371 }
262-
263372 IFlowOperator flowOperator = currentOperator ;
264373 if (nextNode .isAnyOperatorMatcher ()) {
265374 // 如果是任意人员操作时则需要指定为当时审批人员为当前审批人员
@@ -269,16 +378,15 @@ public void createNextRecord(){
269378 }
270379 flowOperator = flowOperatorRepository .getFlowOperatorById (preFlowRecord .getCurrentOperatorId ());
271380 }
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 {
381+ return this .createRecord (nextNode , flowOperator );
382+ }
383+ }
384+ return null ;
385+ }
281386
387+ private List <FlowRecord > createDefaultBackRecord () {
388+ if (flowSourceDirection == FlowSourceDirection .REJECT ) {
389+ if (!flowWork .hasBackRelation ()) {
282390 IFlowOperator flowOperator ;
283391 // 拒绝时,默认返回上一个节点
284392 FlowRecord preRecord = flowRecordService2 .flowRecordRepository .getFlowRecordById (flowRecord .getPreId ());
@@ -287,36 +395,45 @@ public void createNextRecord(){
287395 // 继续寻找上一个节点
288396 preRecord = flowRecordService2 .flowRecordRepository .getFlowRecordById (preRecord .getPreId ());
289397 }
290-
291398 // 获取上一个节点的审批者,继续将审批者设置为当前审批者
292399 flowOperator = flowOperatorRepository .getFlowOperatorById (preRecord .getCurrentOperatorId ());
293-
294- FlowRecordService flowRecordService = new FlowRecordService (flowOperatorRepository , processId , createOperator , flowOperator , snapshot , opinion , flowWork , flowSourceDirection , historyRecords );
295400 FlowNode nextNode = flowWork .getNodeByCode (preRecord .getNodeCode ());
296401 if (nextNode == null ) {
297402 throw new IllegalArgumentException ("next node not found" );
298403 }
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-
404+ return this .createRecord (nextNode , flowOperator );
308405 }
309-
310406 }
407+ return null ;
408+ }
311409
410+ private boolean isDefaultBackRecord () {
411+ return flowSourceDirection == FlowSourceDirection .REJECT && !flowWork .hasBackRelation ();
412+ }
312413
313- int eventState = flowSourceDirection == FlowSourceDirection .PASS ? FlowApprovalEvent .STATE_PASS : FlowApprovalEvent .STATE_REJECT ;
314- EventPusher .push (new FlowApprovalEvent (eventState , flowRecord , currentOperator ,flowWork ));
414+ private boolean isPassBackRecord () {
415+ return flowSourceDirection == FlowSourceDirection .PASS ;
416+ }
315417
418+ private boolean isCustomBackRecord () {
419+ return flowSourceDirection == FlowSourceDirection .REJECT && flowWork .hasBackRelation ();
420+ }
316421
317422
423+ public List <FlowRecord > createNextRecord () {
424+ this .loadCreateOperator ();
425+ List <FlowRecord > records = null ;
426+ if (isDefaultBackRecord ()) {
427+ records = this .createDefaultBackRecord ();
428+ }
429+ if (isCustomBackRecord ()) {
430+ records = this .createCustomBackRecord ();
431+ }
432+ if (isPassBackRecord ()) {
433+ records = this .createPassRecord ();
434+ }
435+ return records ;
318436 }
319437
320438
321-
322439}
0 commit comments