1
0
Bläddra i källkod

修改库存接口查询数据结构,修改库存接口数据组装、查询流程

lvjian 2 år sedan
förälder
incheckning
ac935b1e25

+ 16 - 0
eccs-biz/src/main/java/com/emato/biz/controller/mall/InventoryDataController.java

@@ -2,8 +2,10 @@ package com.emato.biz.controller.mall;
 
 
 import com.alibaba.fastjson.JSONObject;
+import com.emato.biz.domain.OutRequest;
 import com.emato.biz.domain.mall.InventoryDataVo;
 import com.emato.biz.service.mall.ISalesDetaiServicel;
+import com.emato.biz.service.mall.InventoryService;
 import com.emato.common.annotation.AnonymousAccess;
 import com.emato.common.core.Result;
 import org.slf4j.Logger;
@@ -32,6 +34,9 @@ public class InventoryDataController {
     @Autowired
     private ISalesDetaiServicel salesDetaiServicel;
 
+    @Autowired
+    private InventoryService inventoryService;
+
     /**
      * 接收oms向eccs系统推送的库存数据
      * @param inventoryDataVo
@@ -60,5 +65,16 @@ public class InventoryDataController {
         return salesDetaiServicel.queryInventory(msg,httpRequest);
     }
 
+    /**
+     * 获取库存数据
+     *
+     * @param outRequest
+     * @return
+     */
+    @AnonymousAccess
+    @PostMapping("/getInventoryData")
+    public Result getInventoryData(@RequestBody OutRequest outRequest) {
+        return inventoryService.getInventory(outRequest);
+    }
 
 }

+ 74 - 0
eccs-biz/src/main/java/com/emato/biz/domain/OutRequest.java

@@ -0,0 +1,74 @@
+package com.emato.biz.domain;
+
+import java.io.Serializable;
+
+/**
+ * 统一外部请求类
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2023/2/23 14:40
+ */
+public class OutRequest implements Serializable {
+    /**
+     * 商户号
+     */
+    private String merchId;
+
+    /**
+     * 业务数据
+     */
+    private String data;
+
+    /**
+     * 时间戳
+     */
+    private String timestamp;
+
+    /**
+     * 签名
+     */
+    private String sign;
+
+    @Override
+    public String toString() {
+        return "OutRequest{" +
+                "merchId='" + merchId + '\'' +
+                ", data='" + data + '\'' +
+                ", timestamp='" + timestamp + '\'' +
+                ", sign='" + sign + '\'' +
+                '}';
+    }
+
+    public String getMerchId() {
+        return merchId;
+    }
+
+    public void setMerchId(String merchId) {
+        this.merchId = merchId;
+    }
+
+    public String getData() {
+        return data;
+    }
+
+    public void setData(String data) {
+        this.data = data;
+    }
+
+    public String getTimestamp() {
+        return timestamp;
+    }
+
+    public void setTimestamp(String timestamp) {
+        this.timestamp = timestamp;
+    }
+
+    public String getSign() {
+        return sign;
+    }
+
+    public void setSign(String sign) {
+        this.sign = sign;
+    }
+}

+ 46 - 0
eccs-biz/src/main/java/com/emato/biz/domain/mall/InventoryReqVO.java

@@ -0,0 +1,46 @@
+package com.emato.biz.domain.mall;
+
+
+/**
+ * 库存请求 VO
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2023/3/31 16:15
+ */
+public class InventoryReqVO {
+
+    /**
+     * 分页页码
+     */
+    private String pageIndex;
+
+    /**
+     * 分页大小
+     */
+    private String pageSize;
+
+    @Override
+    public String toString() {
+        return "InventoryReqVO{" +
+                "pageIndex=" + pageIndex +
+                ", pageSize=" + pageSize +
+                '}';
+    }
+
+    public String getPageIndex() {
+        return pageIndex;
+    }
+
+    public void setPageIndex(String pageIndex) {
+        this.pageIndex = pageIndex;
+    }
+
+    public String getPageSize() {
+        return pageSize;
+    }
+
+    public void setPageSize(String pageSize) {
+        this.pageSize = pageSize;
+    }
+}

+ 104 - 0
eccs-biz/src/main/java/com/emato/biz/exception/Assert.java

@@ -0,0 +1,104 @@
+package com.emato.biz.exception;
+
+
+
+import com.emato.common.exception.CustomException;
+import com.emato.common.utils.StringUtils;
+
+import java.util.Objects;
+
+/**
+ * 断言类
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2022/12/21 10:39
+ */
+public abstract class Assert {
+
+    /**
+     * 断言对象不为空
+     * 如果对象obj为空,则抛出异常
+     * @param obj 待判断对象
+     */
+    public static void notNull(Object obj, String msg) {
+        if (Objects.isNull(obj)) {
+            throw new CustomException(msg);
+        }
+    }
+
+
+    /**
+     * 断言对象为空
+     * 如果对象obj不为空,则抛出异常
+     * @param object
+     * @param msg
+     */
+    public static void isNull(Object object, String msg) {
+        if (Objects.nonNull(object)) {
+            throw new CustomException(msg);
+        }
+    }
+
+    /**
+     * 断言表达式为真
+     * 如果为假,则抛出异常
+     *
+     * @param expression 是否成功
+     */
+    public static void isTrue(boolean expression, String msg) {
+        if (!expression) {
+            throw new CustomException(msg);
+        }
+    }
+
+    /**
+     * 断言表达式为假
+     * 如果为真,则抛出异常
+     *
+     * @param expression 是否成功
+     */
+    public static void notTrue(boolean expression, String msg) {
+        if (expression) {
+            throw new CustomException(msg);
+        }
+    }
+
+    /**
+     * 断言两个对象不相等
+     * 如果相等,则抛出异常
+     * @param m1
+     * @param m2
+     * @param msg
+     */
+    public static void notEquals(Object m1, Object m2,  String msg) {
+        if (m1.equals(m2)) {
+            throw new CustomException(msg);
+        }
+    }
+
+    /**
+     * 断言两个对象相等
+     * 如果不相等,则抛出异常
+     * @param m1
+     * @param m2
+     * @param msg
+     */
+    public static void equals(Object m1, Object m2,  String msg) {
+        if (!m1.equals(m2)) {
+            throw new CustomException(msg);
+        }
+    }
+
+    /**
+     * 断言参数不为空
+     * 如果为空,则抛出异常
+     * @param s
+     * @param msg
+     */
+    public static void notEmpty(String s, String msg) {
+        if (StringUtils.isBlank(s)) {
+            throw new CustomException(msg);
+        }
+    }
+}

+ 2 - 1
eccs-biz/src/main/java/com/emato/biz/mapper/mall/InventoryDataMapper.java

@@ -2,6 +2,7 @@ package com.emato.biz.mapper.mall;
 
 import com.emato.biz.domain.mall.InventoryDataPushVo;
 import com.emato.biz.domain.mall.InventoryDataVo;
+import com.emato.biz.domain.mall.InventoryReqVO;
 
 import java.util.List;
 import java.util.Map;
@@ -10,7 +11,7 @@ public interface InventoryDataMapper {
     void inserInventory(InventoryDataVo inventoryDataVo);
 
 
-    List<InventoryDataPushVo> getInventoryData(Map<String,Object> map);
+    List<InventoryDataPushVo> getInventoryData(int pageIndex, int pageSize);
 
     Integer queryOneInventory(InventoryDataVo inventoryDataVo);
 

+ 23 - 0
eccs-biz/src/main/java/com/emato/biz/service/SignService.java

@@ -0,0 +1,23 @@
+package com.emato.biz.service;
+
+
+import com.emato.biz.domain.OutRequest;
+
+/**
+ * 验签接口
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2023/2/23 14:46
+ */
+public interface SignService {
+
+    /**
+     * 验签
+     *
+     * @param outRequest
+     * @return true:验签成功, false:验签失败
+     */
+    boolean verifySign(OutRequest outRequest);
+
+}

+ 122 - 0
eccs-biz/src/main/java/com/emato/biz/service/impl/InventoryServiceImpl.java

@@ -0,0 +1,122 @@
+package com.emato.biz.service.impl;
+
+import com.alibaba.fastjson.JSON;
+import com.emato.biz.domain.OutRequest;
+import com.emato.biz.domain.mall.InventoryDataPushVo;
+import com.emato.biz.domain.mall.InventoryDataVo;
+import com.emato.biz.domain.mall.InventoryReqVO;
+import com.emato.biz.domain.merchant.ThirdMerchantBiz;
+import com.emato.biz.exception.Assert;
+import com.emato.biz.mapper.mall.InventoryDataMapper;
+import com.emato.biz.service.SignService;
+import com.emato.biz.service.mall.InventoryService;
+import com.emato.biz.service.merchant.IThirdMerchantBizService;
+import com.emato.common.core.Result;
+import com.emato.common.exception.CustomException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.time.LocalDateTime;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 库存业务接口实现类
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2023/3/31 16:00
+ */
+@Service
+public class InventoryServiceImpl implements InventoryService {
+
+    private static final Logger log = LoggerFactory.getLogger(InventoryServiceImpl.class);
+
+    @Autowired
+    private SignService signService;
+
+    @Autowired
+    private IThirdMerchantBizService thirdMerService;
+
+    @Resource
+    private InventoryDataMapper inventoryDataMapper;
+
+    /**
+     * 查询库存数据
+     *
+     * @param outRequest
+     * @return
+     */
+    @Override
+    public Result getInventory(OutRequest outRequest) {
+        try {
+            log.info("---------- 查询库存数据开始 ----------");
+            log.info("========== 电商请求 eccs 报文 =========> {}", outRequest);
+            // 验签
+            Assert.notTrue(!signService.verifySign(outRequest), "验签失败!");
+            // 转为请求类
+            InventoryReqVO reqVO = JSON.parseObject(outRequest.getData(), InventoryReqVO.class);
+            log.debug("========== 解析后的请求数据 ==========> {}", reqVO);
+            // 分页页码
+            String pageIndexStr = reqVO.getPageIndex();
+            // 分页大小
+            String pageSizeStr = reqVO.getPageSize();
+
+            // 参数校验
+            Assert.notNull(pageIndexStr, "分页页码不能为空!");
+            Assert.notNull(pageSizeStr, "分页大小不能为空!");
+            Assert.notTrue(!isPositiveInteger(pageIndexStr), "分页页码请填入正整数!");
+            Assert.notTrue(!isPositiveInteger(pageSizeStr), "分页大小请填入正整数!");
+
+            // 分页大小
+            int pageSize = Integer.parseInt(pageSizeStr);
+            // 大于500就取500,否则取 pageSize
+            pageSize = Math.min(pageSize, 500);
+
+            int pageIndex = Integer.parseInt(pageIndexStr);
+            // 小于1就取1,否则取 pageIndex
+            pageIndex = Math.max(pageIndex, 1);
+            pageIndex = (pageIndex - 1) * pageSize;
+
+            // 查询参数
+            List<InventoryDataPushVo> inventoryData = inventoryDataMapper.getInventoryData(pageIndex, pageSize);
+
+            // 组装返回数据
+            Map<String, Object> resultMap = new HashMap<>();
+            resultMap.put("row", inventoryData);
+            resultMap.put("invTime", LocalDateTime.now().toString());
+
+            log.info("---------- 查询库存数据结束 ----------");
+            return Result.success(resultMap);
+        }catch (CustomException ce){
+            // 交给统一异常处理
+            throw ce;
+        } catch (Exception e) {
+            log.error("查询库存数据异常 => ", e);
+            throw new CustomException("查询库存数据异常,请稍后再试!");
+        }
+    }
+
+
+    /**
+     * 判断是否是正整数
+     *
+     * @param str
+     * @return
+     */
+    private static boolean isPositiveInteger(String str) {
+        if (str == null || str.isEmpty()) {
+            return false;
+        }
+        // 正则表达式: ^ 表示开头,\\d+ 表示一位或多位数字,$ 表示结尾
+        // 如果要包含0,则将 + 改为 *
+        return str.matches("^\\d+$");
+    }
+
+
+}

+ 102 - 0
eccs-biz/src/main/java/com/emato/biz/service/impl/SignServiceImpl.java

@@ -0,0 +1,102 @@
+package com.emato.biz.service.impl;
+
+import com.emato.biz.domain.OutRequest;
+import com.emato.biz.domain.merchant.MerchantSecret;
+import com.emato.biz.exception.Assert;
+import com.emato.biz.service.SignService;
+import com.emato.biz.service.merchant.IMerchantSecretService;
+import com.emato.common.utils.oms.request.OmsSign;
+import com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.time.Duration;
+import java.time.Instant;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 验签业务实现类
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2023/2/23 14:48
+ */
+@Service
+public class SignServiceImpl implements SignService {
+
+    private static final Logger log = LoggerFactory.getLogger(SignServiceImpl.class);
+
+    @Autowired
+    private IMerchantSecretService merchantSecretService;
+
+    /**
+     * 验签
+     *
+     * @param outRequest
+     * @return true:验签成功, false:验签失败
+     */
+    @Override
+    public boolean verifySign(OutRequest outRequest) {
+        log.info("----------验签开始----------");
+        String merchId = outRequest.getMerchId();            //接口商户账户
+        String sign = outRequest.getSign();               //接口签名
+        String timestamp = outRequest.getTimestamp();     //时间戳
+        Object data = outRequest.getData();                //外部系统推送报文
+        log.info("商户ID:【{}】",merchId);
+        log.info("接口签名【{}】",sign);
+        log.info("时间戳【{}】",timestamp);
+        log.info("业务请求数据:【{}】",data);
+        // 数据空值校验
+        emptyCheck(merchId, sign, timestamp, data);
+        // 校验时间戳是否过期
+        validateTimestamp(timestamp);
+        // 查询商户密钥
+        MerchantSecret merchantSecret = new MerchantSecret();
+        merchantSecret.setMerchSn(merchId);
+        List<MerchantSecret> merchantSecrets = merchantSecretService.selectMerchantSecretList(merchantSecret);
+        Assert.notTrue(merchantSecrets.isEmpty(), "商户密钥不存在!");
+        String md5Salt = merchantSecrets.get(0).getMd5Salt();
+        // 封装校验信息
+        Map params = Maps.newLinkedHashMap();
+        params.put("data", outRequest.getData());
+        params.put("merchId", outRequest.getMerchId());
+        params.put("timestamp", outRequest.getTimestamp());
+        boolean verify = OmsSign.verify(sign, params, md5Salt);
+        log.info("----------验签结束----------");
+        // 返回验签结果
+        return verify;
+    }
+
+    /**
+     * 校验时间戳
+     *
+     * @param timestamp
+     */
+    private void validateTimestamp(String timestamp) {
+        long targetMilli = Long.parseLong(timestamp);
+        Instant target = Instant.ofEpochMilli(targetMilli);
+        // 将当前时间戳转换为 Instant 对象
+        Instant now = Instant.now();
+        Duration duration = Duration.between(target, now);
+        boolean b = duration.toMinutes() > 5L;
+        Assert.notTrue(b, "时间戳超时!");
+    }
+
+    /**
+     * 空值校验
+     *
+     * @param merchId
+     * @param sign
+     * @param timestamp
+     * @param data
+     */
+    private void emptyCheck(String merchId, String sign, String timestamp, Object data) {
+        Assert.notEmpty(merchId, "商户号不能为空!");
+        Assert.notEmpty(sign, "接口签名不能为空!");
+        Assert.notEmpty(timestamp, "时间戳不能为空!");
+        Assert.notNull(data, "业务请求数据不能为空!");
+    }
+}

+ 22 - 0
eccs-biz/src/main/java/com/emato/biz/service/mall/InventoryService.java

@@ -0,0 +1,22 @@
+package com.emato.biz.service.mall;
+
+import com.emato.biz.domain.OutRequest;
+import com.emato.common.core.Result;
+
+/**
+ * 库存业务接口
+ *
+ * @author frankeleyn
+ * @email lvjian@qhdswl.com
+ * @date 2023/3/31 15:44
+ */
+public interface InventoryService {
+
+    /**
+     * 查询库存数据
+     *
+     * @param outRequest
+     * @return
+     */
+    Result getInventory(OutRequest outRequest);
+}

+ 27 - 9
eccs-biz/src/main/resources/mapper/biz/mall/InventoryDataMapper.xml

@@ -4,19 +4,37 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.emato.biz.mapper.mall.InventoryDataMapper">
 
+    <resultMap id="inventoryDataVO" type="com.emato.biz.domain.mall.InventoryDataPushVo">
+        <result column="prod_barcode" property="barcode" />
+        <result column="name" property="productName"/>
+        <result column="shop_sn" property="shopSn" />
+        <result column="shop_inve" property="shopInvent" />
+        <result column="valid_num" property="eMatou" />
+    </resultMap>
+
 
     <!--    外部接口查询-->
-    <select id="getInventoryData"  parameterType="java.util.Map" resultType="com.emato.biz.domain.mall.InventoryDataPushVo">
+    <select id="getInventoryData"  parameterType="com.emato.biz.domain.mall.InventoryReqVO" resultMap="inventoryDataVO">
         SELECT
-            bar_code as barcode,
-            product_name as productName,
-            shop_name as shopName,
-            shop_invent as shopInvent,
-            e_matou as eMatou
+            t.sku,
+            t.prod_barcode,
+            t.plu,
+            t.mychem_id,
+            t.name,
+            t1.shop_sn,
+            t1.shop_inve,
+            t2.valid_num
         FROM
-        o_inventory_data
-        <if test="offset != null and limit != null">
-            limit #{offset}, #{limit}
+            mall_goods t
+            LEFT JOIN wb_merch_shop_inve t1 ON t.sku = t1.sku
+            LEFT JOIN wb_inve_mng t2 ON t2.sku = t.sku
+        <where>
+            t1.is_valid = 0
+            AND t2.inve_status = 0
+            AND t2.is_valid = 0
+        </where>
+        <if test="pageIndex != null and pageSize != null">
+            limit #{pageIndex}, #{pageSize}
         </if>
     </select>