From f2e7521e7740f32fa84d236405f6a78786eaaed1 Mon Sep 17 00:00:00 2001
From: gtsxc <3272660260@qq.com>
Date: 星期五, 28 二月 2025 08:52:11 +0800
Subject: [PATCH] #新增方法calcWhiteList,把结束节点设置白名单

---
 zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/utils/ShuttleDispatcher.java |  306 +++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 234 insertions(+), 72 deletions(-)

diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/utils/ShuttleDispatcher.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/utils/ShuttleDispatcher.java
index 9648ec7..1808c65 100644
--- a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/utils/ShuttleDispatcher.java
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/utils/ShuttleDispatcher.java
@@ -9,14 +9,12 @@
 import com.zy.asrs.wcs.core.entity.*;
 import com.zy.asrs.wcs.core.kernel.AnalyzeService;
 import com.zy.asrs.wcs.core.model.NavigateNode;
-import com.zy.asrs.wcs.core.model.enums.DeviceCtgType;
-import com.zy.asrs.wcs.core.model.enums.NavigationMapType;
-import com.zy.asrs.wcs.core.model.enums.TaskCtgType;
-import com.zy.asrs.wcs.core.model.enums.TaskStsType;
+import com.zy.asrs.wcs.core.model.enums.*;
 import com.zy.asrs.wcs.core.service.*;
 import com.zy.asrs.wcs.rcs.News;
 import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
 import com.zy.asrs.wcs.rcs.entity.Device;
+import com.zy.asrs.wcs.rcs.model.enums.ShuttleProtocolStatusType;
 import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
 import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
 import com.zy.asrs.wcs.rcs.service.DeviceService;
@@ -58,21 +56,58 @@
     private ShuttleStandbyService shuttleStandbyService;
     @Autowired
     private DictService dictService;
+    @Autowired
+    private NavigateUtils navigateUtils;
 
     public synchronized ShuttleThread searchIdleShuttle(Task task) {
         String locNo = taskService.judgeInbound(task) ? task.getDestLoc() : task.getOriginLoc();
         ShuttleThread resThread = null;
-        Integer finalDistance = ShuttleDispatcher.INF;
-
-        //妫�娴嬬洰鏍囨ゼ灞傝溅鏁伴噺鏄惁灏忎簬鍏佽鐨勬渶澶ф暟閲�
-        boolean checkDispatchMaxNum = checkDispatchMaxNum(Utils.getLev(locNo), task.getHostId());
-
+        int lev = Utils.getLev(locNo);
         List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
                 .eq(Device::getDeviceType, DeviceCtgType.SHUTTLE.val())
                 .eq(Device::getHostId, task.getHostId())
                 .eq(Device::getStatus, 1));
-
+        //鑾峰彇鍚屽眰灏忚溅
+        List<Device> currentLevDevices = new ArrayList<>();
+        //鑾峰彇璺ㄥ眰灏忚溅
+        HashMap<Integer,List<Device>> diffLevDeviceMap = new HashMap<>();
         for (Device device : list) {
+            //鑾峰彇鍥涘悜绌挎杞︾嚎绋�
+            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
+            ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
+            if (shuttleProtocol == null || shuttleProtocol.getShuttleNo() == null) {
+                continue;
+            }
+
+            int shuttleLev = Utils.getLev(shuttleProtocol.getCurrentLocNo());
+            if (shuttleLev == lev) {
+                currentLevDevices.add(device);
+            }else {
+                List<Device> devices = null;
+                if(diffLevDeviceMap.containsKey(shuttleLev)) {
+                    devices = diffLevDeviceMap.get(shuttleLev);
+                }else {
+                    devices = new ArrayList<>();
+                }
+                devices.add(device);
+                diffLevDeviceMap.put(shuttleLev, devices);
+            }
+        }
+        //鎼滅储鍚屽眰
+        resThread = this.searchCurrentLevShuttle(currentLevDevices, locNo);
+
+        //鍚屽眰娌℃湁鎼滅储鍒板悎閫傚皬杞︼紝璺ㄦゼ灞傛悳绱�
+        if(resThread == null) {
+            resThread = this.searchDiffLevShuttle(diffLevDeviceMap, locNo, task);
+        }
+
+        return resThread;
+    }
+
+    private synchronized ShuttleThread searchCurrentLevShuttle(List<Device> devices, String locNo) {
+        ShuttleThread resThread = null;
+        Integer finalDistance = ShuttleDispatcher.INF;
+        for (Device device : devices) {
             if (taskService.hasBusyOutboundByShuttle(Integer.parseInt(device.getDeviceNo()))) {
                 continue;
             }
@@ -109,6 +144,12 @@
             }
 
             // 鏈夋病鏈夎鍏朵粬浠诲姟璋冨害
+            List<Task> taskList = taskService.selectWorkingByShuttle(Integer.valueOf(device.getDeviceNo()), null);
+            if (!taskList.isEmpty()) {
+                continue;
+            }
+
+
             int currentLev = Utils.getLev(shuttleProtocol.getCurrentLocNo());//灏忚溅褰撳墠灞傞珮
             String currentLocNo = shuttleProtocol.getCurrentLocNo();//灏忚溅褰撳墠搴撲綅鍙�
 
@@ -117,48 +158,19 @@
                 break;
             }
 
-            String targetLocNo = null;//榛樿鍒版彁鍗囨満寰呮満浣�
             // 鍚屾ゼ灞傜洿鎺ヨ绠楀埌鐩爣搴撲綅
-            if (currentLev == Utils.getLev(locNo)) {
-                targetLocNo = locNo;
-            }else {
-                if (!checkDispatchMaxNum) {
-                    News.info("{}浠诲姟锛寋}灞傦紝宸茬粡杈惧埌褰撳墠妤煎眰璋冨害杞﹁締鏈�澶у��", task.getTaskNo(), Utils.getLev(locNo));
-                    continue;
-                }
-
-                //鑾峰彇璺濈鐩爣浣嶇疆鏈�杩戠殑绌洪棽鍙崲灞傛彁鍗囨満
-                LiftThread liftThread = liftDispatcher.searchIdleLift(locNo, task.getHostId(), true);
-                if (liftThread == null) {
-                    continue;
-                }
-                Device recentTransferLift = liftThread.getDevice();
-
-                //鑾峰彇灏忚溅妤煎眰鎻愬崌鏈哄緟鏈轰綅
-                ShuttleStandby shuttleStandby = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
-                        .eq(ShuttleStandby::getDeviceId, recentTransferLift.getId())
-                        .eq(ShuttleStandby::getDeviceLev, currentLev)
-                        .eq(ShuttleStandby::getStatus, 1));
-                targetLocNo = shuttleStandby.getDeviceLoc();
-            }
-
-            //褰撳墠绌挎杞︾嚎绋嬪埌褰撳墠杞﹀瓙鎵�鍦ㄦゼ灞傜殑鎻愬崌鏈哄彛璺濈
-            List<NavigateNode> currentShuttlePath = NavigateUtils.calc(
+            //褰撳墠绌挎杞︾嚎绋嬪埌褰撳墠杞﹀瓙鎵�鍦ㄦゼ灞傜殑鐩爣搴撲綅璺濈
+            List<NavigateNode> currentShuttlePath = navigateUtils.calcWhiteList(
                     currentLocNo
-                    , targetLocNo
-                    , NavigationMapType.NORMAL.id
+                    , locNo
+                    , NavigationMapType.DFX.id
                     , Utils.getShuttlePoints(Integer.parseInt(shuttleThread.getDevice().getDeviceNo()), currentLev)
             );//鎼滅储绌洪棽绌挎杞︼紝浣跨敤姝e父閫氶亾鍦板浘
             if (currentShuttlePath == null) {
                 continue;
             }
 
-            Integer currDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//璁$畻褰撳墠璺緞琛岃蛋鎬昏窛绂�
-
-            // 涓嶅悓妤煎眰鏉冮噸
-            if (currentLev != Utils.getLev(locNo)) {
-                currDistance += WEIGHT;
-            }
+            Integer currDistance = navigateUtils.getOriginPathAllDistance(currentShuttlePath);//璁$畻褰撳墠璺緞琛岃蛋鎬昏窛绂�
 
             // 鎸傝浇浠诲姟鏉冮噸
             List<Task> tasks = taskService.selectWorkingByShuttle(Integer.valueOf(device.getDeviceNo()), null);
@@ -172,6 +184,117 @@
             }
         }
 
+        return resThread;
+    }
+
+    private synchronized ShuttleThread searchDiffLevShuttle(HashMap<Integer,List<Device>> devicesMap, String locNo, Task task) {
+        ShuttleThread resThread = null;
+        Integer finalDistance = ShuttleDispatcher.INF;
+
+        //妫�娴嬬洰鏍囨ゼ灞傝溅鏁伴噺鏄惁灏忎簬鍏佽鐨勬渶澶ф暟閲�
+        boolean checkDispatchMaxNum = checkDispatchMaxNum(Utils.getLev(locNo), task.getHostId());
+
+        for (Map.Entry<Integer, List<Device>> entry : devicesMap.entrySet()) {
+            Integer lev = entry.getKey();
+            List<Device> devices = entry.getValue();
+            for (Device device : devices) {
+                if (taskService.hasBusyOutboundByShuttle(Integer.parseInt(device.getDeviceNo()))) {
+                    continue;
+                }
+                //鑾峰彇鍥涘悜绌挎杞︾嚎绋�
+                ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
+                ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
+                if (shuttleProtocol == null || shuttleProtocol.getShuttleNo() == null) {
+                    continue;
+                }
+
+                if (!shuttleThread.isIdle()) {
+                    continue;
+                }
+
+                BasShuttle basShuttle = basShuttleService.getOne(new LambdaQueryWrapper<BasShuttle>()
+                        .eq(BasShuttle::getShuttleNo, device.getDeviceNo())
+                        .eq(BasShuttle::getHostId, device.getHostId()));
+                if (basShuttle == null) {
+                    continue;//灏忚溅鍩虹鏁版嵁涓嶅瓨鍦�
+                }
+
+                if (!Cools.isEmpty(basShuttle.getDisableLev())) {
+                    List<Integer> disableLev = JSON.parseArray(basShuttle.getDisableLev(), Integer.class);
+                    //妫�鏌ュ皬杞︽槸鍚︾鐢ㄨ妤煎眰
+                    if (disableLev.contains(Utils.getLev(locNo))) {
+                        continue;//灏忚溅绂佺敤璇ユゼ灞傝烦杩囪杞�
+                    }
+                }
+
+                //妫�娴嬫槸鍚﹀瓨鍦ㄥ厖鐢典换鍔�
+                Task taskCharge = taskService.selectChargeWorking(Integer.valueOf(device.getDeviceNo()));
+                if (taskCharge != null) {
+                    continue;
+                }
+
+                // 鏈夋病鏈夎鍏朵粬浠诲姟璋冨害
+                List<Task> taskList = taskService.selectWorkingByShuttle(Integer.valueOf(device.getDeviceNo()), null);
+                if (!taskList.isEmpty()) {
+                    continue;
+                }
+
+                int currentLev = Utils.getLev(shuttleProtocol.getCurrentLocNo());//灏忚溅褰撳墠灞傞珮
+                String currentLocNo = shuttleProtocol.getCurrentLocNo();//灏忚溅褰撳墠搴撲綅鍙�
+
+                if (!checkDispatchMaxNum) {
+                    News.info("{}浠诲姟锛寋}灞傦紝宸茬粡杈惧埌褰撳墠妤煎眰璋冨害杞﹁締鏈�澶у��", task.getTaskNo(), Utils.getLev(locNo));
+                    continue;
+                }
+
+                //鑾峰彇璺濈灏忚溅浣嶇疆鏈�杩戠殑绌洪棽鍙崲灞傛彁鍗囨満
+                LiftThread liftThread = liftDispatcher.searchIdleLift(currentLocNo, task.getHostId(), true);
+                if (liftThread == null) {
+                    continue;
+                }
+                Device recentTransferLift = liftThread.getDevice();
+
+                //鑾峰彇灏忚溅妤煎眰鎻愬崌鏈哄緟鏈轰綅
+                ShuttleStandby shuttleStandby = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
+                        .eq(ShuttleStandby::getDeviceId, recentTransferLift.getId())
+                        .eq(ShuttleStandby::getDeviceLev, currentLev)
+                        .eq(ShuttleStandby::getStatus, 1));
+                String targetLocNo = shuttleStandby.getDeviceLoc();
+
+                //褰撳墠绌挎杞︾嚎绋嬪埌褰撳墠杞﹀瓙鎵�鍦ㄦゼ灞傜殑鎻愬崌鏈哄緟鏈轰綅璺濈
+                List<NavigateNode> currentShuttlePath = navigateUtils.calc(
+                        currentLocNo
+                        , targetLocNo
+                        , NavigationMapType.DFX.id
+                        , Utils.getShuttlePoints(Integer.parseInt(shuttleThread.getDevice().getDeviceNo()), currentLev)
+                );//鎼滅储绌洪棽绌挎杞︼紝浣跨敤姝e父閫氶亾鍦板浘
+                if (currentShuttlePath == null) {
+                    continue;
+                }
+
+                Integer currDistance = navigateUtils.getOriginPathAllDistance(currentShuttlePath);//璁$畻褰撳墠璺緞琛岃蛋鎬昏窛绂�
+
+                // 涓嶅悓妤煎眰鏉冮噸
+                if (currentLev != Utils.getLev(locNo)) {
+                    currDistance += WEIGHT;
+                }
+
+                // 鎸傝浇浠诲姟鏉冮噸
+                List<Task> tasks = taskService.selectWorkingByShuttle(Integer.valueOf(device.getDeviceNo()), null);
+                if (!Cools.isEmpty(tasks)) {
+                    currDistance += tasks.size() * WEIGHT;
+                }
+
+                if (currDistance < finalDistance) {
+                    finalDistance = currDistance;
+                    resThread = shuttleThread;
+                }
+            }
+
+            if (resThread != null) {
+                break;
+            }
+        }
         return resThread;
     }
 
@@ -286,9 +409,9 @@
     }
 
     /**
-     * 鎼滅储閬胯搴撲綅锛岄�氳繃灏忚溅鍙峰拰鐩爣搴撲綅
+     * 鎼滅储鍙敤搴撲綅锛岄�氳繃灏忚溅鍙峰拰鐩爣搴撲綅
      */
-    public String searchStandByLocNo(Integer shuttleNo, Long hostId, String locNo) {
+    public String searchAvailableLocNo(Integer shuttleNo, Long hostId, String currentLocNo, List<String> locNos) {
         BasShuttle basShuttle = basShuttleService.getOne(new LambdaQueryWrapper<BasShuttle>()
                 .eq(BasShuttle::getShuttleNo, shuttleNo)
                 .eq(BasShuttle::getHostId, hostId));
@@ -296,33 +419,18 @@
             throw new CoolException("灏忚溅鍩虹鏁版嵁涓嶅瓨鍦�");
         }
 
-        String idleLoc = basShuttle.getIdleLoc();
-        if (Cools.isEmpty(idleLoc)) {
-            throw new CoolException("灏忚溅閬胯鏁版嵁涓嶅瓨鍦�");
-        }
-
-        int lev = Utils.getLev(locNo);//褰撳墠妤煎眰
-        List<String> standbyLoc = JSON.parseArray(idleLoc, String.class);
-        if (standbyLoc.isEmpty()) {
-            throw new CoolException("閬胯鏁版嵁寮傚父");
-        }
-        //鑾峰彇褰撳墠灞傞伩璁╀綅缃�
-        List<String> currentLevStandByLoc = new ArrayList<>();
-        for (String loc : standbyLoc) {
-            if (Utils.getLev(loc) == lev) {
-                currentLevStandByLoc.add(loc);
-            }
-        }
-        if (currentLevStandByLoc.isEmpty()) {
+        if (locNos.isEmpty()) {
             throw new CoolException("褰撳墠灞傛棤閬胯浣嶇疆");
         }
 
+        int lev = Utils.getLev(currentLocNo);
+
         Integer finalDistance = ShuttleDispatcher.INF;
         String recentLoc = null;
-        for (String loc : currentLevStandByLoc) {
+        for (String loc : locNos) {
             //褰撳墠绌挎杞﹀埌閬胯浣嶈绠�
-            List<NavigateNode> currentShuttlePath = NavigateUtils.calc(
-                    locNo
+            List<NavigateNode> currentShuttlePath = navigateUtils.calc(
+                    currentLocNo
                     , loc
                     , NavigationMapType.NORMAL.id
                     , Utils.getShuttlePoints(shuttleNo, lev)
@@ -331,7 +439,7 @@
                 continue;
             }
 
-            Integer currDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//璁$畻褰撳墠璺緞琛岃蛋鎬昏窛绂�
+            Integer currDistance = navigateUtils.getOriginPathAllDistance(currentShuttlePath);//璁$畻褰撳墠璺緞琛岃蛋鎬昏窛绂�
             if (currDistance < finalDistance) {
                 finalDistance = currDistance;
                 recentLoc = loc;
@@ -355,7 +463,7 @@
             return false;
         }
 
-        int levCount = 0;//鐩爣妤煎眰杞﹁締鏁伴噺
+        ArrayList<Integer> shuttleNos = new ArrayList<>();
         List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
                 .eq(Device::getDeviceType, DeviceCtgType.SHUTTLE.val())
                 .eq(Device::getHostId, hostId)
@@ -373,12 +481,24 @@
                 continue;
             }
 
+            if (shuttleProtocol.getProtocolStatusType().equals(ShuttleProtocolStatusType.OFFLINE)) {
+                continue;
+            }
+
+            if(!shuttleThread.isDeviceIdle()) {
+                continue;
+            }
+
+            if(shuttleProtocol.getCurrentLocNo() == null) {
+                continue;
+            }
+
             if (Utils.getLev(shuttleProtocol.getCurrentLocNo()) == lev) {
                 if (shuttleProtocol.getHasCharge()) {
                     continue;//鍏呯數涓�
                 }
 
-                levCount++;//鐩爣妤煎眰鏈夎溅锛屾暟閲忓鍔�
+                shuttleNos.add(shuttleProtocol.getShuttleNo());//鐩爣妤煎眰鏈夎溅锛屾坊鍔犺繘list
             }
         }
 
@@ -396,13 +516,55 @@
             }
 
             if (targetLev == lev) {
-                levCount++;//宸ヤ綔妗g洰鏍囨ゼ灞傚拰瀹為檯妤煎眰鐩稿悓锛屾暟閲忓鍔�
+                //宸ヤ綔妗g洰鏍囨ゼ灞傚拰瀹為檯妤煎眰鐩稿悓锛屾暟閲忓鍔�
+                if (!shuttleNos.contains(task.getShuttleNo())) {
+                    shuttleNos.add(task.getShuttleNo());
+                }
+                continue;
+            }
+        }
+
+        //鎼滅储鏄惁瀛樺湪鍓嶅線鐩爣妤煎眰鐨勫皬杞﹀伐浣滄。
+        for (Task task : taskService.list(new LambdaQueryWrapper<Task>()
+                .in(Task::getTaskSts, TaskStsType.NEW_INBOUND.sts, TaskStsType.ANALYZE_INBOUND.sts, TaskStsType.EXECUTE_INBOUND.sts, TaskStsType.COMPLETE_INBOUND.sts
+                        , TaskStsType.NEW_OUTBOUND.sts, TaskStsType.ANALYZE_OUTBOUND.sts, TaskStsType.EXECUTE_OUTBOUND.sts, TaskStsType.COMPLETE_OUTBOUND.sts))) {
+            String locNo = taskService.judgeInbound(task) ? task.getDestLoc() : task.getOriginLoc();
+            if (Utils.getLev(locNo) != lev) {
                 continue;
             }
 
+            if (task.getShuttleNo() == null) {
+                continue;
+            }
+
+            if (!shuttleNos.contains(task.getShuttleNo())) {
+                shuttleNos.add(task.getShuttleNo());
+            }
         }
 
-        return levCount < Integer.parseInt(dict.getValue());
+
+        return shuttleNos.size() < Integer.parseInt(dict.getValue());
+    }
+
+    //鍒嗘瀽鍑哄簱璺緞寰呮満搴撲綅
+    public String analyzeOutPathWaitLoc(String startLoc, String targetLoc, Device shuttleDevice) {
+        //璁$畻璺緞骞跺垎瑙f垚涓ゆ鍔ㄤ綔
+        List<NavigateNode> nodeList = navigateUtils.calc(startLoc, targetLoc, NavigationMapType.DFX.id, Utils.getShuttlePoints(Integer.parseInt(shuttleDevice.getDeviceNo()), Utils.getLev(startLoc)));
+        if (nodeList == null) {
+            News.error("{} dash {} can't find navigate path!", startLoc, targetLoc);
+            return null;
+        }
+        //鑾峰彇鍒嗘璺緞
+        ArrayList<ArrayList<NavigateNode>> data = navigateUtils.getSectionPath(nodeList);
+        if (data.size() <= 1) {
+            return startLoc;//涓ょ偣涔嬮棿鍙湁涓�娈佃矾寰勶紝鍦ㄨ捣鐐逛綅缃瓑寰�
+        }
+
+        //鍙栧嚭鍊掓暟绗簩娈佃矾寰�
+        ArrayList<NavigateNode> navigateNodes = data.get(data.size() - 2);
+        NavigateNode startNode = navigateNodes.get(0);
+        String lastPathStartLoc = Utils.getLocNo(startNode.getX(), startNode.getY(), startNode.getZ());
+        return lastPathStartLoc;
     }
 
 

--
Gitblit v1.9.1