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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TOrderArrears;
import com.bohuikeji.delivery.backstage.entity.socket.AdminNotice;
import com.bohuikeji.delivery.backstage.entity.third.OrderDetailDO;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.delivery.backstage.service.third.JdServer;
import com.bohuikeji.delivery.backstage.service.third.SfExpressServer;
import com.bohuikeji.frame.module.admin.base.MqListener;
import com.bohuikeji.frame.module.admin.component.MqTool;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Duration;

/**
 * 京东订单补付超时取消业务
 * @author Admin
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class JdOrderPayTimeOutListener extends MqListener<String> {
    final ITRefundService refundService;
    final JdServer jdServer;
    final SfExpressServer sfServer;
    final MqTool mqTool;
    final ITPayService payService;
    final ITOrderService orderService;
    final IGmsService gmsService;
    final ISocketService socketService;
    final ITOrderArrearsService arrearsService;
    /**
     * 接收到消息会调用此方法
     *
     * @param orderNum 消息体
     * @return 【保留返回值】暂无实际用途，后续可能用于支持消息失败重试
     */
    @Override
    public boolean onReceive(String orderNum) {
        TOrder order=orderService.getByNum(orderNum);
        if ("已取消".equals(order.getOrderStatus())) {
            log.error("该订单已经退款或取消，订单:{}",orderNum);
            return true;
        }
        BigDecimal price=null;
        if (SfExpressServer.NAME.equals(order.getThirdName())){
            APIEntity<OrderDetailDO> sfRes=sfServer.orderDetail(orderNum,order.getThirdNum());
            if (sfRes.checkSuccess()){
                price=sfRes.getBody().getOrderPrice().subtract(order.getOrderFactPrice().add(order.getPrefAmount()));
            }
        }else {
            price=jdServer.computeSupplyPay(orderNum);
        }
        if (price==null) {
            log.error("【快递订单复核】订单：{}，价格正在核算中,三分钟后再试......",orderNum);
            //没有计费结果，延迟3分钟再查
            mqTool.sendDelayMsg(orderNum, OrderCheckPriceListener.class, Duration.ofMinutes(3));
            return true;
        }
        //已经补付完成或者不需要补付，直接结束
        if (price.compareTo(new BigDecimal("1.00"))<0){
            log.error("【快递订单复核】订单：{}，已经补付完成了!",orderNum);
            return true;
        }
        //发送即将超时短信
        String phone=orderService.getCustomerPhone(orderNum);
        //发送支付提示短信
        TOrderArrears tOrderArrears = arrearsService.getOne(new QueryWrapper<TOrderArrears>().eq("order_num",orderNum));
        if(tOrderArrears != null && !tOrderArrears.getArrearsStatus().equals("核销")){
            gmsService.sendOrderPayTimeOut(phone,orderNum);
        }
        socketService.sendAdminMsg(new AdminNotice("您好，有欠费订单将于2小时后超时，请及时催缴！")
                .setAudio("https://oss.bhynm.com/delivery/upload/2022/3/15/164732629085487.mp3"));
        arrearsService.insertRecord(orderNum,phone,"SMS_ORDER_PAY_TIMEOUT_JD",price,null);
        return true;

    }

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

    }
}
