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

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/4/29 10:39
 * @description 积分回收
 * @since JDK 1.8
 */
@Configuration
public class PlatformIntegralRecoverConfig {

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

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

    @Bean
    public Queue platformIntegralRecoverQueue() {
        return new Queue(QueueTypeEnum.PLATFORM_INTEGRAL_RECOVER.getQueue());
    }

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

    @Bean
    Binding platformIntegralRecoverBinding(DirectExchange platformIntegralRecoverDirect, Queue platformIntegralRecoverQueue) {
        return BindingBuilder.bind(platformIntegralRecoverQueue).to(platformIntegralRecoverDirect).with(QueueTypeEnum.PLATFORM_INTEGRAL_RECOVER.getQueueKey());
    }

    @Bean
    public Binding platformIntegralRecoverTTLBinding(Queue platformIntegralRecoverTTLQueue, DirectExchange platformIntegralRecoverTTLDirect) {
        return BindingBuilder.bind(platformIntegralRecoverTTLQueue).to(platformIntegralRecoverTTLDirect)
                .with(QueueTTLTypeEnum.PLATFORM_INTEGRAL_RECOVER_TTL.getQueueKey());
    }
}
