
package com.tmerclub.cloud.user.listener;

import com.tmerclub.cloud.api.user.bo.UserScoreBO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.user.service.UserScoreLockService;
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
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.SCORE_UNLOCK_TOPIC, consumerGroup = RocketMqConstant.SCORE_UNLOCK_TOPIC)
public class UserScoreUnlockConsumer implements RocketMQListener<UserScoreBO> {

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

    @Autowired
    private UserScoreLockService userScoreLockService;

    /**
     * 1、积分锁定一定时间后，如果订单支付未支付，则解锁积分（有可能积分锁定成功，订单因为异常回滚导致订单未创建）
     * 2、取消订单，直接解锁积分
     */
    @Override
    public void onMessage(UserScoreBO userScoreBo) {
        LOG.info("订单取消解锁库存... message: {}", Json.toJsonString(userScoreBo));
        userScoreLockService.unlock(userScoreBo);
    }
}
