package com.gz.config.rabbitmq;

import com.gz.common.DataGlobalVariable;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Rabbitmq队列配置
 *
 * @author LiuchangLan
 * @date 2021/3/8 19:27
 */
@Slf4j
@Configuration
public class RabbitMQQueueConfig {

    @Value("${rabbitmq.ocr.queue}")
    private String ocrQueueName;

    @Value("${rabbitmq.ocr.queue-binding-key}")
    private String ocrBindingKey;


    @Value("${rabbitmq.ocr.dlx-queue}")
    private String dlxOcrQueueName;

    @Value("${rabbitmq.ocr.dlx-queue-binding-key}")
    private String dlxOcrBindingKey;

    @Value("${rabbitmq.ocr.queue-message-ttl}")
    private String dlxQueueMessageTTL;

    @Resource
    private DirectExchange dataExchange;

    @Resource
    private DirectExchange dlxDataExchange;

    /**
     * @description ocr识别队列
     * @author LiuChangLan
     * @since 2021/3/8 19:43
     */
    @Bean
    public Queue ocrQueue() {
        log.info("[rabbitMQ]创建ocr队列");
        Map<String, Object> arguments = null;
        if ("-1".equals(dlxQueueMessageTTL)){
            arguments = this.buildQueueDLXArguments(dlxDataExchange.getName(),dlxOcrBindingKey);
        }else {
            arguments = this.buildQueueDLXArguments(dlxDataExchange.getName(), dlxOcrBindingKey, Integer.valueOf(dlxQueueMessageTTL) * 1000L);
        }
        return new Queue(ocrQueueName, true, false, false, arguments);
    }

    /**
     * @description ocr死信队列
     * @author LiuChangLan
     * @since 2021/3/8 19:44
     */
    @Bean
    public Queue dlxOcrQueue() {
        log.info("[rabbitMQ]创建ocr死信队列");
        return new Queue(dlxOcrQueueName, true, false, false);
    }

    /**
     * @description 绑定ocr识别队列
     * @author LiuChangLan
     * @since 2021/3/8 19:44
     */
    @Bean
    public Binding bindOcrQueueBinding() {
        log.info("[rabbitMQ]绑定ocr队列到交换机");
        return BindingBuilder.bind(ocrQueue()).to(dataExchange).with(ocrBindingKey);
    }


    /**
     * @description 绑定死信队列
     * @author LiuChangLan
     * @since 2021/3/8 19:44
     */
    @Bean
    public Binding bindDlxOcrQueueBinding() {
        log.info("[rabbitMQ]绑定ocr死信队列到交换机");
        return BindingBuilder.bind(dlxOcrQueue()).to(dlxDataExchange).with(dlxOcrBindingKey);
    }


    /**
     * 构造 queue并指定死信交换机和routingKey
     *
     * @author WangJixin
     * @since 2019/11/28 18:31
     */
    private Map<String, Object> buildQueueDLXArguments(String dlxName, String dlRoutingKey) {
        return buildQueueDLXArguments(dlxName, dlRoutingKey, null);
    }

    /**
     * 构造 queue并指定死信交换机和routingKey
     *
     * @author WangJixin
     * @since 2019/11/28 18:31
     */
    private Map<String, Object> buildQueueDLXArguments(String dlxName, String dlRoutingKey, Long ttl) {
        Map<String, Object> arguments = new HashMap<>(3);
        arguments.put(DataGlobalVariable.MQ_X_DEAD_LETTER_EXCHANGE_KEY, dlxName);
        arguments.put(DataGlobalVariable.MQ_X_DEAD_LETTER_ROUTING_KEY, dlRoutingKey);
        if (ttl != null) {
            arguments.put(DataGlobalVariable.MQ_X_MESSAGE_TTL, ttl);
        }
        return arguments;
    }
}
