package com.cf.imes.module.plan.config;

import com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
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;

import static com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants.ORDER_IMPORT_DEAD_LETTER_EXCHANGE;
import static com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants.ORDER_IMPORT_DEAD_LETTER_QUEUE;
import static com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants.ORDER_IMPORT_DEAD_LETTER_ROUTING_KEY;
import static com.cf.imes.framework.mq.rabbitmq.constant.RabbitMqConstants.ORDER_IMPORT_WEBCAD_QUEUE;

/**
 * @author Gqr
 * @since 2025/5/12 12:00
 */
@Configuration
public class ChenfengRabbitMQPlanConfiguration {
    @Value("${chenfeng.plan.webcad.import.node:1}")
    private String instanceId;

    private static final String X_DEAD_LETTER_EXCHANGE_KEY = "x-dead-letter-exchange";
    private static final String X_DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

    /**
     * webcad拆单导入交换机、队列、绑定声明
     *
     * @return
     */
    @Bean
    public DirectExchange orderImportWebCadFanoutExchange() {
        return new DirectExchange(RabbitMqConstants.ORDER_IMPORT_WEBCAD_EXCHANGE);
    }

    @Bean("orderImportWebCadQueueName")
    public String dynamicOrderImportWebCadQueueName() {
        return String.format(ORDER_IMPORT_WEBCAD_QUEUE, instanceId);
    }

    /**
     * webcad拆单导入队列声明
     *
     * @return
     */
    @Bean
    public Queue orderImportWebCadQueue() {
        Map<String, Object> args = new HashMap<>(1);
        // x-dead-letter-exchange 这里声明当前队列绑定的死信交换机
        args.put(X_DEAD_LETTER_EXCHANGE_KEY, ORDER_IMPORT_DEAD_LETTER_EXCHANGE);
        // 设置死信路由键
        args.put(X_DEAD_LETTER_ROUTING_KEY, ORDER_IMPORT_DEAD_LETTER_ROUTING_KEY);
        return QueueBuilder.durable(dynamicOrderImportWebCadQueueName()).withArguments(args).build();
    }

    /**
     * 生产单xml导入交换机声明
     *
     * @return
     */
    @Bean
    public Binding orderImportWebCadBinding() {
        return BindingBuilder.bind(orderImportWebCadQueue()).to(orderImportWebCadFanoutExchange()).with(instanceId);
    }

    /**
     * 生产单excel导入交换机、队列、绑定声明
     *
     * @return
     */
    @Bean
    public FanoutExchange orderImportDefaultExcelFanoutExchange() {
        return new FanoutExchange(RabbitMqConstants.ORDER_IMPORT_DEFAULT_EXCEL_EXCHANGE);
    }

    /**
     * 生产单excel导入队列声明
     *
     * @return
     */
    @Bean
    public Queue orderImportDefaultExcelQueue() {
        Map<String, Object> args = new HashMap<>(1);
        // x-dead-letter-exchange 这里声明当前队列绑定的死信交换机
        args.put(X_DEAD_LETTER_EXCHANGE_KEY, ORDER_IMPORT_DEAD_LETTER_EXCHANGE);
        // 设置死信路由键
        args.put(X_DEAD_LETTER_ROUTING_KEY, ORDER_IMPORT_DEAD_LETTER_ROUTING_KEY);
        return QueueBuilder.durable(RabbitMqConstants.ORDER_IMPORT_DEFAULT_EXCEL_QUEUE).withArguments(args).build();
    }

    /**
     * 生产单excel导入交换机声明
     *
     * @return
     */
    @Bean
    public Binding orderImportDefaultExcelBinding() {
        return BindingBuilder.bind(orderImportDefaultExcelQueue()).to(orderImportDefaultExcelFanoutExchange());
    }

    /**
     * 生产单xml导入交换机、队列、绑定声明
     *
     * @return
     */
    @Bean
    public FanoutExchange orderImportDefaultXmlFanoutExchange() {
        return new FanoutExchange(RabbitMqConstants.ORDER_IMPORT_DEFAULT_XML_EXCHANGE);
    }

    /**
     * 生产单xml导入队列声明
     *
     * @return
     */
    @Bean
    public Queue orderImportDefaultXmlQueue() {
        Map<String, Object> args = new HashMap<>(1);
        // x-dead-letter-exchange 这里声明当前队列绑定的死信交换机
        args.put(X_DEAD_LETTER_EXCHANGE_KEY, ORDER_IMPORT_DEAD_LETTER_EXCHANGE);
        // 设置死信路由键
        args.put(X_DEAD_LETTER_ROUTING_KEY, ORDER_IMPORT_DEAD_LETTER_ROUTING_KEY);
        return QueueBuilder.durable(RabbitMqConstants.ORDER_IMPORT_DEFAULT_XML_QUEUE).withArguments(args).build();
    }

    /**
     * 生产单xml导入交换机声明
     *
     * @return
     */
    @Bean
    public Binding orderImportDefaultXmlBinding() {
        return BindingBuilder.bind(orderImportDefaultXmlQueue()).to(orderImportDefaultXmlFanoutExchange());
    }

    /**
     * 生产单导入死信交换机声明
     *
     * @return
     */
    @Bean
    public DirectExchange orderImportDeadLetterExchange() {
        return new DirectExchange(ORDER_IMPORT_DEAD_LETTER_EXCHANGE);
    }

    /**
     * 生产单导入死信队列声明
     *
     * @return
     */
    @Bean
    public Queue orderImportDeadLetterQueue() {
        return new Queue(ORDER_IMPORT_DEAD_LETTER_QUEUE);
    }

    /**
     * 生产单导入死信队列、交换机、绑定声明
     *
     * @return
     */
    @Bean
    public Binding deadLetterBindingA() {
        return BindingBuilder.bind(orderImportDeadLetterQueue()).to(orderImportDeadLetterExchange()).with(ORDER_IMPORT_DEAD_LETTER_ROUTING_KEY);
    }
}
