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;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrderPayConfig {

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

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

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

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

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

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

}
