package org.jeecg.modules.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    /**
     * ---------------------------------------------订单超时未支付取消
     */
    public static final String ORDER_DELAY_EXCHANGE = "order_delayed_exchange";
    public static final String ORDER_DELAY_QUEUE = "order_delayed_queue";
    public static final String ORDER_DELAY_ROUTING_KEY = "order_delayed_key";

    // 声明延迟交换机（持久化）
    @Bean
    public CustomExchange orderDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct"); // 延迟交换机类型
        return new CustomExchange(ORDER_DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    // 声明持久化队列
    @Bean
    public Queue orderDelayQueue() {
        return QueueBuilder.durable(ORDER_DELAY_QUEUE)
                .withArgument("x-dead-letter-exchange", "dlx_exchange") // 死信交换机（可选）
                .build();
    }

    // 绑定队列到交换机
    @Bean
    public Binding orderDelayBinding(Queue orderDelayQueue, CustomExchange orderDelayExchange) {
        return BindingBuilder.bind(orderDelayQueue).to(orderDelayExchange).with(ORDER_DELAY_ROUTING_KEY).noargs();
    }

    /**
     * ---------------------------------------------订单过期
     */
    public static final String ORDER_EXPIRE_DELAY_EXCHANGE = "order_expire_delayed_exchange";
    public static final String ORDER_EXPIRE_DELAY_QUEUE = "order_expire_delayed_queue";
    public static final String ORDER_EXPIRE_DELAY_ROUTING_KEY = "order_expire_delayed_key";

    // 声明延迟交换机（持久化）
    @Bean
    public CustomExchange orderExpireDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct"); // 延迟交换机类型
        return new CustomExchange(ORDER_EXPIRE_DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    // 声明持久化队列
    @Bean
    public Queue orderExpireDelayQueue() {
        return QueueBuilder.durable(ORDER_EXPIRE_DELAY_QUEUE)
                .withArgument("x-dead-letter-exchange", "dlx_exchange") // 死信交换机（可选）
                .build();
    }

    // 绑定队列到交换机
    @Bean
    public Binding orderExpireDelayBinding(Queue orderExpireDelayQueue, CustomExchange orderExpireDelayExchange) {
        return BindingBuilder.bind(orderExpireDelayQueue).to(orderExpireDelayExchange).with(ORDER_EXPIRE_DELAY_ROUTING_KEY).noargs();
    }
}