package com.momo.tplusapi.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;

/**
 * @author: mo
 * @description: TODO
 * @date: 2025/6/29 14:00
 * @version: 2.0
 */
@Configuration
public class OrderRabbitConfig {

    // ================== 常量定义：按功能分组 ==================

    public static final class Exchanges {
        public static final String ORDER = "order.event.exchange";
        public static final String DLX = "order.dlx.exchange";
        public static final String DELAY = "order.delay.exchange";
        public static final String XDELAYED = "order.xdelayed.exchange";
    }

    public static final class Queues {
        // 主业务队列
        public static final String SALE_ORDER = "order.sale.queue";
        public static final String SALE_DELIVERY = "order.delivery.queue";

        // 死信队列
        public static final String SALE_ORDER_DLX = "order.sale.dlx.queue";
        public static final String SALE_DELIVERY_DLX = "order.delivery.dlx.queue";

        // TTL延迟队列
        public static final String SALE_ORDER_DELAY = "order.sale.delay.queue";
        public static final String SALE_DELIVERY_DELAY = "order.delivery.delay.queue";

        // 插件延迟队列
        public static final String SALE_ORDER_XDELAYED = "order.sale.xdelayed.queue";
        public static final String SALE_DELIVERY_XDELAYED = "order.delivery.xdelayed.queue";
    }

    public static final class RoutingKeys {
        // 主业务路由 Key
        public static final String SALE_ORDER = "order.sale.#";
        public static final String SALE_DELIVERY = "order.delivery.#";

        // 死信路由 Key
        public static final String SALE_ORDER_DLX = "order.sale.dlx";
        public static final String SALE_DELIVERY_DLX = "order.delivery.dlx";

        // TTL延迟路由 Key
        public static final String SALE_ORDER_DELAY = "order.sale.delay";
        public static final String SALE_DELIVERY_DELAY = "order.delivery.delay";

        // 插件延迟路由 Key
        public static final String SALE_ORDER_XDELAYED = "order.sale.xdelayed";
        public static final String SALE_DELIVERY_XDELAYED = "order.delivery.xdelayed";
    }

    private int x_message_ttl = 30000; // 默认30秒

    // ================== Exchange Definitions ==================

    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange(Exchanges.ORDER, true, false);
    }

    @Bean
    public TopicExchange dlxExchange() {
        return new TopicExchange(Exchanges.DLX, true, false);
    }

    @Bean
    public TopicExchange orderDelayExchange() {
        return new TopicExchange(Exchanges.DELAY, true, false);
    }

    @Bean
    public CustomExchange orderXdelayedExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "topic");
        return new CustomExchange(Exchanges.XDELAYED, "x-delayed-message", true, false, args);
    }

    // ================== Queue Definitions ==================

    @Bean
    public Queue saleOrderQueue() {
        return new Queue(Queues.SALE_ORDER, true, false, false, Map.of(
                "x-dead-letter-exchange", Exchanges.DLX,
                "x-dead-letter-routing-key", RoutingKeys.SALE_ORDER_DLX
        ));
    }

    @Bean
    public Queue saleDeliveryQueue() {
        return new Queue(Queues.SALE_DELIVERY, true, false, false, Map.of(
                "x-dead-letter-exchange", Exchanges.DLX,
                "x-dead-letter-routing-key", RoutingKeys.SALE_DELIVERY_DLX
        ));
    }

    @Bean
    public Queue saleOrderDelayQueue() {
        return new Queue(Queues.SALE_ORDER_DELAY, true, false, false, Map.of(
                "x-dead-letter-exchange", Exchanges.ORDER,
                "x-dead-letter-routing-key", "order.sale.retry",
                "x-message-ttl", x_message_ttl
        ));
    }

    @Bean
    public Queue saleDeliveryDelayQueue() {
        return new Queue(Queues.SALE_DELIVERY_DELAY, true, false, false, Map.of(
                "x-dead-letter-exchange", Exchanges.ORDER,
                "x-dead-letter-routing-key", "order.delivery.retry",
                "x-message-ttl", x_message_ttl
        ));
    }

    @Bean
    public Queue saleOrderDlxQueue() {
        return new Queue(Queues.SALE_ORDER_DLX, true);
    }

    @Bean
    public Queue saleDeliveryDlxQueue() {
        return new Queue(Queues.SALE_DELIVERY_DLX, true);
    }

    @Bean
    public Queue saleOrderXdelayedQueue() {
        return new Queue(Queues.SALE_ORDER_XDELAYED, true);
    }

    @Bean
    public Queue saleDeliveryXdelayedQueue() {
        return new Queue(Queues.SALE_DELIVERY_XDELAYED, true);
    }

    // ================== Binding Definitions ==================

    @Bean
    public Binding saleOrderBinding() {
        return BindingBuilder.bind(saleOrderQueue()).to(orderExchange()).with(RoutingKeys.SALE_ORDER);
    }

    @Bean
    public Binding saleDeliveryBinding() {
        return BindingBuilder.bind(saleDeliveryQueue()).to(orderExchange()).with(RoutingKeys.SALE_DELIVERY);
    }

    @Bean
    public Binding saleOrderDlxBinding() {
        return BindingBuilder.bind(saleOrderDlxQueue()).to(dlxExchange()).with(RoutingKeys.SALE_ORDER_DLX);
    }

    @Bean
    public Binding saleDeliveryDlxBinding() {
        return BindingBuilder.bind(saleDeliveryDlxQueue()).to(dlxExchange()).with(RoutingKeys.SALE_DELIVERY_DLX);
    }

    @Bean
    public Binding saleOrderDelayBinding() {
        return BindingBuilder.bind(saleOrderDelayQueue()).to(orderDelayExchange()).with(RoutingKeys.SALE_ORDER_DELAY);
    }

    @Bean
    public Binding saleDeliveryDelayBinding() {
        return BindingBuilder.bind(saleDeliveryDelayQueue()).to(orderDelayExchange()).with(RoutingKeys.SALE_DELIVERY_DELAY);
    }

    @Bean
    public Binding saleOrderXdelayedBinding() {
        return BindingBuilder.bind(saleOrderXdelayedQueue())
                .to(orderXdelayedExchange())
                .with(RoutingKeys.SALE_ORDER_XDELAYED)
                .noargs();
    }

    @Bean
    public Binding saleDeliveryXdelayedBinding() {
        return BindingBuilder.bind(saleDeliveryXdelayedQueue())
                .to(orderXdelayedExchange())
                .with(RoutingKeys.SALE_DELIVERY_XDELAYED)
                .noargs();
    }
}


这个是不是只是表示对列是durable，并不是表示消息是durable？