Selaa lähdekoodia

状态通知接口

叶君翔 3 vuotta sitten
vanhempi
commit
db2706a7c1

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

@@ -0,0 +1,100 @@
+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.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.data.redis.core.RedisTemplate;
+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 IAppInfoService iAppInfoService;
+
+    public RedisTemplate redisTemplate;
+
+    private final RedisCache redisCache;
+
+    @RabbitListener(queues = RabbitConstant.QUEUE_DELAY_PUSH_STORE_STATUS)
+    @RabbitHandler
+    public void pushStoreStatusDelayListener(Object msg) {
+        log.info("收到延迟推送状态的消息:{}", msg);
+        try {
+            if (msg instanceof StoreStatusNotifyVo) {
+                StoreStatusNotifyVo storeStatusNotifyVo = (StoreStatusNotifyVo) msg;
+                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);
+                    }
+                } catch (Exception e) {
+                    log.error("门店状态同步异常: {}", e.getMessage());
+                    e.printStackTrace();
+                }
+            }
+            if (msg instanceof OrderStatusNotifyVo) {
+                // todo:处理订单状态推送
+            }
+        } catch (Exception e) {
+            log.error("状态推送异常", e);
+        }
+
+    }
+
+}

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

@@ -9,6 +9,6 @@ import com.ydd.app.vo.StoreStatusNotifyVo;
  */
 public interface OpenApiNotifyService {
 
-    String storeStatus(StoreStatusNotifyVo storeStatusNotifyVo);
+    void storeStatus(StoreStatusNotifyVo storeStatusNotifyVo);
 
 }

+ 12 - 49
lb-app/src/main/java/com/ydd/app/service/impl/OpenApiNotifyServiceImpl.java

@@ -1,28 +1,16 @@
 package com.ydd.app.service.impl;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import com.alibaba.fastjson.parser.Feature;
 import com.ydd.app.service.OpenApiNotifyService;
 import com.ydd.app.vo.StoreStatusNotifyVo;
-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.domain.AppInfo;
-import com.ydd.module.expection.CustomAppException;
-import com.ydd.module.service.IAppInfoService;
+import com.ydd.common.constant.RabbitConstant;
+import com.ydd.module.constants.AppConstant;
+import com.ydd.module.producer.RabbitSender;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-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.List;
 
 /**
  *  通知接口
@@ -34,41 +22,16 @@ import java.util.Set;
 @RequiredArgsConstructor(onConstructor_ = @Autowired)
 public class OpenApiNotifyServiceImpl implements OpenApiNotifyService {
 
-    private final IAppInfoService iAppInfoService;
+    private final RabbitSender rabbitSender;
 
     @Override
-    public String storeStatus(StoreStatusNotifyVo storeStatusNotifyVo) {
-        storeStatusNotifyVo.setTimeStamp(System.currentTimeMillis());
-        String json = JsonMapper.nonNullMapper().toJson(storeStatusNotifyVo);
-        HashMap<String, String> signParam = JSON.parseObject(json, LinkedHashMap.class, Feature.OrderedField);
-        Set<String> exclude = new HashSet<>();
-        exclude.add("sign");
-        AppInfo appInfo = iAppInfoService.getByAppId(storeStatusNotifyVo.getAppId());
-        if (appInfo == null) {
-            throw new CustomAppException("应用不存在");
+    public void storeStatus(StoreStatusNotifyVo storeStatusNotifyVo) {
+        List<Integer> durations = AppConstant.PUSH_INTERVAL_DURATION;
+        for (int i = 0; i < durations.size(); i++) {
+            // 记录推送次数, 超过规定总次数后做处理: 入库或者其他
+            storeStatusNotifyVo.setNotifyCount(i);
+            rabbitSender.sendDelay(storeStatusNotifyVo, RabbitConstant.QUEUE_DELAY_PUSH_STORE_STATUS_ROUTING_KEY, durations.get(i) * 1000);
         }
-        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)) {
-                // todo: 未收到响应开启重试机制
-            }
-        } catch (Exception e) {
-            log.error("门店状态同步异常: {}", e.getMessage());
-            e.printStackTrace();
-        }
-        return null;
     }
+
 }

+ 62 - 0
lb-app/src/main/java/com/ydd/app/vo/OrderStatusNotifyVo.java

@@ -0,0 +1,62 @@
+package com.ydd.app.vo;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.ydd.app.dto.BaseDto;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * 订单状态通知参数
+ * @author 叶君翔
+ * @date 2022/04/14 21:12
+ */
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class OrderStatusNotifyVo extends BaseDto {
+
+    /**
+     * 外部订单号
+     */
+    private String outOrderSn;
+
+    /**
+     * 猎豹订单号
+     */
+    private String orderSn;
+
+    /**
+     * 订单状态, 2、已接单,3、配送中,4、已完成,5、已取消
+     */
+    private Integer status;
+
+    /**
+     * 运力id, 即猎豹运力type
+     */
+    private Integer deliveryId;
+
+    /**
+     * 运力名称
+     */
+    private String deliveryName;
+
+    /**
+     * 骑手姓名
+     */
+    private String shipperName;
+
+    /**
+     * 骑手电话
+     */
+    private String shipperPhone;
+
+    /**
+     * 通知次数
+     */
+    @JsonIgnore
+    private Integer notifyCount;
+
+}

+ 7 - 0
lb-app/src/main/java/com/ydd/app/vo/StoreStatusNotifyVo.java

@@ -1,5 +1,6 @@
 package com.ydd.app.vo;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.ydd.app.dto.BaseDto;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
@@ -25,4 +26,10 @@ public class StoreStatusNotifyVo extends BaseDto {
 
     private String failMsg;
 
+    /**
+     * 通知次数
+     */
+    @JsonIgnore
+    private Integer notifyCount;
+
 }

+ 29 - 0
lb-common/src/main/java/com/ydd/common/constant/RabbitConstant.java

@@ -0,0 +1,29 @@
+package com.ydd.common.constant;
+
+/**
+ * 消息队列常量
+ * @author 叶君翔
+ * @date 2021/11/16 19:35
+ */
+public interface RabbitConstant {
+
+    /**
+     * 推送门店状态延迟队列
+     */
+    String QUEUE_DELAY_PUSH_STORE_STATUS = "queue.pushStoreStatus.delay";
+    /**
+     * 推送订单状态延迟队列
+     */
+    String QUEUE_DELAY_PUSH_ORDER_STATUS = "queue.pushOrderStatus.delay";
+
+    /**
+     * 推送门店状态路由
+     */
+    String QUEUE_DELAY_PUSH_STORE_STATUS_ROUTING_KEY = "queue.pushStoreStatus";
+
+    /**
+     * 推送订单状态路由
+     */
+    String QUEUE_DELAY_PUSH_ORDER_STATUS_ROUTING_KEY = "queue.pushOrderStatus";
+
+}

+ 22 - 0
lb-module/src/main/java/com/ydd/module/config/RabbitConfig.java

@@ -1,5 +1,6 @@
 package com.ydd.module.config;
 
+import com.ydd.common.constant.RabbitConstant;
 import org.springframework.amqp.core.*;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.context.annotation.Bean;
@@ -32,6 +33,16 @@ public class RabbitConfig {
     public Queue queueDelayMessage() {
         return new Queue(RabbitConfig.QUEUE_DELAY_WAIMAI);
     }
+
+    @Bean(RabbitConstant.QUEUE_DELAY_PUSH_STORE_STATUS)
+    public Queue queuePushStoreStatusDelayMessage() {
+        return new Queue(RabbitConstant.QUEUE_DELAY_PUSH_STORE_STATUS);
+    }
+
+    @Bean(RabbitConstant.QUEUE_DELAY_PUSH_ORDER_STATUS)
+    public Queue queuePushOrderStatusDelayMessage() {
+        return new Queue(RabbitConstant.QUEUE_DELAY_PUSH_ORDER_STATUS);
+    }
     //创建队列
     @Bean("topic.cancelOrder")
     public Queue cancelOrder() {
@@ -87,4 +98,15 @@ public class RabbitConfig {
     Binding bindingExchangeDelayMessage(@Qualifier("queue.waimai.delay") Queue queue, @Qualifier("delay_exchange")CustomExchange exchange) {
         return BindingBuilder.bind(queue).to(exchange).with("delay.waimai").noargs();
     }
+
+    @Bean
+    Binding bindingExchangePushStoreStatusDelayMessage(@Qualifier(RabbitConstant.QUEUE_DELAY_PUSH_STORE_STATUS) Queue queue, @Qualifier("delay_exchange")CustomExchange exchange) {
+        return BindingBuilder.bind(queue).to(exchange).with(RabbitConstant.QUEUE_DELAY_PUSH_STORE_STATUS_ROUTING_KEY).noargs();
+    }
+
+    @Bean
+    Binding bindingExchangePushOrderStatusDelayMessage(@Qualifier(RabbitConstant.QUEUE_DELAY_PUSH_ORDER_STATUS) Queue queue, @Qualifier("delay_exchange")CustomExchange exchange) {
+        return BindingBuilder.bind(queue).to(exchange).with(RabbitConstant.QUEUE_DELAY_PUSH_ORDER_STATUS_ROUTING_KEY).noargs();
+    }
+
 }

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

@@ -1,5 +1,10 @@
 package com.ydd.module.constants;
 
+import com.ydd.common.enums.UserTypeJoinRoleEnums;
+
+import java.util.Arrays;
+import java.util.List;
+
 /**
  *  常量
  * @author 叶君翔
@@ -25,4 +30,9 @@ public interface AppConstant {
      */
     int VALUATION_RESULT_EFFECTIVE_MINUTES = 10;
 
+    /**
+     * 推送间隔时长, 单位秒
+     */
+    List<Integer> PUSH_INTERVAL_DURATION = Arrays.asList(0, 5, 20, 60, 120, 300, 600, 900, 1800, 3600, 7200);
+
 }