package com.neusoft.smart.pos.framework.config;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.amqp.core.Queue;

@Configuration
public class RabbitExchangeConfiguration {

    private static Logger logger = LoggerFactory.getLogger(RabbitExchangeConfiguration.class);

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    /**
     * login交换机名称:
     */
    public static final String FANOUT_EXCHANGE_LOGIN = "fanout.exchange.login";
    /**
     * login队列名称1
     */
    public static final String LONGIN_QUEUE1_NAME = "login.queue1.name";
//    /**
//     * login队列名称2
//     */
//    public static final String LONGIN_QUEUE2_NAME = "login.queue2.name";

    /**
     * 订单完成交换机名称:
     */
    public static final String FANOUT_EXCHANGE_ORDER_COMPLETE = "fanout.exchange.order.complete";
    /**
     * 订单完成队列名称1
     */
    public static final String ORDER_COMPLETE_QUEUE1_NAME = "order.complete.queue1.name";
    /**
     * 订单完成队列名称2
     */
    public static final String ORDER_COMPLETE_QUEUE2_NAME = "order.complete.queue2.name";

    /**
     * 文件上传通知交换机名称:
     */
    public static final String FANOUT_EXCHANGE_FILE_UPLOADED_NOTIFY = "fanout.exchange.file.uploaded.notify";
    /**
     * 文件上传通知队列名称1
     */
    public static final String FILE_UPLOADED_NOTIFY_QUEUE1_NAME = "file.uploaded.notify.queue1.name";
    /**
     * 文件上传通知队列名称2
     */
    public static final String FILE_UPLOADED_NOTIFY_QUEUE2_NAME = "file.uploaded.notify.queue2.name";

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    //必须是prototype类型
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory());
    }

    //配置fanout_exchange
    @Bean
    public FanoutExchange fanoutExchangeLogin() {
        logger.info("【【【Fanout登录交换机实例创建成功】】】");
        return new FanoutExchange(FANOUT_EXCHANGE_LOGIN);
    }

    /**
     * 队列一
     *
     * @return 队列实例
     */
    @Bean(name = "login.queue1.name")
    public Queue loginQueue1() {
        logger.info("【【【队列一实例创建成功】】】");
        return new Queue(LONGIN_QUEUE1_NAME, true); //队列持久
    }

//    /**
//     * 队列二
//     *
//     * @return 队列实例
//     */
//    @Bean(name = "login.queue2.name")
//    public Queue loginQueue2() {
////        logger.info("【【【队列二实例创建成功】】】");
//        return new Queue(LONGIN_QUEUE2_NAME, true); //队列持久
//    }

    //把所有的login队列都绑定到这个login交换机上去
    @Bean
    Binding bindingLoginQueue1ToLoginExchange(@Qualifier("login.queue1.name")Queue queue1) {
    	logger.info("【【【绑定队列一到Login交换机成功】】】");
        return BindingBuilder.bind(queue1).to(fanoutExchangeLogin());
    }

//    @Bean
//    Binding bindingLoginQueue2ToLoginExchange(@Qualifier("login.queue2.name")Queue queue2) {
//    	logger.info("【【【绑定队列二到交换机成功】】】");
//        return BindingBuilder.bind(queue2).to(fanoutExchangeLogin());
//    }


    //***********************************************************************************
    //配置fanout_exchange
    @Bean
    public FanoutExchange fanoutExchangeOrderComplete() {
        logger.info("【【【Fanout订单完成交换机实例创建成功】】】");
        return new FanoutExchange(FANOUT_EXCHANGE_ORDER_COMPLETE);
    }

    /**
     * 队列一
     *
     * @return 队列实例
     */
    @Bean(name = "order.complete.queue1.name")
    public Queue orderCompleteQueue1() {
        logger.info("【【【队列一实例创建成功】】】");
        return new Queue(ORDER_COMPLETE_QUEUE1_NAME, true); //队列持久
    }

    /**
     * 队列二
     *
     * @return 队列实例
     */
    @Bean(name = "order.complete.queue2.name")
    public Queue orderCompleteQueue2() {
        logger.info("【【【队列二实例创建成功】】】");
        return new Queue(ORDER_COMPLETE_QUEUE2_NAME, true); //队列持久
    }

    //把所有的order complete队列都绑定到这个OrderComplete交换机上去
    @Bean
    Binding bindingOrderCompleteQueue1ToOrderCompleteExchange(@Qualifier("order.complete.queue1.name")Queue queue1) {
        logger.info("【【【绑定队列一到OrderComplete交换机成功】】】");
        return BindingBuilder.bind(queue1).to(fanoutExchangeOrderComplete());
    }

    @Bean
    Binding bindingOrderCompleteQueue2ToOrderCompleteExchange(@Qualifier("order.complete.queue2.name")Queue queue2) {
    	logger.info("【【【绑定队列二到OrderComplete交换机成功】】】");
        return BindingBuilder.bind(queue2).to(fanoutExchangeOrderComplete());
    }

    //***********************************************************************************
    //配置fanout_exchange
    @Bean
    public FanoutExchange fanoutExchangeFileUploadedNotify() {
        logger.info("【【【Fanout文件上传交换机实例创建成功】】】");
        return new FanoutExchange(FANOUT_EXCHANGE_FILE_UPLOADED_NOTIFY);
    }

    /**
     * 队列一
     *
     * @return 队列实例
     */
    @Bean(name = "file.uploaded.notify.queue1.name")
    public Queue fileUploadedNotifyQueue1() {
        logger.info("【【【队列一实例创建成功】】】");
        return new Queue(FILE_UPLOADED_NOTIFY_QUEUE1_NAME, true); //队列持久
    }

    /**
     * 队列二
     *
     * @return 队列实例
     */
    @Bean(name = "file.uploaded.notify.queue2.name")
    public Queue fileUploadedNotifyQueue2() {
        logger.info("【【【队列二实例创建成功】】】");
        return new Queue(FILE_UPLOADED_NOTIFY_QUEUE2_NAME, true); //队列持久
    }

    //把所有的file uploaded notify队列都绑定到这个fileUploadedNotify交换机上去
    @Bean
    Binding bindingfileUploadedQueue1TofileUploadedNotifyExchange(@Qualifier("file.uploaded.notify.queue1.name")Queue queue1) {
        logger.info("【【【绑定队列一到fileUploadedNotify交换机成功】】】");
        return BindingBuilder.bind(queue1).to(fanoutExchangeFileUploadedNotify());
    }

    @Bean
    Binding bindingfileUploadedQueue2TofileUploadedNotifyExchange(@Qualifier("file.uploaded.notify.queue2.name")Queue queue2) {
        logger.info("【【【绑定队列二到fileUploadedNotify交换机成功】】】");
        return BindingBuilder.bind(queue2).to(fanoutExchangeFileUploadedNotify());
    }
}
