
package com.tmerclub.cloud.user.listener;

import com.tmerclub.cloud.api.user.bo.BuyVipNotifyBO;
import com.tmerclub.cloud.common.cache.constant.UserCacheNames;
import com.tmerclub.cloud.common.cache.util.CacheManagerUtil;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.user.model.UserLevelLog;
import com.tmerclub.cloud.user.service.UserLevelLogService;
import com.tmerclub.cloud.user.service.UserLevelService;
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;

/**
 * @author tmerclub
 * @date 2021/1/7
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.BUY_VIP_NOTIFY_TOPIC, consumerGroup = RocketMqConstant.BUY_VIP_NOTIFY_TOPIC)
public class BuyVipNotifyConsumer implements RocketMQListener<BuyVipNotifyBO> {

    @Autowired
    private UserLevelLogService userLevelLogService;
    @Autowired
    private UserLevelService userLevelService;
    @Autowired
    private CacheManagerUtil cacheManagerUtil;

    private static final Logger LOG = LoggerFactory.getLogger(BuyVipNotifyConsumer.class);

    @Override
    public void onMessage(BuyVipNotifyBO message) {
        LOG.info("购买vip回调开始... message: {}", Json.toJsonString(message));
        UserLevelLog userLevelLog = userLevelLogService.getByLevelLogId(message.getUserLevelLogId());

        userLevelService.paySuccess(userLevelLog, message);
        // 移除用户缓存
        cacheManagerUtil.evictCache(UserCacheNames.USER_INFO, userLevelLog.getUserId().toString());
    }
}
