package com.xingxi.mq.consumer.clear;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.rabbitmq.client.Channel;
import com.xingxi.common.mq.bo.order.OrderCanceledBO;
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.IClearOrderVoService;
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;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ClearOrderCanceledMqConsumer
 * @projectName xingxi
 * @Deacription 订单取消消息接受处理
 * @Author FZ.Dong
 * @Date 2020/9/15 16:56
 * @Version 1.0
 **/
@Slf4j
@Component
public class ClearOrderCanceledMqConsumer extends AbstractRabbitConsumer {

    @Resource
    private IClearOrderVoService iClearOrderVoService;

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

    @RabbitListener(queues = OrderMqKey.EDIT_QUEUE_CLEAR_FOR_CANCELED)
    @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<OrderCanceledBO> canceledBos = JSON.parseArray(body, OrderCanceledBO.class);

        // 按照订单分组
        Map<Long, List<OrderCanceledBO>> orderMap = canceledBos.stream().collect(Collectors.groupingBy(item -> item.getOrderId()));
        // 按照订单分组
        for (Map.Entry<Long, List<OrderCanceledBO>> orderEntry : orderMap.entrySet()) {

            // 订单ID
            Long orderId = orderEntry.getKey();

            // 退款明细
            List<OrderCanceledBO> cancels = orderEntry.getValue();
//            for (OrderCanceledBO ocbo : cancels) {
//                ClearOrderDetail scod = new ClearOrderDetail();
//                // 设置订单编号
//                scod.setOrderId(ocbo.getOrderId());
//                scod.setSuiteId(ocbo.getSuiteId());
//                scod.setOrderDetailId(ocbo.getOrderDetailId());
//                // 订单明细数据取得
//                List<ClearOrderDetail> detailList = iClearOrderDetailService.selectClearOrderDetailList(scod);
//                for(ClearOrderDetail cod : detailList) {
//                    ClearOrderDetail ucod = new ClearOrderDetail();
//                    ucod.setId(cod.getId());
//                    // 设置退货数量等于订单数量
//                    ucod.setReturnQty(cod.getOrderQty());
//                    // 取消状态
//                    ucod.setOrderStatus(EOrderStatus.CANCELLED.getVal());
//                    // 设置更新用户
//                    ucod.setUpdateUser("清分订单取消MQ");
//                    // 设置更新时间
//                    ucod.setUpdateTime(nowDate);
//                    iClearOrderDetailService.updateClearOrderDetail(ucod);
//                }
//            }

            // 明细全取消的情况，主订单也更新取消状态
//            ClearOrder uco = new ClearOrder();
//            uco.setOrderId(orderId);
//            uco.setOrderStatus(EOrderStatus.CANCELLED.getVal());
//            uco.setUpdateTime(nowDate);
//            uco.setUpdateUser("清分订单取消MQ");
//            iClearOrderVoService.syncOrderStatusByOrderId(uco);
        }
        log.info("清分订单取消MQ： 推送的订单数据处理完成.");
        return true;
    }
    
}
