From 6296120dadc9088e7c2318358abf4b8bdef01ad2 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 24 三月 2026 17:02:32 +0800
Subject: [PATCH] refactor: unify station reroute execution flow

---
 src/main/java/com/zy/core/utils/StationOperateProcessUtils.java |  347 ++++++++++++++++++++++++---------------------------------
 1 files changed, 149 insertions(+), 198 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java b/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
index a75648a..c54062f 100644
--- a/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
+++ b/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
@@ -483,169 +483,25 @@
                         }
                         redisUtil.set(RedisKeyType.CHECK_STATION_RUN_BLOCK_LIMIT_.key + stationProtocol.getTaskNo(), "lock", 15);
 
-                        if (wrkMast.getIoType() == WrkIoType.IN.id && runBlockReassignLocStationList.contains(stationProtocol.getStationId())) {
-                            //绔欑偣澶勪簬閲嶆柊鍒嗛厤搴撲綅鍖哄煙
-                            int currentTaskBufferCommandCount = countCurrentTaskBufferCommands(
-                                    stationProtocol.getTaskBufferItems(),
-                                    stationProtocol.getTaskNo()
-                            );
-                            if (currentTaskBufferCommandCount > 0) {
-                                News.info("杈撻�佺珯鐐硅繍琛屽牭濉為噸鍒嗛厤宸茶烦杩囷紝缂撳瓨鍖轰粛瀛樺湪褰撳墠浠诲姟鍛戒护銆傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽綋鍓嶄换鍔″懡浠ゆ暟={}",
-                                        stationProtocol.getStationId(),
-                                        stationProtocol.getTaskNo(),
-                                        currentTaskBufferCommandCount);
-                                continue;
-                            }
-                            if (stationMoveCoordinator != null) {
-                                stationMoveCoordinator.cancelSession(wrkMast.getWrkNo());
-                            }
-                            //杩愯鍫靛锛岄噸鏂扮敵璇蜂换鍔�
-                            String response = wmsOperateUtils.applyReassignTaskLocNo(wrkMast.getWrkNo(), stationProtocol.getStationId());
-                            if (Cools.isEmpty(response)) {
-                                News.taskError(wrkMast.getWrkNo(), "璇锋眰WMS閲嶆柊鍒嗛厤搴撲綅鎺ュ彛澶辫触锛屾帴鍙f湭鍝嶅簲锛侊紒锛乺esponse锛歿}", response);
-                                continue;
-                            }
-                            JSONObject jsonObject = JSON.parseObject(response);
-                            if (jsonObject.getInteger("code").equals(200)) {
-                                StartupDto dto = jsonObject.getObject("data", StartupDto.class);
-
-                                String sourceLocNo = wrkMast.getLocNo();
-                                String locNo = dto.getLocNo();
-
-                                LocMast sourceLocMast = locMastService.queryByLoc(sourceLocNo);
-                                if (sourceLocMast == null) {
-                                    News.taskInfo(wrkMast.getWrkNo(), "搴撲綅鍙�:{} 婧愬簱浣嶄俊鎭笉瀛樺湪", sourceLocNo);
-                                    continue;
-                                }
-
-                                if (!sourceLocMast.getLocSts().equals("S")) {
-                                    News.taskInfo(wrkMast.getWrkNo(), "搴撲綅鍙�:{} 婧愬簱浣嶇姸鎬佷笉澶勪簬鍏ュ簱棰勭害", sourceLocNo);
-                                    continue;
-                                }
-
-                                LocMast locMast = locMastService.queryByLoc(locNo);
-                                if (locMast == null) {
-                                    News.taskInfo(wrkMast.getWrkNo(), "搴撲綅鍙�:{} 鐩爣搴撲綅淇℃伅涓嶅瓨鍦�", locNo);
-                                    continue;
-                                }
-
-                                if (!locMast.getLocSts().equals("O")) {
-                                    News.taskInfo(wrkMast.getWrkNo(), "搴撲綅鍙�:{} 鐩爣搴撲綅鐘舵�佷笉澶勪簬绌哄簱浣�", locNo);
-                                    continue;
-                                }
-
-                                FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
-                                if (findCrnNoResult == null) {
-                                    News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鏈尮閰嶅埌鍫嗗灈鏈�", wrkMast.getWrkNo());
-                                    continue;
-                                }
-                                Integer crnNo = findCrnNoResult.getCrnNo();
-
-                                Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationProtocol.getStationId());
-                                if (targetStationId == null) {
-                                    News.taskInfo(wrkMast.getWrkNo(), "{}绔欑偣,鎼滅储鍏ュ簱绔欑偣澶辫触", stationProtocol.getStationId());
-                                    continue;
-                                }
-
-                                StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationProtocol.getStationId(), targetStationId, 0);
-                                if (command == null) {
-                                    News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鑾峰彇杈撻�佺嚎鍛戒护澶辫触", wrkMast.getWrkNo());
-                                    continue;
-                                }
-
-                                //鏇存柊婧愬簱浣�
-                                sourceLocMast.setLocSts("O");
-                                sourceLocMast.setModiTime(new Date());
-                                locMastService.updateById(sourceLocMast);
-
-                                //鏇存柊鐩爣搴撲綅
-                                locMast.setLocSts("S");
-                                locMast.setModiTime(new Date());
-                                locMastService.updateById(locMast);
-
-                                //鏇存柊宸ヤ綔妗f暟鎹�
-                                wrkMast.setLocNo(locNo);
-                                wrkMast.setStaNo(targetStationId);
-
-                                if (findCrnNoResult.getCrnType().equals(SlaveType.Crn)) {
-                                    wrkMast.setCrnNo(crnNo);
-                                } else if (findCrnNoResult.getCrnType().equals(SlaveType.DualCrn)) {
-                                    wrkMast.setDualCrnNo(crnNo);
-                                } else {
-                                    throw new CoolException("鏈煡璁惧绫诲瀷");
-                                }
-
-                                if (wrkMastService.updateById(wrkMast)) {
-                                    boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationRunBlock_direct");
-                                    if (!offered) {
-                                        continue;
-                                    }
-                                    if (stationMoveCoordinator != null) {
-                                        stationMoveCoordinator.recordDispatch(
-                                                wrkMast.getWrkNo(),
-                                                stationProtocol.getStationId(),
-                                                "checkStationRunBlock_direct",
-                                                command,
-                                                false
-                                        );
-                                    }
-                                }
-                            } else {
-                                News.error("璇锋眰WMS鎺ュ彛澶辫触锛侊紒锛乺esponse锛歿}", response);
-                            }
-                        } else {
-                            //杩愯鍫靛锛岄噸鏂拌绠楄矾绾�
-                            Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
-                            OutOrderDispatchDecision dispatchDecision = resolveOutboundDispatchDecision(
-                                    stationProtocol.getStationId(),
-                                    wrkMast,
-                                    outOrderStationIds,
-                                    pathLenFactor
-                            );
-                            Integer moveStaNo = dispatchDecision == null ? null : dispatchDecision.getTargetStationId();
-                            if (moveStaNo == null || Objects.equals(moveStaNo, stationProtocol.getStationId())) {
-                                continue;
-                            }
-
-                            if (countCurrentTaskBufferCommands(stationProtocol.getTaskBufferItems(), stationProtocol.getTaskNo()) > 0) {
-                                continue;
-                            }
-
-                            StationCommand command = stationThread.getRunBlockRerouteCommand(
-                                    wrkMast.getWrkNo(),
-                                    stationProtocol.getStationId(),
-                                    moveStaNo,
-                                    0,
-                                    pathLenFactor
-                            );
-                            if (command == null) {
-                                News.taskInfo(wrkMast.getWrkNo(),
-                                        "杈撻�佺珯鐐瑰牭濉為噸瑙勫垝鏈壘鍒板彲涓嬪彂璺嚎锛屽綋鍓嶇珯鐐�={}锛岀洰鏍囩珯鐐�={}",
-                                        stationProtocol.getStationId(),
-                                        moveStaNo);
-                                continue;
-                            }
-
-                            if (stationMoveCoordinator != null) {
-                                stationMoveCoordinator.cancelSession(wrkMast.getWrkNo());
-                            }
-                            resetSegmentMoveCommandsBeforeReroute(wrkMast.getWrkNo());
-                            boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationRunBlock_reroute");
-                            if (!offered) {
-                                continue;
-                            }
-                            syncOutOrderWatchState(wrkMast, stationProtocol.getStationId(), outOrderStationIds, dispatchDecision, command);
-                            if (stationMoveCoordinator != null) {
-                                stationMoveCoordinator.recordDispatch(
-                                        wrkMast.getWrkNo(),
-                                        stationProtocol.getStationId(),
-                                        "checkStationRunBlock_reroute",
-                                        command,
-                                        dispatchDecision != null && dispatchDecision.isCircle()
-                                );
-                            }
-                            News.info("杈撻�佺珯鐐瑰牭濉炲悗閲嶆柊璁$畻璺緞鍛戒护涓嬪彂鎴愬姛锛岀珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}", stationProtocol.getStationId(), wrkMast.getWrkNo(), JSON.toJSONString(command));
+                        if (shouldUseRunBlockDirectReassign(wrkMast, stationProtocol.getStationId(), runBlockReassignLocStationList)) {
+                            executeRunBlockDirectReassign(basDevp, stationThread, stationProtocol, wrkMast);
+                            continue;
                         }
+
+                        Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
+                        RerouteContext context = RerouteContext.create(
+                                RerouteSceneType.RUN_BLOCK_REROUTE,
+                                basDevp,
+                                stationThread,
+                                stationProtocol,
+                                wrkMast,
+                                outOrderStationIds,
+                                pathLenFactor,
+                                "checkStationRunBlock_reroute"
+                        ).withRunBlockCommand()
+                                .withCancelSessionBeforeDispatch()
+                                .withResetSegmentCommandsBeforeDispatch();
+                        executeSharedReroute(context);
                     }
                 }
             }
@@ -878,6 +734,19 @@
                 context.pathLenFactor()
         );
         if (command == null) {
+            if (context.sceneType() == RerouteSceneType.RUN_BLOCK_REROUTE) {
+                News.taskInfo(context.wrkMast().getWrkNo(),
+                        "杈撻�佺珯鐐瑰牭濉為噸瑙勫垝鏈壘鍒板彲涓嬪彂璺嚎锛屽綋鍓嶇珯鐐�={}锛岀洰鏍囩珯鐐�={}",
+                        currentStationId,
+                        targetStationId);
+            } else if (context.sceneType() == RerouteSceneType.IDLE_RECOVER) {
+                News.taskInfo(context.wrkMast().getWrkNo(),
+                        "绔欑偣浠诲姟鍋滅暀瓒呮椂鍚庨噸绠楄矾寰勫け璐ワ紝褰撳墠绔欑偣={}锛岀洰鏍囩珯鐐�={}",
+                        currentStationId,
+                        targetStationId);
+            } else {
+                News.taskInfo(context.wrkMast().getWrkNo(), "鑾峰彇杈撻�佺嚎鍛戒护澶辫触");
+            }
             return RerouteCommandPlan.skip("missing-command");
         }
         return RerouteCommandPlan.dispatch(command, decision, context.dispatchScene());
@@ -908,6 +777,12 @@
             return RerouteExecutionResult.skip("recent-dispatch");
         }
         if (countCurrentTaskBufferCommands(stationProtocol.getTaskBufferItems(), taskNo) > 0) {
+            if (context.sceneType() == RerouteSceneType.IDLE_RECOVER) {
+                News.info("杈撻�佺珯鐐逛换鍔″仠鐣欒秴鏃讹紝浣嗙紦瀛樺尯浠嶅瓨鍦ㄥ綋鍓嶄换鍔″懡浠わ紝宸茶烦杩囬噸绠椼�傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽綋鍓嶄换鍔″懡浠ゆ暟={}",
+                        stationId,
+                        taskNo,
+                        countCurrentTaskBufferCommands(stationProtocol.getTaskBufferItems(), taskNo));
+            }
             return RerouteExecutionResult.skip("buffer-has-current-task");
         }
         if (context.checkSuppressDispatch()
@@ -1571,67 +1446,143 @@
         if (lock != null) {
             return;
         }
+        Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
+        RerouteContext context = RerouteContext.create(
+                RerouteSceneType.IDLE_RECOVER,
+                basDevp,
+                stationThread,
+                stationProtocol,
+                wrkMast,
+                outOrderList,
+                pathLenFactor,
+                "checkStationIdleRecover"
+        ).withCancelSessionBeforeDispatch()
+                .withExecutionLock(RedisKeyType.CHECK_STATION_IDLE_RECOVER_LIMIT_.key + stationProtocol.getTaskNo(), STATION_IDLE_RECOVER_LIMIT_SECONDS)
+                .withResetSegmentCommandsBeforeDispatch()
+                .clearIdleIssuedCommands(idleTrack);
+        executeSharedReroute(context);
+    }
+
+    boolean shouldUseRunBlockDirectReassign(WrkMast wrkMast,
+                                            Integer stationId,
+                                            List<Integer> runBlockReassignLocStationList) {
+        return wrkMast != null
+                && Objects.equals(wrkMast.getIoType(), WrkIoType.IN.id)
+                && stationId != null
+                && runBlockReassignLocStationList != null
+                && runBlockReassignLocStationList.contains(stationId);
+    }
+
+    private void executeRunBlockDirectReassign(BasDevp basDevp,
+                                               StationThread stationThread,
+                                               StationProtocol stationProtocol,
+                                               WrkMast wrkMast) {
+        if (basDevp == null || stationThread == null || stationProtocol == null || wrkMast == null) {
+            return;
+        }
         int currentTaskBufferCommandCount = countCurrentTaskBufferCommands(
                 stationProtocol.getTaskBufferItems(),
                 stationProtocol.getTaskNo()
         );
         if (currentTaskBufferCommandCount > 0) {
-            News.info("杈撻�佺珯鐐逛换鍔″仠鐣欒秴鏃讹紝浣嗙紦瀛樺尯浠嶅瓨鍦ㄥ綋鍓嶄换鍔″懡浠わ紝宸茶烦杩囬噸绠椼�傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽綋鍓嶄换鍔″懡浠ゆ暟={}",
+            News.info("杈撻�佺珯鐐硅繍琛屽牭濉為噸鍒嗛厤宸茶烦杩囷紝缂撳瓨鍖轰粛瀛樺湪褰撳墠浠诲姟鍛戒护銆傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽綋鍓嶄换鍔″懡浠ゆ暟={}",
                     stationProtocol.getStationId(),
                     stationProtocol.getTaskNo(),
                     currentTaskBufferCommandCount);
             return;
         }
-
-        Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast);
-        OutOrderDispatchDecision dispatchDecision = null;
-        Integer moveStaNo;
-        if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)) {
-            dispatchDecision = resolveOutboundDispatchDecision(stationProtocol.getStationId(), wrkMast, outOrderList, pathLenFactor);
-            moveStaNo = dispatchDecision == null ? null : dispatchDecision.getTargetStationId();
-        } else {
-            moveStaNo = wrkMast.getStaNo();
-        }
-        if (moveStaNo == null || Objects.equals(moveStaNo, stationProtocol.getStationId())) {
-            return;
-        }
-
         if (stationMoveCoordinator != null) {
-            stationMoveCoordinator.cancelSession(stationProtocol.getTaskNo());
+            stationMoveCoordinator.cancelSession(wrkMast.getWrkNo());
         }
-        redisUtil.set(RedisKeyType.CHECK_STATION_IDLE_RECOVER_LIMIT_.key + stationProtocol.getTaskNo(), "lock", STATION_IDLE_RECOVER_LIMIT_SECONDS);
-        resetSegmentMoveCommandsBeforeReroute(stationProtocol.getTaskNo());
-        int clearedCommandCount = clearIssuedMoveCommandsDuringIdleStay(idleTrack, stationProtocol.getTaskNo(), stationProtocol.getStationId());
-
-        StationCommand command = buildOutboundMoveCommand(
-                stationThread,
-                wrkMast,
-                stationProtocol.getStationId(),
-                moveStaNo,
-                pathLenFactor
-        );
-        if (command == null) {
-            News.taskInfo(wrkMast.getWrkNo(), "绔欑偣浠诲姟鍋滅暀瓒呮椂鍚庨噸绠楄矾寰勫け璐ワ紝褰撳墠绔欑偣={}锛岀洰鏍囩珯鐐�={}", stationProtocol.getStationId(), moveStaNo);
+        String response = wmsOperateUtils.applyReassignTaskLocNo(wrkMast.getWrkNo(), stationProtocol.getStationId());
+        if (Cools.isEmpty(response)) {
+            News.taskError(wrkMast.getWrkNo(), "璇锋眰WMS閲嶆柊鍒嗛厤搴撲綅鎺ュ彛澶辫触锛屾帴鍙f湭鍝嶅簲锛侊紒锛乺esponse锛歿}", response);
+            return;
+        }
+        JSONObject jsonObject = JSON.parseObject(response);
+        if (!jsonObject.getInteger("code").equals(200)) {
+            News.error("璇锋眰WMS鎺ュ彛澶辫触锛侊紒锛乺esponse锛歿}", response);
             return;
         }
 
-        boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationIdleRecover");
+        StartupDto dto = jsonObject.getObject("data", StartupDto.class);
+        String sourceLocNo = wrkMast.getLocNo();
+        String locNo = dto.getLocNo();
+
+        LocMast sourceLocMast = locMastService.queryByLoc(sourceLocNo);
+        if (sourceLocMast == null) {
+            News.taskInfo(wrkMast.getWrkNo(), "搴撲綅鍙�:{} 婧愬簱浣嶄俊鎭笉瀛樺湪", sourceLocNo);
+            return;
+        }
+        if (!sourceLocMast.getLocSts().equals("S")) {
+            News.taskInfo(wrkMast.getWrkNo(), "搴撲綅鍙�:{} 婧愬簱浣嶇姸鎬佷笉澶勪簬鍏ュ簱棰勭害", sourceLocNo);
+            return;
+        }
+
+        LocMast locMast = locMastService.queryByLoc(locNo);
+        if (locMast == null) {
+            News.taskInfo(wrkMast.getWrkNo(), "搴撲綅鍙�:{} 鐩爣搴撲綅淇℃伅涓嶅瓨鍦�", locNo);
+            return;
+        }
+        if (!locMast.getLocSts().equals("O")) {
+            News.taskInfo(wrkMast.getWrkNo(), "搴撲綅鍙�:{} 鐩爣搴撲綅鐘舵�佷笉澶勪簬绌哄簱浣�", locNo);
+            return;
+        }
+
+        FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
+        if (findCrnNoResult == null) {
+            News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鏈尮閰嶅埌鍫嗗灈鏈�", wrkMast.getWrkNo());
+            return;
+        }
+        Integer crnNo = findCrnNoResult.getCrnNo();
+
+        Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationProtocol.getStationId());
+        if (targetStationId == null) {
+            News.taskInfo(wrkMast.getWrkNo(), "{}绔欑偣,鎼滅储鍏ュ簱绔欑偣澶辫触", stationProtocol.getStationId());
+            return;
+        }
+
+        StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationProtocol.getStationId(), targetStationId, 0);
+        if (command == null) {
+            News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鑾峰彇杈撻�佺嚎鍛戒护澶辫触", wrkMast.getWrkNo());
+            return;
+        }
+
+        sourceLocMast.setLocSts("O");
+        sourceLocMast.setModiTime(new Date());
+        locMastService.updateById(sourceLocMast);
+
+        locMast.setLocSts("S");
+        locMast.setModiTime(new Date());
+        locMastService.updateById(locMast);
+
+        wrkMast.setLocNo(locNo);
+        wrkMast.setStaNo(targetStationId);
+
+        if (findCrnNoResult.getCrnType().equals(SlaveType.Crn)) {
+            wrkMast.setCrnNo(crnNo);
+        } else if (findCrnNoResult.getCrnType().equals(SlaveType.DualCrn)) {
+            wrkMast.setDualCrnNo(crnNo);
+        } else {
+            throw new CoolException("鏈煡璁惧绫诲瀷");
+        }
+
+        if (!wrkMastService.updateById(wrkMast)) {
+            return;
+        }
+        boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationRunBlock_direct");
         if (!offered) {
             return;
         }
-        syncOutOrderWatchState(wrkMast, stationProtocol.getStationId(), outOrderList, dispatchDecision, command);
         if (stationMoveCoordinator != null) {
             stationMoveCoordinator.recordDispatch(
                     wrkMast.getWrkNo(),
                     stationProtocol.getStationId(),
-                    "checkStationIdleRecover",
+                    "checkStationRunBlock_direct",
                     command,
-                    dispatchDecision != null && dispatchDecision.isCircle()
+                    false
             );
         }
-        saveStationTaskIdleTrack(new StationTaskIdleTrack(wrkMast.getWrkNo(), stationProtocol.getStationId(), System.currentTimeMillis()));
-        News.info("杈撻�佺珯鐐逛换鍔″仠鐣檣}绉掓湭杩愯锛屽凡閲嶆柊璁$畻璺緞骞堕噸鍚繍琛岋紝绔欑偣鍙�={}锛岀洰鏍囩珯={}锛屽伐浣滃彿={}锛屾竻鐞嗘棫鍒嗘鍛戒护鏁�={}锛屽懡浠ゆ暟鎹�={}",
-                STATION_IDLE_RECOVER_SECONDS, stationProtocol.getStationId(), moveStaNo, wrkMast.getWrkNo(), clearedCommandCount, JSON.toJSONString(command));
     }
 
     private boolean canRecoverIdleStationTask(WrkMast wrkMast, Integer currentStationId) {

--
Gitblit v1.9.1