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

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

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

	@Bean
	public Queue orderFinishQueue() {
		return new Queue(QueueTypeEnum.ORDER_FINISH.getQueue());
	}

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


	@Bean
	Binding orderFinishBinding(DirectExchange orderFinishDirect, Queue orderFinishQueue) {
		return BindingBuilder.bind(orderFinishQueue).to(orderFinishDirect)
				.with(QueueTypeEnum.ORDER_FINISH.getQueueKey());
	}

	@Bean
	public Binding orderFinishTTLBinding(Queue orderFinishTTLQueue, DirectExchange orderFinishTTLDirect) {
		return BindingBuilder.bind(orderFinishTTLQueue).to(orderFinishTTLDirect)
				.with(QueueTTLTypeEnum.ORDER_FINISH_TTL.getQueueKey());
	}

}
