package org.springblade.common.config.mq;

import org.springblade.common.constant.OrderConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Silent
 * @Description
 * @Date: Created in 16:25 2021/11/1
 * @Modified By:
 */
@Configuration
public class OrderRabbitConfig {
	/**
	 * TTL队列
	 * @return
	 */
	@Bean
	public Queue orderQueueTTL(){
		Map<String, Object> arguments = new HashMap<>();
		// 超时时间(单位毫秒)
		arguments.put("x-message-ttl", OrderConstant.ORDER_DELAY_TIME_OUT);
		// 设置关联的死信队列
		arguments.put("x-dead-letter-routing-key", OrderConstant.DLX_ROUTE_KEY);
		arguments.put("x-dead-letter-exchange", OrderConstant.DLX_ORDER_EXCHANGE);
		return new Queue(OrderConstant.TTL_ORDER_QUEUE, true, false, false, arguments);
	}

	/**
	 * TTL交换机
	 * @return
	 */
	@Bean
	public Exchange orderExchangeTTL() {
		return new DirectExchange(OrderConstant.TTL_ORDER_EXCHANGE, true, false);
	}

	/**
	 * 绑定TTL
	 * @return
	 */
	@Bean
	public Binding orderBindingTTL(){
		return BindingBuilder.bind(orderQueueTTL()).to(orderExchangeTTL()).with(OrderConstant.TTL_ROUTE_KEY).noargs();
	}

	/**
	 * 死信队列
	 * @return
	 */
	@Bean
	public Queue orderQueueDLX(){
		return new Queue(OrderConstant.DLX_ORDER_QUEUE, true, false, false, null);
	}

	/**
	 * 死信交换机
	 * @return
	 */
	@Bean
	public Exchange orderExchangeDLX() {
		return new DirectExchange(OrderConstant.DLX_ORDER_EXCHANGE, true, false);
	}

	/**
	 * 绑定死信队列和死信交换机
	 * @return
	 */
	@Bean
	public Binding orderBindingDLX(){
		return BindingBuilder.bind(orderQueueDLX()).to(orderExchangeDLX()).with(OrderConstant.DLX_ROUTE_KEY).noargs();
	}
}
