package com.fzm.mall.server.front.mq.config;

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

public class OrderCloseConfig {
    @Bean
    DirectExchange orderPayDirect() {
        return (DirectExchange) ExchangeBuilder.directExchange(QueueTypeEnum.ORDER_CLOSE.getExchange())
                .durable(true).build();
    }

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

    @Bean
    public Queue orderPayQueue() {
        return new Queue(QueueTypeEnum.ORDER_CLOSE.getQueue());
    }

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

    @Bean
    Binding orderPayBinding(DirectExchange orderPayDirect, Queue orderPayQueue) {
        return BindingBuilder.bind(orderPayQueue).to(orderPayDirect).with(QueueTypeEnum.ORDER_CLOSE.getQueueKey());
    }

    @Bean
    public Binding orderPayTTLBinding(Queue orderPayTTLQueue, DirectExchange orderPayTTLDirect) {
        return BindingBuilder.bind(orderPayTTLQueue).to(orderPayTTLDirect)
                .with(QueueTTLTypeEnum.ORDER_CLOSE_TTL.getQueueKey());
    }

}
