package cn.edu.ecnu.stu.bookstore.listener;

import cn.edu.ecnu.stu.bookstore.config.RabbitMqConfig;
import cn.edu.ecnu.stu.bookstore.mapper.OrderMapper;
import cn.edu.ecnu.stu.bookstore.pojo.OrderStatus;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExpiredOrderListener {

    @Autowired
    private OrderMapper orderMapper;

    @RabbitListener(queues = RabbitMqConfig.EXPIRED_ORDER_QUEUE)
    public void processExpiredOrder(String msg, Channel channel, Message message) {
        System.out.println(msg + "过期");
        OrderStatus status = OrderStatus.getByValue(orderMapper.selectStatus(msg));
        if(OrderStatus.WAIT_PAYMENT.equals(status)) {
            orderMapper.updateOrderStatus(msg, OrderStatus.CANCEL);
        }
    }
}
