
package com.tmerclub.cloud.user.listener;

import com.tmerclub.cloud.api.order.constant.OrderStatus;
import com.tmerclub.cloud.api.user.bo.UserOrderScoreBo;
import com.tmerclub.cloud.common.order.bo.RefundNotifyBO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.user.service.UserExtensionService;
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 cl
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_REFUND_SUCCESS_SERVICE_TOPIC, consumerGroup = RocketMqConstant.ORDER_REFUND_SUCCESS_SCORE_GROUP)
public class OrderRefundSuccessScoreConsumer implements RocketMQListener<RefundNotifyBO> {

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

    @Autowired
    private UserExtensionService userExtensionService;


    /**
     * 订单退货退款，还原用户的成长值
     *
     * @param message
     */
    @Override
    public void onMessage(RefundNotifyBO message) {
        // 订单已关闭就不退积分
        if (Objects.equals(message.getDbOrderStatus(), OrderStatus.CLOSE.value())) {
            return;
        }
        LOG.info("订单退款成功，还原用户成长值... message: {}", Json.toJsonString(message));
        UserOrderScoreBo userOrderScoreBo = new UserOrderScoreBo();
        userOrderScoreBo.setUserId(message.getUserId());
        userOrderScoreBo.setRefundId(message.getRefundId());
        userOrderScoreBo.setOrderId(message.getOrderId());
        userOrderScoreBo.setOrderScore(message.getOrderScore());
        userExtensionService.refundScore(userOrderScoreBo);
    }
}
