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

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.service.IGmsService;
import com.bohuikeji.delivery.backstage.service.ITCourierService;
import com.bohuikeji.delivery.backstage.service.ITNewsService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.frame.module.admin.base.MqListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 订单完成业务监听
 * @author Admin
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderEndListener extends MqListener<String> {
    final ITOrderService orderService;
    final IGmsService gmsService;
    final ITCourierService courierService;
    final ITNewsService newsService;
    @Override
    public boolean onReceive(String orderNum) {
        TOrder order = orderService.getByNum(orderNum);
        if (order == null) {
            log.error("【消息队列监听错误】订单完成后发送通知消息失败，无法找到订单：[{}]", orderNum);
            return true;
        }
        if ("已取消".equals(order.getOrderStatus())) {
            log.info("该订单已退款:[{}]", orderNum);
            return true;
        }
        TOrder tOrder = orderService.extractExtract(order);
        orderService.updateById(orderService.updatePriceByTime(tOrder));
        //发送短信给寄件人
        gmsService.sendOrderSign(order.getMailingPhone(), order.getOrderNum());
        //发送短信给客户
        gmsService.sendOrderEnd(order.getReceiptPhone(), order.getOrderNum());
        //推送小程序通知
        newsService.pushCustomerMsg1(order.getPlayerId(), "订单完成通知", "尊敬的客户您好,您的订单{0}，已经配送完成，感谢您的信赖！", order.getOrderNum());
        //更新派送员派送次数
        courierService.update(new UpdateWrapper<TCourier>().setSql("courier_num=courier_num+1").eq("courier_id", order.getCourierId()));
        return true;
    }

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

    }
}
