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 ConfirmReceiptConfig {

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

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

	    @Bean
	    public Queue confirmReceiptQueue() {
	        return new Queue(QueueTypeEnum.CONFIRM_RECEIPT.getQueue());
	    }

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

	    @Bean
	    Binding confirmReceiptBinding(DirectExchange confirmReceiptDirect, Queue confirmReceiptQueue) {
	        return BindingBuilder.bind(confirmReceiptQueue).to(confirmReceiptDirect).with(QueueTypeEnum.CONFIRM_RECEIPT.getQueueKey());
	    }

	    @Bean
	    public Binding confirmReceiptTTLBinding(Queue confirmReceiptTTLQueue, DirectExchange confirmReceiptTTLDirect) {
	        return BindingBuilder.bind(confirmReceiptTTLQueue).to(confirmReceiptTTLDirect)
	                .with(QueueTTLTypeEnum.CONFIRM_RECEIPT_TTL.getQueueKey());
	    }

}
