|
@@ -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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|