package com.yida.data.system.configure;

import com.yida.data.rabbit.constant.RabbitConstant;
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 org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

@Configuration
public class RabbitConfig {

    /**
     * 微信公众号扫描事件回调交换机
     *
     * @return
     */
    @Bean
    public DirectExchange wxPublicQrExchange() {
        return new DirectExchange(RabbitConstant.WXPUBLIC_QR_EXCHANGE);
    }

    /**
     * 区域后台绑定接收消息队列队列
     *
     * @return
     */
    @Bean
    public Queue agentReceiveMsgQueue() {
        return new Queue(RabbitConstant.AGENT_RECEIVE_MSG_QUEUE, true);
    }

    /**
     * 区域后台绑定接收消息绑定
     */
    @Bean
    public Binding agentReceiveMsgBinding(@Qualifier("agentReceiveMsgQueue") Queue queue,
                                          @Qualifier("wxPublicQrExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitConstant.AGENT_RECEIVE_MSG_KEY);
    }

    /**
     * 区域后台绑定接收消息队列队列
     *
     * @return
     */
    @Bean
    public Queue visitorInviteCodeQueue() {
        return new Queue(RabbitConstant.VISITOR_INVITE_CODE_QUEUE, true);
    }

    /**
     * 区域后台绑定接收消息绑定
     */
    @Bean
    public Binding visitorInviteCodeBinding(@Qualifier("visitorInviteCodeQueue") Queue queue,
                                            @Qualifier("wxPublicQrExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitConstant.VISITOR_INVITE_CODE_KEY);
    }

    @Bean
    public DirectExchange qywxHandleExchange() {
        return new DirectExchange(RabbitConstant.QYWX_HANDLE_EXCHANGE);
    }

    @Bean
    public Queue qywxHandleQueue() {
        Map<String, Object> args = new HashMap<>();
        // 设置同时只能一个消费者进行消费
        args.put("x-single-active-consumer", true);
        return new Queue(RabbitConstant.QYWX_HANDLE_QUEUE, true, false, false, args);
    }

    @Bean
    public Binding qywxHandleBinding(@Qualifier("qywxHandleQueue") Queue queue,
                                     @Qualifier("qywxHandleExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RabbitConstant.QYWX_HANDLE_KEY);
    }
}
