package cd.casic.framework.protection.idempotent.config;


import cd.casic.framework.protection.idempotent.core.aop.IdempotentAspect;
import cd.casic.framework.protection.idempotent.core.keyresolver.IdempotentKeyResolver;
import cd.casic.framework.protection.idempotent.core.keyresolver.impl.DefaultIdempotentKeyResolver;
import cd.casic.framework.protection.idempotent.core.keyresolver.impl.ExpressionIdempotentKeyResolver;
import cd.casic.framework.protection.idempotent.core.keyresolver.impl.UserIdempotentKeyResolver;
import cd.casic.framework.protection.idempotent.core.redis.IdempotentRedisDAO;
import cd.casic.framework.redis.config.OpsRedisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

@AutoConfiguration(after = OpsRedisAutoConfiguration.class)
public class OpsIdempotentConfiguration {

    @Bean
    public IdempotentAspect idempotentAspect(List<IdempotentKeyResolver> keyResolvers, IdempotentRedisDAO idempotentRedisDAO) {
        return new IdempotentAspect(keyResolvers, idempotentRedisDAO);
    }

    @Bean
    public IdempotentRedisDAO idempotentRedisDAO(StringRedisTemplate stringRedisTemplate) {
        return new IdempotentRedisDAO(stringRedisTemplate);
    }

    // ========== 各种 IdempotentKeyResolver Bean ==========

    @Bean
    public DefaultIdempotentKeyResolver defaultIdempotentKeyResolver() {
        return new DefaultIdempotentKeyResolver();
    }

    @Bean
    public UserIdempotentKeyResolver userIdempotentKeyResolver() {
        return new UserIdempotentKeyResolver();
    }

    @Bean
    public ExpressionIdempotentKeyResolver expressionIdempotentKeyResolver() {
        return new ExpressionIdempotentKeyResolver();
    }

}
