From 1817a79605b464735014a00e762e715788229a4b Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期三, 01 四月 2026 11:41:14 +0800
Subject: [PATCH] #入库获取Row优化

---
 src/main/java/com/zy/asrs/utils/Utils.java |  222 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 143 insertions(+), 79 deletions(-)

diff --git a/src/main/java/com/zy/asrs/utils/Utils.java b/src/main/java/com/zy/asrs/utils/Utils.java
index 1fa569f..db6e307 100644
--- a/src/main/java/com/zy/asrs/utils/Utils.java
+++ b/src/main/java/com/zy/asrs/utils/Utils.java
@@ -4,15 +4,14 @@
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.core.common.Arith;
 import com.core.common.Cools;
 import com.core.common.SpringUtils;
 import com.core.exception.CoolException;
+import com.zy.asrs.domain.vo.WrkTaskCountVo;
 import com.zy.asrs.entity.BasCrnp;
 import com.zy.asrs.entity.BasDualCrnp;
 import com.zy.asrs.entity.BasStationDevice;
-import com.zy.asrs.entity.WrkMast;
 import com.zy.asrs.service.BasCrnpService;
 import com.zy.asrs.service.BasDualCrnpService;
 import com.zy.asrs.service.BasStationDeviceService;
@@ -36,6 +35,8 @@
 public class Utils {
 
     private static final String LOC_NO_FLAG = "-";
+    private static final long STATION_REACHABLE_CACHE_SECONDS = 7 * 24 * 60 * 60L;
+    private static final long PRECOMPUTE_IN_TASK_ROW_CACHE_SECONDS = 60L;
     private static final DecimalFormat fmt = new DecimalFormat("##0.00");
     private static Integer defaultDeviceLogCollectTime = 200;
     private static Long lastUpdateDeviceLogCollectTime = -1L;
@@ -150,6 +151,24 @@
         return getInTaskEnableRow(stationId, new ArrayList<>(), new ArrayList<>(), true);
     }
 
+    public static void precomputeInTaskEnableRow(Integer stationId) {
+        if (stationId == null) {
+            return;
+        }
+        List<Integer> rowList = getInTaskEnableRow(stationId);
+        cacheInTaskEnableRow(stationId, rowList);
+    }
+
+    public static List<Integer> getInTaskEnableRowWithCache(Integer stationId) {
+        List<Integer> cachedRowList = getCachedInTaskEnableRow(stationId);
+        if (cachedRowList != null) {
+            return cachedRowList;
+        }
+        List<Integer> rowList = getInTaskEnableRow(stationId);
+        cacheInTaskEnableRow(stationId, rowList);
+        return rowList;
+    }
+
     //鑾峰彇鍏ュ簱浠诲姟鍙敤鎺�
     public static List<Integer> getInTaskEnableRow(Integer stationId, List<Integer> excludeCrnList, List<Integer> excludeDualCrnList, boolean maxInTaskControl) {
         //鑾峰彇鍫嗗灈鏈哄叆搴撲换鍔″彲鐢ㄦ帓
@@ -189,34 +208,29 @@
                 currentCircleTaskCrnNo = Integer.parseInt(String.valueOf(object));
             }
 
+            Map<Integer, WrkTaskCountVo> crnTaskCountMap = toTaskCountMap(wrkMastService.selectCrnTaskCountList());
             QueryWrapper<BasCrnp> wrapper = new QueryWrapper<BasCrnp>()
                     .eq("in_enable", "Y")
                     .eq("status", 1);
+            List<BasCrnp> basCrnps = basCrnpService.list(wrapper);
 
             if (currentCircleTaskCrnNo != null) {
-                currentCircleTaskCrnNo = currentCircleTaskCrnNo + 1;
-                BasCrnp basCrnp = basCrnpService.getOne(new QueryWrapper<BasCrnp>()
-                        .eq("crn_no", currentCircleTaskCrnNo)
-                        .eq("in_enable", "Y")
-                        .eq("status", 1)
-                );
-                List<WrkMast> currentCrnTask = wrkMastService.list(new QueryWrapper<WrkMast>()
-                        .eq("crn_no", currentCircleTaskCrnNo)
-                );
-                if (currentCrnTask.size() <= 1) {
-                    if (basCrnp != null) {
-                        List<List<Integer>> rowList = basCrnp.getControlRows$();
-                        for (List<Integer> rows : rowList) {
-                            list.addAll(rows);
+                Integer nextCrnNo = currentCircleTaskCrnNo + 1;
+                WrkTaskCountVo countVo = crnTaskCountMap.get(nextCrnNo);
+                if (getTotalCount(countVo) <= 1) {
+                    BasCrnp firstBasCrnp = null;
+                    for (BasCrnp basCrnp : basCrnps) {
+                        if (Objects.equals(nextCrnNo, basCrnp.getCrnNo())) {
+                            firstBasCrnp = basCrnp;
+                            break;
                         }
-                        wrapper.ne("crn_no", currentCircleTaskCrnNo);
+                    }
+                    if (firstBasCrnp != null) {
+                        appendRowList(list, firstBasCrnp.getControlRows$());
+                        basCrnps.remove(firstBasCrnp);
                     }
                 }
             }
-
-            HashMap<Integer, Integer> map = new HashMap<>();
-            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<>());
-            List<BasCrnp> basCrnps = basCrnpService.list(wrapper);
 
             List<BasCrnp> baseList = new ArrayList<>();
             List<BasCrnp> extraList = new ArrayList<>();
@@ -229,11 +243,6 @@
             }
             baseList.addAll(extraList);
 
-            for (WrkMast wrkMast : wrkMasts) {
-                Integer crnNo = wrkMast.getCrnNo();
-                map.put(crnNo, map.getOrDefault(crnNo, 0) + 1);
-            }
-
             List<BasCrnp> enabledCrnps = new ArrayList<>();
             for (BasCrnp basCrnp : baseList) {
                 CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
@@ -245,13 +254,11 @@
                     continue;
                 }
 
-                List<WrkMast> inWrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>()
-                        .eq("crn_no", basCrnp.getCrnNo())
-                        .eq("io_type", WrkIoType.IN.id)
-                );
+                WrkTaskCountVo countVo = crnTaskCountMap.get(basCrnp.getCrnNo());
+                int inTaskCount = getInCount(countVo);
                 // 妫�鏌ユ槸鍚﹁秴杩囨渶澶у叆搴撲换鍔℃暟
-                if (maxInTaskControl && inWrkMasts.size() >= basCrnp.getMaxInTask()) {
-                    News.info("鍫嗗灈鏈�:{} 宸茶揪鏈�澶у叆搴撲换鍔℃暟锛屽綋鍓嶄换鍔℃暟:{}", basCrnp.getCrnNo(), inWrkMasts.size());
+                if (maxInTaskControl && inTaskCount >= basCrnp.getMaxInTask()) {
+                    News.info("鍫嗗灈鏈�:{} 宸茶揪鏈�澶у叆搴撲换鍔℃暟锛屽綋鍓嶄换鍔℃暟:{}", basCrnp.getCrnNo(), inTaskCount);
                     continue;
                 }
 
@@ -259,14 +266,9 @@
                 List<StationObjModel> inStationList = basCrnp.getInStationList$();
                 boolean enableGo = false;
                 for (StationObjModel stationObjModel : inStationList) {
-                    try {
-                        List<NavigateNode> navigateNodes = navigateUtils.calcReachablePathByStationId(stationId, stationObjModel.getStationId());
-                        if(navigateNodes != null && !navigateNodes.isEmpty()) {
-                            enableGo = true;
-                            break;
-                        }
-                    } catch (Exception e) {
-
+                    if (isReachableWithCache(redisUtil, navigateUtils, stationId, stationObjModel.getStationId())) {
+                        enableGo = true;
+                        break;
                     }
                 }
 
@@ -275,18 +277,15 @@
                 }
             }
 
-            enabledCrnps.sort(Comparator.comparingInt(o -> map.getOrDefault(o.getCrnNo(), 0)));
+            enabledCrnps.sort(Comparator.comparingInt(o -> getTotalCount(crnTaskCountMap.get(o.getCrnNo()))));
 
             List<Integer> recommendList = new ArrayList<>();
             List<Integer> baseRowList = new ArrayList<>();
             for (BasCrnp basCrnp : enabledCrnps) {
-                List<List<Integer>> rowList = basCrnp.getControlRows$();
-                for (List<Integer> rows : rowList) {
-                    if(allowedCrnNos.contains(basCrnp.getCrnNo())) {
-                        recommendList.addAll(rows);
-                    }else {
-                        baseRowList.addAll(rows);
-                    }
+                if(allowedCrnNos.contains(basCrnp.getCrnNo())) {
+                    appendRowList(recommendList, basCrnp.getControlRows$());
+                }else {
+                    appendRowList(baseRowList, basCrnp.getControlRows$());
                 }
             }
 
@@ -302,11 +301,11 @@
     public static List<Integer> getDualCrnInTaskEnableRow(Integer stationId, List<Integer> excludeCrnList, boolean maxInTaskControl) {
         List<Integer> list = new ArrayList<>();
         try {
-            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
-            NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
             WrkMastService wrkMastService = SpringUtils.getBean(WrkMastService.class);
             BasDualCrnpService basDualCrnpService = SpringUtils.getBean(BasDualCrnpService.class);
             BasStationDeviceService basStationDeviceService = SpringUtils.getBean(BasStationDeviceService.class);
+            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
+            NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
 
             List<BasStationDevice> stationDevices = basStationDeviceService.list(new QueryWrapper<BasStationDevice>().eq("station_id", stationId));
             boolean hasConfig = !stationDevices.isEmpty();
@@ -323,8 +322,7 @@
                     .eq("in_enable", "Y")
                     .eq("status", 1);
 
-            HashMap<Integer, Integer> map = new HashMap<>();
-            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<>());
+            Map<Integer, WrkTaskCountVo> dualCrnTaskCountMap = toTaskCountMap(wrkMastService.selectDualCrnTaskCountList());
             List<BasDualCrnp> basDualCrnps = basDualCrnpService.list(wrapper);
 
             List<BasDualCrnp> baseList = new ArrayList<>();
@@ -338,11 +336,6 @@
             }
             baseList.addAll(extraList);
 
-            for (WrkMast wrkMast : wrkMasts) {
-                Integer dualCrnNo = wrkMast.getDualCrnNo();
-                map.put(dualCrnNo, map.getOrDefault(dualCrnNo, 0) + 1);
-            }
-
             List<BasDualCrnp> enabledCrnps = new ArrayList<>();
             for (BasDualCrnp basDualCrnp : baseList) {
                 DualCrnThread dualCrnThread = (DualCrnThread) SlaveConnection.get(SlaveType.DualCrn, basDualCrnp.getCrnNo());
@@ -354,13 +347,11 @@
                     continue;
                 }
 
-                List<WrkMast> inWrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>()
-                        .eq("dual_crn_no", basDualCrnp.getCrnNo())
-                        .eq("io_type", WrkIoType.IN.id)
-                );
+                WrkTaskCountVo countVo = dualCrnTaskCountMap.get(basDualCrnp.getCrnNo());
+                int inTaskCount = getInCount(countVo);
                 // 妫�鏌ユ槸鍚﹁秴杩囨渶澶у叆搴撲换鍔℃暟
-                if (maxInTaskControl && inWrkMasts.size() >= basDualCrnp.getMaxInTask()) {
-                    News.info("鍙屽伐浣嶅爢鍨涙満:{} 宸茶揪鏈�澶у叆搴撲换鍔℃暟锛屽綋鍓嶄换鍔℃暟:{}", basDualCrnp.getCrnNo(), inWrkMasts.size());
+                if (maxInTaskControl && inTaskCount >= basDualCrnp.getMaxInTask()) {
+                    News.info("鍙屽伐浣嶅爢鍨涙満:{} 宸茶揪鏈�澶у叆搴撲换鍔℃暟锛屽綋鍓嶄换鍔℃暟:{}", basDualCrnp.getCrnNo(), inTaskCount);
                     continue;
                 }
 
@@ -368,14 +359,9 @@
                 List<StationObjModel> inStationList = basDualCrnp.getInStationList$();
                 boolean enableGo = false;
                 for (StationObjModel stationObjModel : inStationList) {
-                    try {
-                        List<NavigateNode> navigateNodes = navigateUtils.calcReachablePathByStationId(stationId, stationObjModel.getStationId());
-                        if(navigateNodes != null && !navigateNodes.isEmpty()) {
-                            enableGo = true;
-                            break;
-                        }
-                    } catch (Exception e) {
-
+                    if (isReachableWithCache(redisUtil, navigateUtils, stationId, stationObjModel.getStationId())) {
+                        enableGo = true;
+                        break;
                     }
                 }
 
@@ -384,18 +370,15 @@
                 }
             }
 
-            enabledCrnps.sort(Comparator.comparingInt(o -> map.getOrDefault(o.getCrnNo(), 0)));
+            enabledCrnps.sort(Comparator.comparingInt(o -> getTotalCount(dualCrnTaskCountMap.get(o.getCrnNo()))));
 
             List<Integer> recommendList = new ArrayList<>();
             List<Integer> baseRowList = new ArrayList<>();
             for (BasDualCrnp basDualCrnp : enabledCrnps) {
-                List<List<Integer>> rowList = basDualCrnp.getControlRows$();
-                for (List<Integer> rows : rowList) {
-                    if(allowedCrnNos.contains(basDualCrnp.getCrnNo())) {
-                        recommendList.addAll(rows);
-                    }else {
-                        baseRowList.addAll(rows);
-                    }
+                if(allowedCrnNos.contains(basDualCrnp.getCrnNo())) {
+                    appendRowList(recommendList, basDualCrnp.getControlRows$());
+                }else {
+                    appendRowList(baseRowList, basDualCrnp.getControlRows$());
                 }
             }
 
@@ -407,6 +390,87 @@
         return list;
     }
 
+    private static List<Integer> getCachedInTaskEnableRow(Integer stationId) {
+        if (stationId == null) {
+            return null;
+        }
+        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
+        Object object = redisUtil.get(RedisKeyType.PRECOMPUTE_IN_TASK_ROW_CACHE.key + stationId);
+        if (object == null) {
+            return null;
+        }
+        try {
+            return JSONArray.parseArray(String.valueOf(object), Integer.class);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    private static void cacheInTaskEnableRow(Integer stationId, List<Integer> rowList) {
+        if (stationId == null || rowList == null || rowList.isEmpty()) {
+            return;
+        }
+        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
+        redisUtil.set(RedisKeyType.PRECOMPUTE_IN_TASK_ROW_CACHE.key + stationId,
+                JSON.toJSONString(rowList), PRECOMPUTE_IN_TASK_ROW_CACHE_SECONDS);
+    }
+
+    private static boolean isReachableWithCache(RedisUtil redisUtil,
+                                                NavigateUtils navigateUtils,
+                                                Integer sourceStationId,
+                                                Integer targetStationId) {
+        if (sourceStationId == null || targetStationId == null) {
+            return false;
+        }
+
+        String cacheKey = RedisKeyType.STATION_REACHABLE_CACHE.key + sourceStationId + "_" + targetStationId;
+        if (redisUtil.get(cacheKey) != null) {
+            return true;
+        }
+
+        try {
+            List<NavigateNode> navigateNodes = navigateUtils.calcReachablePathByStationId(sourceStationId, targetStationId);
+            if (navigateNodes != null && !navigateNodes.isEmpty()) {
+                redisUtil.set(cacheKey, "1", STATION_REACHABLE_CACHE_SECONDS);
+                return true;
+            }
+        } catch (Exception ignored) {
+        }
+        return false;
+    }
+
+    private static Map<Integer, WrkTaskCountVo> toTaskCountMap(List<WrkTaskCountVo> countVoList) {
+        Map<Integer, WrkTaskCountVo> countMap = new HashMap<>();
+        if (countVoList == null) {
+            return countMap;
+        }
+        for (WrkTaskCountVo countVo : countVoList) {
+            if (countVo != null && countVo.getDeviceNo() != null) {
+                countMap.put(countVo.getDeviceNo(), countVo);
+            }
+        }
+        return countMap;
+    }
+
+    private static int getTotalCount(WrkTaskCountVo countVo) {
+        return countVo == null || countVo.getTotalCount() == null ? 0 : countVo.getTotalCount();
+    }
+
+    private static int getInCount(WrkTaskCountVo countVo) {
+        return countVo == null || countVo.getInCount() == null ? 0 : countVo.getInCount();
+    }
+
+    private static void appendRowList(List<Integer> targetRowList, List<List<Integer>> sourceRowList) {
+        if (targetRowList == null || sourceRowList == null) {
+            return;
+        }
+        for (List<Integer> rows : sourceRowList) {
+            if (rows != null && !rows.isEmpty()) {
+                targetRowList.addAll(rows);
+            }
+        }
+    }
+
     public static Map<String, Object> convertObjectToMap(Object obj) {
         Map<String, Object> map = new HashMap<>();
         Class<?> clazz = obj.getClass();

--
Gitblit v1.9.1