package com.fzm.mall.server.admin.mq.config.order_center;

import com.fzm.mall.server.admin.mq.enums.QueueTTLTypeEnum;
import com.fzm.mall.server.admin.mq.enums.QueueTypeEnum;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wulixian
 * @description 发行货币
 * @date 2021/04/16  11:28
 */
@Configuration
public class OrderDeliveryConfig {

    @Bean
    DirectExchange orderDeliveryDirect() {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueTypeEnum.ORDER_DELIVERY.getExchange())
                .durable(true).build();
    }

    @Bean
    DirectExchange orderDeliveryTTLDirect() {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueTTLTypeEnum.ORDER_DELIVERY_TTL.getExchange())
                .durable(true).build();
    }

    @Bean
    public Queue orderDeliveryQueue() {
        return new Queue(QueueTypeEnum.ORDER_DELIVERY.getQueue());
    }

    @Bean
    public Queue orderDeliveryTTLQueue() {
        return QueueBuilder.durable(QueueTTLTypeEnum.ORDER_DELIVERY_TTL.getQueue())
                // 配置到期后转发的交换
                .withArgument("x-dead-letter-exchange", QueueTypeEnum.ORDER_DELIVERY.getExchange())
                // 配置到期后转发的路由键
                .withArgument("x-dead-letter-routing-key", QueueTypeEnum.ORDER_DELIVERY.getQueueKey()).build();
    }

    @Bean
    Binding orderDeliveryBinding(DirectExchange orderDeliveryDirect, Queue orderDeliveryQueue) {
        return BindingBuilder.bind(orderDeliveryQueue).to(orderDeliveryDirect).with(QueueTypeEnum.ORDER_DELIVERY.getQueueKey());
    }

    @Bean
    public Binding orderDeliveryTTLBinding(Queue orderDeliveryTTLQueue, DirectExchange orderDeliveryTTLDirect) {
        return BindingBuilder.bind(orderDeliveryTTLQueue).to(orderDeliveryTTLDirect)
                .with(QueueTTLTypeEnum.ORDER_DELIVERY_TTL.getQueueKey());
    }

}
