
package com.heyu.api.data.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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


/**
 * @author wutao
 * @description mq配置
 * @date 2020-12-25
 */
@Configuration
@Slf4j
public class RabbitConfig {

    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Bean
    public RabbitTemplate rabbitTemplate(){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setConfirmCallback((correlationData,ack,cause)->{
            if(ack){
                log.info("消息发送成功 {}", correlationData);
            }else {
                log.info("消息发送到Exchange失败, {}, cause: {}", correlationData, cause);
            }
        });
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnCallback(((message, replyCode, replyText, exchange, routingKey) -> {
            log.info("消息从Exchange路由到Queue失败: exchange: {}, route: {}, replyCode: {}, replyText: {}, message: {}", exchange, routingKey, replyCode, replyText, message);
        }));
        return rabbitTemplate;
    }

    @Bean
    public Queue accountAmountQueue(@Value("${eb.config.rabbitQueue.accountAmountQueue}") String queueName) {
        return new Queue(queueName);
    }



    @Bean
    public Queue createDataQueue(@Value("${eb.config.rabbitQueue.createDataQueue}") String queueName) {
        return new Queue(queueName);
    }




    @Bean
    public Queue sendDingDingQueue(@Value("${eb.config.rabbitQueue.sendDingDingQueue}") String queueName) {
        return new Queue(queueName);
    }



    @Bean
    public Queue delayQueue(@Value("${eb.config.rabbitQueue.delayQueue}") String queueName) {
        return new Queue(queueName,true);
    }


    // ----------------------------   定义exchange   ---------------------------------
    // 定义广播模式的延时交换机 无需绑定路由
    @Bean
    FanoutExchange delayExchange(@Value("${eb.config.rabbitQueue.delayExchangeName}") String delay_exchange_name) {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        FanoutExchange topicExchange = new FanoutExchange(delay_exchange_name, true, false, args);
        topicExchange.setDelayed(true);
        return topicExchange;
    }

    // 绑定延时队列与交换机
    @Bean
    public Binding delayPayBind(@Value("${eb.config.rabbitQueue.delayQueue}") String delay_queue_name,
                                @Value("${eb.config.rabbitQueue.delayExchangeName}") String delay_exchange_name) {
        return BindingBuilder
                .bind(delayQueue(delay_queue_name))
                .to(delayExchange(delay_exchange_name));
    }


    @Bean(name = "accountAmountQueueSimpleRabbitListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory accountAmountQueueSimpleRabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        listenerContainerFactory.setConnectionFactory(connectionFactory);
        listenerContainerFactory.setConcurrentConsumers(50);
        listenerContainerFactory.setMaxConcurrentConsumers(100);
        listenerContainerFactory.setPrefetchCount(20);//预处理消息个数
        listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);//开启消息确认机制
        return listenerContainerFactory;
    }

    @Bean
    public Queue accountLogNameQueue(@Value("${eb.config.rabbitQueue.accountLogName}") String queueName) {
        return new Queue(queueName);
    }

    @Bean
    public Queue urlStatisticQueue(@Value("${eb.config.rabbitQueue.urlStatisticQueue}") String queueName) {
        return new Queue(queueName);
    }

    @Bean(name = "accountLogNameQueueSimpleRabbitListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory accountLogNameQueueSimpleRabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        listenerContainerFactory.setConnectionFactory(connectionFactory);
        listenerContainerFactory.setConcurrentConsumers(50);
        listenerContainerFactory.setMaxConcurrentConsumers(100);
        listenerContainerFactory.setPrefetchCount(20);//预处理消息个数
        listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);//开启消息确认机制
        return listenerContainerFactory;
    }




    @Bean
    public Queue postCodeDataNameQueue(@Value("${eb.config.rabbitQueue.postCodeData}") String queueName) {
        return new Queue(queueName);
    }


}
