
package com.tmerclub.cloud.user.listener;

import com.tmerclub.cloud.api.order.constant.OrderStatus;
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.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;

import java.util.Objects;

/**
 * @author cl
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_REFUND_SUCCESS_SERVICE_TOPIC, consumerGroup = RocketMqConstant.ORDER_REFUND_SUCCESS_GROWTH_GROUP)
public class OrderRefundSuccessGrowthConsumer implements RocketMQListener<RefundNotifyBO> {

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

    @Autowired
    private UserLevelService userLevelService;


    /**
     * 订单退货退款，还原用户的成长值
     *
     * @param message
     */
    @Override
    public void onMessage(RefundNotifyBO message) {
        // 订单的成长值是再确认收货时才发给用户的,所以成长值的退还，必须是已收货状态的订单
        // 并且订单退款状态将修改成已关闭的订单
        if (Objects.equals(message.getDbOrderStatus(), OrderStatus.SUCCESS.value()) || Objects.equals(message.getOrderStatus(), OrderStatus.CLOSE.value())) {
            LOG.info("订单退款成功，还原用户成长值... message: {}", Json.toJsonString(message));
            userLevelService.refundGrowth(message.getOrderId());
        }
    }
}
