Sfoglia il codice sorgente

订单状态通知

叶君翔 3 anni fa
parent
commit
4a1ba83f63

+ 0 - 105
lb-app/src/main/java/com/ydd/app/consumer/DelayPushStoreStatusListener.java

@@ -1,105 +0,0 @@
-package com.ydd.app.consumer;
-
-
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import com.alibaba.fastjson.parser.Feature;
-import com.ydd.app.vo.OrderStatusNotifyVo;
-import com.ydd.app.vo.StoreStatusNotifyVo;
-import com.ydd.common.constant.RabbitConstant;
-import com.ydd.common.core.redis.RedisCache;
-import com.ydd.common.enums.ResponseResultCodeEnum;
-import com.ydd.common.utils.http.HttpUtils;
-import com.ydd.common.utils.sign.SignatureUtil;
-import com.ydd.ecloud.core.utils.JsonMapper;
-import com.ydd.module.constants.AppConstant;
-import com.ydd.module.domain.AppInfo;
-import com.ydd.module.expection.CustomAppException;
-import com.ydd.module.service.IAppInfoService;
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.amqp.rabbit.annotation.RabbitHandler;
-import org.springframework.amqp.rabbit.annotation.RabbitListener;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-
-@Slf4j
-@Component
-@RequiredArgsConstructor(onConstructor_ = @Autowired)
-public class DelayPushStoreStatusListener {
-
-    private final IAppInfoService iAppInfoService;
-
-    private final RedisCache redisCache;
-
-    @RabbitListener(queues = RabbitConstant.QUEUE_DELAY_OPEN_STATUS_NOTIFY)
-    @RabbitHandler
-    public void pushStoreStatusDelayListener(Object msg) {
-        log.info("收到延迟推送状态的消息:{}", msg);
-        try {
-            if (msg instanceof StoreStatusNotifyVo) {
-                StoreStatusNotifyVo storeStatusNotifyVo = (StoreStatusNotifyVo) msg;
-                Integer notifyCount = storeStatusNotifyVo.getNotifyCount();
-                log.info("第 {} 次推送状态通知, 通知内容: {}", notifyCount, JSONObject.toJSONString(storeStatusNotifyVo));
-                String key = storeStatusNotifyVo.getShopCode() + "-" + storeStatusNotifyVo.getStatus();
-                // 判断缓存是否有成功响应
-                if (redisCache.hasKey(key)) {
-                    return;
-                }
-                storeStatusNotifyVo.setTimeStamp(System.currentTimeMillis());
-                String json = JsonMapper.nonNullMapper().toJson(storeStatusNotifyVo);
-                HashMap<String, String> signParam = JSON.parseObject(json, LinkedHashMap.class, Feature.OrderedField);
-
-                AppInfo appInfo = iAppInfoService.getByAppId(storeStatusNotifyVo.getAppId());
-                if (appInfo == null) {
-                    throw new CustomAppException("应用不存在");
-                }
-                Set<String> exclude = new HashSet<>();
-                exclude.add("sign");
-                String sign = null;
-                try {
-                    // 获取签名
-                    sign = SignatureUtil.getRequestMD5Sign(signParam, exclude, appInfo.getAppSecret());
-                } catch (UnsupportedEncodingException e) {
-                    log.error("获取签名失败: {}", e.getMessage());
-                    e.printStackTrace();
-                }
-                storeStatusNotifyVo.setSign(sign);
-                String storeStatusNotifyVoStr = JSONObject.toJSONString(storeStatusNotifyVo);
-                try {
-                    storeStatusNotifyVoStr = URLEncoder.encode(storeStatusNotifyVoStr,"utf-8");
-                    log.info("门店状态同步请求参数: {}", storeStatusNotifyVoStr);
-                    String resp = HttpUtils.sendPost(appInfo.getStoreStatusNotifyUrl(), storeStatusNotifyVoStr);
-                    log.info("门店状态同步返回结果: {}", resp);
-                    // 收到成功响应后存入缓存, 后续不再推送
-                    if (ResponseResultCodeEnum.SUCCESS.getMessage().equals(resp)) {
-                        redisCache.setCacheObject(key, "SUCCESS", 24, TimeUnit.HOURS);
-                    } else {
-                        if (notifyCount == AppConstant.NOTIFY_INTERVAL_DURATION.size()) {
-                            // todo: 未收到三方的状态通知回复, 待处理: 增加入库记录...
-                            log.warn("门店状态通知结束, 未收到三方的成功响应");
-                        }
-                    }
-                } catch (Exception e) {
-                    log.error("门店状态同步异常: {}", e.getMessage());
-                    e.printStackTrace();
-                }
-            }
-            if (msg instanceof OrderStatusNotifyVo) {
-                // todo:处理订单状态推送
-            }
-        } catch (Exception e) {
-            log.error("状态推送异常", e);
-        }
-
-    }
-
-}

+ 158 - 0
lb-app/src/main/java/com/ydd/app/consumer/DelayStatusNotifyListener.java

@@ -0,0 +1,158 @@
+package com.ydd.app.consumer;
+
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.parser.Feature;
+import com.ydd.app.vo.OrderStatusNotifyVo;
+import com.ydd.app.vo.StoreStatusNotifyVo;
+import com.ydd.common.constant.RabbitConstant;
+import com.ydd.common.core.redis.RedisCache;
+import com.ydd.common.enums.ResponseResultCodeEnum;
+import com.ydd.common.utils.http.HttpUtils;
+import com.ydd.common.utils.sign.SignatureUtil;
+import com.ydd.ecloud.core.utils.JsonMapper;
+import com.ydd.module.constants.AppConstant;
+import com.ydd.module.domain.AppInfo;
+import com.ydd.module.expection.CustomAppException;
+import com.ydd.module.service.IAppInfoService;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+@Component
+@RequiredArgsConstructor(onConstructor_ = @Autowired)
+public class DelayStatusNotifyListener {
+
+    private final IAppInfoService iAppInfoService;
+
+    private final RedisCache redisCache;
+
+    @RabbitListener(queues = RabbitConstant.QUEUE_DELAY_OPEN_STATUS_NOTIFY)
+    @RabbitHandler
+    public void statusNotifyDelayListener(Object msg) {
+        log.info("收到延迟推送状态的消息:{}", msg);
+        try {
+            // 门店状态通知
+            if (msg instanceof StoreStatusNotifyVo) {
+                this.doStoreStatusNotify((StoreStatusNotifyVo) msg);
+            }
+            // 订单状态通知
+            if (msg instanceof OrderStatusNotifyVo) {
+                this.doOrderStatusNotify((OrderStatusNotifyVo) msg);
+            }
+        } catch (Exception e) {
+            log.error("状态推送异常", e);
+        }
+
+    }
+
+    private void doStoreStatusNotify(StoreStatusNotifyVo notifyVo) {
+        Integer notifyCount = notifyVo.getNotifyCount();
+        log.info("第 {} 次推送门店状态通知, 通知内容: {}", notifyCount, JSONObject.toJSONString(notifyVo));
+        String key = notifyVo.getShopCode() + "-" + notifyVo.getStatus();
+        // 若缓存在, 说明已经通知过并收到成功响应
+        if (redisCache.hasKey(key)) {
+            return;
+        }
+        notifyVo.setTimeStamp(System.currentTimeMillis());
+        String json = JsonMapper.nonNullMapper().toJson(notifyVo);
+        HashMap<String, String> signParam = JSON.parseObject(json, LinkedHashMap.class, Feature.OrderedField);
+
+        AppInfo appInfo = iAppInfoService.getByAppId(notifyVo.getAppId());
+        if (appInfo == null) {
+            throw new CustomAppException("应用不存在");
+        }
+        Set<String> exclude = new HashSet<>();
+        exclude.add("sign");
+        String sign = null;
+        try {
+            // 获取签名
+            sign = SignatureUtil.getRequestMD5Sign(signParam, exclude, appInfo.getAppSecret());
+        } catch (UnsupportedEncodingException e) {
+            log.error("获取签名失败: {}", e.getMessage());
+            e.printStackTrace();
+        }
+        notifyVo.setSign(sign);
+        String storeStatusNotifyVoStr = JSONObject.toJSONString(notifyVo);
+        try {
+            storeStatusNotifyVoStr = URLEncoder.encode(storeStatusNotifyVoStr,"utf-8");
+            log.info("门店状态同步请求参数: {}", storeStatusNotifyVoStr);
+            String resp = HttpUtils.sendPost(appInfo.getStoreStatusNotifyUrl(), storeStatusNotifyVoStr);
+            log.info("门店状态同步返回结果: {}", resp);
+            // 收到成功响应后存入缓存, 后续不再推送
+            if (ResponseResultCodeEnum.SUCCESS.getMessage().equals(resp)) {
+                redisCache.setCacheObject(key, "SUCCESS", 24, TimeUnit.HOURS);
+            } else {
+                if (notifyCount == AppConstant.NOTIFY_INTERVAL_DURATION.size()) {
+                    // todo: 未收到三方的状态通知回复, 待处理: 增加入库记录...
+                    log.warn("门店状态通知结束, 未收到三方的成功响应");
+                }
+            }
+        } catch (Exception e) {
+            log.error("门店状态同步异常: {}", e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+    private void doOrderStatusNotify(OrderStatusNotifyVo notifyVo) {
+        Integer notifyCount = notifyVo.getNotifyCount();
+        log.info("第 {} 次推送订单状态通知, 通知内容: {}", notifyCount, JSONObject.toJSONString(notifyVo));
+        String key = notifyVo.getOrderSn() + "-" + notifyVo.getStatus();
+        // 若缓存在, 说明已经通知过并收到成功响应
+        if (redisCache.hasKey(key)) {
+            return;
+        }
+        notifyVo.setTimeStamp(System.currentTimeMillis());
+        String json = JsonMapper.nonNullMapper().toJson(notifyVo);
+        HashMap<String, String> signParam = JSON.parseObject(json, LinkedHashMap.class, Feature.OrderedField);
+
+        AppInfo appInfo = iAppInfoService.getByAppId(notifyVo.getAppId());
+        if (appInfo == null) {
+            throw new CustomAppException("应用不存在");
+        }
+        Set<String> exclude = new HashSet<>();
+        exclude.add("sign");
+        String sign = null;
+        try {
+            // 获取签名
+            sign = SignatureUtil.getRequestMD5Sign(signParam, exclude, appInfo.getAppSecret());
+        } catch (UnsupportedEncodingException e) {
+            log.error("获取签名失败: {}", e.getMessage());
+            e.printStackTrace();
+        }
+        notifyVo.setSign(sign);
+        String storeStatusNotifyVoStr = JSONObject.toJSONString(notifyVo);
+        try {
+            storeStatusNotifyVoStr = URLEncoder.encode(storeStatusNotifyVoStr,"utf-8");
+            log.info("订单状态同步请求参数: {}", storeStatusNotifyVoStr);
+            String resp = HttpUtils.sendPost(appInfo.getStoreStatusNotifyUrl(), storeStatusNotifyVoStr);
+            log.info("订单状态同步返回结果: {}", resp);
+            // 收到成功响应后存入缓存, 后续不再推送
+            if (ResponseResultCodeEnum.SUCCESS.getMessage().equals(resp)) {
+                redisCache.setCacheObject(key, "SUCCESS", 24, TimeUnit.HOURS);
+            } else {
+                if (notifyCount == AppConstant.NOTIFY_INTERVAL_DURATION.size()) {
+                    // todo: 未收到三方的状态通知回复, 待处理: 增加入库记录...
+                    log.warn("订单状态通知结束, 未收到三方的成功响应");
+                }
+            }
+        } catch (Exception e) {
+            log.error("订单状态通知异常: {}", e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+}

+ 10 - 0
lb-app/src/main/java/com/ydd/app/service/OpenApiNotifyService.java

@@ -9,6 +9,16 @@ import com.ydd.app.vo.StoreStatusNotifyVo;
  */
 public interface OpenApiNotifyService {
 
+    /**
+     * 门店状态通知
+     * @param storeStatusNotifyVo
+     */
     void storeStatusNotify(StoreStatusNotifyVo storeStatusNotifyVo);
 
+    /**
+     * 订单状态通知
+     * @param orderStatusNotifyVo
+     */
+    void orderStatusNotify(StoreStatusNotifyVo orderStatusNotifyVo);
+
 }

+ 10 - 0
lb-app/src/main/java/com/ydd/app/service/impl/OpenApiNotifyServiceImpl.java

@@ -34,4 +34,14 @@ public class OpenApiNotifyServiceImpl implements OpenApiNotifyService {
         }
     }
 
+    @Override
+    public void orderStatusNotify(StoreStatusNotifyVo orderStatusNotifyVo) {
+        List<Integer> durations = AppConstant.NOTIFY_INTERVAL_DURATION;
+        for (int i = 0; i < durations.size(); i++) {
+            // 记录推送次数, 超过规定总次数后做处理: 入库或者其他
+            orderStatusNotifyVo.setNotifyCount(i);
+            rabbitSender.sendDelay(orderStatusNotifyVo, RabbitConstant.QUEUE_DELAY_OPEN_STATUS_NOTIFY_ROUTING_KEY, durations.get(i) * 1000);
+        }
+    }
+
 }

+ 1 - 1
lb-module/src/main/java/com/ydd/module/constants/AppConstant.java

@@ -33,6 +33,6 @@ public interface AppConstant {
     /**
      * 推送间隔时长, 单位秒
      */
-    List<Integer> NOTIFY_INTERVAL_DURATION = Arrays.asList(0, 5, 20, 60, 120, 300, 600, 900, 1800, 3600, 7200);
+    List<Integer> NOTIFY_INTERVAL_DURATION = Arrays.asList(0, 10, 30, 60, 120, 300, 600, 900, 1800, 3600, 7200);
 
 }