package com.fzm.mall.server.admin.mq.config.message;

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

/**
 * @author wangp
 * @date 2021/5/7 10:18
 * @description 发送定时短信
 * @since JDK 1.8
 */
@Configuration
public class SendTimingSmsConfig {

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

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

    @Bean
    public Queue sendTimingSmsQueue() {
        return new Queue(QueueTypeEnum.SEND_TIMING_SMS.getQueue());
    }

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

    @Bean
    Binding sendTimingSmsBinding(DirectExchange sendTimingSmsDirect, Queue sendTimingSmsQueue) {
        return BindingBuilder.bind(sendTimingSmsQueue).to(sendTimingSmsDirect).with(QueueTypeEnum.SEND_TIMING_SMS.getQueueKey());
    }

    @Bean
    public Binding sendTimingSmsTTLBinding(Queue sendTimingSmsTTLQueue, DirectExchange sendTimingSmsTTLDirect) {
        return BindingBuilder.bind(sendTimingSmsTTLQueue).to(sendTimingSmsTTLDirect)
                .with(QueueTTLTypeEnum.SEND_TIMING_SMS_TTL.getQueueKey());
    }
}
