package com.ssj.framework.core.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 
 * 按我们系统，常用的是这种方式
 * 2019年4月17日15:30:10 
 * @author wuwen
 *
 */

@Configuration
public class TopicRabbitConfig {
	
	/**
	 * 交换机
	 */
	//task Exchange
	//Sys Exchange
	//Kmt Exchange
	//weixin Exchange
	//weixin_api Exchange
	//web  Exchange
	public final static String exchange_task = "exchange_task";
	public final static String exchange_aijia = "exchange_aijia";
	public final static String exchange_homeworkfeedback_push = "exchange_homeworkfeedback_push";
	public final static String exchange_sys = "exchange_sys";
	public final static String exchange_kmt = "exchange_kmt";
	public final static String exchange_weixin = "exchange_weixin";
	public final static String exchange_weixin_api = "exchange_weixin_api";
	public final static String exchange_web = "exchange_web";
	
	
	/**
	 * topic定义
	 */
	//推送到微信
	public  final static String topic_push_messages_wx = "topic.task.push.wx";
	
	//推送到邮箱
	public  final static String topic_push_messages_mail = "topic.task.push.mail";
	
	//推送到APP
	public  final static String topic_push_messages_app = "topic.task.pash.app";
	
	//作业图片识别
	public final static String topic_homework_discern = "topic.homework.discern";

    //作业图片识别
    public final static String topic_homework_picture_discern = "topic.homeworkpicture.discern";

    /**
     * 相似题查询
     */
	public final static String topic_problem_similar_query = "topic.problem.similar_query";

    /**
     * 相似题查询
     */
    public final static String topic_submit_homework_picture_discern = "topic.submit.homeworkpicture.discern";

	//接口请求日志保存
	public final static String topic_homeworkfeedback_push = "topic.homeworkfeedback.push";
	
	//爱加分配设备到ssj
	public final static String topic_fx_ssj = "topic.fx.ssj";
	
	//ssj分配设备到爱加
	public final static String topic_fx_aijia = "topic.fx.aijia";
	
	
	//注册
    @Bean
    TopicExchange exchangeSys() {
        return new TopicExchange(TopicRabbitConfig.exchange_sys);
    }
    @Bean
    public Queue queueFXSys() {
        return new Queue(TopicRabbitConfig.topic_fx_ssj);
    }
    @Bean
    Binding bindingExchangeSys(Queue queueFXSys, TopicExchange exchangeSys) {
        return BindingBuilder.bind(queueFXSys).to(exchangeSys).with(topic_fx_ssj);
    }
    
    //注册
    @Bean
    public Queue queueFXAijia() {
        return new Queue(TopicRabbitConfig.topic_fx_aijia);
    }
    @Bean
    Binding bindingExchangeAijia(Queue queueFXAijia, TopicExchange exchangeSys) {
        return BindingBuilder.bind(queueFXAijia).to(exchangeSys).with(topic_fx_aijia);
    }
    
	
	//注册
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(TopicRabbitConfig.exchange_task);
    }
    @Bean
    public Queue queuePushMessagesWx() {
        return new Queue(TopicRabbitConfig.topic_push_messages_wx);
    }
    
    @Bean
    public Queue queuePushMessagesMail() {
        return new Queue(TopicRabbitConfig.topic_push_messages_mail);
    }
    
    @Bean
    public Queue queuePushMessagesAPP() {
        return new Queue(TopicRabbitConfig.topic_push_messages_app);
    }
    
    //注册
    @Bean
    TopicExchange exchangeHomework() {
        return new TopicExchange(TopicRabbitConfig.exchange_aijia);
    }

    @Bean
    TopicExchange exchangeHomeworkfeedbackPush() {
        return new TopicExchange(TopicRabbitConfig.exchange_homeworkfeedback_push);
    }

    @Bean
    public Queue queueHomeworkDiscern() {
        return new Queue(TopicRabbitConfig.topic_homework_discern);
    }

    @Bean
    public Queue queueHomeworkPictureDiscern() {
        return new Queue(TopicRabbitConfig.topic_homework_picture_discern);
    }

    @Bean
    public Queue queueSubmitHomeworkPictureDiscern() {
        return new Queue(TopicRabbitConfig.topic_submit_homework_picture_discern);
    }

    @Bean
    public Queue queueProblemSimilarQuery() {
        return new Queue(TopicRabbitConfig.topic_problem_similar_query);
    }

    @Bean
    public Queue queueHomeworkfeedbackPush() {
        return new Queue(TopicRabbitConfig.topic_homeworkfeedback_push);
    }
 
    //绑定到交换机
    @Bean
    Binding bindingExchangePushMessagesWx(Queue queuePushMessagesWx, TopicExchange exchange) {
        return BindingBuilder.bind(queuePushMessagesWx).to(exchange).with(topic_push_messages_wx);
    }
    
    @Bean
    Binding bindingExchangePushMessagesMail(Queue queuePushMessagesMail, TopicExchange exchange) {
        return BindingBuilder.bind(queuePushMessagesMail).to(exchange).with(topic_push_messages_mail);
    }
    
    
    @Bean
    Binding bindingExchangePushMessagesAPP(Queue queuePushMessagesAPP, TopicExchange exchange) {
        return BindingBuilder.bind(queuePushMessagesAPP).to(exchange).with(topic_push_messages_app);
    }

    @Bean
    Binding bindingExchangHomeworkDiscern(Queue queueHomeworkDiscern, TopicExchange exchangeHomework) {
        return BindingBuilder.bind(queueHomeworkDiscern).to(exchangeHomework).with(topic_homework_discern);
    }

    @Bean
    Binding bindingExchangHomeworkPictureDiscern(Queue queueHomeworkPictureDiscern, TopicExchange exchangeHomework) {
        return BindingBuilder.bind(queueHomeworkPictureDiscern).to(exchangeHomework).with(topic_homework_picture_discern);
    }

    @Bean
    Binding bindingExchangeSubmitHomeworkPictureDiscern(Queue queueSubmitHomeworkPictureDiscern, TopicExchange exchangeHomework) {
        return BindingBuilder.bind(queueSubmitHomeworkPictureDiscern).to(exchangeHomework).with(topic_submit_homework_picture_discern);
    }


    @Bean
    Binding bindingExchangQuestionSimilarQuery(Queue queueProblemSimilarQuery, TopicExchange exchangeHomework) {
        return BindingBuilder.bind(queueProblemSimilarQuery).to(exchangeHomework).with(topic_problem_similar_query);
    }

    @Bean
    Binding bindingExchangHomeworkfeedbackPush(Queue queueHomeworkfeedbackPush, TopicExchange exchangeHomeworkfeedbackPush) {
        return BindingBuilder.bind(queueHomeworkfeedbackPush).to(exchangeHomeworkfeedbackPush).with(topic_homeworkfeedback_push);
    }


    @Bean("customContainerFactory")
    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                 ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConcurrentConsumers(100);  //设置线程数
        factory.setMaxConcurrentConsumers(200); //最大线程数
        configurer.configure(factory, connectionFactory);
        return factory;
    }

  
}
