From 21c9d5267c4ca9a7815e47641dd60a04b9643bae Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期六, 21 三月 2026 15:11:52 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java |  177 +++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 123 insertions(+), 54 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java b/src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java
index 4c25c66..3e5a8e6 100644
--- a/src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java
+++ b/src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java
@@ -33,6 +33,8 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -73,23 +75,24 @@
     //鍏ュ嚭搴�  ===>>  鍫嗗灈鏈哄叆鍑哄簱浣滀笟涓嬪彂
     public synchronized void crnIoExecuteNormal() {
         List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
+        Map<Integer, BasCrnp> dispatchCrnMap = new HashMap<>();
+        Map<Integer, CrnThread> dispatchThreadMap = new HashMap<>();
+        Map<Integer, CrnProtocol> dispatchProtocolMap = new HashMap<>();
         for (BasCrnp basCrnp : basCrnps) {
             CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
-            if(crnThread == null){
+            if (crnThread == null) {
                 continue;
             }
 
             CrnProtocol crnProtocol = crnThread.getStatus();
-            if(crnProtocol == null){
+            if (crnProtocol == null) {
                 continue;
             }
 
-            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>()
+            long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
                     .eq("crn_no", basCrnp.getCrnNo())
-                    .in("wrk_sts", WrkStsType.INBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN.sts, WrkStsType.LOC_MOVE_RUN.sts)
-                    .orderBy(true, false, "batch_seq")
-            );
-            if(!wrkMasts.isEmpty()){
+                    .in("wrk_sts", WrkStsType.INBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN.sts, WrkStsType.LOC_MOVE_RUN.sts));
+            if (runningCount > 0) {
                 continue;
             }
 
@@ -105,47 +108,106 @@
                 if (clearLock != null) {
                     continue;
                 }
+                dispatchCrnMap.put(basCrnp.getCrnNo(), basCrnp);
+                dispatchThreadMap.put(basCrnp.getCrnNo(), crnThread);
+                dispatchProtocolMap.put(basCrnp.getCrnNo(), crnProtocol);
+            }
+        }
 
-                // 濡傛灉鏈�杩戜竴娆℃槸鍏ュ簱妯″紡
-                if (crnProtocol.getLastIo().equals("I")) {
-                    if (basCrnp.getInEnable().equals("Y")) {
-                        boolean result = this.crnExecuteIn(basCrnp, crnThread);//  鍏ュ簱
-                        crnProtocol.setLastIo("O");
-                        if (result) {
-                            break;
-                        }
-                    } else if (basCrnp.getOutEnable().equals("Y")) {
-                        boolean result = this.crnExecuteOut(basCrnp, crnThread);//  鍑哄簱
-                        crnProtocol.setLastIo("I");
-                        if (result) {
-                            break;
-                        }
-                    }
-                }
-                // 濡傛灉鏈�杩戜竴娆℃槸鍑哄簱妯″紡
-                else if (crnProtocol.getLastIo().equals("O")) {
-                    if (basCrnp.getOutEnable().equals("Y")) {
-                        boolean result = this.crnExecuteOut(basCrnp, crnThread);//  鍑哄簱
-                        crnProtocol.setLastIo("I");
-                        if (result) {
-                            break;
-                        }
-                    } else if (basCrnp.getInEnable().equals("Y")) {
-                        boolean result = this.crnExecuteIn(basCrnp, crnThread);//  鍏ュ簱
-                        crnProtocol.setLastIo("O");
-                        if (result) {
-                            break;
-                        }
-                    }
-                }
+        if (dispatchCrnMap.isEmpty()) {
+            return;
+        }
 
-                //搴撲綅绉昏浆
-                boolean transfer = this.crnExecuteLocTransfer(basCrnp, crnThread);
+        List<WrkMast> taskQueue = wrkMastService.list(new QueryWrapper<WrkMast>()
+                .in("crn_no", new ArrayList<>(dispatchCrnMap.keySet()))
+                .in("wrk_sts",
+                        WrkStsType.INBOUND_DEVICE_RUN.sts,
+                        WrkStsType.NEW_OUTBOUND.sts,
+                        WrkStsType.NEW_LOC_MOVE.sts));
+        taskQueue.sort(Comparator
+                .comparingInt(this::resolveBatchOutboundRank)
+                .thenComparingInt(this::resolveBatchSeqOrder)
+                .thenComparingDouble(this::resolveTaskIoPri)
+                .thenComparingLong(this::resolveTaskQueueTime)
+                .thenComparingInt(this::resolveTaskQueueNo));
+
+        for (WrkMast wrkMast : taskQueue) {
+            if (wrkMast == null || wrkMast.getCrnNo() == null) {
+                continue;
+            }
+
+            Integer crnNo = wrkMast.getCrnNo();
+            BasCrnp basCrnp = dispatchCrnMap.get(crnNo);
+            CrnThread crnThread = dispatchThreadMap.get(crnNo);
+            CrnProtocol crnProtocol = dispatchProtocolMap.get(crnNo);
+            if (basCrnp == null || crnThread == null || crnProtocol == null) {
+                continue;
+            }
+
+            if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.INBOUND_DEVICE_RUN.sts) {
+                boolean result = this.crnExecuteInPlanner(basCrnp, crnThread, wrkMast);
+                if (result) {
+                    crnProtocol.setLastIo("O");
+                    return;
+                }
+                continue;
+            }
+
+            if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.NEW_OUTBOUND.sts) {
+                boolean result = this.crnExecuteOutPlanner(basCrnp, crnThread, wrkMast);
+                if (result) {
+                    crnProtocol.setLastIo("I");
+                    return;
+                }
+                continue;
+            }
+
+            if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.NEW_LOC_MOVE.sts) {
+                boolean transfer = this.crnExecuteMovePlanner(basCrnp, crnThread, wrkMast);
                 if (transfer) {
-                    break;
+                    return;
                 }
             }
         }
+    }
+
+    private int resolveBatchOutboundRank(WrkMast wrkMast) {
+        return isBatchOutboundTask(wrkMast) ? 0 : 1;
+    }
+
+    private int resolveBatchSeqOrder(WrkMast wrkMast) {
+        if (!isBatchOutboundTask(wrkMast) || wrkMast.getBatchSeq() == null) {
+            return Integer.MAX_VALUE;
+        }
+        return wrkMast.getBatchSeq();
+    }
+
+    private double resolveTaskIoPri(WrkMast wrkMast) {
+        if (wrkMast == null || wrkMast.getIoPri() == null) {
+            return Double.MAX_VALUE;
+        }
+        return wrkMast.getIoPri();
+    }
+
+    private long resolveTaskQueueTime(WrkMast wrkMast) {
+        if (wrkMast == null) {
+            return Long.MAX_VALUE;
+        }
+        Date queueTime = wrkMast.getAppeTime() != null ? wrkMast.getAppeTime() : wrkMast.getIoTime();
+        return queueTime == null ? Long.MAX_VALUE : queueTime.getTime();
+    }
+
+    private int resolveTaskQueueNo(WrkMast wrkMast) {
+        if (wrkMast == null || wrkMast.getWrkNo() == null) {
+            return Integer.MAX_VALUE;
+        }
+        return wrkMast.getWrkNo();
+    }
+
+    private boolean isBatchOutboundTask(WrkMast wrkMast) {
+        return wrkMast != null
+                && Integer.valueOf(WrkIoType.OUT.id).equals(wrkMast.getIoType())
+                && !Cools.isEmpty(wrkMast.getBatch());
     }
 
     private synchronized boolean crnExecuteIn(BasCrnp basCrnp, CrnThread crnThread) {
@@ -265,18 +327,7 @@
             return false;
         }
 
-        int stationMaxTaskCount = 30;
-        Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
-        if (systemConfigMapObj != null) {
-            try {
-                HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
-                stationMaxTaskCount = Integer.parseInt(systemConfigMap.getOrDefault("stationMaxTaskCountLimit", "30"));
-            } catch (Exception ignore) {}
-        }
-
-        int currentStationTaskCount = stationOperateProcessUtils.getCurrentStationTaskCount();
-        if (stationMaxTaskCount > 0 && currentStationTaskCount >= stationMaxTaskCount) {
-            News.warn("杈撻�佺珯鐐逛换鍔℃暟閲忚揪鍒颁笂闄愶紝宸插仠姝换鍔′笅鍙戙�傚綋鍓嶄换鍔℃暟={}锛屼笂闄�={}", currentStationTaskCount, stationMaxTaskCount);
+        if (isOutboundStationTaskLimitReached()) {
             return false;
         }
 
@@ -469,6 +520,10 @@
             return false;
         }
 
+        if (isOutboundStationTaskLimitReached()) {
+            return false;
+        }
+
         Integer crnNo = basCrnp.getCrnNo();
 
         if (!allowBatchOutboundExecute(wrkMast)) {
@@ -594,6 +649,20 @@
         return true;
     }
 
+    private boolean isOutboundStationTaskLimitReached() {
+        int stationMaxTaskCount = getSystemConfigInt("stationMaxTaskCountLimit", 30);
+        if (stationMaxTaskCount <= 0) {
+            return false;
+        }
+
+        int currentStationTaskCount = stationOperateProcessUtils.getCurrentStationTaskCount();
+        if (currentStationTaskCount >= stationMaxTaskCount) {
+            News.warn("杈撻�佺珯鐐逛换鍔℃暟閲忚揪鍒颁笂闄愶紝宸插仠姝换鍔′笅鍙戙�傚綋鍓嶄换鍔℃暟={}锛屼笂闄�={}", currentStationTaskCount, stationMaxTaskCount);
+            return true;
+        }
+        return false;
+    }
+
     private int getSystemConfigInt(String code, int defaultValue) {
         Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
         if (systemConfigMapObj == null) {

--
Gitblit v1.9.1