
package com.tmerclub.cloud.coupon.listener;

import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.coupon.service.CouponLockService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.COUPON_UNLOCK_TOPIC, consumerGroup = RocketMqConstant.COUPON_UNLOCK_TOPIC)
public class CouponUnlockConsumer implements RocketMQListener<List<Long>> {

    @Autowired
    private CouponLockService couponLockService;

    /**
     * 1、优惠券锁定一定时间后，如果订单支付未支付，则解锁优惠券（有可能优惠券锁定成功，订单因为异常回滚导致订单未创建）
     * 2、取消订单，直接解锁优惠券
     */
    @Override
    public void onMessage(List<Long> orderIds) {
        couponLockService.unlockCoupon(orderIds);
    }
}
