package com.muyu.cargateway.config;

import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Lenovo
 * @ Tool：IntelliJ IDEA
 * @ Author：CHX
 * @ Date：2024-10-04-15:13
 * @ Version：1.0
 * @ Description：rabbitmq配置类
 */
@Log4j2
@Configuration
public class RabbitmqConfig {
    // 日志
    private static final Logger logger = LoggerFactory.getLogger(RabbitmqConfig.class);

    /**
     * 队列
     */
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    /**
     * 队列
     */
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    /**
     * 交换机
     */
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
    /**
     * 路由key
     */
    public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
    /**
     * 路由key
     */
    public static final String ROUTINGKEY_SMS = "inform.#.sms.#";

    /**
     * 声明交换机,做持久化
     */
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange exchangeTopicsInform() {
        try {
            Exchange exchange = ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
            log.info("创建的交换机为: {}", EXCHANGE_TOPICS_INFORM);
            return exchange;
        } catch (Exception e) {
            log.error("创建该: {} 交换机失败", EXCHANGE_TOPICS_INFORM, e);
            throw e;
        }
    }

    // 声明QUEUE_INFORM_EMAIL队列
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue queueInformEmail() {
        try {
            Queue queue = new Queue(QUEUE_INFORM_EMAIL);
            log.info("创建的队列为: {}", QUEUE_INFORM_EMAIL);
            return queue;
        } catch (Exception e) {
            log.error("创建该: {} 队列失败", QUEUE_INFORM_EMAIL, e);
            throw e;
        }
    }

    // 声明QUEUE_INFORM_SMS队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue queueInformSms() {
        try {
            Queue queue = new Queue(QUEUE_INFORM_SMS);
            log.info("创建的队列为: {}", QUEUE_INFORM_SMS);
            return queue;
        } catch (Exception e) {
            log.error("创建该: {} 队列失败", QUEUE_INFORM_SMS, e);
            throw e;
        }
    }

    //ROUTINGKEY_EMAIL队列绑定交换机，指定routingKey
    @Bean
    public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                           @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
    }

    //ROUTINGKEY_SMS队列绑定交换机，指定routingKey
    @Bean
    public Binding bindingRoutingKeySms(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                        @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
    }
}
