
package com.tmerclub.cloud.coupon.listener;

import com.tmerclub.cloud.common.order.bo.PayNotifyBO;
import com.tmerclub.cloud.common.order.constant.OrderType;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.coupon.service.CouponLockService;
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 tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_NOTIFY_SERVICE_TOPIC, consumerGroup = RocketMqConstant.ORDER_NOTIFY_COUPON_GROUP)
public class OrderNotifyCouponConsumer implements RocketMQListener<PayNotifyBO> {

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

    @Autowired
    private CouponLockService couponLockService;


    /**
     * 订单支付成功，将优惠券标记成已使用状态
     *
     * @param message
     */
    @Override
    public void onMessage(PayNotifyBO message) {
        // 普通订单和预售订单才能使用优惠券
        if (!Objects.equals(message.getOrderType(), OrderType.ORDINARY.value())) {
            return;
        }
        LOG.info("订单支付成功，将优惠券标记成已使用状态... message: {}", Json.toJsonString(message));
        couponLockService.markerCouponUse(message.getOrderIds());
    }
}
