package com.tzy.framework.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

/**
 * RabbitMQ配置类
 * @author zheng
 * @version 1.0
 */

@Configuration
public class RabbitMQConfig {
	/**
     * 测试队列
     */
    public static final String QUEUE_TEST_HELLO = "QUEUE_TEST_HELLO";

    /**死信交换机**/
    public static final String SYS_ORDER_DELAY_EXCHANGE = "SYS_ORDER_DELAY_EXCHANGE";
    /**接收死信队列消息**/
    public static final String SYS_ORDER_RECEIVE_EXCHANGE = "SYS_ORDER_RECEIVE_EXCHANGE";
    /**死信接收队列**/
    public static final String SYS_ORDER_RECEIVE_QUEUE = "SYS_ORDER_RECEIVE_QUEUE";
    /**延时队列**/
    public static final String SYS_ORDER_DELAY_QUEUE = "SYS_ORDER_DELAY_QUEUE";
    /**路由key**/
    public static final String SYS_ORDER_RECEIVE_KEY = "SYS_ORDER_RECEIVE_KEY";
    /**死信队列路由key**/
    public static final String SYS_ORDER_DELAY_KEY = "SYS_ORDER_DELAY_KEY";
    /**
     * 测试发送消息到MQ
     * @return
     */
    @Bean
    public Queue testHello() {
        return new Queue(QUEUE_TEST_HELLO);
    }


    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange sysOrderDelayExchange() {
        return new DirectExchange(SYS_ORDER_DELAY_EXCHANGE);
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue sysOrderDelayQueue() {
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("x-dead-letter-exchange",SYS_ORDER_RECEIVE_EXCHANGE); //指定死信送往的交换机
        map.put("x-dead-letter-routing-key", SYS_ORDER_RECEIVE_KEY); //指定死信的routingkey
        return new Queue(SYS_ORDER_DELAY_QUEUE, true, false, false, map);
    }

    /**
     * 给死信队列绑定死信交换机
     * @return
     */
    @Bean
    public Binding sysOrderDelayBinding() {
        return BindingBuilder.bind(sysOrderDelayQueue()).to(sysOrderDelayExchange()).with(SYS_ORDER_DELAY_KEY);
    }

    /**
     * 死信接收交换机,用于接收死信队列的消息
     * @return
     */
    @Bean
    public DirectExchange sysOrderReceiveExchange() {
        return new DirectExchange(SYS_ORDER_RECEIVE_EXCHANGE);
    }

    /**
     * 死信接收队列
     * @return
     */
    @Bean
    public Queue sysOrderReceiveQueue() {
        return new Queue(SYS_ORDER_RECEIVE_QUEUE);
    }

    /**
     * 死信接收交换机绑定接收死信队列消费队列
     * @return
     */
    @Bean
    public Binding sysOrdeReceiveBinding() {
        return BindingBuilder.bind(sysOrderReceiveQueue()).to(sysOrderReceiveExchange()).with(SYS_ORDER_RECEIVE_KEY);
    }
}
