
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-06-05 16:58:36
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.LEVEL_DOWN_COUPON_EXPIRE_TOPIC, consumerGroup = RocketMqConstant.LEVEL_DOWN_COUPON_EXPIRE_TOPIC)
public class LevelDownCouponExpireConsumer implements RocketMQListener<List<BindCouponDTO>> {
    private static final Logger logger = LoggerFactory.getLogger(LevelDownCouponExpireConsumer.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.batchDeleteUserCoupon(bindCouponDTOList);
        if (CollUtil.isEmpty(couponIds)) {
            return;
        }
        for (Long couponId : couponIds) {
            couponService.removeCacheByShopId(null, couponId);
        }
    }
}
