package com.ydd.app.callback.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ydd.api.DeliveryCallbackApi; import com.ydd.app.service.ApiOrderBackService; import com.ydd.app.service.OpenApiNotifyService; import com.ydd.app.service.impl.ApiCallBackContent; import com.ydd.app.service.impl.ApiCallBackMeiTuan; import com.ydd.app.service.impl.ApiCallBackMeiTuanZb; import com.ydd.app.vo.StoreStatusNotifyVo; import com.ydd.common.enums.DeliveryTypeEnums; import com.ydd.module.domain.*; import com.ydd.module.dto.CallBackOrderDto; import com.ydd.module.enums.BindStatusEnum; import com.ydd.module.enums.OrderStatusEnum; import com.ydd.module.service.*; import com.ydd.third.common.utils.StringUtils; import com.ydd.third.common.vo.ResObject; import com.ydd.third.common.vo.callback.CallBackOrderVo; import com.ydd.third.common.vo.callback.MeituanCallbackOrderVo; import com.ydd.third.common.vo.callback.MeituanZbCallbackOrderVo; import com.ydd.third.common.vo.waimai.meituan.MeituanShop; import com.ydd.third.common.vo.waimai.meituan.MeituanToken; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.Map; //import com.ydd.third.waimai.meituan.callback.MTCallBa @Slf4j @RestController @RequestMapping("/app/delivery/meituan/callback") public class MeituanDeliveryCallback { @Autowired private DeliveryCallbackApi meituanClient; // @Autowired // private MeituanCallback callback; @Resource private ApiOrderBackService apiOrderService; @Resource private IShopDeliveryService iShopDeliveryService; @Resource private IOrderDeliveryService iOrderDeliveryService; @Resource private IOrderService iOrderService; @Resource private IMerchantService iMerchantService; @Resource private OpenApiNotifyService openApiNotifyService; @Resource private IDspDeliveryService iDspDeliveryService; @Resource private IShopService iShopService; /** * 授权 * @param params * @return */ @RequestMapping("/doAuth") public String doAuth(@RequestParam Map params){ log.info("美团配送授权回调---->>>>"+JSONObject.toJSONString(params)); String code = params.get("code"); String state = params.get("state"); if (StringUtils.isNotBlank(code) && StringUtils.isNotBlank(state)) { ResObject res = meituanClient.mtAuth(params); if (res.getCode() == 0) { MeituanToken meituanToken = res.getData(); String accessToken = meituanToken.getAccessToken(); //访问token Long expireIn = meituanToken.getExpireIn(); //访问token过期时间 String refreshToken = meituanToken.getRefreshToken(); //刷新token String merchantId = meituanToken.getMerchantId(); //本地商家id /** 这里需要把返回的accessToken作保存 ***********/ return "获取token成功"; } else { return res.getMsg(); } } else { return "非法访问!"; } } /** * 请求方式:post * * 请求格式:application/x-www-form-urlencoded * @param params * @return */ @PostMapping("/doOrder") public String doOrder(@RequestParam Map params){ log.info("美团配送订单回调---->>>>>>"+JSONObject.toJSONString(params)); JSONObject res = new JSONObject(); if (null == params) { res.put("code",1); res.put("message","fail"); return res.toJSONString(); } CallBackOrderVo vo = new CallBackOrderVo(); vo.setLbClient(DeliveryTypeEnums.MEI_TUAN.getName()); vo.setMap(params); ResObject callbackRes =( ResObject) meituanClient.doOrder(vo); if (callbackRes.getCode() == 0) { MeituanCallbackOrderVo orderVo = callbackRes.getData(); log.info(JSONObject.toJSONString(orderVo)); String msgType = params.get("msgType"); if (msgType.equals("19101")) { //订单状态发生变化 //订单状态发生变化 作要应的处理 ApiCallBackContent content = new ApiCallBackContent(new ApiCallBackMeiTuan(orderVo)); OrderDelivery orderDelivery = iOrderDeliveryService.getOne(new QueryWrapper().eq("child_order_sn",orderVo.getOrderId())); CallBackOrderDto dto = content.createCallBack(); dto.setParentOrderSn(orderDelivery.getOrderSn()); apiOrderService.callBackOrder(dto); } else if (msgType.equals("19102")) { //订单发生异常 //订单发生异常,作相应的处理 log.info("美团订单异常==="); } /** * 对返数的数据作处理 */ res.put("code",0); res.put("message","success"); } else { res.put("code",1); res.put("message","fail"); } return res.toJSONString(); } /** * 美团跑腿回调 * * @param params * @return */ @RequestMapping("/deliveryStatus") public String doWmNewOrder(@RequestParam Map params) { log.info("美团跑腿配送订单回调---->>>>>>"+JSONObject.toJSONString(params)); JSONObject res = new JSONObject(); res.put("data","OK"); if (null == params) { return res.toJSONString(); } CallBackOrderVo vo = new CallBackOrderVo(); vo.setLbClient(DeliveryTypeEnums.MEI_TUAN_ZB.getName()); vo.setMap(params); ResObject callbackRes =( ResObject) meituanClient.doOrder(vo); if (callbackRes.getCode() == 0) { MeituanZbCallbackOrderVo orderVo = callbackRes.getData(); //订单状态发生变化 作要应的处理 ApiCallBackContent content = new ApiCallBackContent(new ApiCallBackMeiTuanZb(orderVo)); OrderDelivery orderDelivery = iOrderDeliveryService.getOne(new QueryWrapper(). eq("out_trade_no",orderVo.getOrderId()). gt("delivery_status",0)); if (orderDelivery == null){ return res.toJSONString(); } CallBackOrderDto dto = content.createCallBack(); dto.setParentOrderSn(orderDelivery.getOrderSn()); dto.setOrderSn(orderDelivery.getChildOrderSn()); Order order = iOrderService.getByOrderSn(orderDelivery.getOrderSn()); if (order.getDeliveryStatus() >= OrderStatusEnum.ORDERS_TO_BE_RECEIVED.status){ apiOrderService.callBackOrder(dto); }else { log.info("美团跑腿配送订单--订单号=="+orderDelivery.getOrderSn()+"===状态为"+order.getDeliveryStatus()+"==不进行处理"); } } return res.toJSONString(); } /** * 门店状态回调 */ @RequestMapping("/doShop") public String doShop(@RequestParam Map params) { JSONObject jsonObject = new JSONObject(); jsonObject.put("code",0); log.info("美团门店状态回调-->>>>..."+JSONObject.toJSONString(params)); String developerId = params.get("developerId"); String message = params.get("message"); if (StringUtils.isNotBlank(developerId) && StringUtils.isNotBlank(message)) { MeituanShop meituanShop = ((JSONObject) JSONObject.toJSON(params)).toJavaObject(MeituanShop.class); MeituanShop.DataEntity info = (JSONObject.parseObject(meituanShop.getMessage())).toJavaObject(MeituanShop.DataEntity.class); // 查询门店 ShopDelivery shopDelivery = iShopDeliveryService.getByThirdShopId(info.getShop_id()); // ShopDeliveryBind shopDeliveryBind = iShopDeliveryBindService.getByThirdShopId(info.getShop_id()); if (shopDelivery != null){ if (info.getStatus().equals(10) || info.getStatus().equals(50)){ shopDelivery.setBindStatus(BindStatusEnum.REFUND.status); shopDelivery.setAuthMsg(info.getReject_message()); } else if (info.getStatus().equals(20) || info.getStatus().equals(30)){ shopDelivery.setBindStatus(BindStatusEnum.CHECK.status); } else if (info.getStatus().equals(40) || info.getStatus().equals(60)){ shopDelivery.setBindStatus(BindStatusEnum.BIND.status); } iShopDeliveryService.updateById(shopDelivery); // 开放平台发送状态变更通知 Merchant merchant = iMerchantService.getById(shopDelivery.getMerchantId()); if (merchant != null && merchant.getAppId() != null) { DspDelivery dspDelivery = iDspDeliveryService.findById(shopDelivery.getDeliveryId()); Shop shop = iShopService.getById(shopDelivery.getShopId()); StoreStatusNotifyVo notifyVo = StoreStatusNotifyVo.builder() .shopCode(shop.getCode()) .deliveryId(dspDelivery.getType()) .status(shopDelivery.getBindStatus()) .failMsg(shopDelivery.getAuthMsg()) .build(); notifyVo.setAppId(merchant.getAppId()); openApiNotifyService.storeStatusNotify(notifyVo); } } } else { jsonObject.put("code",-1); } return jsonObject.toString(); } }