
package com.tmerclub.cloud.order.listener;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.distribution.bo.DistributionNotifyOrderAndShopBO;
import com.tmerclub.cloud.common.order.bo.EsOrderItemBO;
import com.tmerclub.cloud.common.order.vo.OrderItemVO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.order.service.OrderItemService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 分销修改订单项分销金额
 *
 * @author cl
 * @date 2021-08-20 09:10:00
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.DISTRIBUTION_NOTIFY_ORDER_SHOP_TOPIC, consumerGroup = RocketMqConstant.DISTRIBUTION_NOTIFY_ORDER_GROUP)
public class DistributionNotifyOrderConsumer implements RocketMQListener<DistributionNotifyOrderAndShopBO> {

    private static final Logger logger = LoggerFactory.getLogger(DistributionNotifyOrderConsumer.class);

    @Autowired
    private OrderItemService orderItemService;

    @Override
    public void onMessage(DistributionNotifyOrderAndShopBO message) {
        List<OrderItemVO> orderItemList = message.getDistributionOrderItems();
        if (CollUtil.isEmpty(orderItemList)) {
            return;
        }
        List<EsOrderItemBO> esOrderItemList = BeanUtil.mapAsList(orderItemList, EsOrderItemBO.class);
        logger.info("分销金额计算完成后,分销修改订单项分销金额回调开始... message: {}", Json.toJsonString(esOrderItemList));
        orderItemService.updateBatchDistributionAmount(esOrderItemList);
    }
}
