package com.yami.shop.seckill.comment.listener;

import com.yami.shop.bean.enums.OrderType;
import com.yami.shop.bean.event.PaySuccessOrderEvent;
import com.yami.shop.bean.order.PaySuccessOrderOrder;
import com.yami.shop.seckill.comment.dao.SeckillOrderMapper;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 秒杀成功回调
 * @author LGH
 */
@Component("seckillPaySuccessListener")
@AllArgsConstructor
public class PaySuccessOrderListener {

    private SeckillOrderMapper seckillOrderMapper;

    /**
     * 更新秒杀状态
     */
    @EventListener(PaySuccessOrderEvent.class)
    @Order(PaySuccessOrderOrder.SECKILL)
    public void seckillPaySuccessListener(PaySuccessOrderEvent event) {
        List<com.yami.shop.bean.model.Order> orders = event.getOrders();

        for (com.yami.shop.bean.model.Order order : orders) {
            if (!Objects.equals(order.getOrderType(), OrderType.SECKILL.value())) {
                return;
            }
            // 更新秒杀状态
            seckillOrderMapper.updateStateByOrderNumber(order.getOrderNumber(), 1);

        }

    }
}
