package com.bz.rmcp.dap.config;

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

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

/**
 * @author linqingwei
 * @date 2021-08-09 17:11
 */
@Configuration
public class RabbitMqConfig {
    public static final String DELAY_EXCHANGE_NAME = "rmcp.dap.delay.device.offline.exchange";
    public static final String DELAY_QUEUE_NAME = "rmcp.dap.delay.device.offline.queue";
    public static final String DELAY_QUEUE_ROUTING_KEY = "rmcp.dap.delay.device.offline.queue.routingkey";
    public static final String DEAD_LETTER_EXCHANGE_NAME = "rmcp.dap.dead_letter.device.offline.exchange";
    public static final String DEAD_LETTER_QUEUE_NAME = "rmcp.dap.dead_letter.device.offline.queuea";
    public static final String DEAD_LETTER_QUEUE_ROUTING_KEY = "rmcp.dap.dead_letter.device.offline.routingkey";
    public static final String DEVICE_DATA_EXCHANGE_NAME = "rmcp.dap.device.data.message.exchange";
    public static final String DEVICE_DATA_QUEUE_NAME = "rmcp.dap.device.data.message.queue";

    // 声明延时Exchange
    @Bean("delayExchange")
    public DirectExchange delayExchange(){
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }

    // 声明死信Exchange
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME);
    }

    @Bean("deviceDataExchange")
    public DirectExchange deviceDataExchange(){
        return new DirectExchange(DEVICE_DATA_EXCHANGE_NAME);
    }

    // 声明延时队列
    // 并绑定到对应的死信交换机
    @Bean("delayQueue")
    public Queue delayQueue(){
        Map<String, Object> args = new HashMap<>(2);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_ROUTING_KEY);
        // x-message-ttl  声明队列的TTL
        //args.put("x-message-ttl", 10000);//10秒
        args.put("x-message-ttl", 86400000);//24小时
        return QueueBuilder.durable(DELAY_QUEUE_NAME).withArguments(args).build();
    }



    // 声明死信队列
    @Bean("deadLetterQueue")
    public Queue deadLetterQueue(){
        return new Queue(DEAD_LETTER_QUEUE_NAME);
    }

    // 声明死信队列
    @Bean("deviceDataQueue")
    public Queue deviceDataQueue(){
        return new Queue(DEVICE_DATA_QUEUE_NAME);
    }



    // 声明延时队列绑定关系
    @Bean
    public Binding delayBinding(@Qualifier("delayQueue") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_ROUTING_KEY);
    }



    // 声明死信队列绑定关系
    @Bean
    public Binding deadLetterBinding(@Qualifier("deadLetterQueue") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_ROUTING_KEY);
    }

    // 声明延时队列绑定关系
    @Bean
    Binding deviceDataBinding(@Qualifier("deviceDataQueue") Queue queue,
                              @Qualifier("deviceDataExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).withQueueName();
    }


}
