package com.fdkankan.ucenter.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.rabbitmq.util.RabbitMqProducer;
import com.fdkankan.ucenter.entity.DownloadOrder;
import com.fdkankan.ucenter.httpClient.vo.PayOrderVo;
import com.fdkankan.ucenter.pay.strategy.impl.DownloadOrderImpl;
import com.fdkankan.ucenter.pay.strategy.impl.EntityOrderImpl;
import com.fdkankan.ucenter.pay.strategy.impl.IncrementOrderImpl;
import com.fdkankan.ucenter.service.IIncrementAutoOrderService;
import com.fdkankan.ucenter.service.IIncrementOrderService;
import com.fdkankan.ucenter.service.IProductOrderService;
import com.fdkankan.ucenter.service.IProductService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.rmi.runtime.Log;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@Slf4j
@Component
public class OrderDownConsumer {


    @Autowired
    IncrementOrderImpl incrementOrder;
    @Autowired
    DownloadOrderImpl downloadOrder;
    @Autowired
    EntityOrderImpl entityOrder;
    @Autowired
    IIncrementAutoOrderService autoOrderService;
    @Autowired
    IProductOrderService productOrderService;


    @RabbitListener(
            queuesToDeclare = @Queue("${queue.orderPay.order-payResult:order-payResult}")
            ,concurrency = "1"
    )
    public void consumerQueue(Channel channel, Message message)  {
        try {
            String messageId = message.getMessageProperties().getMessageId();
            String msg = new String(message.getBody(), StandardCharsets.UTF_8);
            log.info("接受下单结果mq--messageId:{},msg:{}",messageId,msg);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

            PayOrderVo order = JSONObject.parseObject(msg, PayOrderVo.class);
            if(order == null ){
                return;
            }
            if(order.getAutoPay() != null && order.getAutoPay() == 1){
                autoOrderService.subOrder(order.getOrderSn(),order.getTradeNo());
                return;
            }
            //更新订单状态
            Integer payType = 3;
            //付款方式，0表示微信，1表示支付宝，2表示paypal，3表示其他
            switch (order.getPayType()){
                case 0 : case 1: case 2: payType = 0;break;
                case 3 : case 4:  payType = 1;break;
                case 5 :   payType = 2;break;
                default: payType = order.getPayType();
            }
            if(order.getPayStatus() != 1){
                log.info("order-payResult----支付失败:{}",order);
                return;
            }
            if("incrementOrder".equals(order.getOrderType())){
                incrementOrder.handleOrder(order.getOrderSn(),order.getTradeNo(),order.getOpenId(),payType,null);
            }
            if("downOrder".equals(order.getOrderType())){
                downloadOrder.handleOrder(order.getOrderSn(),order.getTradeNo(),order.getOpenId(),payType,null);
            }
            if("partOrder".equals(order.getOrderType())){
                entityOrder.handleOrder(order.getOrderSn(),order.getTradeNo(),order.getOpenId(),payType,null);
            }
            if("cooperationOrder".equals(order.getOrderType())){
                productOrderService.handleOrder(order.getOrderSn(),order.getTradeNo(),order.getOpenId(),payType);
            }
        }catch (Exception e){
            log.info("order-payResult----消费失败",e);
        }finally {

        }

    }

}
