MeituanDeliveryCallback.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package com.ydd.app.callback.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.ydd.api.DeliveryCallbackApi;
  5. import com.ydd.app.service.ApiOrderBackService;
  6. import com.ydd.app.service.OpenApiNotifyService;
  7. import com.ydd.app.service.impl.ApiCallBackContent;
  8. import com.ydd.app.service.impl.ApiCallBackMeiTuan;
  9. import com.ydd.app.service.impl.ApiCallBackMeiTuanZb;
  10. import com.ydd.app.vo.StoreStatusNotifyVo;
  11. import com.ydd.common.enums.DeliveryTypeEnums;
  12. import com.ydd.module.domain.*;
  13. import com.ydd.module.dto.CallBackOrderDto;
  14. import com.ydd.module.enums.BindStatusEnum;
  15. import com.ydd.module.enums.OrderStatusEnum;
  16. import com.ydd.module.service.*;
  17. import com.ydd.third.common.utils.StringUtils;
  18. import com.ydd.third.common.vo.ResObject;
  19. import com.ydd.third.common.vo.callback.CallBackOrderVo;
  20. import com.ydd.third.common.vo.callback.MeituanCallbackOrderVo;
  21. import com.ydd.third.common.vo.callback.MeituanZbCallbackOrderVo;
  22. import com.ydd.third.common.vo.waimai.meituan.MeituanShop;
  23. import com.ydd.third.common.vo.waimai.meituan.MeituanToken;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.web.bind.annotation.PostMapping;
  27. import org.springframework.web.bind.annotation.RequestMapping;
  28. import org.springframework.web.bind.annotation.RequestParam;
  29. import org.springframework.web.bind.annotation.RestController;
  30. import javax.annotation.Resource;
  31. import java.util.Map;
  32. //import com.ydd.third.waimai.meituan.callback.MTCallBa
  33. @Slf4j
  34. @RestController
  35. @RequestMapping("/app/delivery/meituan/callback")
  36. public class MeituanDeliveryCallback {
  37. @Autowired
  38. private DeliveryCallbackApi meituanClient;
  39. // @Autowired
  40. // private MeituanCallback callback;
  41. @Resource
  42. private ApiOrderBackService apiOrderService;
  43. @Resource
  44. private IShopDeliveryService iShopDeliveryService;
  45. @Resource
  46. private IOrderDeliveryService iOrderDeliveryService;
  47. @Resource
  48. private IOrderService iOrderService;
  49. @Resource
  50. private IMerchantService iMerchantService;
  51. @Resource
  52. private OpenApiNotifyService openApiNotifyService;
  53. @Resource
  54. private IDspDeliveryService iDspDeliveryService;
  55. @Resource
  56. private IShopService iShopService;
  57. /**
  58. * 授权
  59. * @param params
  60. * @return
  61. */
  62. @RequestMapping("/doAuth")
  63. public String doAuth(@RequestParam Map<String, String> params){
  64. log.info("美团配送授权回调---->>>>"+JSONObject.toJSONString(params));
  65. String code = params.get("code");
  66. String state = params.get("state");
  67. if (StringUtils.isNotBlank(code) && StringUtils.isNotBlank(state)) {
  68. ResObject<MeituanToken> res = meituanClient.mtAuth(params);
  69. if (res.getCode() == 0) {
  70. MeituanToken meituanToken = res.getData();
  71. String accessToken = meituanToken.getAccessToken(); //访问token
  72. Long expireIn = meituanToken.getExpireIn(); //访问token过期时间
  73. String refreshToken = meituanToken.getRefreshToken(); //刷新token
  74. String merchantId = meituanToken.getMerchantId(); //本地商家id
  75. /** 这里需要把返回的accessToken作保存 ***********/
  76. return "获取token成功";
  77. } else {
  78. return res.getMsg();
  79. }
  80. } else {
  81. return "非法访问!";
  82. }
  83. }
  84. /**
  85. * 请求方式:post
  86. *
  87. * 请求格式:application/x-www-form-urlencoded
  88. * @param params
  89. * @return
  90. */
  91. @PostMapping("/doOrder")
  92. public String doOrder(@RequestParam Map<String, String> params){
  93. log.info("美团配送订单回调---->>>>>>"+JSONObject.toJSONString(params));
  94. JSONObject res = new JSONObject();
  95. if (null == params) {
  96. res.put("code",1);
  97. res.put("message","fail");
  98. return res.toJSONString();
  99. }
  100. CallBackOrderVo vo = new CallBackOrderVo();
  101. vo.setLbClient(DeliveryTypeEnums.MEI_TUAN.getName());
  102. vo.setMap(params);
  103. ResObject<MeituanCallbackOrderVo> callbackRes =( ResObject<MeituanCallbackOrderVo>) meituanClient.doOrder(vo);
  104. if (callbackRes.getCode() == 0) {
  105. MeituanCallbackOrderVo orderVo = callbackRes.getData();
  106. log.info(JSONObject.toJSONString(orderVo));
  107. String msgType = params.get("msgType");
  108. if (msgType.equals("19101")) { //订单状态发生变化
  109. //订单状态发生变化 作要应的处理
  110. ApiCallBackContent content = new ApiCallBackContent(new ApiCallBackMeiTuan(orderVo));
  111. OrderDelivery orderDelivery = iOrderDeliveryService.getOne(new QueryWrapper<OrderDelivery>().eq("child_order_sn",orderVo.getOrderId()));
  112. CallBackOrderDto dto = content.createCallBack();
  113. dto.setParentOrderSn(orderDelivery.getOrderSn());
  114. apiOrderService.callBackOrder(dto);
  115. } else if (msgType.equals("19102")) { //订单发生异常
  116. //订单发生异常,作相应的处理
  117. log.info("美团订单异常===");
  118. }
  119. /**
  120. * 对返数的数据作处理
  121. */
  122. res.put("code",0);
  123. res.put("message","success");
  124. } else {
  125. res.put("code",1);
  126. res.put("message","fail");
  127. }
  128. return res.toJSONString();
  129. }
  130. /**
  131. * 美团跑腿回调
  132. *
  133. * @param params
  134. * @return
  135. */
  136. @RequestMapping("/deliveryStatus")
  137. public String doWmNewOrder(@RequestParam Map<String, String> params) {
  138. log.info("美团跑腿配送订单回调---->>>>>>"+JSONObject.toJSONString(params));
  139. JSONObject res = new JSONObject();
  140. res.put("data","OK");
  141. if (null == params) {
  142. return res.toJSONString();
  143. }
  144. CallBackOrderVo vo = new CallBackOrderVo();
  145. vo.setLbClient(DeliveryTypeEnums.MEI_TUAN_ZB.getName());
  146. vo.setMap(params);
  147. ResObject<MeituanZbCallbackOrderVo> callbackRes =( ResObject<MeituanZbCallbackOrderVo>) meituanClient.doOrder(vo);
  148. if (callbackRes.getCode() == 0) {
  149. MeituanZbCallbackOrderVo orderVo = callbackRes.getData();
  150. //订单状态发生变化 作要应的处理
  151. ApiCallBackContent content = new ApiCallBackContent(new ApiCallBackMeiTuanZb(orderVo));
  152. OrderDelivery orderDelivery = iOrderDeliveryService.getOne(new QueryWrapper<OrderDelivery>().
  153. eq("out_trade_no",orderVo.getOrderId()).
  154. gt("delivery_status",0));
  155. if (orderDelivery == null){
  156. return res.toJSONString();
  157. }
  158. CallBackOrderDto dto = content.createCallBack();
  159. dto.setParentOrderSn(orderDelivery.getOrderSn());
  160. dto.setOrderSn(orderDelivery.getChildOrderSn());
  161. Order order = iOrderService.getByOrderSn(orderDelivery.getOrderSn());
  162. if (order.getDeliveryStatus() >= OrderStatusEnum.ORDERS_TO_BE_RECEIVED.status){
  163. apiOrderService.callBackOrder(dto);
  164. }else {
  165. log.info("美团跑腿配送订单--订单号=="+orderDelivery.getOrderSn()+"===状态为"+order.getDeliveryStatus()+"==不进行处理");
  166. }
  167. }
  168. return res.toJSONString();
  169. }
  170. /**
  171. * 门店状态回调
  172. */
  173. @RequestMapping("/doShop")
  174. public String doShop(@RequestParam Map<String, String> params) {
  175. JSONObject jsonObject = new JSONObject();
  176. jsonObject.put("code",0);
  177. log.info("美团门店状态回调-->>>>..."+JSONObject.toJSONString(params));
  178. String developerId = params.get("developerId");
  179. String message = params.get("message");
  180. if (StringUtils.isNotBlank(developerId) && StringUtils.isNotBlank(message)) {
  181. MeituanShop meituanShop = ((JSONObject) JSONObject.toJSON(params)).toJavaObject(MeituanShop.class);
  182. MeituanShop.DataEntity info = (JSONObject.parseObject(meituanShop.getMessage())).toJavaObject(MeituanShop.DataEntity.class);
  183. // 查询门店
  184. ShopDelivery shopDelivery = iShopDeliveryService.getByThirdShopId(info.getShop_id());
  185. // ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getByThirdShopId(info.getShop_id());
  186. if (shopDelivery != null){
  187. if (info.getStatus().equals(10) || info.getStatus().equals(50)){
  188. shopDelivery.setBindStatus(BindStatusEnum.REFUND.status);
  189. shopDelivery.setAuthMsg(info.getReject_message());
  190. } else if (info.getStatus().equals(20) || info.getStatus().equals(30)){
  191. shopDelivery.setBindStatus(BindStatusEnum.CHECK.status);
  192. } else if (info.getStatus().equals(40) || info.getStatus().equals(60)){
  193. shopDelivery.setBindStatus(BindStatusEnum.BIND.status);
  194. }
  195. iShopDeliveryService.updateById(shopDelivery);
  196. // 开放平台发送状态变更通知
  197. Merchant merchant = iMerchantService.getById(shopDelivery.getMerchantId());
  198. if (merchant != null && merchant.getAppId() != null) {
  199. DspDelivery dspDelivery = iDspDeliveryService.findById(shopDelivery.getDeliveryId());
  200. Shop shop = iShopService.getById(shopDelivery.getShopId());
  201. StoreStatusNotifyVo notifyVo = StoreStatusNotifyVo.builder()
  202. .shopCode(shop.getCode())
  203. .deliveryId(dspDelivery.getType())
  204. .status(shopDelivery.getBindStatus())
  205. .failMsg(shopDelivery.getAuthMsg())
  206. .build();
  207. notifyVo.setAppId(merchant.getAppId());
  208. openApiNotifyService.storeStatusNotify(notifyVo);
  209. }
  210. }
  211. } else {
  212. jsonObject.put("code",-1);
  213. }
  214. return jsonObject.toString();
  215. }
  216. }