package com.xingxi.mq.consumer.clear;

import com.google.common.collect.Lists;
import com.rabbitmq.client.Channel;
import com.xingxi.common.mq.constant.OrderMqKey;
import com.xingxi.common.mq.core.AbstractRabbitConsumer;
import com.xingxi.common.utils.DateUtils;
import com.xingxi.mq.server.order.service.IClearOrderDetailVoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ClearOrderReturnedMqConsumer
 * @projectName xingxi
 * @Deacription 订单退货消息接受处理
 *              更新退货数量
 * @Author FZ.Dong
 * @Date 2020/9/15 16:56
 * @Version 1.0
 **/
@Slf4j
@Component
public class ClearOrderReturnedMqConsumer extends AbstractRabbitConsumer {

    @Resource
    private IClearOrderDetailVoService iClearOrderDetailVoService;

    @Bean(OrderMqKey.EDIT_QUEUE_CLEAR_FOR_RETURNED)
    @Override
    protected List<Binding> getBindingList() {
        return Lists.newArrayList(this.bind(OrderMqKey.EDIT_EXCHANGE,
                OrderMqKey.EDIT_QUEUE_CLEAR_FOR_RETURNED,
                OrderMqKey.EDIT_ROUTE_FOR_CANCELED));
    }

    @RabbitListener(queues = OrderMqKey.EDIT_QUEUE_CLEAR_FOR_RETURNED)
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        this.convertMessage(message, channel);
    }

    @Override
    public boolean execute(String body, Channel channel, Message message) throws Exception {
        log.info("清分订单退货完成MQ： 处理推送的订单数据【{}】", body);

        Date nowDate = DateUtils.getNowDate();

        // json对象转换
//        List<OrderReturnedBO> returnList = JSON.parseArray(body, OrderReturnedBO.class);
//
//        for (OrderReturnedBO orbo : returnList) {
//
//            int returnQty = 0;
//            if(orbo.getStockQty() != null) {
//                returnQty = orbo.getStockQty();
//            }
//            if(orbo.getScrapQty() != null) {
//                returnQty = returnQty + orbo.getScrapQty();
//            }
//            if(returnQty <= 0) {
//                continue;
//            }
//            ClearOrderDetail ucod = new ClearOrderDetail();
//            // 设置订单编号
//            ucod.setOrderId(orbo.getOrderId());
//            ucod.setOrderDetailId(orbo.getOrderDetailId());
//            ucod.setSuiteId(orbo.getSuiteId());
//            ucod.setReturnQty(returnQty);
//            ucod.setUpdateTime(nowDate);
//            ucod.setUpdateUser("清分订单退货完成MQ");
//            iClearOrderDetailVoService.updateReturnQty(ucod);
//        }

        log.info("清分订单退货完成MQ： 推送的订单数据处理完成.");
        return true;
    }
}
