package com.dxhy.common.rabbitmq;

import com.dxhy.common.constant.RabbitMqConstant;
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;

    private String exchange = RabbitMqConstant.EXCHANGE;
    private String tomessagelog = RabbitMqConstant.TO_MESSAGE_LOG;
    private String toinvoicelog = RabbitMqConstant.TO_INVOICE_LOG;
    private String tocustomslog = RabbitMqConstant.TO_CUSTOMS_LOG;
    private String toCustomsOcr = RabbitMqConstant.TO_CUSTOMS_OCR;
    private String toExcelApply = RabbitMqConstant.TO_EXCEL_APPLY;
    private String toMsgExcelApply = RabbitMqConstant.TO_MSG_EXCEL_APPLY;
    private String toInvoiceStatus = RabbitMqConstant.TO_INVOICE_STATUS;
    private String toSaleCountItem  = RabbitMqConstant.TO_SALE_COUNT_ITEM;

    //乐企初始化数据下载队列
    private String toLqcshsjxz = RabbitMqConstant.LQ_CSHSJ_XZ;

    @Bean
    public Queue toMsqLogSave() {
        return new Queue(tomessagelog, true);
    }

    @Bean
    public Queue queueExcelApply() {
        return new Queue(toExcelApply, true);
    }

    @Bean
    public Queue msgExcelApply() {
        return new Queue(toMsgExcelApply, true);
    }

    @Bean
    public Queue invoiceLog() {
        return new Queue(toinvoicelog, true);
    }

    @Bean
    public Queue customsLog() {
        return new Queue(tocustomslog, true);
    }

    @Bean
    public Queue invoiceStatus() {
        return new Queue(toInvoiceStatus, true);
    }


    @Bean
    public Queue customsOcr() {
        return new Queue(toCustomsOcr, true);
    }

    @Bean
    public Queue saleCountItem() {
        return new Queue(toSaleCountItem, true);
    }

    @Bean
    public Queue lqcshsjxz() {
        return new Queue(toLqcshsjxz, 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 bindingExchangeCustomsOcr() {
        return BindingBuilder.bind(this.customsOcr()).to(this.topicExchange()).with(toCustomsOcr);
    }

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


    @Bean
    Binding bindingExchangeLqcshsjxz() {
        return BindingBuilder.bind(this.lqcshsjxz()).to(this.topicExchange()).with(toLqcshsjxz);
    }

    /**
     * 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;
    }

}
