package cn.config;

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

import cn.facade.abs.QueueEnum;

/**
 * 消息队列配置
 */
@Configuration
public class RabbitMqConfig {

    @Bean
    DirectExchange atvUpdateDirectExchange() {
        return new DirectExchange(QueueEnum.QUEUE_ATV_UPDATE.getExchange(), true, false);
    }

    @Bean
    DirectExchange atvUpdateTtlDirectExchange() {
        return new DirectExchange(QueueEnum.QUEUE_TTL_ATV_UPDATE.getExchange(), true, false);
    }

    @Bean
    public Queue atvUpdateQueue() {
        return new Queue(QueueEnum.QUEUE_ATV_UPDATE.getName());
    }

    @Bean
    public Queue atvTtlUpdateQueue() {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_TTL_ATV_UPDATE.getName())
                .withArgument("x-dead-letter-exchange", QueueEnum.QUEUE_ATV_UPDATE.getExchange())//到期后转发的交换机
                .withArgument("x-dead-letter-routing-key", QueueEnum.QUEUE_ATV_UPDATE.getRouteKey())//到期后转发的路由键
                .build();
    }

    @Bean
    public Binding atvUpdateBindingQueue() {
        return BindingBuilder.bind(atvUpdateQueue())
                .to(atvUpdateDirectExchange())
                .with(QueueEnum.QUEUE_ATV_UPDATE.getRouteKey());
    }

    @Bean
    public Binding atvTtlUpdateBindingQueue() {
        return BindingBuilder.bind(atvTtlUpdateQueue())
                .to(atvUpdateTtlDirectExchange())
                .with(QueueEnum.QUEUE_TTL_ATV_UPDATE.getRouteKey());
    }

    /**
     * ATV消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange atvDirect() {
        return ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_ATV.getExchange())
                .durable(true)
                .build();
    }

    /**
     * ATV延迟队列队列所绑定的交换机DirectExchange
     */
    @Bean
    DirectExchange atvTtlDirect() {
        return ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_TTL_ATV.getExchange())
                .durable(true)
                .build();
    }

    /**
     * ATV实际消费队列
     */
    @Bean
    public Queue atvQueue() {
        return new Queue(QueueEnum.QUEUE_ATV.getName());
    }

    /**
     * ATV延迟队列（死信队列）
     */
    @Bean
    public Queue atvTtlQueue() {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_TTL_ATV.getName())
                .withArgument("x-dead-letter-exchange", QueueEnum.QUEUE_ATV.getExchange())//到期后转发的交换机
                .withArgument("x-dead-letter-routing-key", QueueEnum.QUEUE_ATV.getRouteKey())//到期后转发的路由键
                .build();
    }

    /**
     * 将ATV队列绑定到交换机
     */
    @Bean
    Binding atvBinding(DirectExchange atvDirect, Queue atvQueue) {
        return BindingBuilder
                .bind(atvQueue)
                .to(atvDirect)
                .with(QueueEnum.QUEUE_ATV.getRouteKey());
    }

    /**
     * 将ATV延迟队列绑定到交换机
     */
    @Bean
    Binding atvTtlBinding(DirectExchange atvTtlDirect, Queue atvTtlQueue) {
        return BindingBuilder
                .bind(atvTtlQueue)
                .to(atvTtlDirect)
                .with(QueueEnum.QUEUE_TTL_ATV.getRouteKey());
    }

    /**
     * ATV_CMD1消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange atvCmd1Direct() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_ATV_CMD1.getExchange())
                .durable(true)
                .build();
    }

    /**
     * ATV_CMD1实际消费队列
     */
    @Bean
    public Queue atvCmd1Queue() {
        return new Queue(QueueEnum.QUEUE_ATV_CMD1.getName());
    }

    /**
     * 将ATV_CMD1队列绑定到交换机
     */
    @Bean
    Binding atvCmd1Binding(DirectExchange atvCmd1Direct, Queue atvCmd1Queue) {
        return BindingBuilder
                .bind(atvCmd1Queue)
                .to(atvCmd1Direct)
                .with(QueueEnum.QUEUE_ATV_CMD1.getRouteKey());
    }


}
