package com.elab.spring.config;

import com.elab.core.aop.annotations.EnableElabDB;
import com.elab.mq.config.RocketMQConfiguration;
import com.elab.mq.listener.ConsumerInterceptor;
import com.elab.mq.msg.ProducerInterceptor;
import com.elab.redis.CacheTemplate;
import com.elab.spring.dao.IConsumerDao;
import com.elab.spring.dao.IProducerDao;
import com.elab.spring.intercept.RMQCacheConsumerInterceptor;
import com.elab.spring.intercept.RMQCacheProducerInterceptor;
import com.elab.spring.intercept.RMQMsgTableConsumerInterceptor;
import com.elab.spring.intercept.RMQMsgTableProducerInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @Module 配置类
 * @Description 幂等配置
 * @Author liukaixiong
 * @Date 2020/12/30 15:15
 */
@Configuration
@ConditionalOnClass(value = {RocketMQConfiguration.class})
@EnableElabDB(basePackageClasses = {IConsumerDao.class, IProducerDao.class})
@Import(value = {RocketMQConfiguration.class})
public class RmqConfiguration {

    @Configuration
    @ConditionalOnClass(value = CacheTemplate.class)
    @ConditionalOnProperty(prefix = "elab.mq", value = "store", havingValue = "cache", matchIfMissing = true)
    protected static class CacheRocketMqConfiguration {

        @Bean
        @ConditionalOnMissingBean(value = {ConsumerInterceptor.class})
        public ConsumerInterceptor cacheConsumerInterceptor() {
            return new RMQCacheConsumerInterceptor();
        }

        @Bean
        @ConditionalOnMissingBean(value = {ProducerInterceptor.class})
        public ProducerInterceptor cacheProducerInterceptor() {
            return new RMQCacheProducerInterceptor();
        }
    }

    @Configuration
    @ConditionalOnMissingBean(value = CacheTemplate.class)
    @ConditionalOnProperty(prefix = "elab.mq", value = "store", havingValue = "mysql", matchIfMissing = false)
    protected static class MysqlRocketMqConfiguration {

        @Bean
        @ConditionalOnMissingBean(value = {ConsumerInterceptor.class})
        public ConsumerInterceptor mysqlConsumerInterceptor() {
            return new RMQMsgTableConsumerInterceptor();
        }

        @Bean
        @ConditionalOnMissingBean(value = {ProducerInterceptor.class})
        public ProducerInterceptor mysqlProducerInterceptor() {
            return new RMQMsgTableProducerInterceptor();
        }
    }


}
