package com.yiqi.delay.impl;

import cn.hutool.json.JSONUtil;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.*;
import com.yiqi.delay.DelayMessage;
import com.yiqi.delay.DelayQueueCache;
import com.yiqi.order.domain.OrderPayInfo;
import com.yiqi.order.domain.dto.PayRefundDTO;
import com.yiqi.order.domain.dto.PayResponseDTO;
import com.yiqi.order.domain.dto.RefundResponseDTO;
import com.yiqi.order.service.IPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DelayTaskRunService {

    @Autowired
    private IPayService payService;

    @Autowired
    private DelayQueueCache delayQueueCache;

    /**
     * 处理延迟队列的消息
     *
     * @param tags
     */
    public void handleDelayMessage(String tags, DelayMessage delayMessage) {
        switch (DelayTagsEnum.valueOf(tags)) {
            case ORDER_PAY_DELAY_QUEUE:
                //订单支付延迟队列
                log.info("订单支付延迟队列，订单：{}", JSONUtil.toJsonStr(delayMessage));
                OrderPayInfo orderPayInfo = JSONUtil.toBean(delayMessage.getData(), OrderPayInfo.class);
                PayResponseDTO payResponseDTO = payService.orderPayQuery(orderPayInfo);
                log.info("订单支付查询结果：{}", JSONUtil.toJsonStr(payResponseDTO));
                if (payResponseDTO.getPayResult().equals(PayResult.success.getCode())) {
                    R result = payService.unifiePaySuccessNotify(orderPayInfo.getRequestNo(), orderPayInfo.getOrderNo(), payResponseDTO.getOutTradeNo());
                    if (R.isSuccess(result)) {
                        delayQueueCache.deleteDelayJob(orderPayInfo.getRequestNo());
                    } else {
                        log.error("订单支付成功，更新订单失败，订单号：{}，错误信息：{}", orderPayInfo.getOrderNo(), result.getMsg());
                        //继续增加到延迟队列
                        delayMessage.setPollingNum(delayMessage.getPollingNum() + 1);
                        delayQueueCache.addDelayJob(delayMessage, PayQueryTimesEnum.getDelayTime(delayMessage.getPollingNum()));
                    }
                } else if (payResponseDTO.getPayResult().equals(PayResult.fail.getCode())) {
                    delayQueueCache.deleteDelayJob(orderPayInfo.getRequestNo());
                } else {
                    //继续增加到延迟队列
                    delayMessage.setPollingNum(delayMessage.getPollingNum() + 1);
                    delayQueueCache.addDelayJob(delayMessage, PayQueryTimesEnum.getDelayTime(delayMessage.getPollingNum()));
                }
                if(delayMessage.getPollingNum() > 16){
                    delayQueueCache.deleteDelayJob(orderPayInfo.getRequestNo());
                }
                break;
            case ORDER_REUND_DELAY_QUEUE_TASK:
                //退款查询
                log.info("订单退款延迟队列，订单：{}", JSONUtil.toJsonStr(delayMessage));
                PayRefundDTO payRefundDTO = JSONUtil.toBean(delayMessage.getData(), PayRefundDTO.class);
                RefundResponseDTO refundResponseDTO = payService.orderRefundQuery(payRefundDTO);
                log.info("订单退款查询结果：{}", JSONUtil.toJsonStr(refundResponseDTO));
                if (refundResponseDTO.getRefundResult().equals(PayResult.success.getCode())) {
                    R result = payService.unifieRefundSuccessNotify(payRefundDTO.getOutRequestNo(), payRefundDTO.getOrderNo(), null);
                    if (R.isSuccess(result)) {
                        delayQueueCache.deleteDelayJob(payRefundDTO.getOutRequestNo());
                    } else {
                        log.error("订单退款成功，更新订单失败，订单号：{}，错误信息：{}", payRefundDTO.getOutRequestNo(), result.getMsg());
                        //继续增加到延迟队列
                        delayMessage.setPollingNum(delayMessage.getPollingNum() + 1);
                        delayQueueCache.addDelayJob(delayMessage, PayQueryTimesEnum.getDelayTime(delayMessage.getPollingNum()));
                    }
                } else if (refundResponseDTO.getRefundResult().equals(PayResult.fail.getCode())) {
                    delayQueueCache.deleteDelayJob(payRefundDTO.getOutRequestNo());
                } else {
                    //继续增加到延迟队列
                    delayMessage.setPollingNum(delayMessage.getPollingNum() + 1);
                    delayQueueCache.addDelayJob(delayMessage, RefundQueryTimesEnum.getDelayTime(delayMessage.getPollingNum()));
                }
                if(delayMessage.getPollingNum() > 7){
                    delayQueueCache.deleteDelayJob(payRefundDTO.getOutRequestNo());
                }
                break;
            default:
                break;
        }
    }

}
