From 5095e9cf3e1c9ec9564793ebac5674d5c6c6ba00 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期三, 01 四月 2026 14:47:19 +0800
Subject: [PATCH] #入库任务路径计算增加cache

---
 src/main/java/com/zy/common/service/CommonService.java |  270 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 266 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/zy/common/service/CommonService.java b/src/main/java/com/zy/common/service/CommonService.java
index 68468a9..9169502 100644
--- a/src/main/java/com/zy/common/service/CommonService.java
+++ b/src/main/java/com/zy/common/service/CommonService.java
@@ -1,6 +1,7 @@
 package com.zy.common.service;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.core.common.Cools;
 import com.core.exception.CoolException;
 import com.zy.asrs.domain.Result.CancelTaskBatchResult;
@@ -21,6 +22,7 @@
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.Date;
 import java.util.List;
 import java.util.Random;
@@ -28,6 +30,8 @@
 @Slf4j
 @Service
 public class CommonService {
+
+    private static final long OUT_STATION_ROUTE_CACHE_SECONDS = 60 * 60 * 24 * 7;
 
     @Autowired
     private WrkMastService wrkMastService;
@@ -47,6 +51,8 @@
     private BasOutStationAreaService basOutStationAreaService;
     @Autowired
     private WrkCommandRollbackService wrkCommandRollbackService;
+    @Autowired
+    private WrkAnalysisService wrkAnalysisService;
 
     /**
      * 鐢熸垚宸ヤ綔鍙�
@@ -59,8 +65,8 @@
         }
 
         int workNo = wrkLastno.getWrkNo();
-        int sNo = wrkLastno.getSNo();
-        int eNo = wrkLastno.getENo();
+        int sNo = wrkLastno.getsNo();
+        int eNo = wrkLastno.geteNo();
         workNo = workNo>=eNo ? sNo : workNo+1;
         while (true) {
             WrkMast wrkMast = wrkMastService.selectByWorkNo(workNo);
@@ -109,6 +115,8 @@
             wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
         } else if (wrkMast.getIoType() == WrkIoType.LOC_MOVE.id) {
             wrkMast.setWrkSts(WrkStsType.COMPLETE_LOC_MOVE.sts);
+        } else if (wrkMast.getIoType() == WrkIoType.CRN_MOVE.id) {
+            wrkMast.setWrkSts(WrkStsType.COMPLETE_CRN_MOVE.sts);
         }
 
         wrkMast.setModiTime(new Date());
@@ -139,6 +147,8 @@
         } else if (wrkMast.getIoType().equals(WrkIoType.OUT.id) && !wrkMast.getWrkSts().equals(WrkStsType.NEW_OUTBOUND.sts)) {
             cancelSuccess = true;
         } else if (wrkMast.getIoType().equals(WrkIoType.LOC_MOVE.id) && !wrkMast.getWrkSts().equals(WrkStsType.NEW_LOC_MOVE.sts)) {
+            cancelSuccess = true;
+        } else if (wrkMast.getIoType().equals(WrkIoType.CRN_MOVE.id) && !wrkMast.getWrkSts().equals(WrkStsType.NEW_CRN_MOVE.sts)) {
             cancelSuccess = true;
         }
 
@@ -177,6 +187,80 @@
         wrkMast.setModiTime(new Date());
         wrkMastService.updateById(wrkMast);
         return true;
+    }
+
+    public CancelTaskBatchResult cancelOutTaskBatchInfo(CancelTaskBatchParam param) {
+        if (param == null) {
+            throw new CoolException("鍙傛暟涓嶈兘涓虹┖");
+        }
+        List<CancelTaskParam> taskList = param.getTaskList();
+        if (taskList == null || taskList.isEmpty()) {
+            throw new CoolException("浠诲姟鍙傛暟鍒楄〃涓虹┖");
+        }
+
+        List<String> successList = new ArrayList<>();
+        List<String> failList = new ArrayList<>();
+        for (CancelTaskParam taskParam : taskList) {
+            if (taskParam == null) {
+                throw new CoolException("浠诲姟鍙傛暟涓嶈兘涓虹┖");
+            }
+            boolean cancelStatus = false;
+            try {
+                cancelStatus = cancelSingleOutTaskBatchInfo(taskParam);
+            } catch (Exception e) {
+            }
+
+            if (cancelStatus) {
+                successList.add(resolveTaskIdentifier(taskParam));
+            } else {
+                failList.add(resolveTaskIdentifier(taskParam));
+            }
+        }
+
+        CancelTaskBatchResult result = new CancelTaskBatchResult();
+        result.setSuccessList(successList);
+        result.setFailList(failList);
+        return result;
+    }
+
+    private boolean cancelSingleOutTaskBatchInfo(CancelTaskParam param) {
+        WrkMast wrkMast = null;
+        Integer wrkNo = param.getWrkNo();
+        String taskNo = param.getTaskNo();
+        if (wrkNo == null) {
+            if (Cools.isEmpty(taskNo)) {
+                throw new CoolException("WMS浠诲姟鍙蜂笉鑳戒负绌�");
+            }
+            wrkMast = wrkMastService.getOne(new QueryWrapper<WrkMast>().eq("wms_wrk_no", taskNo));
+        } else {
+            wrkMast = wrkMastService.selectByWorkNo(wrkNo);
+        }
+        if (wrkMast == null) {
+            throw new CoolException("浠诲姟涓嶅瓨鍦�");
+        }
+        if (!wrkMast.getIoType().equals(WrkIoType.OUT.id)) {
+            throw new CoolException("浠呮敮鎸佸嚭搴撲换鍔�");
+        }
+
+        boolean updated = wrkMast.getWrkNo() != null && wrkMastService.update(null, new UpdateWrapper<WrkMast>()
+                .eq("wrk_no", wrkMast.getWrkNo())
+                .set("batch", null)
+                .set("batch_seq", null)
+                .set("modi_time", new Date()));
+        if (!updated) {
+            throw new CoolException("鍙栨秷鍑哄簱浠诲姟鎵规鍜屾壒娆″簭鍙峰け璐�");
+        }
+        return true;
+    }
+
+    private String resolveTaskIdentifier(CancelTaskParam param) {
+        if (!Cools.isEmpty(param.getTaskNo())) {
+            return param.getTaskNo();
+        }
+        if (param.getWrkNo() != null) {
+            return String.valueOf(param.getWrkNo());
+        }
+        return "";
     }
 
     public CancelTaskBatchResult cancelTaskBatch(CancelTaskBatchParam param) {
@@ -289,6 +373,7 @@
             News.error("绉诲簱浠诲姟 --- 淇濆瓨宸ヤ綔妗eけ璐ワ紒");
             throw new CoolException("淇濆瓨宸ヤ綔妗eけ璐�");
         }
+        wrkAnalysisService.initForTask(wrkMast);
 
         sourceLocMast.setLocSts("R");
         sourceLocMast.setModiTime(new Date());
@@ -357,6 +442,7 @@
             News.error("鍏ュ簱浠诲姟 --- 淇濆瓨宸ヤ綔妗eけ璐ワ紒");
             throw new CoolException("淇濆瓨宸ヤ綔妗eけ璐�");
         }
+        wrkAnalysisService.initForTask(wrkMast);
 
         locMast.setLocSts("S");
         locMast.setModiTime(new Date());
@@ -474,11 +560,54 @@
             News.error("鍑哄簱浠诲姟 --- 淇濆瓨宸ヤ綔妗eけ璐ワ紒");
             throw new CoolException("淇濆瓨宸ヤ綔妗eけ璐�");
         }
+        wrkAnalysisService.initForTask(wrkMast);
 
         locMast.setLocSts("R");
         locMast.setModiTime(new Date());
         locMastService.updateById(locMast);
         return true;
+    }
+
+    public boolean createOutTaskBatch(CreateOutTaskBatchParam param) {
+        if (param == null) {
+            throw new CoolException("鍙傛暟涓嶈兘涓虹┖");
+        }
+
+        List<CreateOutTaskParam> taskList = param.getTaskList();
+        if (taskList == null || taskList.isEmpty()) {
+            throw new CoolException("浠诲姟鍒楄〃涓嶈兘涓虹┖");
+        }
+
+        List<CreateOutTaskParam> sortedTaskList = new ArrayList<>(taskList);
+        sortedTaskList.sort(Comparator.nullsLast(Comparator
+                .comparing(this::getSortableBatch, Comparator.nullsLast(String::compareTo))
+                .thenComparing(this::getSortableBatchSeq, Comparator.nullsLast(Integer::compareTo))));
+
+        for (CreateOutTaskParam createOutTaskParam : sortedTaskList) {
+            if (createOutTaskParam == null) {
+                throw new CoolException("浠诲姟鍙傛暟涓嶈兘涓虹┖");
+            }
+            createOutTask(createOutTaskParam);
+        }
+        return true;
+    }
+
+    private String getSortableBatch(CreateOutTaskParam param) {
+        if (param == null) {
+            return null;
+        }
+        String batch = param.getBatch();
+        if (batch == null || batch.trim().isEmpty()) {
+            return null;
+        }
+        return batch;
+    }
+
+    private Integer getSortableBatchSeq(CreateOutTaskParam param) {
+        if (param == null) {
+            return null;
+        }
+        return param.getBatchSeq();
     }
 
     public FindCrnNoResult findCrnNoByLocNo(String locNo) {
@@ -527,12 +656,18 @@
             stationList = basDualCrnp.getInStationList$();
         }
 
+        Integer cachedTargetStationId = resolveCachedInStationId(findCrnNoResult, sourceStationId, stationList);
+        if (cachedTargetStationId != null) {
+            return cachedTargetStationId;
+        }
+
         Integer targetStationId = null;
         for (StationObjModel stationObjModel : stationList) {
             try {
-                List<NavigateNode> navigateNodes = navigateUtils.calcByStationId(sourceStationId, stationObjModel.getStationId());
+                List<NavigateNode> navigateNodes = navigateUtils.calcReachablePathByStationId(sourceStationId, stationObjModel.getStationId());
                 if(!navigateNodes.isEmpty()) {
                     targetStationId = stationObjModel.getStationId();
+                    cacheInStationId(findCrnNoResult, sourceStationId, targetStationId);
                     break;
                 }
             } catch (Exception e) {
@@ -559,12 +694,18 @@
             stationList = basDualCrnp.getOutStationList$();
         }
 
+        Integer cachedSourceStationId = resolveCachedOutStationId(findCrnNoResult, targetStationId, stationList);
+        if (cachedSourceStationId != null) {
+            return cachedSourceStationId;
+        }
+
         Integer finalSourceStationId = null;
         for (StationObjModel stationObjModel : stationList) {
             try {
-                List<NavigateNode> navigateNodes = navigateUtils.calcByStationId(stationObjModel.getStationId(), targetStationId);
+                List<NavigateNode> navigateNodes = navigateUtils.calcReachablePathByStationId(stationObjModel.getStationId(), targetStationId);
                 if(!navigateNodes.isEmpty()) {
                     finalSourceStationId = stationObjModel.getStationId();
+                    cacheOutStationId(findCrnNoResult, targetStationId, finalSourceStationId);
                     break;
                 }
             } catch (Exception e) {
@@ -574,4 +715,125 @@
         return finalSourceStationId;
     }
 
+    /**
+     * 鍏ュ簱璺緞鎼滅储鍚屾牱浠d环杈冮珮锛屽彧缂撳瓨宸茬‘璁ゅ彲杈剧殑鐩爣绔欑偣缁撴灉锛屽苟閫氳繃 TTL 闄愬埗闄堟棫椋庨櫓銆�
+     */
+    private Integer resolveCachedInStationId(FindCrnNoResult findCrnNoResult,
+                                             Integer sourceStationId,
+                                             List<StationObjModel> stationList) {
+        if (findCrnNoResult == null || findCrnNoResult.getCrnType() == null
+                || findCrnNoResult.getCrnNo() == null || sourceStationId == null
+                || stationList == null || stationList.isEmpty()) {
+            return null;
+        }
+
+        Object cacheValue = redisUtil.get(buildInStationRouteCacheKey(findCrnNoResult, sourceStationId));
+        if (cacheValue == null) {
+            return null;
+        }
+
+        Integer cachedStationId = parseInteger(cacheValue);
+        if (cachedStationId == null) {
+            return null;
+        }
+
+        for (StationObjModel stationObjModel : stationList) {
+            if (stationObjModel != null && cachedStationId.equals(stationObjModel.getStationId())) {
+                return cachedStationId;
+            }
+        }
+        return null;
+    }
+
+    private void cacheInStationId(FindCrnNoResult findCrnNoResult,
+                                  Integer sourceStationId,
+                                  Integer targetStationId) {
+        if (findCrnNoResult == null || findCrnNoResult.getCrnType() == null
+                || findCrnNoResult.getCrnNo() == null || sourceStationId == null
+                || targetStationId == null) {
+            return;
+        }
+        redisUtil.set(buildInStationRouteCacheKey(findCrnNoResult, sourceStationId),
+                targetStationId,
+                OUT_STATION_ROUTE_CACHE_SECONDS);
+    }
+
+    private String buildInStationRouteCacheKey(FindCrnNoResult findCrnNoResult, Integer sourceStationId) {
+        return RedisKeyType.IN_STATION_ROUTE_CACHE.key
+                + findCrnNoResult.getCrnType().name()
+                + "_"
+                + findCrnNoResult.getCrnNo()
+                + "_"
+                + sourceStationId;
+    }
+
+    /**
+     * 鍑哄簱璺緞鎼滅储浠d环杈冮珮锛屽彧缂撳瓨宸茬‘璁ゅ彲杈剧殑绔欑偣缁撴灉锛屽苟閫氳繃 TTL 鎺у埗闄堟棫椋庨櫓銆�
+     */
+    private Integer resolveCachedOutStationId(FindCrnNoResult findCrnNoResult,
+                                              Integer targetStationId,
+                                              List<StationObjModel> stationList) {
+        if (findCrnNoResult == null || findCrnNoResult.getCrnType() == null
+                || findCrnNoResult.getCrnNo() == null || targetStationId == null
+                || stationList == null || stationList.isEmpty()) {
+            return null;
+        }
+
+        Object cacheValue = redisUtil.get(buildOutStationRouteCacheKey(findCrnNoResult, targetStationId));
+        if (cacheValue == null) {
+            return null;
+        }
+
+        Integer cachedStationId = parseInteger(cacheValue);
+        if (cachedStationId == null) {
+            return null;
+        }
+
+        for (StationObjModel stationObjModel : stationList) {
+            if (stationObjModel != null && cachedStationId.equals(stationObjModel.getStationId())) {
+                return cachedStationId;
+            }
+        }
+        return null;
+    }
+
+    private void cacheOutStationId(FindCrnNoResult findCrnNoResult,
+                                   Integer targetStationId,
+                                   Integer sourceStationId) {
+        if (findCrnNoResult == null || findCrnNoResult.getCrnType() == null
+                || findCrnNoResult.getCrnNo() == null || targetStationId == null
+                || sourceStationId == null) {
+            return;
+        }
+        redisUtil.set(buildOutStationRouteCacheKey(findCrnNoResult, targetStationId),
+                sourceStationId,
+                OUT_STATION_ROUTE_CACHE_SECONDS);
+    }
+
+    private String buildOutStationRouteCacheKey(FindCrnNoResult findCrnNoResult, Integer targetStationId) {
+        return RedisKeyType.OUT_STATION_ROUTE_CACHE.key
+                + findCrnNoResult.getCrnType().name()
+                + "_"
+                + findCrnNoResult.getCrnNo()
+                + "_"
+                + targetStationId;
+    }
+
+    private Integer parseInteger(Object value) {
+        if (value == null) {
+            return null;
+        }
+        if (value instanceof Integer) {
+            return (Integer) value;
+        }
+        if (value instanceof Number) {
+            return ((Number) value).intValue();
+        }
+        try {
+            return Integer.parseInt(String.valueOf(value));
+        } catch (Exception ignore) {
+            return null;
+        }
+    }
+
 }

--
Gitblit v1.9.1