From 1654b0a8c149f86d38f3202cb88c655ad0a25384 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期三, 01 四月 2026 10:33:25 +0800
Subject: [PATCH] #出库任务路径计算增加cache

---
 src/main/java/com/zy/core/utils/WmsOperateUtils.java |  196 +++++++++++++++++++------------------------------
 1 files changed, 76 insertions(+), 120 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/WmsOperateUtils.java b/src/main/java/com/zy/core/utils/WmsOperateUtils.java
index 4ef1979..45eedcd 100644
--- a/src/main/java/com/zy/core/utils/WmsOperateUtils.java
+++ b/src/main/java/com/zy/core/utils/WmsOperateUtils.java
@@ -2,7 +2,7 @@
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.baomidou.mybatisplus.mapper.EntityWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.core.common.Cools;
 import com.core.exception.CoolException;
 import com.zy.asrs.entity.BasCrnp;
@@ -23,6 +23,7 @@
 import com.zy.core.News;
 import com.zy.core.enums.RedisKeyType;
 import com.zy.core.enums.SlaveType;
+import com.zy.core.plugin.store.InTaskApplyRequest;
 import com.zy.system.entity.Config;
 import com.zy.system.service.ConfigService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -31,11 +32,14 @@
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 @Component
 public class WmsOperateUtils {
+    private static final int APPLY_IN_TASK_TIMEOUT_SECONDS = 5;
 
     @Autowired
     private ConfigService configService;
@@ -55,7 +59,15 @@
     private RedisUtil redisUtil;
 
     // 鐢宠鍏ュ簱浠诲姟
-    public synchronized String applyInTask(String barcode, Integer sourceStaNo, Integer locType1) {
+    public String applyInTask(String barcode, Integer sourceStaNo, Integer locType1) {
+        InTaskApplyRequest request = new InTaskApplyRequest();
+        request.setBarcode(barcode);
+        request.setSourceStaNo(sourceStaNo);
+        request.setLocType1(locType1);
+        return applyInTask(request);
+    }
+
+    public String applyInTask(InTaskApplyRequest request) {
         Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
         if (systemConfigMapObj == null) {
             News.error("绯荤粺Config缂撳瓨澶辨晥");
@@ -75,51 +87,55 @@
             return null;
         }
 
-        HashMap<String, Object> requestParam = new HashMap<>();
+        Map<String, Object> requestParam = new LinkedHashMap<>();
         String response = null;
         int result = 0;
+        long startMs = System.currentTimeMillis();
         try {
             BasStation basStation = basStationService
-                    .selectOne(new EntityWrapper<BasStation>().eq("station_id", sourceStaNo));
+                    .getOne(new QueryWrapper<BasStation>().eq("station_id", request.getSourceStaNo()));
             if (basStation == null) {
-                News.error("绔欑偣{}涓嶅瓨鍦�", sourceStaNo);
+                News.error("绔欑偣{}涓嶅瓨鍦�", request.getSourceStaNo());
                 return null;
             }
 
-            String stationNo = String.valueOf(sourceStaNo);
+            String stationNo = String.valueOf(request.getSourceStaNo());
             if (!Cools.isEmpty(basStation.getStationAlias())) {
                 stationNo = basStation.getStationAlias();
             }
 
-            requestParam.put("barcode", barcode);
+            requestParam.put("barcode", request.getBarcode());
             requestParam.put("sourceStaNo", stationNo);
-            requestParam.put("locType1", locType1 == null ? 1 : locType1);
-            requestParam.put("row", Utils.getInTaskEnableRow(sourceStaNo));
+            requestParam.put("locType1", request.getLocType1() == null ? 1 : request.getLocType1());
+            requestParam.put("row", Utils.getInTaskEnableRow(request.getSourceStaNo()));
+            if (request.getExtraParams() != null && !request.getExtraParams().isEmpty()) {
+                requestParam.putAll(request.getExtraParams());
+            }
 
             response = new HttpHandler.Builder()
                     .setUri(wmsUrl)
                     .setPath(wmsSystemInUrl)
                     .setJson(JSON.toJSONString(requestParam))
-                    .setTimeout(30, TimeUnit.SECONDS)
+                    .setTimeout(APPLY_IN_TASK_TIMEOUT_SECONDS, TimeUnit.SECONDS)
                     .build()
                     .doPost();
-            if (response != null) {
+            if (!Cools.isEmpty(response)) {
                 JSONObject jsonObject = JSON.parseObject(response);
                 if (jsonObject.getInteger("code") == 200) {
                     result = 1;
-                    News.info("璇锋眰WMS鍏ュ簱鎺ュ彛鎴愬姛锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + wmsSystemInUrl,
-                            JSON.toJSONString(requestParam), response);
+                    News.info("璇锋眰WMS鍏ュ簱鎺ュ彛鎴愬姛锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                            wmsUrl + wmsSystemInUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs));
                 } else {
-                    News.info("璇锋眰WMS鍏ュ簱鎺ュ彛澶辫触锛屾帴鍙h繑鍥濩ode寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + wmsSystemInUrl,
-                            JSON.toJSONString(requestParam), response);
+                    News.info("璇锋眰WMS鍏ュ簱鎺ュ彛澶辫触锛屾帴鍙h繑鍥濩ode寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                            wmsUrl + wmsSystemInUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs));
                 }
             } else {
-                News.info("璇锋眰WMS鍏ュ簱鎺ュ彛澶辫触锛屾帴鍙f湭鍝嶅簲锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + wmsSystemInUrl,
-                        JSON.toJSONString(requestParam), response);
+                News.info("璇锋眰WMS鍏ュ簱鎺ュ彛澶辫触锛屾帴鍙f湭鍝嶅簲锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                        wmsUrl + wmsSystemInUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs));
             }
         } catch (Exception e) {
-            News.error("璇锋眰WMS鍏ュ簱鎺ュ彛寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + wmsSystemInUrl,
-                    JSON.toJSONString(requestParam), response, e);
+            News.error("璇锋眰WMS鍏ュ簱鎺ュ彛寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms", wmsUrl + wmsSystemInUrl,
+                    JSON.toJSONString(requestParam), response, elapsedMs(startMs), e);
         } finally {
             HttpRequestLog httpRequestLog = new HttpRequestLog();
             httpRequestLog.setName(wmsUrl + wmsSystemInUrl);
@@ -127,89 +143,15 @@
             httpRequestLog.setResponse(response);
             httpRequestLog.setCreateTime(new Date());
             httpRequestLog.setResult(result);
-            httpRequestLogService.insert(httpRequestLog);
+            httpRequestLogService.save(httpRequestLog);
         }
         return response;
-    }
-
-    /**
-     * 寮傛鐢宠鍏ュ簱浠诲姟 - 闈為樆濉炵増鏈�
-     * 灏嗚姹傛彁浜ゅ埌绾跨▼姹犲紓姝ユ墽琛岋紝缁撴灉瀛樺偍鍒癛edis涓�
-     * 
-     * @param barcode     鎵樼洏鐮�
-     * @param sourceStaNo 绔欑偣缂栧彿
-     * @param locType1    鎵樼洏楂樺害
-     */
-    public void applyInTaskAsync(String barcode, Integer sourceStaNo, Integer locType1) {
-        String requestKey = RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key + barcode + "_" + sourceStaNo;
-        String responseKey = RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key + barcode + "_" + sourceStaNo;
-
-        // 妫�鏌ユ槸鍚﹀凡鏈夎姹傚湪杩涜涓�
-        Object existingRequest = redisUtil.get(requestKey);
-        if (existingRequest != null) {
-            return; // 宸叉湁璇锋眰鍦ㄨ繘琛屼腑锛岃烦杩�
-        }
-
-        // 鏍囪璇锋眰杩涜涓紝璁剧疆60绉掕秴鏃�
-        redisUtil.set(requestKey, "processing", 60);
-
-        // 鎻愪氦寮傛浠诲姟
-        new Thread(() -> {
-            try {
-                String response = applyInTask(barcode, sourceStaNo, locType1);
-                if (response != null) {
-                    // 瀛樺偍鍝嶅簲缁撴灉锛岃缃�60绉掕秴鏃�
-                    redisUtil.set(responseKey, response, 60);
-                    News.info("寮傛WMS鍏ュ簱璇锋眰瀹屾垚锛宐arcode={}锛宻tationId={}锛宺esponse={}", barcode, sourceStaNo, response);
-                } else {
-                    // 璇锋眰澶辫触锛屽瓨鍌ㄥけ璐ユ爣璁�
-                    redisUtil.set(responseKey, "FAILED", 10);
-                    News.error("寮傛WMS鍏ュ簱璇锋眰澶辫触锛宐arcode={}锛宻tationId={}", barcode, sourceStaNo);
-                }
-            } catch (Exception e) {
-                News.error("寮傛WMS鍏ュ簱璇锋眰寮傚父锛宐arcode={}锛宻tationId={}锛宔rror={}", barcode, sourceStaNo, e.getMessage());
-                redisUtil.set(responseKey, "ERROR:" + e.getMessage(), 10);
-            } finally {
-                // 娓呴櫎璇锋眰杩涜涓爣璁�
-                redisUtil.del(requestKey);
-            }
-        }).start();
-    }
-
-    /**
-     * 鏌ヨ寮傛鍏ュ簱浠诲姟璇锋眰缁撴灉
-     * 
-     * @param barcode   鎵樼洏鐮�
-     * @param stationId 绔欑偣缂栧彿
-     * @return 鍝嶅簲缁撴灉锛宯ull琛ㄧず杩樻湭瀹屾垚鎴栨湭鎵惧埌
-     */
-    public String queryAsyncInTaskResponse(String barcode, Integer stationId) {
-        String responseKey = RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key + barcode + "_" + stationId;
-        Object response = redisUtil.get(responseKey);
-        if (response != null) {
-            // 鑾峰彇鍚庡垹闄わ紝閬垮厤閲嶅澶勭悊
-            redisUtil.del(responseKey);
-            return response.toString();
-        }
-        return null;
-    }
-
-    /**
-     * 妫�鏌ユ槸鍚︽湁寮傛璇锋眰姝e湪杩涜涓�
-     * 
-     * @param barcode   鎵樼洏鐮�
-     * @param stationId 绔欑偣缂栧彿
-     * @return true琛ㄧず姝e湪璇锋眰涓�
-     */
-    public boolean isAsyncRequestInProgress(String barcode, Integer stationId) {
-        String requestKey = RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key + barcode + "_" + stationId;
-        return redisUtil.get(requestKey) != null;
     }
 
     // 鐢宠浠诲姟閲嶆柊鍒嗛厤搴撲綅
     public synchronized String applyReassignTaskLocNo(Integer taskNo, Integer stationId) {
         String wmsUrl = null;
-        Config wmsSystemUriConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemUri"));
+        Config wmsSystemUriConfig = configService.getOne(new QueryWrapper<Config>().eq("code", "wmsSystemUri"));
         if (wmsSystemUriConfig != null) {
             wmsUrl = wmsSystemUriConfig.getValue();
         }
@@ -221,7 +163,7 @@
 
         String wmsSystemReassignInTaskUrl = null;
         Config wmsSystemReassignInTaskUrlConfig = configService
-                .selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemReassignInTaskUrl"));
+                .getOne(new QueryWrapper<Config>().eq("code", "wmsSystemReassignInTaskUrl"));
         if (wmsSystemReassignInTaskUrlConfig != null) {
             wmsSystemReassignInTaskUrl = wmsSystemReassignInTaskUrlConfig.getValue();
         }
@@ -240,6 +182,7 @@
         HashMap<String, Object> requestParam = new HashMap<>();
         String response = null;
         int result = 0;
+        long startMs = System.currentTimeMillis();
         try {
             List<Integer> excludeCrnList = new ArrayList<>();
             List<Integer> excludeDualCrnList = new ArrayList<>();
@@ -260,23 +203,27 @@
                     .setTimeout(30, TimeUnit.SECONDS)
                     .build()
                     .doPost();
-            if (response != null) {
+            if (!Cools.isEmpty(response)) {
                 JSONObject jsonObject = JSON.parseObject(response);
                 if (jsonObject.getInteger("code") == 200) {
                     result = 1;
-                    News.info("璇锋眰鐢宠浠诲姟閲嶆柊鍒嗛厤鍏ュ簱鎺ュ彛鎴愬姛锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + wmsSystemReassignInTaskUrl,
-                            JSON.toJSONString(requestParam), response);
+                    News.info("璇锋眰鐢宠浠诲姟閲嶆柊鍒嗛厤鍏ュ簱鎺ュ彛鎴愬姛锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                            wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response,
+                            elapsedMs(startMs));
                 } else {
-                    News.info("璇锋眰鐢宠浠诲姟閲嶆柊鍒嗛厤鍏ュ簱鎺ュ彛澶辫触锛屾帴鍙h繑鍥濩ode寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}",
-                            wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response);
+                    News.info("璇锋眰鐢宠浠诲姟閲嶆柊鍒嗛厤鍏ュ簱鎺ュ彛澶辫触锛屾帴鍙h繑鍥濩ode寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                            wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response,
+                            elapsedMs(startMs));
                 }
             } else {
-                News.info("璇锋眰鐢宠浠诲姟閲嶆柊鍒嗛厤鍏ュ簱鎺ュ彛澶辫触锛屾帴鍙f湭鍝嶅簲锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + wmsSystemReassignInTaskUrl,
-                        JSON.toJSONString(requestParam), response);
+                News.info("璇锋眰鐢宠浠诲姟閲嶆柊鍒嗛厤鍏ュ簱鎺ュ彛澶辫触锛屾帴鍙f湭鍝嶅簲锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                        wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response,
+                        elapsedMs(startMs));
             }
         } catch (Exception e) {
-            News.error("璇锋眰鐢宠浠诲姟閲嶆柊鍒嗛厤鍏ュ簱鎺ュ彛寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛� response锛歿}", wmsUrl + wmsSystemReassignInTaskUrl,
-                    JSON.toJSONString(requestParam), response, e);
+            News.error("璇锋眰鐢宠浠诲姟閲嶆柊鍒嗛厤鍏ュ簱鎺ュ彛寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛� response锛歿}锛沜ost={}ms",
+                    wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response,
+                    elapsedMs(startMs), e);
         } finally {
             HttpRequestLog httpRequestLog = new HttpRequestLog();
             httpRequestLog.setName(wmsUrl + wmsSystemReassignInTaskUrl);
@@ -284,7 +231,7 @@
             httpRequestLog.setResponse(response);
             httpRequestLog.setCreateTime(new Date());
             httpRequestLog.setResult(result);
-            httpRequestLogService.insert(httpRequestLog);
+            httpRequestLogService.save(httpRequestLog);
         }
         return response;
     }
@@ -292,7 +239,7 @@
     // 鐢宠鍦ㄥ簱搴撲綅鏇存崲搴撲綅
     public synchronized String applyChangeLocNo(String locNo) {
         String wmsUrl = null;
-        Config wmsSystemUriConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemUri"));
+        Config wmsSystemUriConfig = configService.getOne(new QueryWrapper<Config>().eq("code", "wmsSystemUri"));
         if (wmsSystemUriConfig != null) {
             wmsUrl = wmsSystemUriConfig.getValue();
         }
@@ -304,7 +251,7 @@
 
         String wmsSystemChangeLocNoUrl = null;
         Config wmsSystemChangeLocNoUrlConfig = configService
-                .selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemChangeLocNoUrl"));
+                .getOne(new QueryWrapper<Config>().eq("code", "wmsSystemChangeLocNoUrl"));
         if (wmsSystemChangeLocNoUrlConfig != null) {
             wmsSystemChangeLocNoUrl = wmsSystemChangeLocNoUrlConfig.getValue();
         }
@@ -322,7 +269,7 @@
         List<Integer> crnRows = new ArrayList<>();
 
         if (findCrnNoResult.getCrnType().equals(SlaveType.Crn)) {
-            BasCrnp basCrnp = basCrnpService.selectOne(new EntityWrapper<BasCrnp>().eq("crn_no", crnNo));
+            BasCrnp basCrnp = basCrnpService.getOne(new QueryWrapper<BasCrnp>().eq("crn_no", crnNo));
             if (basCrnp == null) {
                 return null;
             }
@@ -332,7 +279,7 @@
             }
         } else if (findCrnNoResult.getCrnType().equals(SlaveType.DualCrn)) {
             BasDualCrnp basDualCrnp = basDualCrnpService
-                    .selectOne(new EntityWrapper<BasDualCrnp>().eq("crn_no", crnNo));
+                    .getOne(new QueryWrapper<BasDualCrnp>().eq("crn_no", crnNo));
             if (basDualCrnp == null) {
                 return null;
             }
@@ -347,6 +294,7 @@
         HashMap<String, Object> requestParam = new HashMap<>();
         String response = null;
         int result = 0;
+        long startMs = System.currentTimeMillis();
         try {
             requestParam.put("locNo", locNo);
             requestParam.put("row", crnRows);
@@ -359,23 +307,27 @@
                     .build()
                     .doPost();
 
-            if (response != null) {
+            if (!Cools.isEmpty(response)) {
                 JSONObject jsonObject = JSON.parseObject(response);
                 if (jsonObject.getInteger("code") == 200) {
                     result = 1;
-                    News.info("璇锋眰WMS鐢宠鏇存崲搴撲綅鎺ュ彛鎴愬姛锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + wmsSystemChangeLocNoUrl,
-                            JSON.toJSONString(requestParam), response);
+                    News.info("璇锋眰WMS鐢宠鏇存崲搴撲綅鎺ュ彛鎴愬姛锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                            wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response,
+                            elapsedMs(startMs));
                 } else {
-                    News.info("璇锋眰WMS鐢宠鏇存崲搴撲綅鎺ュ彛澶辫触锛屾帴鍙h繑鍥濩ode寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}",
-                            wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response);
+                    News.info("璇锋眰WMS鐢宠鏇存崲搴撲綅鎺ュ彛澶辫触锛屾帴鍙h繑鍥濩ode寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                            wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response,
+                            elapsedMs(startMs));
                 }
             } else {
-                News.info("璇锋眰WMS鐢宠鏇存崲搴撲綅鎺ュ彛澶辫触锛屾帴鍙f湭鍝嶅簲锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + wmsSystemChangeLocNoUrl,
-                        JSON.toJSONString(requestParam), response);
+                News.info("璇锋眰WMS鐢宠鏇存崲搴撲綅鎺ュ彛澶辫触锛屾帴鍙f湭鍝嶅簲锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                        wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response,
+                        elapsedMs(startMs));
             }
         } catch (Exception e) {
-            News.error("璇锋眰WMS鐢宠鏇存崲搴撲綅鎺ュ彛寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", wmsUrl + wmsSystemChangeLocNoUrl,
-                    JSON.toJSONString(requestParam), response, e);
+            News.error("璇锋眰WMS鐢宠鏇存崲搴撲綅鎺ュ彛寮傚父锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}锛沜ost={}ms",
+                    wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response,
+                    elapsedMs(startMs), e);
         } finally {
             HttpRequestLog httpRequestLog = new HttpRequestLog();
             httpRequestLog.setName(wmsUrl + wmsSystemChangeLocNoUrl);
@@ -383,9 +335,13 @@
             httpRequestLog.setResponse(response);
             httpRequestLog.setCreateTime(new Date());
             httpRequestLog.setResult(result);
-            httpRequestLogService.insert(httpRequestLog);
+            httpRequestLogService.save(httpRequestLog);
         }
         return response;
     }
 
+    private long elapsedMs(long startMs) {
+        return System.currentTimeMillis() - startMs;
+    }
+
 }

--
Gitblit v1.9.1