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

import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.third.OrderDetailDO;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
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.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.io.IOException;
import java.time.Duration;

/**
 * 顺丰订单
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SfOrderReview extends MqListener<OrderDetailDO> {

    final ITOrderService orderService;
    final RedisUtil redisUtil;
    final MqTool mqTool;
    final SfExpressServer sfExpressServer;

    /**
     * 接收到消息会调用此方法
     *
     * @param msg 消息体
     * @return 【保留返回值】暂无实际用途，后续可能用于支持消息失败重试
     */
    @Override
    public boolean onReceive(OrderDetailDO msg) throws IOException {
        APIEntity<OrderDetailDO> res=sfExpressServer.orderDetail(msg.getOrderNumber(),msg.getThirdNum());
        if (!res.checkSuccess()){
            log.error("【顺丰取件后计费核实】未找到费用信息，三分钟后再试....");
            mqTool.sendDelayMsg(msg,SfOrderReview.class,Duration.ofMinutes(3));
            return true;
        }
        TOrder order=orderService.getByNum(msg.getOrderNumber());
        //有差价，需要补付
        if(res.getBody().getOrderPrice().compareTo(order.getOrderFactPrice().add(order.getPrefAmount()))>0){

        }
        return true;

    }

    /**
     * 当消费者业务代码出现异常时会调用该方法
     *
     * @param msg 消息体
     * @param e   异常信息
     */
    @Override
    public void onError(OrderDetailDO msg, Exception e) {

    }
}
