package com.tzy.config;

import com.tzy.common.constant.MqConstans;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

@Configuration
public class MqConfig {
    /**
     * 队列
     * durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
     * exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
     * autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
     * return new Queue("test",true,true,false);
     * 一般设置一下队列的持久化就好,其余两个就是默认false
     * @return
     */
    @Bean
    public Queue TestDirectQueue() {
        return new Queue(MqConstans.TEST_QUEUE,true);
    }

    @Bean
    public Binding bindingDirect() {
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("test");
    }

    //@Bean
    //public Queue TestDirectQueue2() {
    //    return new Queue(MqConstans.TEST_QUEUE+2,true);
    //}

    /**
     * 绑定  将队列和交换机绑定, 并设置用于匹配键：test
     * 星号（*） ：只能匹配一个单词
     * 井号（#）：可以匹配0个或多个单词
     * @return
     */
    //@Bean
    //public Binding bindingDirect() {
    //    return BindingBuilder.bind(TestDirectQueue()).to(TestTopicExchange()).with("test");
    //}
    //@Bean
    //public Binding bindingDirect2() {
    //    return BindingBuilder.bind(TestDirectQueue2()).to(TestTopicExchange()).with("test.#");
    //}

    /**
     * Topic交换机
     *  return new DirectExchange("test_exchange",true,true);
     * @return
     */
    //@Bean
    //public TopicExchange TestTopicExchange() {
    //    return new TopicExchange("exchange_test2");
    //}

    /**
     * Direct交换机
     *  return new DirectExchange("test_exchange",true,true);
     * @return
     */
    @Bean
    public DirectExchange TestDirectExchange() {
        return new DirectExchange("exchange_test",true,false);
    }

    @Bean
    public Queue OrderPayCallBackQueue() {
        return new Queue(MqConstans.QUEUE_ORDER_PAY_CALLBACK,true);
    }

    @Bean
    public Queue PrePayCallBackQueue() {
        return new Queue(MqConstans.QUEUE_PRE_PAY_CALLBACK,true);
    }



    @Bean
    public Queue orderSuccessQueue() {
        return new Queue(MqConstans.QUEUE_ORDER_SUCCESS,true);
    }

    @Bean
    public Queue userBehavior() {
        return new Queue(MqConstans.QUEUE_USER_BEHAVIOR,true);
    }

    @Bean
    public Queue userPoint() {
        return new Queue(MqConstans.QUEUE_USER_POINT,true);
    }

    @Bean
    public Queue OrderRefundQueue() {
        return new Queue(MqConstans.QUEUE_ORDER_REFUND,true);
    }

    @Bean
    public Queue groupFullQueue() {
        return new Queue(MqConstans.QUEUE_GROUP_FULL,true);
    }

    @Bean
    public Queue groupSortQueue() {
        return new Queue(MqConstans.QUEUE_GROUP_SORT_UPDATE,true);
    }


    @Bean
    public DirectExchange OrderPayCallBackExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_ORDER_PAY_CALLBACK,true,false);
    }

    @Bean
    public DirectExchange PrePayCallBackExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_PRE_PAY_CALLBACK,true,false);
    }

    @Bean
    public DirectExchange orderSuccessExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_ORDER_SUCCESS,true,false);
    }

    @Bean
    public DirectExchange userBehaviorExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_USER_BEHAVIOR,true,false);
    }

    @Bean
    public DirectExchange userPointExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_USER_POINT,true,false);
    }



    @Bean
    public DirectExchange OrderRefundExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_ORDER_REFUND,true,false);
    }

    @Bean
    public DirectExchange groupFullExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_GROUP_FULL,true,false);
    }

    @Bean
    public DirectExchange groupSortExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_GROUP_SORT_UPDATE,true,false);
    }


    @Bean
    public Queue OrderExpiredQueue() {
        return new Queue(MqConstans.QUEUE_ORDER_EXPIRED,true);
    }

    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>(1);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqConstans.EXCHANGE_DELAYED_ORDER_EXPIRED, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding bindingDelayExchange()
    {
        return BindingBuilder.bind(OrderExpiredQueue()).to(delayExchange()).with(MqConstans.ROUTING_KEY_ORDER_EXPIRED).noargs();
    }

    @Bean
    public Binding bindingOrderPayCallBackDirect() {
        return BindingBuilder.bind(OrderPayCallBackQueue()).to(OrderPayCallBackExchange()).with(MqConstans.ROUTING_KEY_CALLBACK);
    }

    @Bean
    public Binding bindingPrePayCallBackDirect() {
        return BindingBuilder.bind(PrePayCallBackQueue()).to(PrePayCallBackExchange()).with(MqConstans.ROUTING_KEY_PRE_CALLBACK);
    }

    @Bean
    public Binding bindingOrderSuccessDirect() {
        return BindingBuilder.bind(orderSuccessQueue()).to(orderSuccessExchange()).with(MqConstans.ROUTING_KEY_ORDER_SUCCESS);
    }

    @Bean
    public Binding bindingUserBehaviorDirect() {
        return BindingBuilder.bind(userBehavior()).to(userBehaviorExchange()).with(MqConstans.ROUTING_KEY_USER_BEHAVIOR);
    }

    @Bean
    public Binding bindingUserPointDirect() {
        return BindingBuilder.bind(userPoint()).to(userPointExchange()).with(MqConstans.ROUTING_KEY_USER_POINT);
    }

    @Bean
    public Binding bindingOrderRefundDirect() {
        return BindingBuilder.bind(OrderRefundQueue()).to(OrderRefundExchange()).with(MqConstans.ROUTING_KEY_ORDER_REFUND);
    }

    @Bean
    public Binding bindingGroupFullDirect() {
        return BindingBuilder.bind(groupFullQueue()).to(groupFullExchange()).with(MqConstans.ROUTING_KEY_GROUP_FULL);
    }

    @Bean
    public Binding bindingGroupSortDirect() {
        return BindingBuilder.bind(groupSortQueue()).to(groupSortExchange()).with(MqConstans.ROUTING_KEY_GROUP_SORT_UPDATE);
    }
    
    @Bean
    public Queue groupAllocateGoodsQueue() {
        return new Queue(MqConstans.QUEUE_GROUP_ALLOCATE_GOODS,true);
    }

    @Bean
    public DirectExchange groupAllocateGoodsExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_GROUP_ALLOCATE_GOODS,true,false);
    }

    @Bean
    public Binding bindingGroupAllocateGoodsDirect() {
        return BindingBuilder.bind(groupAllocateGoodsQueue()).to(groupAllocateGoodsExchange()).with(MqConstans.ROUTING_KEY_GROUP_ALLOCATE_GOODS);
    }

    @Bean
    public Queue groupGoodsEsQueue() {
        return new Queue(MqConstans.QUEUE_GROUP_GOODS_SYNC_ES,true);
    }

    @Bean
    public DirectExchange groupGoodsEsExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_GROUP_GOODS_SYNC_ES,true,false);
    }

    @Bean
    public Binding bindingGroupGoodsEsDirect() {
        return BindingBuilder.bind(groupGoodsEsQueue()).to(groupGoodsEsExchange()).with(MqConstans.ROUTING_KEY_GROUP_GOODS_SYNC_ES);
    }

    @Bean
    public Queue syncUserWinCardQueue() {
        return new Queue(MqConstans.QUEUE_SYNC_USER_WIN_CARD,true);
    }

    @Bean
    public DirectExchange syncUserWinExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_SYNC_USER_WIN_CARD,true,false);
    }

    @Bean
    public Binding bindingSyncUserWinDirect() {
        return BindingBuilder.bind(syncUserWinCardQueue()).to(syncUserWinExchange()).with(MqConstans.ROUTING_KEY_SYNC_USER_WIN_CARD);
    }

    @Bean
    public Queue orderAftAllocateQueue() {
        return new Queue(MqConstans.QUEUE_ORDER_AFT_ALLOCATE, true);
    }

    @Bean
    public DirectExchange orderAftAllocateExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_ORDER_AFT_ALLOCATE, true, false);
    }

    @Bean
    public Binding bindingOrderAftAllocateDirect() {
        return BindingBuilder.bind(orderAftAllocateQueue()).to(orderAftAllocateExchange()).with(MqConstans.ROUTING_KEY_ORDER_AFT_ALLOCATE);
    }


    @Bean
    public Queue initUserMemberQueue() {
        return new Queue(MqConstans.QUEUE_INIT_USER_USER_MEMBER, true);
    }

    @Bean
    public DirectExchange initUserMemberExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_INIT_USER_MEMBER, true, false);
    }

    @Bean
    public Binding bindingInitUserMemberDirect() {
        return BindingBuilder.bind(initUserMemberQueue()).to(initUserMemberExchange()).with(MqConstans.ROUTING_KEY_INIT_USER_MEMBER);
    }

    @Bean
    public Queue userPointJson() {
        return new Queue(MqConstans.QUEUE_USER_POINT_JSON,true);
    }

    @Bean
    public Binding bindingUserPointJsonDirect() {
        return BindingBuilder.bind(userPointJson()).to(userPointExchange()).with(MqConstans.ROUTING_KEY_USER_POINT_JSON);
    }


    @Bean
    public Queue appLiveConfigQueue() {
        return new Queue(MqConstans.QUEUE_APP_LIVE_CONFIG,true);
    }

    @Bean
    public CustomExchange appLiveConfigDelayExchange() {
        Map<String, Object> args = new HashMap<>(1);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqConstans.EXCHANGE_DELAYED_APP_LIVE_CONFIG, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding bindingAppLiveConfigDelayExchange()
    {
        return BindingBuilder.bind(appLiveConfigQueue()).to(appLiveConfigDelayExchange()).with(MqConstans.ROUTING_KEY_APP_LIVE_CONFIG).noargs();
    }


    @Bean
    public Queue autoAddActQueue() {
        return new Queue(MqConstans.QUEUE_LUCKY_ACT_AUTO_ADD, true);
    }

    @Bean
    public DirectExchange autoAddActExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_LUCKY_ACT_AUTO_ADD, true, false);
    }

    @Bean
    public Binding bindingAutoAddActDirect() {
        return BindingBuilder.bind(autoAddActQueue()).to(autoAddActExchange()).with(MqConstans.ROUTING_KEY_LUCKY_ACT_AUTO_ADD);
    }


    @Bean
    public Queue userListActQueue() {
        return new Queue(MqConstans.QUEUE_USER_LIST_ACT, true);
    }

    @Bean
    public DirectExchange userListActExchange() {
        return new DirectExchange(MqConstans.EXCHANGE_USER_LIST_ACT, true, false);
    }

    @Bean
    public Binding bindingUserListActQueueDirect() {
        return BindingBuilder.bind(userListActQueue()).to(userListActExchange()).with(MqConstans.ROUTING_KEY_USER_LIST_ACT);
    }

    @Bean
    public Queue merchantNotifyQueue() {
        return new Queue(MqConstans.QUEUE_MERCHANT_NOTIFY, true);
    }

    @Bean
    public Queue communityNotifyQueue() {
        return new Queue(MqConstans.QUEUE_COMMUNITY_NOTIFY, true);
    }

    @Bean
    public Queue actQueue() {
        return new Queue(MqConstans.QUEUE_GROUP_ORDER_ACT, true);
    }

    @Bean
    public Queue giftCardQueue() {
        return new Queue(MqConstans.QUEUE_GROUP_GIFT_CARD, true);
    }

    @Bean
    public Queue initUserGiftCardQueue() {
        return new Queue(MqConstans.QUEUE_INIT_USER_GIFT_CARD, true);
    }


    // @Bean
    // public Queue TestDirectQueueV2() {
    //     return new Queue("TestDirectQueueV2");
    // }
    // @Bean
    // public Queue TestDirectQueueV3() {
    //     return new Queue("TestDirectQueueV3");
    // }
    // @Bean
    // public Queue actDrawOrderQueue() {
    //     return new Queue(MqConstans.QUEUE_ORDER_DRAW_CARD, true);
    // }
    // @Bean
    // public Queue drawOrderCallbackQueue() {
    //     return new Queue(MqConstans.QUEUE_DRAW_ORDER_CALLBACK, true);
    // }
    // @Bean
    // public Binding bindingTestDirectV2() {
    //     return BindingBuilder.bind(TestDirectQueueV2()).to(testTopicExchangeV3()).with("test");
    // }
    // @Bean
    // public Binding bindingTestDirectV3() {
    //     return BindingBuilder.bind(TestDirectQueueV3()).to(testTopicExchangeV3()).with("test.#");
    // }
    // @Bean
    // public TopicExchange testTopicExchangeV3() {
    //     return new TopicExchange("TestTopicExchangeV3");
    // }

    @Bean
    public Queue orderOverQueue() {
        return new Queue(MqConstans.QUEUE_ORDER_OVER, true);
    }

    @Bean
    public Queue asyncRefundQueue() {
        return new Queue(MqConstans.QUEUE_ASYNC_REFUND, true);
    }


    @Bean
    public Queue luckyBagQueue() {
        return new Queue(MqConstans.QUEUE_LUCKY_BAG_EXPIRED,true);
    }

    @Bean
    public CustomExchange luckyBagDelayExchange() {
        Map<String, Object> args = new HashMap<>(1);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqConstans.EXCHANGE_DELAYED_LUCKY_BAG_EXPIRED, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding bindingLuckyBagDelayExchange()
    {
        return BindingBuilder.bind(luckyBagQueue()).to(luckyBagDelayExchange()).with(MqConstans.ROUTING_KEY_LUCKY_BAG_EXPIRED).noargs();
    }

    @Bean
    public Queue userBrowseRecordQueue() {
        return new Queue(MqConstans.QUEUE_USER_BROWSE_RECORD,true);
    }

    @Bean
    public CustomExchange userBrowseRecordDelayExchange() {
        Map<String, Object> args = new HashMap<>(1);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqConstans.EXCHANGE_USER_BROWSE_RECORD, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding bindingUserBrowseRecordDelayExchange() {
        return BindingBuilder.bind(userBrowseRecordQueue()).to(userBrowseRecordDelayExchange()).with(MqConstans.ROUTING_KEY_USER_BROWSE_RECORD).noargs();
    }
}
