
package com.tmerclub.cloud.seckill.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.seckill.service.SeckillOrderService;
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_SECKILL_GROUP)
public class OrderNotifySeckillConsumer implements RocketMQListener<PayNotifyBO> {

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

    @Autowired
    private SeckillOrderService seckillOrderService;

    /**
     * 秒杀订单支付成功
     */
    @Override
    public void onMessage(PayNotifyBO message) {
        if (!Objects.equals(message.getOrderType(), OrderType.SECKILL.value())) {
            return;
        }
        LOG.info("秒杀订单支付成功... 准备删除redis中的订单，message: {}", Json.toJsonString(message));
        seckillOrderService.paySuccessOrderByOrderId(message.getOrderIds().get(0));
    }
}
