package com.bohuikeji.delivery.backstage.listener.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bohuikeji.delivery.backstage.constants.KeyConst;
import com.bohuikeji.delivery.backstage.controller.open.KeRuYunApi;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TMaltField;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TOrderCircu;
import com.bohuikeji.delivery.backstage.entity.vo.CourierAddressVo;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.delivery.backstage.service.third.ThirdUtils;
import com.bohuikeji.delivery.backstage.util.maiyatian.maltUtils;
import com.bohuikeji.frame.module.admin.base.MqListener;
import com.bohuikeji.frame.module.admin.component.RedisUtil;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 订单抢单业务监听
 * @author Admin
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderStartListener extends MqListener<String> {
    final ITOrderService orderService;
    final IGmsService gmsService;
    final ITCourierService courierService;
    final ITNewsService newsService;
    final ITMaltFieldService maltFieldService;
    final RedisUtil redisUtil;
    @Override
    public boolean onReceive(String orderNum) {
        TOrder order = orderService.getByNum(orderNum);
        if (order == null) {
            log.error("【消息队列监听错误】订单完成后发送通知消息失败，无法找到订单：[{}]", orderNum);
            return true;
        }
        TCourier courier = courierService.getById(order.getCourierId());
        order.setSurplusPickTime(orderService.getCourSurplusTime(order));//预估剩余取件时间
        order.setOrderExpectTime(orderService.getExpectTime(order,0));//预计送达时间
        order = orderService.extractExtract(order);
        orderService.updateById(order);
        if("麦芽田".equals(order.getOrderChannel())){
            TMaltField maltField = maltFieldService.getOne(new QueryWrapper<TMaltField>().eq("shop_id",order.getShopId()));
            CourierAddressVo cuv=redisUtil.get(KeyConst.REDIS_COURIER_ADDRESS +courier.getCourierId(), CourierAddressVo.class);
            if(cuv == null){
                maltUtils.getDeliveryChange(order,courier.getCourierName(),maltField,null,"",1);
            }else {
                maltUtils.getDeliveryChange(order,courier.getCourierName(),maltField,cuv.getAddress(),"",1);
            }
        }
        //向客户推送订阅消息
        orderService.post(order.getOrderId(),"派送员已接单，请留意接听电话");
        newsService.pushCourierMsg(order.getCourierId(),"业务通知","接单成功提醒","您已成功抢到订单：{0}，请及时联系客户取件。",order.getOrderNum());
        gmsService.sendOrderReceipt(order.getMailingPhone(),order.getOrderNum());
        //系统接单则取消第三方订单
        ThirdUtils.orderCancelAll(order.getOrderNum());
        //上报给客如云订单已接单
        KeRuYunApi.statusPush(order.getOrderNum(),2,courier);
        //返回接单后的订单实体
        //插入订单流转记录
        TOrderCircu.insertCircu(order);
        return true;
    }

    @Override
    public void onError(String msg, Exception e) {

    }
}
