From 450929e258dec4af0cd0519e23d1d4cf0bfac687 Mon Sep 17 00:00:00 2001
From: TanYibin <5491541@qq.com>
Date: Mon, 16 Oct 2023 14:15:39 +0800
Subject: [PATCH] 对接WCS获取可用巷道API,并设置开关,开关开启时,获取可用巷道库存信息

---
 huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsService.java                                               | 11 +++++++++++
 huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java                                           | 31 +++++++++++++++++++++++++++++++
 huaheng-wms-core/src/main/java/org/jeecg/modules/wms/shipment/shipmentCombination/service/impl/ShipmentCombinationServiceImpl.java | 29 +++++++++++++++++++++++++++++
 huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java                                                                    | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java                                                      |  9 +++++++++
 5 files changed, 149 insertions(+), 3 deletions(-)

diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsService.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsService.java
index 63399c8..1823f1d 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsService.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsService.java
@@ -1,5 +1,7 @@
 package org.jeecg.modules.wms.api.wcs.service;
 
+import java.util.List;
+
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.modules.wms.api.wcs.entity.MaterialInfoEntity;
 import org.jeecg.modules.wms.api.wcs.entity.WarecellDomain;
@@ -58,4 +60,13 @@ public interface WcsService {
      */
     Result manyEmptyOut(String zoneCode, String port, String warehouseCode);
 
+    /**
+     * 获取可用巷道
+     * @author     TanYibin
+     * @createDate 2023年10月16日
+     * @param zoneCode
+     * @return
+     */
+    Result<List<Integer>> getAvailableRowdway(String zoneCode);
+
 }
diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java
index 8c9ba4f..d7938ab 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java
@@ -41,13 +41,16 @@ import org.jeecg.modules.wms.task.taskHeader.service.ITaskHeaderService;
 import org.jeecg.utils.OkHttpUtils;
 import org.jeecg.utils.StringUtils;
 import org.jeecg.utils.constant.QuantityConstant;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.TypeReference;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.monitor4all.logRecord.annotation.OperationLog;
 import cn.monitor4all.logRecord.context.LogRecordContext;
 import lombok.extern.slf4j.Slf4j;
@@ -286,6 +289,34 @@ public class WcsServiceImpl implements WcsService {
         log.info("完成分配库位,任务号:" + taskNo + ", 库位编码:" + locationCode);
         return Result.OK(wcsTask);
     }
+    
+    @Override
+    @Cacheable(cacheNames = "getAvailableRowdway#10", key = "#root.methodName + '_' + #zoneCode", unless = "#result == null ")
+    public Result<List<Integer>> getAvailableRowdway(String zoneCode) {
+        WcsTask wcsTask = new WcsTask();
+        wcsTask.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
+        List<Integer> resultList = CollUtil.newArrayList(1, 2, 3, 4);
+        String value = parameterConfigurationService.getValueByCode(QuantityConstant.RULE_CONNECT_WCS);
+        if (StringUtils.isEmpty(value)) {
+            throw new JeecgBootException("下发任务时,没有找到连接WCS的数据配置");
+        }
+        int connectWCS = Integer.parseInt(value);
+        if (connectWCS == QuantityConstant.RULE_WCS_CONNECT) {
+            String url = addressService.getUrlByParam(QuantityConstant.RULE_AVAILABLE_ROWDWAY, QuantityConstant.DEFAULT_WAREHOUSE, zoneCode);
+            String jsonParam = JSON.toJSONString(wcsTask);
+            String body = OkHttpUtils.sendPostByJsonStrFast(url, jsonParam);
+            if (StringUtils.isEmpty(body)) {
+                throw new JeecgBootException("接口地址错误或返回为空,url:" + url);
+            }
+            Result<List<Integer>> result = JSON.parseObject(body, new TypeReference<Result<List<Integer>>>() {});
+            if (result.getCode() != QuantityConstant.HTTP_OK) {
+                throw new JeecgBootException(result.getMessage());
+            } else {
+                return result;
+            }
+        }
+        return Result.OK(resultList);
+    }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/shipment/shipmentCombination/service/impl/ShipmentCombinationServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/shipment/shipmentCombination/service/impl/ShipmentCombinationServiceImpl.java
index 81c729f..1f98fb1 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/shipment/shipmentCombination/service/impl/ShipmentCombinationServiceImpl.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/shipment/shipmentCombination/service/impl/ShipmentCombinationServiceImpl.java
@@ -10,6 +10,7 @@ import javax.annotation.Resource;
 
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.exception.JeecgBootException;
+import org.jeecg.modules.wms.api.wcs.service.WcsService;
 import org.jeecg.modules.wms.config.container.entity.Container;
 import org.jeecg.modules.wms.config.container.service.IContainerService;
 import org.jeecg.modules.wms.config.location.entity.Location;
@@ -48,12 +49,14 @@ import org.jeecg.utils.constant.QuantityConstant;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
 
 import com.alibaba.fastjson.JSON;
 import com.aliyun.oss.ServiceException;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.monitor4all.logRecord.annotation.OperationLog;
 import cn.monitor4all.logRecord.context.LogRecordContext;
 import lombok.extern.slf4j.Slf4j;
@@ -67,37 +70,55 @@ public class ShipmentCombinationServiceImpl implements IShipmentCombinationServi
 
     @Autowired
     private IShipmentCombinationService shipmentCombinationService;
+
     @Autowired
     private IInventoryDetailService inventoryDetailService;
+
     @Autowired
     private IShipmentHeaderService shipmentHeaderService;
+
     @Autowired
     private IShipmentDetailService shipmentDetailService;
+
     @Autowired
     private IReceiptContainerHeaderService receiptContainerHeaderService;
+
     @Autowired
     private IShipmentContainerHeaderService shipmentContainerHeaderService;
+
     @Autowired
     private IContainerService containerService;
+
     @Autowired
     private ILocationService locationService;
+
     @Resource
     private IZoneService zoneService;
+
     @Autowired
     private IShipmentContainerDetailService shipmentContainerDetailService;
+
     @Autowired
     private IMaterialService materialService;
+
     @Autowired
     private IParameterConfigurationService parameterConfigurationService;
+
     @Autowired
     private ITaskHeaderService taskHeaderService;
+
     @Autowired
     private ITaskDetailService taskDetailService;
+
     @Autowired
     private IInventoryHeaderService inventoryHeaderService;
+
     @Resource
     private IInventoryTransactionService inventoryTransactionService;
 
+    @Resource
+    private WcsService wcsService;
+
     /**
      * 根据出库单详情,匹配到可用出库的库存详情
      * 默认匹配条件是 仓库,货主,物料编码,库存状态,批次 是一致的就可以出库
@@ -138,6 +159,14 @@ public class ShipmentCombinationServiceImpl implements IShipmentCombinationServi
             }
         }
         List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
+        String availableRowdwayStatus = parameterConfigurationService.getValueByCode(QuantityConstant.AVAILABLE_ROWDWAY_OPEN);
+        if (!CollectionUtils.isEmpty(inventoryDetailList) && QuantityConstant.AVAILABLE_ROWDWAY_OPEN.equals(availableRowdwayStatus)) {
+            Result<List<Integer>> result = wcsService.getAvailableRowdway(zoneCode);
+            if (!result.isSuccess() || CollectionUtils.isEmpty(result.getResult())) {
+                return CollUtil.newArrayList();
+            }
+            inventoryDetailList = inventoryDetailList.stream().filter(t -> result.getResult().contains(t.getRoadWay())).collect(Collectors.toList());
+        }
         return inventoryDetailList;
     }
 
diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java
index bc75b68..f281310 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java
@@ -41,7 +41,7 @@ public class OkHttpUtils {
     /**
      * 最多重试次数
      */
-    public final static int MAX_RENTRY_COUNT = 3;
+    public final static int MAX_RENTRY_COUNT = 1;
 
     /**
      * 最大连接时间(秒)
@@ -51,12 +51,12 @@ public class OkHttpUtils {
     /**
      * 最大读取时间(秒)
      */
-    public final static int READ_TIMEOUT = 30;
+    public final static int READ_TIMEOUT = 20;
 
     /**
      * 最大写入时间(秒)
      */
-    public final static int WRITE_TIMEOUT = 30;
+    public final static int WRITE_TIMEOUT = 20;
 
     /**
      * JSON格式
@@ -72,6 +72,22 @@ public class OkHttpUtils {
      * OkHTTP线程池空闲线程存活时间(秒)
      */
     public final static long KEEP_ALIVE_DURATION = 60;
+    
+    /**
+     * 最大读取时间(秒)
+     */
+    public final static int READ_TIMEOUT_FAST = 5;
+
+    /**
+     * 最大写入时间(秒)
+     */
+    public final static int WRITE_TIMEOUT_FAST = 5;
+
+    /**
+     * 最多重试次数
+     */
+    public final static int MAX_RENTRY_COUNT_FAST = 0;
+
 
     private static final String CONTENT_TYPE = "Content-Type";
 
@@ -81,6 +97,12 @@ public class OkHttpUtils {
             .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS).addInterceptor(new OkHttpUtils.OkhttpInterceptor(MAX_RENTRY_COUNT)) // 过滤器,设置最大重试次数
             .retryOnConnectionFailure(false).connectionPool(new ConnectionPool(MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION, TimeUnit.SECONDS)).build();
 
+    /** 访问接口参数配置 */
+    private final static OkHttpClient HTTP_CLIENT_FAST =
+        new OkHttpClient.Builder().connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS).readTimeout(READ_TIMEOUT_FAST, TimeUnit.SECONDS)
+            .writeTimeout(WRITE_TIMEOUT_FAST, TimeUnit.SECONDS).addInterceptor(new OkHttpUtils.OkhttpInterceptor(MAX_RENTRY_COUNT_FAST)) // 过滤器,设置最大重试次数
+            .retryOnConnectionFailure(false).connectionPool(new ConnectionPool(MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION, TimeUnit.SECONDS)).build();
+
     /**
      * 向指定 URL 发送GET方法的请求
      * @param  url       发送请求的 URL
@@ -171,6 +193,50 @@ public class OkHttpUtils {
         }
         return result;
     }
+    
+    /**
+     * JSONString形式发送POST请求
+     * @throws Exception
+     * @throws IOException
+     */
+    public static String sendPostByJsonStrFast(String url, Map<String, String> headers, String jsonString) {
+        String result = null;
+        if (StringUtils.isEmpty(url)) {
+            return result;
+        }
+        ApiLog apiLog = new ApiLog();
+        headers.put("Accept", "application/json");// 设置接收数据的格式
+        headers.put("Content-Type", "application/json");// 设置发送数据的格式
+        Request.Builder builder = new Request.Builder();
+        buildHeader(builder, headers);
+        RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, jsonString);
+        Request request = builder.url(url).post(body).build();
+        Response response = null;
+        try {
+            ApiLoggerAspect.initApiLog(apiLog, request, jsonString);
+            response = HTTP_CLIENT_FAST.newCall(request).execute();
+            result = response.body().string();
+        } catch (Exception e) {
+            String errorString = StrUtil.format("执行POST请求异常,url: {},{}", url, ExceptionUtil.getMessage(e));
+            log.error(errorString, e);
+            ApiLoggerAspect.setApiLogException(apiLog, e);
+            throw new JeecgBootException(errorString, e);
+        } finally {
+            ApiLoggerAspect.finishApiLog(apiLog, response, result);
+        }
+        // 调用成功
+        if (response.isSuccessful() && Objects.nonNull(response.body())) {
+            log.info("执行POST请求成功,url: {},result: {}", url, result);
+        } else {
+            throw new JeecgBootException(StrUtil.format("执行POST请求失败,url: {},responseCode: {},responseMessage: {}", url, JSON.toJSONString(headers), jsonString,
+                response.code(), response.message()));
+        }
+        return result;
+    }
+
+    public static String sendPostByJsonStrFast(String url, String jsonString) {
+        return sendPostByJsonStrFast(url, new HashMap<String, String>(), jsonString);
+    }
 
     /**
      * 设置请求头
diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java
index 6a96874..ae29745 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java
@@ -511,6 +511,15 @@ public class QuantityConstant {
      * 出库依赖库区
      */
     public static final int SHIPMENT_BY_ZONE = 1;
+    
+    /** 获取可用巷道:关  */
+    public static final String AVAILABLE_ROWDWAY_CLOSE = "0";
+    
+    /** 获取可用巷道:开 */
+    public static final String AVAILABLE_ROWDWAY_OPEN = "1";
+    
+    /** 获取可用巷道:开 */
+    public static final String RULE_AVAILABLE_ROWDWAY = "RULE_AVAILABLE_ROWDWAY";
 
     public static final int RULE_TASK_NOT_CLEAR = 0;
     public static final int RULE_TASK_AllOW_CLEAR = 1;
--
libgit2 0.22.2