package com.xingxi.mq.consumer.clear;

import com.xingxi.common.mq.bo.order.OrderFinishedBO;
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.IOrderDetailVoService;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.rabbitmq.client.Channel;
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 org.springframework.util.CollectionUtils;

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 ClearOrderFinishedMqConsumer extends AbstractRabbitConsumer
{
    @Resource
    private IOrderDetailVoService orderDetailVoService;

//    @Resource
//    private IClearOrderService iClearOrderService;

//    @Resource
//    private IClearOrderDetailService iClearOrderDetailService;

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

    @RabbitListener(queues = OrderMqKey.EDIT_QUEUE_CLEAR_FOR_FINISHED)
    @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对象转换
        OrderFinishedBO orderFinishedBO = JSON.parseObject(body, OrderFinishedBO.class);

        // 订单明细
//        ClearOrderDetail scod = new ClearOrderDetail();
//        // 设置订单编号
//        scod.setOrderId(orderFinishedBO.getOrderId());
//        scod.setOrderDetailId(orderFinishedBO.getOrderDetailId());
//        scod.setSuiteId(orderFinishedBO.getSuiteId());
//        // 订单明细数据取得
//        List<ClearOrderDetail> detailList = iClearOrderDetailService.selectClearOrderDetailList(scod);

//        if(CollectionUtils.isEmpty(detailList)){
//            log.info("清分订单完成MQ： 订单【{}】明细表不存在，不做处理.", orderFinishedBO.getOrderId());
//            return true;
//        }
//        for(ClearOrderDetail cod : detailList) {
//            ClearOrderDetail ucod = new ClearOrderDetail();
//            ucod.setId(cod.getId());
//            // 完成状态
//            ucod.setOrderStatus(EOrderStatus.FINISHED.getVal());
//            // 完成时间
//            ucod.setFinishTime(nowDate);
//            // 设置更新用户
//            ucod.setUpdateUser("清分订单完成MQ");
//            // 设置更新时间
//            ucod.setUpdateTime(nowDate);
//            iClearOrderDetailService.updateClearOrderDetail(ucod);
//        }

        // 明细全完成的情况，主订单也更新完成状态
//        if(orderDetailVoService.checkOrderFinished(orderFinishedBO.getOrderId()) <= 0) {
//            ClearOrder uco = new ClearOrder();
//            uco.setOrderId(orderFinishedBO.getOrderId());
//            uco.setOrderStatus(EOrderStatus.FINISHED.getVal());
//            uco.setUpdateTime(nowDate);
//            uco.setUpdateUser("清分订单完成MQ");
//            iClearOrderService.updateClearOrder(uco);
//        }
        log.info("清分订单完成MQ： 推送的订单数据处理完成.");
        return true;
    }
}
