package com.fzm.mall.server.front.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fzm.mall.server.front.constant.MALLGlobalConfig;
import com.fzm.mall.server.front.order.model.Pay;
import com.fzm.mall.server.front.order.model.po.TranDto;
import com.fzm.mall.server.front.order.model.vo.DeliveryVO;
import com.fzm.mall.server.front.order.service.IDeliveryService;
import com.fzm.mall.server.front.order.service.IOrderService;
import com.fzm.mall.server.front.order.service.IPayService;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 订单超时处理
 *
 * @author tdz
 * @date 创建时间：2021年5月27日 下午3:01:41
 */
@Slf4j
@Component
public class CoinPayPostfeeConsumer {

    @Autowired
    private IPayService payService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IDeliveryService deliveryService;


    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = MALLGlobalConfig.projectMq+".coin.pay.postfee.queue"), exchange = @Exchange(value = MALLGlobalConfig.projectMq+"-coinpay-postfee-exchange"), key = MALLGlobalConfig.projectMq+".coin.pay.postfee.key")})
    public void receiver(String message) {
        System.out.println("CoinPayPostfeeConsumer:" + message);
        if (StringUtil.isNullOrEmpty(message)) {
            log.error("CoinPayPostfeeConsumer:message为空");
            return;
        }
        DeliveryVO deliveryVO = JSON.parseObject(message, DeliveryVO.class);
        if (deliveryVO == null) {
            log.error("CoinPayPostfeeConsumer :" + message + "-deliveryVO == null");
            return;
        }
        String pid = deliveryVO.getPayId();
        Pay pay = payService.getByPid(pid);
        if (pay == null) {
            log.error("CoinPayPostfeeConsumer:pid:" + message + "不存在");
            return;
        }
        try {
            deliveryService.handle(deliveryVO);
        }catch (Exception e){
            return;
        }

    }
}
