package com.tmerclub.cloud.product.listener;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.common.order.dto.OrderSubmitShopCartItemDTO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.product.service.ShopCartService;
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.Objects;

/**
 * @author lanhai
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_SUBMIT_SHOP_CART, consumerGroup = RocketMqConstant.ORDER_SUBMIT_SHOP_CART)
public class OrderSubmitShopCartListener implements RocketMQListener<OrderSubmitShopCartItemDTO> {
    private static final Logger logger = LoggerFactory.getLogger(OrderSubmitShopCartListener.class);

    @Autowired
    private ShopCartService shopCartService;

    /**
     * 订单提交-清除购物车
     *
     * @param orderSubmitShopCartItemDTO 购物车项id列表
     */
    @Override
    public void onMessage(OrderSubmitShopCartItemDTO orderSubmitShopCartItemDTO) {
        logger.info("订单提交-删除购物车商品， 购物车项id列表：{}", orderSubmitShopCartItemDTO);
        if (Objects.isNull(orderSubmitShopCartItemDTO) || CollUtil.isEmpty(orderSubmitShopCartItemDTO.getShopCartItemIds())) {
            return;
        }
        shopCartService.deleteShopCartItemsByShopCartItemIds(orderSubmitShopCartItemDTO.getUserId(),
                orderSubmitShopCartItemDTO.getShopCartItemIds());
    }
}
