From 03c3ae747f82ad22c761c79e7b1c0e0031c57d41 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 06 四月 2026 20:28:35 +0800
Subject: [PATCH] #出库站点命令下发

---
 src/main/java/com/zy/core/utils/station/StationOutboundDispatchProcessor.java |  323 ++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 208 insertions(+), 115 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/station/StationOutboundDispatchProcessor.java b/src/main/java/com/zy/core/utils/station/StationOutboundDispatchProcessor.java
index 644090a..fcb5b3c 100644
--- a/src/main/java/com/zy/core/utils/station/StationOutboundDispatchProcessor.java
+++ b/src/main/java/com/zy/core/utils/station/StationOutboundDispatchProcessor.java
@@ -2,8 +2,10 @@
 
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.zy.asrs.entity.BasStation;
 import com.zy.asrs.domain.enums.NotifyMsgType;
 import com.zy.asrs.entity.WrkMast;
+import com.zy.asrs.service.BasStationService;
 import com.zy.asrs.service.WrkAnalysisService;
 import com.zy.asrs.service.WrkMastService;
 import com.zy.asrs.utils.NotifyUtils;
@@ -30,12 +32,17 @@
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 @Component
 public class StationOutboundDispatchProcessor {
 
+    private static final int PENDING_DISPATCH_EXPIRE_SECONDS = 60 * 10;
+
     @Autowired
     private WrkMastService wrkMastService;
+    @Autowired
+    private BasStationService basStationService;
     @Autowired
     private WrkAnalysisService wrkAnalysisService;
     @Autowired
@@ -53,115 +60,171 @@
 
     public void crnStationOutExecute() {
         try {
-            DispatchLimitConfig baseLimitConfig =
-                    stationDispatchLoadSupport.getDispatchLimitConfig(null, null);
-            int[] currentStationTaskCountRef = new int[]{stationDispatchLoadSupport.countCurrentStationTask()};
-            LoadGuardState loadGuardState =
-                    stationDispatchLoadSupport.buildLoadGuardState(baseLimitConfig);
-
             List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>()
                     .eq("wrk_sts", WrkStsType.OUTBOUND_RUN_COMPLETE.sts)
                     .isNotNull("crn_no"));
-            List<Integer> outOrderList = stationOutboundDecisionSupport.getAllOutOrderList();
-
             for (WrkMast wrkMast : wrkMasts) {
-                Object infoObj = redisUtil.get(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo());
-                if (infoObj == null) {
-                    News.info("鍑哄簱浠诲姟{}鏁版嵁缂撳瓨涓嶅瓨鍦�", wrkMast.getWrkNo());
-                    continue;
+                crnStationOutExecute(wrkMast);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void crnStationOutExecute(WrkMast wrkMast) {
+        try {
+            if (wrkMast == null || wrkMast.getWrkNo() == null) {
+                return;
+            }
+            Object pendingObj = redisUtil.get(RedisKeyType.STATION_OUT_PENDING_DISPATCH_.key + wrkMast.getWrkNo());
+            if (pendingObj != null) {
+                if (!Objects.equals(wrkMast.getWrkSts(), WrkStsType.OUTBOUND_RUN_COMPLETE.sts)) {
+                    clearPendingDispatch(wrkMast.getWrkNo());
+                    return;
                 }
 
-                StationObjModel stationObjModel = JSON.parseObject(infoObj.toString(), StationObjModel.class);
-                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
-                if (stationThread == null) {
-                    continue;
+                StationObjModel pendingStationObjModel = getOutboundSourceStation(wrkMast);
+                if (pendingStationObjModel == null
+                        || pendingStationObjModel.getDeviceNo() == null
+                        || pendingStationObjModel.getStationId() == null) {
+                    clearPendingDispatch(wrkMast.getWrkNo());
+                    return;
                 }
 
-                Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
-                StationProtocol stationProtocol = stationMap.get(stationObjModel.getStationId());
-                if (stationProtocol == null) {
-                    continue;
-                }
-
-                Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId());
-                if (lock != null) {
-                    continue;
-                }
-
-                if (stationProtocol.isAutoing()
-                        && stationProtocol.isLoading()
-                        && stationProtocol.getTaskNo() == 0) {
-                    Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
-                    OutOrderDispatchDecision dispatchDecision =
-                            stationOutboundDecisionSupport.resolveOutboundDispatchDecision(
-                                    stationProtocol.getStationId(),
-                                    wrkMast,
-                                    outOrderList,
-                                    pathLenFactor
-                            );
-                    Integer moveStaNo = dispatchDecision == null ? null : dispatchDecision.getTargetStationId();
-                    if (moveStaNo == null) {
-                        continue;
-                    }
-
-                    DispatchLimitConfig limitConfig =
-                            stationDispatchLoadSupport.getDispatchLimitConfig(stationProtocol.getStationId(), moveStaNo);
-                    LoopHitResult loopHitResult =
-                            stationDispatchLoadSupport.findPathLoopHit(
-                                    limitConfig,
-                                    stationProtocol.getStationId(),
-                                    moveStaNo,
-                                    loadGuardState,
-                                    wrkMast,
-                                    pathLenFactor
-                            );
-                    if (stationDispatchLoadSupport.isDispatchBlocked(
-                            limitConfig,
-                            currentStationTaskCountRef[0],
-                            loadGuardState,
-                            loopHitResult.isThroughLoop())) {
+                StationThread pendingStationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, pendingStationObjModel.getDeviceNo());
+                if (pendingStationThread != null) {
+                    List<Integer> taskNoList = pendingStationThread.getAllTaskNoList();
+                    if (taskNoList != null && taskNoList.contains(wrkMast.getWrkNo())) {
+                        Date now = new Date();
+                        wrkMast.setWrkSts(WrkStsType.STATION_RUN.sts);
+                        wrkMast.setSystemMsg("");
+                        wrkMast.setIoTime(now);
+                        wrkMast.setModiTime(now);
+                        if (wrkMastService.updateById(wrkMast)) {
+                            wrkAnalysisService.markOutboundStationStart(wrkMast, now);
+                            notifyUtils.notify(String.valueOf(SlaveType.Devp), pendingStationObjModel.getDeviceNo(),
+                                    String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(),
+                                    NotifyMsgType.STATION_OUT_TASK_RUN, null);
+                            clearPendingDispatch(wrkMast.getWrkNo());
+                            News.info("杈撻�佽澶囧凡鍙戠幇浠诲姟鍙凤紝浠诲姟杞繍琛屼腑銆俤eviceNo={}锛屾簮绔�={}锛屽伐浣滃彿={}",
+                                    pendingStationObjModel.getDeviceNo(), pendingStationObjModel.getStationId(), wrkMast.getWrkNo());
+                        }
                         return;
                     }
+                }
 
-                    StationCommand command = stationOutboundDecisionSupport.buildOutboundMoveCommand(
-                            stationThread,
-                            wrkMast,
+                long createdAt;
+                try {
+                    createdAt = Long.parseLong(String.valueOf(pendingObj));
+                } catch (Exception ignore) {
+                    createdAt = System.currentTimeMillis();
+                }
+                if (System.currentTimeMillis() - createdAt < 15_000L) {
+                    return;
+                }
+                clearPendingDispatch(wrkMast.getWrkNo());
+                News.warn("杈撻�佺珯鐐规墽琛岃秴鏃讹紝宸查噴鏀鹃噸璇曡祫鏍笺�傚伐浣滃彿={}", wrkMast.getWrkNo());
+            }
+
+            Object infoObj = redisUtil.get(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo());
+            if (infoObj == null) {
+                News.info("鍑哄簱浠诲姟{}鏁版嵁缂撳瓨涓嶅瓨鍦�", wrkMast.getWrkNo());
+                return;
+            }
+
+            StationObjModel stationObjModel = JSON.parseObject(infoObj.toString(), StationObjModel.class);
+            if (stationObjModel == null || stationObjModel.getDeviceNo() == null || stationObjModel.getStationId() == null) {
+                return;
+            }
+
+            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
+            if (stationThread == null) {
+                return;
+            }
+
+            Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
+            StationProtocol stationProtocol = stationMap == null ? null : stationMap.get(stationObjModel.getStationId());
+            if (stationProtocol == null) {
+                return;
+            }
+
+            Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId());
+            if (lock != null) {
+                return;
+            }
+
+            if (!(stationProtocol.isAutoing()
+                    && stationProtocol.isLoading()
+                    && stationProtocol.getTaskNo() == 0)) {
+                return;
+            }
+
+            Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
+            List<Integer> outOrderList = stationOutboundDecisionSupport.getAllOutOrderList();
+            OutOrderDispatchDecision dispatchDecision =
+                    stationOutboundDecisionSupport.resolveOutboundDispatchDecision(
                             stationProtocol.getStationId(),
-                            moveStaNo,
+                            wrkMast,
+                            outOrderList,
                             pathLenFactor
                     );
-                    if (command == null) {
-                        News.taskInfo(wrkMast.getWrkNo(), "鑾峰彇杈撻�佺嚎鍛戒护澶辫触");
-                        continue;
-                    }
-
-                    Date now = new Date();
-                    wrkMast.setWrkSts(WrkStsType.STATION_RUN.sts);
-                    wrkMast.setSystemMsg("");
-                    wrkMast.setIoTime(now);
-                    wrkMast.setModiTime(now);
-                    if (wrkMastService.updateById(wrkMast)) {
-                        wrkAnalysisService.markOutboundStationStart(wrkMast, now);
-                        boolean offered = offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "crnStationOutExecute");
-                        if (offered && stationMoveCoordinator != null) {
-                            stationMoveCoordinator.recordDispatch(
-                                    wrkMast.getWrkNo(),
-                                    stationProtocol.getStationId(),
-                                    "crnStationOutExecute",
-                                    command,
-                                    false
-                            );
-                        }
-                        News.info("杈撻�佺珯鐐瑰嚭搴撳懡浠や笅鍙戞垚鍔燂紝绔欑偣鍙�={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}",
-                                stationProtocol.getStationId(), wrkMast.getWrkNo(), JSON.toJSONString(command));
-                        redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId(), "lock", 5);
-                        redisUtil.del(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo());
-                        currentStationTaskCountRef[0]++;
-                        loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
-                        stationDispatchLoadSupport.saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
-                    }
-                }
+            Integer moveStaNo = dispatchDecision == null ? null : dispatchDecision.getTargetStationId();
+            if (moveStaNo == null) {
+                return;
             }
+
+            DispatchLimitConfig limitConfig =
+                    stationDispatchLoadSupport.getDispatchLimitConfig(stationProtocol.getStationId(), moveStaNo);
+            int currentStationTaskCount = stationDispatchLoadSupport.countCurrentStationTask();
+            LoadGuardState loadGuardState = stationDispatchLoadSupport.buildLoadGuardState(limitConfig);
+            LoopHitResult loopHitResult =
+                    stationDispatchLoadSupport.findPathLoopHit(
+                            limitConfig,
+                            stationProtocol.getStationId(),
+                            moveStaNo,
+                            loadGuardState,
+                            wrkMast,
+                            pathLenFactor
+                    );
+            if (stationDispatchLoadSupport.isDispatchBlocked(
+                    limitConfig,
+                    currentStationTaskCount,
+                    loadGuardState,
+                    loopHitResult.isThroughLoop())) {
+                return;
+            }
+
+            StationCommand command = stationOutboundDecisionSupport.buildOutboundMoveCommand(
+                    stationThread,
+                    wrkMast,
+                    stationProtocol.getStationId(),
+                    moveStaNo,
+                    pathLenFactor
+            );
+            if (command == null) {
+                News.taskInfo(wrkMast.getWrkNo(), "鑾峰彇杈撻�佺嚎鍛戒护澶辫触");
+                return;
+            }
+
+            boolean offered = offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "crnStationOutExecute");
+            if (!offered) {
+                return;
+            }
+            if (stationMoveCoordinator != null) {
+                stationMoveCoordinator.recordDispatch(
+                        wrkMast.getWrkNo(),
+                        stationProtocol.getStationId(),
+                        "crnStationOutExecute",
+                        command,
+                        false
+                );
+            }
+            markPendingDispatch(wrkMast.getWrkNo());
+            News.info("杈撻�佺珯鐐瑰嚭搴撳懡浠ゅ凡鍏ヨ澶囨墽琛岄摼璺紝绛夊緟婧愮珯鎵ц銆傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}",
+                    stationProtocol.getStationId(), wrkMast.getWrkNo(), JSON.toJSONString(command));
+            redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId(), "lock", 5);
+            loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
+            stationDispatchLoadSupport.saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -173,6 +236,9 @@
                     .eq("wrk_sts", WrkStsType.OUTBOUND_RUN_COMPLETE.sts)
                     .isNotNull("dual_crn_no"));
             for (WrkMast wrkMast : wrkMasts) {
+                if (hasPendingDispatch(wrkMast.getWrkNo())) {
+                    continue;
+                }
                 Object infoObj = redisUtil.get(RedisKeyType.DUAL_CRN_OUT_TASK_STATION_INFO.key + wrkMast.getWrkNo());
                 if (infoObj == null) {
                     News.info("鍑哄簱浠诲姟{}鏁版嵁缂撳瓨涓嶅瓨鍦�", wrkMast.getWrkNo());
@@ -212,28 +278,23 @@
                         continue;
                     }
 
-                    wrkMast.setWrkSts(WrkStsType.STATION_RUN.sts);
-                    wrkMast.setSystemMsg("");
-                    wrkMast.setIoTime(new Date());
-                    if (wrkMastService.updateById(wrkMast)) {
-                        boolean offered = offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "dualCrnStationOutExecute");
-                        if (offered && stationMoveCoordinator != null) {
-                            stationMoveCoordinator.recordDispatch(
-                                    wrkMast.getWrkNo(),
-                                    stationProtocol.getStationId(),
-                                    "dualCrnStationOutExecute",
-                                    command,
-                                    false
-                            );
-                        }
-                        notifyUtils.notify(String.valueOf(SlaveType.Devp), stationObjModel.getDeviceNo(),
-                                String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(),
-                                NotifyMsgType.STATION_OUT_TASK_RUN, null);
-                        News.info("杈撻�佺珯鐐瑰嚭搴撳懡浠や笅鍙戞垚鍔燂紝绔欑偣鍙�={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}",
-                                stationProtocol.getStationId(), wrkMast.getWrkNo(), JSON.toJSONString(command));
-                        redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId(), "lock", 5);
-                        redisUtil.del(RedisKeyType.DUAL_CRN_OUT_TASK_STATION_INFO.key + wrkMast.getWrkNo());
+                    boolean offered = offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "dualCrnStationOutExecute");
+                    if (!offered) {
+                        continue;
                     }
+                    if (stationMoveCoordinator != null) {
+                        stationMoveCoordinator.recordDispatch(
+                                wrkMast.getWrkNo(),
+                                stationProtocol.getStationId(),
+                                "dualCrnStationOutExecute",
+                                command,
+                                false
+                        );
+                    }
+                    markPendingDispatch(wrkMast.getWrkNo());
+                    News.info("杈撻�佺珯鐐瑰嚭搴撳懡浠ゅ凡鍏ヨ澶囨墽琛岄摼璺紝绛夊緟婧愮珯鎺ュ崟銆傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}",
+                            stationProtocol.getStationId(), wrkMast.getWrkNo(), JSON.toJSONString(command));
+                    redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId(), "lock", 5);
                 }
             }
         } catch (Exception e) {
@@ -246,4 +307,36 @@
                 .dispatch(deviceNo, command, "station-operate-process", scene);
         return dispatchResult.isAccepted();
     }
+
+    private StationObjModel getOutboundSourceStation(WrkMast wrkMast) {
+        if (wrkMast == null || wrkMast.getSourceStaNo() == null) {
+            return null;
+        }
+        BasStation basStation = basStationService.getById(wrkMast.getSourceStaNo());
+        if (basStation == null || basStation.getDeviceNo() == null) {
+            return null;
+        }
+        StationObjModel stationObjModel = new StationObjModel();
+        stationObjModel.setStationId(wrkMast.getSourceStaNo());
+        stationObjModel.setDeviceNo(basStation.getDeviceNo());
+        return stationObjModel;
+    }
+
+    private boolean hasPendingDispatch(Integer wrkNo) {
+        return wrkNo != null && redisUtil.get(RedisKeyType.STATION_OUT_PENDING_DISPATCH_.key + wrkNo) != null;
+    }
+
+    private void markPendingDispatch(Integer wrkNo) {
+        if (wrkNo == null) {
+            return;
+        }
+        redisUtil.set(RedisKeyType.STATION_OUT_PENDING_DISPATCH_.key + wrkNo, String.valueOf(System.currentTimeMillis()), PENDING_DISPATCH_EXPIRE_SECONDS);
+    }
+
+    private void clearPendingDispatch(Integer wrkNo) {
+        if (wrkNo == null) {
+            return;
+        }
+        redisUtil.del(RedisKeyType.STATION_OUT_PENDING_DISPATCH_.key + wrkNo);
+    }
 }

--
Gitblit v1.9.1