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

---
 src/main/java/com/zy/core/utils/WmsOperateUtils.java |  197 +++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 166 insertions(+), 31 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/WmsOperateUtils.java b/src/main/java/com/zy/core/utils/WmsOperateUtils.java
index a99d80a..e11ffec 100644
--- a/src/main/java/com/zy/core/utils/WmsOperateUtils.java
+++ b/src/main/java/com/zy/core/utils/WmsOperateUtils.java
@@ -23,6 +23,9 @@
 import com.zy.core.News;
 import com.zy.core.enums.RedisKeyType;
 import com.zy.core.enums.SlaveType;
+import com.zy.core.plugin.store.AsyncInTaskResult;
+import com.zy.core.plugin.store.AsyncInTaskStatus;
+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,7 +34,9 @@
 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.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -82,6 +87,23 @@
 
     private final ConcurrentMap<String, Boolean> asyncInTaskInflight = new ConcurrentHashMap<>();
 
+    private String buildAsyncInTaskBizKey(InTaskApplyRequest request) {
+        if (!Cools.isEmpty(request.getBizKey())) {
+            return request.getBizKey();
+        }
+        StringBuilder keyBuilder = new StringBuilder(request.getBarcode())
+                .append("_")
+                .append(request.getSourceStaNo());
+        if (request.getTaskNo() != null && request.getTaskNo() > 0) {
+            keyBuilder.append("_").append(request.getTaskNo());
+        }
+        return keyBuilder.toString();
+    }
+
+    private String buildAsyncInTaskKey(String prefix, InTaskApplyRequest request) {
+        return prefix + buildAsyncInTaskBizKey(request);
+    }
+
     private String buildAsyncInTaskKey(String prefix, String barcode, Integer stationId, Integer taskNo) {
         StringBuilder keyBuilder = new StringBuilder(prefix)
                 .append(barcode)
@@ -95,6 +117,14 @@
 
     // 鐢宠鍏ュ簱浠诲姟
     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缂撳瓨澶辨晥");
@@ -114,26 +144,29 @@
             return null;
         }
 
-        HashMap<String, Object> requestParam = new HashMap<>();
+        Map<String, Object> requestParam = new LinkedHashMap<>();
         String response = null;
         int result = 0;
         try {
             BasStation basStation = basStationService
-                    .getOne(new QueryWrapper<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)
@@ -184,8 +217,17 @@
     }
 
     public void applyInTaskAsync(String barcode, Integer sourceStaNo, Integer taskNo, Integer locType1) {
-        String requestKey = buildAsyncInTaskKey(RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key, barcode, sourceStaNo, taskNo);
-        String responseKey = buildAsyncInTaskKey(RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key, barcode, sourceStaNo, taskNo);
+        InTaskApplyRequest request = new InTaskApplyRequest();
+        request.setBarcode(barcode);
+        request.setSourceStaNo(sourceStaNo);
+        request.setTaskNo(taskNo);
+        request.setLocType1(locType1);
+        applyInTaskAsync(request);
+    }
+
+    public void applyInTaskAsync(InTaskApplyRequest request) {
+        String requestKey = buildAsyncInTaskKey(RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key, request);
+        String responseKey = buildAsyncInTaskKey(RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key, request);
 
         if (asyncInTaskInflight.putIfAbsent(requestKey, Boolean.TRUE) != null) {
             return;
@@ -204,20 +246,17 @@
         try {
             APPLY_IN_TASK_EXECUTOR.execute(() -> {
                 try {
-                    String response = applyInTask(barcode, sourceStaNo, locType1);
-                    if (response != null) {
-                        redisUtil.set(responseKey, response, APPLY_IN_TASK_RESPONSE_TTL_SECONDS);
-                        News.info("寮傛WMS鍏ュ簱璇锋眰瀹屾垚锛宐arcode={}锛宻tationId={}锛宼askNo={}锛宺esponse={}",
-                                barcode, sourceStaNo, taskNo, response);
-                    } else {
-                        redisUtil.set(responseKey, "FAILED", 10);
-                        News.error("寮傛WMS鍏ュ簱璇锋眰澶辫触锛宐arcode={}锛宻tationId={}锛宼askNo={}",
-                                barcode, sourceStaNo, taskNo);
-                    }
+                    String response = applyInTask(request);
+                    AsyncInTaskResult result = buildAsyncInTaskResult(request, response, null);
+                    redisUtil.set(responseKey, JSON.toJSONString(result), APPLY_IN_TASK_RESPONSE_TTL_SECONDS);
+                    News.info("寮傛WMS鍏ュ簱璇锋眰瀹屾垚锛宐arcode={}锛宻tationId={}锛宼askNo={}锛宻tatus={}锛宺esponse={}",
+                            request.getBarcode(), request.getSourceStaNo(), request.getTaskNo(),
+                            result.getStatus(), response);
                 } catch (Exception e) {
                     News.error("寮傛WMS鍏ュ簱璇锋眰寮傚父锛宐arcode={}锛宻tationId={}锛宼askNo={}锛宔rror={}",
-                            barcode, sourceStaNo, taskNo, e.getMessage());
-                    redisUtil.set(responseKey, "ERROR:" + e.getMessage(), 10);
+                            request.getBarcode(), request.getSourceStaNo(), request.getTaskNo(), e.getMessage());
+                    AsyncInTaskResult result = buildAsyncInTaskResult(request, null, e);
+                    redisUtil.set(responseKey, JSON.toJSONString(result), APPLY_IN_TASK_RESPONSE_TTL_SECONDS);
                 } finally {
                     asyncInTaskInflight.remove(requestKey);
                     redisUtil.del(requestKey);
@@ -226,9 +265,13 @@
         } catch (RejectedExecutionException e) {
             asyncInTaskInflight.remove(requestKey);
             redisUtil.del(requestKey);
-            redisUtil.set(responseKey, "ERROR:ASYNC_QUEUE_FULL", 10);
+            AsyncInTaskResult result = new AsyncInTaskResult();
+            result.setBizKey(buildAsyncInTaskBizKey(request));
+            result.setStatus(AsyncInTaskStatus.RETRYABLE_FAIL);
+            result.setMessage("ERROR:ASYNC_QUEUE_FULL");
+            redisUtil.set(responseKey, JSON.toJSONString(result), APPLY_IN_TASK_RESPONSE_TTL_SECONDS);
             News.error("寮傛WMS鍏ュ簱璇锋眰琚嫆缁濓紝绾跨▼姹犲凡婊★紝barcode={}锛宻tationId={}锛宼askNo={}",
-                    barcode, sourceStaNo, taskNo);
+                    request.getBarcode(), request.getSourceStaNo(), request.getTaskNo());
         }
     }
 
@@ -244,14 +287,68 @@
     }
 
     public String queryAsyncInTaskResponse(String barcode, Integer stationId, Integer taskNo) {
-        String responseKey = buildAsyncInTaskKey(RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key, barcode, stationId, taskNo);
-        Object response = redisUtil.get(responseKey);
-        if (response != null) {
-            // 鑾峰彇鍚庡垹闄わ紝閬垮厤閲嶅澶勭悊
-            redisUtil.del(responseKey);
-            return response.toString();
+        InTaskApplyRequest request = new InTaskApplyRequest();
+        request.setBarcode(barcode);
+        request.setSourceStaNo(stationId);
+        request.setTaskNo(taskNo);
+        AsyncInTaskResult result = queryAsyncInTaskResponse(request);
+        if (result == null) {
+            return null;
         }
-        return null;
+        clearAsyncInTaskResponse(request);
+        if (result.isSuccess()) {
+            return result.getResponse();
+        }
+        if (!Cools.isEmpty(result.getMessage())) {
+            return result.getMessage();
+        }
+        return result.getResponse();
+    }
+
+    public AsyncInTaskResult queryAsyncInTaskResponse(InTaskApplyRequest request) {
+        String responseKey = buildAsyncInTaskKey(RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key, request);
+        Object response = redisUtil.get(responseKey);
+        if (response == null) {
+            return null;
+        }
+        return parseAsyncInTaskResult(buildAsyncInTaskBizKey(request), response.toString());
+    }
+
+    public void clearAsyncInTaskResponse(InTaskApplyRequest request) {
+        String responseKey = buildAsyncInTaskKey(RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key, request);
+        redisUtil.del(responseKey);
+    }
+
+    private AsyncInTaskResult parseAsyncInTaskResult(String bizKey, String responseValue) {
+        AsyncInTaskResult result = null;
+        try {
+            JSONObject jsonObject = JSON.parseObject(responseValue);
+            if (jsonObject != null && jsonObject.containsKey("status")) {
+                result = jsonObject.toJavaObject(AsyncInTaskResult.class);
+            } else if (jsonObject != null && jsonObject.containsKey("code")) {
+                result = new AsyncInTaskResult();
+                result.setBizKey(bizKey);
+                result.setResponse(responseValue);
+                if (Integer.valueOf(200).equals(jsonObject.getInteger("code"))) {
+                    result.setStatus(AsyncInTaskStatus.SUCCESS);
+                } else {
+                    result.setStatus(AsyncInTaskStatus.RETRYABLE_FAIL);
+                    result.setMessage(responseValue);
+                }
+            }
+        } catch (Exception ignored) {
+        }
+
+        if (result != null) {
+            return result;
+        }
+
+        result = new AsyncInTaskResult();
+        result.setBizKey(bizKey);
+        result.setResponse(responseValue);
+        result.setStatus(AsyncInTaskStatus.RETRYABLE_FAIL);
+        result.setMessage(responseValue);
+        return result;
     }
 
     /**
@@ -266,10 +363,48 @@
     }
 
     public boolean isAsyncRequestInProgress(String barcode, Integer stationId, Integer taskNo) {
-        String requestKey = buildAsyncInTaskKey(RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key, barcode, stationId, taskNo);
+        InTaskApplyRequest request = new InTaskApplyRequest();
+        request.setBarcode(barcode);
+        request.setSourceStaNo(stationId);
+        request.setTaskNo(taskNo);
+        return isAsyncRequestInProgress(request);
+    }
+
+    public boolean isAsyncRequestInProgress(InTaskApplyRequest request) {
+        String requestKey = buildAsyncInTaskKey(RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key, request);
         return asyncInTaskInflight.containsKey(requestKey) || redisUtil.get(requestKey) != null;
     }
 
+    private AsyncInTaskResult buildAsyncInTaskResult(InTaskApplyRequest request, String response, Exception exception) {
+        AsyncInTaskResult result = new AsyncInTaskResult();
+        result.setBizKey(buildAsyncInTaskBizKey(request));
+        result.setResponse(response);
+        if (exception != null) {
+            result.setStatus(AsyncInTaskStatus.RETRYABLE_FAIL);
+            result.setMessage("ERROR:" + exception.getMessage());
+            return result;
+        }
+
+        if (response == null) {
+            result.setStatus(AsyncInTaskStatus.RETRYABLE_FAIL);
+            result.setMessage("FAILED");
+            return result;
+        }
+
+        try {
+            JSONObject jsonObject = JSON.parseObject(response);
+            if (jsonObject != null && Integer.valueOf(200).equals(jsonObject.getInteger("code"))) {
+                result.setStatus(AsyncInTaskStatus.SUCCESS);
+                return result;
+            }
+        } catch (Exception ignored) {
+        }
+
+        result.setStatus(AsyncInTaskStatus.RETRYABLE_FAIL);
+        result.setMessage(response);
+        return result;
+    }
+
     // 鐢宠浠诲姟閲嶆柊鍒嗛厤搴撲綅
     public synchronized String applyReassignTaskLocNo(Integer taskNo, Integer stationId) {
         String wmsUrl = null;

--
Gitblit v1.9.1