
package com.tmerclub.cloud.coupon.listener;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.coupon.dto.BindCouponDTO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.coupon.service.CouponService;
import com.tmerclub.cloud.coupon.service.CouponUserService;
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.Collection;
import java.util.List;

/**
 * @author cl
 * @date 2021-05-17 14:58:36
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.LEVEL_UP_COUPON_GIVE_TOPIC, consumerGroup = RocketMqConstant.LEVEL_UP_COUPON_GIVE_TOPIC)
public class LevelUpCouponGiveConsumer implements RocketMQListener<List<BindCouponDTO>> {
    private static final Logger logger = LoggerFactory.getLogger(LevelUpCouponGiveConsumer.class);

    @Autowired
    private CouponUserService couponUserService;

    @Autowired
    private CouponService couponService;

    /**
     * 等级提升发送优惠券给客户
     *
     * @param bindCouponDTOList 用户绑定优惠券
     */
    @Override
    public void onMessage(List<BindCouponDTO> bindCouponDTOList) {
        logger.info("等级提升发放优惠券给用户{}", bindCouponDTOList);
        Collection<Long> couponIds = couponUserService.batchBindCoupon(bindCouponDTOList);
        if (CollUtil.isEmpty(couponIds)) {
            return;
        }
        for (Long couponId : couponIds) {
            couponService.removeCacheByShopId(null, couponId);
        }
    }
}
