From 71a5ae03389119dc6975d7cfb87e63601f3c5305 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期四, 02 四月 2026 16:52:22 +0800
Subject: [PATCH] #算法优化

---
 src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java |  310 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 169 insertions(+), 141 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java b/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java
index f55f553..e33d401 100644
--- a/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java
+++ b/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java
@@ -65,100 +65,11 @@
 
     public void stationInExecute() {
         try {
-            DispatchLimitConfig baseLimitConfig = stationDispatchLoadSupport.getDispatchLimitConfig(null, null);
-            int[] currentStationTaskCountRef = new int[]{stationDispatchLoadSupport.countCurrentStationTask()};
-            LoadGuardState loadGuardState = stationDispatchLoadSupport.buildLoadGuardState(baseLimitConfig);
-
             List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
             for (BasDevp basDevp : basDevps) {
-                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
-                if (stationThread == null) {
-                    continue;
-                }
-
-                Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
                 List<StationObjModel> stationList = basDevp.getBarcodeStationList$();
                 for (StationObjModel entity : stationList) {
-                    Integer stationId = entity.getStationId();
-                    if (!stationMap.containsKey(stationId)) {
-                        continue;
-                    }
-
-                    StationProtocol stationProtocol = stationMap.get(stationId);
-                    if (stationProtocol == null) {
-                        continue;
-                    }
-
-                    Object lock = redisUtil.get(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId);
-                    if (lock != null) {
-                        continue;
-                    }
-
-                    if (!stationProtocol.isAutoing()
-                            || !stationProtocol.isLoading()
-                            || stationProtocol.getTaskNo() <= 0) {
-                        continue;
-                    }
-
-                    WrkMast wrkMast = wrkMastService.getOne(new QueryWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
-                    if (wrkMast == null || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_INBOUND.sts)) {
-                        continue;
-                    }
-
-                    String locNo = wrkMast.getLocNo();
-                    FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
-                    if (findCrnNoResult == null) {
-                        News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鏈尮閰嶅埌鍫嗗灈鏈�", wrkMast.getWrkNo());
-                        continue;
-                    }
-
-                    Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationId);
-                    if (targetStationId == null) {
-                        News.taskInfo(wrkMast.getWrkNo(), "{}绔欑偣,鎼滅储鍏ュ簱绔欑偣澶辫触", stationId);
-                        continue;
-                    }
-
-                    DispatchLimitConfig limitConfig = stationDispatchLoadSupport.getDispatchLimitConfig(stationProtocol.getStationId(), targetStationId);
-                    LoopHitResult loopHitResult = stationDispatchLoadSupport.findPathLoopHit(
-                            limitConfig,
-                            stationProtocol.getStationId(),
-                            targetStationId,
-                            loadGuardState
-                    );
-                    if (stationDispatchLoadSupport.isDispatchBlocked(limitConfig, currentStationTaskCountRef[0], loadGuardState, loopHitResult.isThroughLoop())) {
-                        continue;
-                    }
-
-                    StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationId, targetStationId, 0);
-                    if (command == null) {
-                        News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鑾峰彇杈撻�佺嚎鍛戒护澶辫触", wrkMast.getWrkNo());
-                        continue;
-                    }
-
-                    Date now = new Date();
-                    wrkMast.setWrkSts(WrkStsType.INBOUND_STATION_RUN.sts);
-                    wrkMast.setSourceStaNo(stationProtocol.getStationId());
-                    wrkMast.setStaNo(targetStationId);
-                    wrkMast.setSystemMsg("");
-                    wrkMast.setIoTime(now);
-                    wrkMast.setModiTime(now);
-                    if (wrkMastService.updateById(wrkMast)) {
-                        wrkAnalysisService.markInboundStationStart(wrkMast, now);
-                        boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "stationInExecute");
-                        if (offered && stationMoveCoordinator != null) {
-                            stationMoveCoordinator.recordDispatch(
-                                    wrkMast.getWrkNo(),
-                                    stationProtocol.getStationId(),
-                                    "stationInExecute",
-                                    command,
-                                    false
-                            );
-                        }
-                        News.info("杈撻�佺珯鐐瑰叆搴撳懡浠や笅鍙戞垚鍔燂紝绔欑偣鍙�={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}", stationId, wrkMast.getWrkNo(), JSON.toJSONString(command));
-                        redisUtil.set(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId, "lock", 5);
-                        loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
-                        stationDispatchLoadSupport.saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
-                    }
+                    stationInExecute(basDevp, entity);
                 }
             }
         } catch (Exception e) {
@@ -170,36 +81,47 @@
         try {
             List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN.sts));
             for (WrkMast wrkMast : wrkMasts) {
-                Integer wrkNo = wrkMast.getWrkNo();
-                Integer targetStaNo = wrkMast.getStaNo();
-                if (wrkNo == null || targetStaNo == null) {
-                    continue;
-                }
+                stationOutExecuteFinish(wrkMast);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
-                boolean complete = false;
-                Integer targetDeviceNo = null;
-                StationThread stationThread = null;
-                BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
-                if (basStation != null) {
-                    targetDeviceNo = basStation.getDeviceNo();
-                    stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
-                    if (stationThread != null) {
-                        Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
-                        StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(basStation.getStationId());
-                        boolean arrived = stationProtocol != null && wrkNo.equals(stationProtocol.getTaskNo());
-                        if (arrived || stationThread.hasRecentArrival(basStation.getStationId(), wrkNo)) {
-                            complete = true;
-                            if (!arrived) {
-                                News.info("杈撻�佺珯鐐瑰嚭搴撳埌杈惧垽瀹氫娇鐢ㄦ渶杩戝埌绔欒ˉ鍋匡紝宸ヤ綔鍙�={}锛岀洰鏍囩珯={}", wrkNo, targetStaNo);
-                            }
+    public void stationOutExecuteFinish(WrkMast wrkMast) {
+        try {
+            if (wrkMast == null) {
+                return;
+            }
+            Integer wrkNo = wrkMast.getWrkNo();
+            Integer targetStaNo = wrkMast.getStaNo();
+            if (wrkNo == null || targetStaNo == null) {
+                return;
+            }
+
+            boolean complete = false;
+            Integer targetDeviceNo = null;
+            StationThread stationThread = null;
+            BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
+            if (basStation != null) {
+                targetDeviceNo = basStation.getDeviceNo();
+                stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
+                if (stationThread != null) {
+                    Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
+                    StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(basStation.getStationId());
+                    boolean arrived = stationProtocol != null && wrkNo.equals(stationProtocol.getTaskNo());
+                    if (arrived || stationThread.hasRecentArrival(basStation.getStationId(), wrkNo)) {
+                        complete = true;
+                        if (!arrived) {
+                            News.info("杈撻�佺珯鐐瑰嚭搴撳埌杈惧垽瀹氫娇鐢ㄦ渶杩戝埌绔欒ˉ鍋匡紝宸ヤ綔鍙�={}锛岀洰鏍囩珯={}", wrkNo, targetStaNo);
                         }
                     }
                 }
+            }
 
-                if (complete) {
-                    attemptClearTaskPath(stationThread, wrkNo);
-                    completeStationRunTask(wrkMast, targetDeviceNo);
-                }
+            if (complete) {
+                attemptClearTaskPath(stationThread, wrkNo);
+                completeStationRunTask(wrkMast, targetDeviceNo);
             }
         } catch (Exception e) {
             e.printStackTrace();
@@ -210,38 +132,49 @@
         try {
             List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts));
             for (WrkMast wrkMast : wrkMasts) {
-                Integer wrkNo = wrkMast.getWrkNo();
-                Integer targetStaNo = wrkMast.getStaNo();
+                checkTaskToComplete(wrkMast);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
-                Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkNo);
-                if (lock != null) {
-                    continue;
-                }
+    public void checkTaskToComplete(WrkMast wrkMast) {
+        try {
+            if (wrkMast == null || wrkMast.getWrkNo() == null || wrkMast.getStaNo() == null) {
+                return;
+            }
+            Integer wrkNo = wrkMast.getWrkNo();
+            Integer targetStaNo = wrkMast.getStaNo();
 
-                BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
-                if (basStation == null) {
-                    continue;
-                }
+            Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkNo);
+            if (lock != null) {
+                return;
+            }
 
-                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
-                if (stationThread == null) {
-                    continue;
-                }
+            BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
+            if (basStation == null) {
+                return;
+            }
 
-                Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
-                StationProtocol stationProtocol = statusMap.get(basStation.getStationId());
-                if (stationProtocol == null) {
-                    continue;
-                }
+            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
+            if (stationThread == null) {
+                return;
+            }
 
-                if (!Objects.equals(stationProtocol.getTaskNo(), wrkNo)) {
-                    if (stationMoveCoordinator != null) {
-                        stationMoveCoordinator.finishSession(wrkNo);
-                    }
-                    wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
-                    wrkMast.setIoTime(new Date());
-                    wrkMastService.updateById(wrkMast);
+            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
+            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(basStation.getStationId());
+            if (stationProtocol == null) {
+                return;
+            }
+
+            if (!Objects.equals(stationProtocol.getTaskNo(), wrkNo)) {
+                if (stationMoveCoordinator != null) {
+                    stationMoveCoordinator.finishSession(wrkNo);
                 }
+                wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
+                wrkMast.setIoTime(new Date());
+                wrkMastService.updateById(wrkMast);
             }
         } catch (Exception e) {
             e.printStackTrace();
@@ -285,4 +218,99 @@
         StationCommandDispatchResult dispatchResult = stationCommandDispatcher.dispatch(deviceNo, command, "station-operate-process", scene);
         return dispatchResult.isAccepted();
     }
+
+    public void stationInExecute(BasDevp basDevp, StationObjModel entity) {
+        if (basDevp == null || basDevp.getDevpNo() == null || entity == null || entity.getStationId() == null) {
+            return;
+        }
+
+        Integer stationId = entity.getStationId();
+        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
+        if (stationThread == null) {
+            return;
+        }
+
+        Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
+        if (stationMap == null || !stationMap.containsKey(stationId)) {
+            return;
+        }
+
+        StationProtocol stationProtocol = stationMap.get(stationId);
+        if (stationProtocol == null) {
+            return;
+        }
+
+        Object lock = redisUtil.get(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId);
+        if (lock != null) {
+            return;
+        }
+
+        if (!stationProtocol.isAutoing()
+                || !stationProtocol.isLoading()
+                || stationProtocol.getTaskNo() <= 0) {
+            return;
+        }
+
+        WrkMast wrkMast = wrkMastService.getOne(new QueryWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
+        if (wrkMast == null || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_INBOUND.sts)) {
+            return;
+        }
+
+        String locNo = wrkMast.getLocNo();
+        FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
+        if (findCrnNoResult == null) {
+            News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鏈尮閰嶅埌鍫嗗灈鏈�", wrkMast.getWrkNo());
+            return;
+        }
+
+        Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationId);
+        if (targetStationId == null) {
+            News.taskInfo(wrkMast.getWrkNo(), "{}绔欑偣,鎼滅储鍏ュ簱绔欑偣澶辫触", stationId);
+            return;
+        }
+
+        DispatchLimitConfig limitConfig = stationDispatchLoadSupport.getDispatchLimitConfig(stationProtocol.getStationId(), targetStationId);
+        int currentStationTaskCount = stationDispatchLoadSupport.countCurrentStationTask();
+        LoadGuardState loadGuardState = stationDispatchLoadSupport.buildLoadGuardState(limitConfig);
+        LoopHitResult loopHitResult = stationDispatchLoadSupport.findPathLoopHit(
+                limitConfig,
+                stationProtocol.getStationId(),
+                targetStationId,
+                loadGuardState
+        );
+        if (stationDispatchLoadSupport.isDispatchBlocked(limitConfig, currentStationTaskCount, loadGuardState, loopHitResult.isThroughLoop())) {
+            return;
+        }
+
+        StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationId, targetStationId, 0);
+        if (command == null) {
+            News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鑾峰彇杈撻�佺嚎鍛戒护澶辫触", wrkMast.getWrkNo());
+            return;
+        }
+
+        Date now = new Date();
+        wrkMast.setWrkSts(WrkStsType.INBOUND_STATION_RUN.sts);
+        wrkMast.setSourceStaNo(stationProtocol.getStationId());
+        wrkMast.setStaNo(targetStationId);
+        wrkMast.setSystemMsg("");
+        wrkMast.setIoTime(now);
+        wrkMast.setModiTime(now);
+        if (wrkMastService.updateById(wrkMast)) {
+            wrkAnalysisService.markInboundStationStart(wrkMast, now);
+            boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "stationInExecute");
+            if (offered && stationMoveCoordinator != null) {
+                stationMoveCoordinator.recordDispatch(
+                        wrkMast.getWrkNo(),
+                        stationProtocol.getStationId(),
+                        "stationInExecute",
+                        command,
+                        false
+                );
+            }
+            News.info("杈撻�佺珯鐐瑰叆搴撳懡浠や笅鍙戞垚鍔燂紝绔欑偣鍙�={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}", stationId, wrkMast.getWrkNo(), JSON.toJSONString(command));
+            redisUtil.set(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId, "lock", 5);
+            loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
+            stationDispatchLoadSupport.saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
+        }
+    }
 }

--
Gitblit v1.9.1