package com.clx.performance.config;

import com.clx.performance.constant.RabbitKeyConstants;
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 xujianke
 * @date 2017年9月14日
 * @description rabbit配置文件
 */
@Configuration
public class RabbitBeanConfig {
    /**
     * 订单已挂单队列
     */
    @Bean
    public Queue orderPostedQueue() {
        return new Queue(RabbitKeyConstants.ORDER_POSTED_QUEUE, true);
    }
    /**
     * 订单已挂单交换机
     **/
    @Bean
    public DirectExchange orderPostedExchange() {
        return new DirectExchange(RabbitKeyConstants.ORDER_POSTED_EXCHANGE);
    }
    /**
     * 订单已挂单绑定
     */
    @Bean
    public Binding orderPostedExchangeBind() {
        return BindingBuilder.bind(orderPostedQueue()).to(orderPostedExchange()).with(RabbitKeyConstants.ORDER_POSTED_ROUTE_KEY);
    }


    @Bean
    public Queue orderOnQueue() {
        Map<String, Object> params = new HashMap<>(6);
        params.put("x-dead-letter-exchange", RabbitKeyConstants.ORDER_ON_DEAD_EXCHANGE);
        params.put("x-dead-letter-routing-key", RabbitKeyConstants.ORDER_ON_DEAD_ROUTE_KEY);
        return new Queue(RabbitKeyConstants.ORDER_ON_QUEUE, true, false, false, params);
    }

    /**
     * 订单挂单中交换机
     **/
    @Bean
    public DirectExchange orderOnExchange() {
        return new DirectExchange(RabbitKeyConstants.ORDER_ON_EXCHANGE);
    }

    /**
     * 订单挂单中绑定
     */
    @Bean
    public Binding orderOnExchangeBind() {
        return BindingBuilder.bind(orderOnQueue()).to(orderOnExchange()).with(RabbitKeyConstants.ORDER_ON_ROUTE_KEY);
    }

    /**
     * 死信队列：死信队列处理延迟消息
     * @return
     */
    @Bean
    public Queue orderOnDeadQueue() {
        return new Queue(RabbitKeyConstants.ORDER_ON_DEAD_QUEUE, true, false, false);
    }

    /**
     * 订单挂单中交换机：死信队列处理延迟消息
     **/
    @Bean
    public DirectExchange orderOnDeadExchange() {
        return new DirectExchange(RabbitKeyConstants.ORDER_ON_DEAD_EXCHANGE);
    }

    /**
     * 订单挂单中绑定:死信队列处理延迟消息
     */
    @Bean
    public Binding orderDeadExchangeBind() {
        return BindingBuilder.bind(orderOnDeadQueue()).to(orderOnDeadExchange()).with(RabbitKeyConstants.ORDER_ON_DEAD_ROUTE_KEY);
    }


    /**
//     * 货单挂单队列
//     * @return
//     */
//    @Bean
//    public Queue orderGoodsOnQueue() {
//        Map<String, Object> params = new HashMap<>(6);
//        params.put("x-dead-letter-exchange", RabbitKeyConstants.ORDER_GOODS_ON_DEAD_EXCHANGE);
//        params.put("x-dead-letter-routing-key", RabbitKeyConstants.ORDER_GOODS_ON_DEAD_ROUTE_KEY);
//        return new Queue(RabbitKeyConstants.ORDER_GOODS_ON_QUEUE, true, false, false, params);
//    }
//
//    /**
//     * 货单挂单中交换机
//     **/
//    @Bean
//    public DirectExchange orderGoodsOnExchange() {
//        return new DirectExchange(RabbitKeyConstants.ORDER_GOODS_ON_EXCHANGE);
//    }
//
//    /**
//     * 货单挂单中绑定
//     */
//    @Bean
//    public Binding orderGoodsOnExchangeBind() {
//        return BindingBuilder.bind(orderGoodsOnQueue()).to(orderGoodsOnExchange()).with(RabbitKeyConstants.ORDER_GOODS_ON_ROUTE_KEY);
//    }

    /**
     * 死信队列：死信队列处理延迟消息货单挂单
     * @return
     */
    @Bean
    public Queue orderGoodsOnDeadQueue() {
        return new Queue(RabbitKeyConstants.ORDER_GOODS_ON_DEAD_QUEUE, true, false, false);
    }

    /**
     * 货单挂单中交换机：死信队列处理延迟消息
     **/
    @Bean
    public CustomExchange orderGoodsOnDeadExchange() {
        Map<String, Object> arguments = new HashMap<>();
        //设置路由模式 direct模式
        arguments.put("x-delayed-type", "direct");
        return new CustomExchange(RabbitKeyConstants.ORDER_GOODS_ON_DEAD_EXCHANGE,"x-delayed-message", true, false, arguments);
    }

    /**
     * 货单挂单中绑定:死信队列处理延迟消息
     */
//    @Bean
//    public Binding orderGoodsDeadExchangeBind() {
//        return BindingBuilder.bind(orderGoodsOnDeadQueue()).to(orderGoodsOnDeadExchange()).with(RabbitKeyConstants.ORDER_GOODS_ON_DEAD_ROUTE_KEY);
//    }
    //绑定交换机,队列和routingKey
    @Bean
    public Binding delayedQueueBindingDelayedExchange(@Qualifier("orderGoodsOnDeadQueue")Queue queue,@Qualifier("orderGoodsOnDeadExchange")CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(RabbitKeyConstants.ORDER_GOODS_ON_DEAD_ROUTE_KEY).noargs();
    }

    /**
     * 订单取消队列
     * @return
     */
    @Bean
    public Queue orderCancelQueue() {
        return new Queue(RabbitKeyConstants.ORDER_CANCEL_QUEUE, true, false, false);
    }

    /**
     * 订单取消队列交换机
     **/
    @Bean
    public DirectExchange orderCancelExchange() {
        return new DirectExchange(RabbitKeyConstants.ORDER_CANCEL_EXCHANGE);
    }

    /**
     * 订单取消队列绑定
     */
    @Bean
    public Binding orderCancelExchangeBind() {
        return BindingBuilder.bind(orderCancelQueue()).to(orderCancelExchange()).with(RabbitKeyConstants.ORDER_CANCEL_ROUTE_KEY);
    }
}
