package com.dxhy.common.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 */
@Configuration
@Slf4j
public class RabbitMsgConfig {

    @Value("${rabbitmq.prefetchCount:2}")
    private int prefetchCount;
    @Value("${rabbitmq.concurrentConsumers:2}")
    private int concurrentConsumers;
    @Value("${rabbitmq.maxConcurrentConsumers:5}")
    private int maxConcurrentConsumers;
    @Value("${TO_MESSAGE_LOG:TO_MESSAGE_LOG}")
    private String tomessagelog;
    @Value("${TO_EXCEL_APPLY:TO_EXCEL_APPLY}")
    private String toexcelapply;
    @Value("${TO_MSG_EXCEL_APPLY:TO_MSG_EXCEL_APPLY}")
    private String tomsgexcelapply;
    @Value("${TO_INVOICE_LOG:TO_INVOICE_LOG}")
    private String toinvoicelog;
    @Value("${TO_CUSTOMS_LOG:TO_CUSTOMS_LOG}")
    private String tocustomslog;
    @Value("${TO_INVOICE_STATUS:TO_INVOICE_STATUS}")
    private String toinvoicestatus;
    @Value("${TO_MENU_TABULATE:TO_MENU_TABULATE}")
    private String toMenuTabulate;
    @Value("${TO_CUSTOMS_OCR:TO_CUSTOMS_OCR}")
    private String toCustomsOcr;
    @Value("${TO_SALE_COUNT_ITEM:TO_SALE_COUNT_ITEM}")
    private String toSaleCountItem;

    @Bean(name = "TO_MESSAGE_LOG")
    public Queue toMsqLogSave() {
        return new Queue(tomessagelog, true);
    }

    @Bean(name = "TO_EXCEL_APPLY")
    public Queue queueExcelApply() {
        return new Queue(toexcelapply, true);
    }

    @Bean(name = "TO_MSG_EXCEL_APPLY")
    public Queue msgExcelApply() {
        return new Queue(tomsgexcelapply, true);
    }

    @Bean(name = "TO_INVOICE_LOG")
    public Queue invoiceLog() {
        return new Queue(toinvoicelog, true);
    }

    @Bean(name = "TO_CUSTOMS_LOG")
    public Queue customsLog() {
        return new Queue(tocustomslog, true);
    }

    @Bean(name = "TO_INVOICE_STATUS")
    public Queue invoiceStatus() {
        return new Queue(toinvoicestatus, true);
    }

    @Bean(name = "TO_MENU_TABULATE")
    public Queue menuTabulate() {
        return new Queue(toMenuTabulate, true);
    }

    @Bean(name = "TO_CUSTOMS_OCR")
    public Queue customsOcr() {
        return new Queue(toCustomsOcr, true);
    }

    @Bean(name = "TO_SALE_COUNT_ITEM")
    public Queue saleCountItem() {
        return new Queue(toSaleCountItem, true);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("exchange");
    }

    @Bean
    Binding bindingExchangetoMessageLog() {
        return BindingBuilder.bind(this.toMsqLogSave()).to(this.topicExchange()).with(tomessagelog);
    }

    @Bean
    Binding bindingExchangetoExcelApply() {
        return BindingBuilder.bind(this.queueExcelApply()).to(this.topicExchange()).with(toexcelapply);
    }

    @Bean
    Binding bindingExchangetoMsgExcelApply() {
        return BindingBuilder.bind(this.msgExcelApply()).to(this.topicExchange()).with(tomsgexcelapply);
    }

    @Bean
    Binding bindingExchangeInvoiceLog() {
        return BindingBuilder.bind(this.invoiceLog()).to(this.topicExchange()).with(toinvoicelog);
    }

    @Bean
    Binding bindingExchangeCustomsLog() {
        return BindingBuilder.bind(this.customsLog()).to(this.topicExchange()).with(tocustomslog);
    }

    @Bean
    Binding bindingExchangeInvoiceStatus() {
        return BindingBuilder.bind(this.invoiceStatus()).to(this.topicExchange()).with(toinvoicestatus);
    }

    @Bean
    Binding bindingExchangeMenuTabulate() {
        return BindingBuilder.bind(this.menuTabulate()).to(this.topicExchange()).with(toMenuTabulate);
    }

    @Bean
    Binding bindingExchangeCustomsOcr() {
        return BindingBuilder.bind(this.customsOcr()).to(this.topicExchange()).with(toCustomsOcr);
    }

    @Bean
    Binding bindingExchangeSaleCountItem() {
        return BindingBuilder.bind(this.saleCountItem()).to(this.topicExchange()).with(toSaleCountItem);
    }

    /**
     * rabbitmq: prefetchCount: 2 # 个消费者可以fetch的最大消息数量 concurrentConsumers: 2 # 设置并发消费者数量 maxConcurrentConsumers: 10 #
     * 设置最大消费者数量，自动扩展消费者数量关键配置
     */
    @Bean
    public SimpleRabbitListenerContainerFactory myRabbitListenerContainerFactory(ConnectionFactory connectionFactory,
        SimpleRabbitListenerContainerFactoryConfigurer configurer) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        // 一个消费者可以fetch的最大消息数量
        factory.setPrefetchCount(prefetchCount);
        // 设置并发消费者数量
        factory.setConcurrentConsumers(concurrentConsumers);
        // 设置最大消费者数量，自动扩展消费者数量关键配置
        factory.setMaxConcurrentConsumers(maxConcurrentConsumers);
        // 设置手动ACK
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        configurer.configure(factory, connectionFactory);
        return factory;
    }

}
