From febc49cd36666823893c233667b212dfb5527dfb Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 07 四月 2026 09:38:24 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java |  136 +++++++++++++++++++++++++++++----------------
 1 files changed, 87 insertions(+), 49 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 35c260c..f8ef8b9 100644
--- a/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java
+++ b/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java
@@ -2,6 +2,7 @@
 
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.zy.asrs.domain.enums.NotifyMsgType;
 import com.zy.asrs.entity.BasDevp;
 import com.zy.asrs.entity.BasStation;
@@ -21,6 +22,7 @@
 import com.zy.core.enums.RedisKeyType;
 import com.zy.core.enums.SlaveType;
 import com.zy.core.enums.StationCommandType;
+import com.zy.core.enums.WrkIoType;
 import com.zy.core.enums.WrkStsType;
 import com.zy.core.model.StationObjModel;
 import com.zy.core.model.command.StationCommand;
@@ -63,23 +65,13 @@
     @Autowired
     private StationDispatchLoadSupport stationDispatchLoadSupport;
 
-    public void stationInExecute() {
-        try {
-            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
-            for (BasDevp basDevp : basDevps) {
-                List<StationObjModel> stationList = basDevp.getBarcodeStationList$();
-                for (StationObjModel entity : stationList) {
-                    stationInExecute(basDevp, entity);
-                }
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
     public void stationOutExecuteFinish(StationObjModel stationObjModel) {
         try {
             if (stationObjModel == null) {
+                return;
+            }
+            Integer stationId = stationObjModel.getStationId();
+            if (stationId == null) {
                 return;
             }
 
@@ -88,46 +80,18 @@
                 return;
             }
             Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
-            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationObjModel.getStationId());
-            if (stationProtocol == null) {
-                return;
+            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationId);
+            WrkMast wrkMast = findCurrentStationRunTask(stationProtocol);
+            boolean matchedByRecentArrival = false;
+            if (wrkMast == null) {
+                wrkMast = findRecentArrivalStationRunTask(stationThread, stationId);
+                matchedByRecentArrival = wrkMast != null;
             }
-            if (stationProtocol.getTaskNo() <= 0) {
-                return;
-            }
-            WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
             if (wrkMast == null) {
                 return;
             }
 
-            if (!wrkMast.getWrkSts().equals(WrkStsType.STATION_RUN.sts)) {
-                return;
-            }
-            Integer wrkNo = wrkMast.getWrkNo();
-
-            if (stationMoveCoordinator != null) {
-                stationMoveCoordinator.finishSession(wrkMast.getWrkNo());
-            }
-            Date now = new Date();
-            wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts);
-            wrkMast.setIoTime(now);
-            wrkMast.setModiTime(now);
-            wrkMastService.updateById(wrkMast);
-            wrkAnalysisService.markOutboundStationComplete(wrkMast, now);
-            notifyUtils.notify(String.valueOf(SlaveType.Devp), stationObjModel.getDeviceNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN_COMPLETE, null);
-            redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkMast.getWrkNo(), "lock", 60);
-            attemptClearTaskPath(stationThread, wrkNo);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public void checkTaskToComplete() {
-        try {
-            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts));
-            for (WrkMast wrkMast : wrkMasts) {
-                checkTaskToComplete(wrkMast);
-            }
+            completeOutboundStationRun(stationObjModel, stationThread, wrkMast, matchedByRecentArrival);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -169,12 +133,86 @@
                 wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
                 wrkMast.setIoTime(new Date());
                 wrkMastService.updateById(wrkMast);
+                clearOutboundDispatchCache(wrkMast);
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
+    private void clearOutboundDispatchCache(WrkMast wrkMast) {
+        if (wrkMast == null || wrkMast.getWrkNo() == null) {
+            return;
+        }
+        redisUtil.del(RedisKeyType.DUAL_CRN_OUT_TASK_STATION_INFO.key + wrkMast.getWrkNo());
+    }
+
+    private WrkMast findCurrentStationRunTask(StationProtocol stationProtocol) {
+        if (stationProtocol == null || stationProtocol.getTaskNo() <= 0) {
+            return null;
+        }
+        WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
+        if (wrkMast == null || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)) {
+            return null;
+        }
+        return wrkMast;
+    }
+
+    private WrkMast findRecentArrivalStationRunTask(StationThread stationThread, Integer stationId) {
+        if (stationThread == null || stationId == null || stationId <= 0) {
+            return null;
+        }
+        List<WrkMast> stationRunTasks = wrkMastService.list(new QueryWrapper<WrkMast>()
+                .eq("io_type", WrkIoType.OUT.id)
+                .eq("sta_no", stationId)
+                .eq("wrk_sts", WrkStsType.STATION_RUN.sts)
+                .orderByAsc("io_time", "wrk_no"));
+        for (WrkMast candidate : stationRunTasks) {
+            Integer wrkNo = candidate == null ? null : candidate.getWrkNo();
+            if (wrkNo == null || wrkNo <= 0) {
+                continue;
+            }
+            if (stationThread.hasRecentArrival(stationId, wrkNo)) {
+                return candidate;
+            }
+        }
+        return null;
+    }
+
+    private void completeOutboundStationRun(StationObjModel stationObjModel,
+                                            StationThread stationThread,
+                                            WrkMast wrkMast,
+                                            boolean matchedByRecentArrival) {
+        if (stationObjModel == null || stationThread == null || wrkMast == null || wrkMast.getWrkNo() == null) {
+            return;
+        }
+        Date now = new Date();
+        boolean updated = wrkMastService.update(null, new UpdateWrapper<WrkMast>()
+                .set("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts)
+                .set("io_time", now)
+                .set("modi_time", now)
+                .eq("wrk_no", wrkMast.getWrkNo())
+                .eq("wrk_sts", WrkStsType.STATION_RUN.sts));
+        if (!updated) {
+            return;
+        }
+
+        wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts);
+        wrkMast.setIoTime(now);
+        wrkMast.setModiTime(now);
+        if (stationMoveCoordinator != null) {
+            stationMoveCoordinator.finishSession(wrkMast.getWrkNo());
+        }
+        wrkAnalysisService.markOutboundStationComplete(wrkMast, now);
+        notifyUtils.notify(String.valueOf(SlaveType.Devp), stationObjModel.getDeviceNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN_COMPLETE, null);
+        redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkMast.getWrkNo(), "lock", 60);
+        clearOutboundDispatchCache(wrkMast);
+        attemptClearTaskPath(stationThread, wrkMast.getWrkNo());
+        if (matchedByRecentArrival) {
+            News.info("杈撻�佺珯鍑哄簱瀹屾垚琛ュ伩鍛戒腑鏈�杩戝埌绔欑紦瀛橈紝鐩爣绔�={}锛屽伐浣滃彿={}", stationObjModel.getStationId(), wrkMast.getWrkNo());
+        }
+    }
+
     public void attemptClearTaskPath(StationThread stationThread, Integer taskNo) {
         if (stationThread == null || taskNo == null || taskNo <= 0) {
             return;

--
Gitblit v1.9.1