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

import cn.edu.ecnu.stu.bookstore.component.Result;
import cn.edu.ecnu.stu.bookstore.config.RabbitMqConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/test")
    public Result test() {
        return Result.success("hello, test");
    }

    @GetMapping("/testmq")
    public Result testmq(@RequestParam("message") String message) {
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXPIRED_ORDER_EXCHANGE, RabbitMqConfig.EXPIRED_ORDER_ROUTING_KEY,
                message, message1 -> {
                    message1.getMessageProperties().setDelay(5000);
                    return message1;
                });
        return Result.success();
    }

//    @RabbitListener(queues = RabbitMqConfig.EXPIRED_ORDER_QUEUE)
//    public void processExpiredOrder(String msg, Channel channel, Message message) {
//        System.out.println(msg);
//    }
}
