package cn.superdesk.uniorder.config.redis;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.lang.reflect.Method;

/**
 * @Description: 说明
 * 1) RedisTemplate(或StringRedisTemplate)虽然已经自动配置,但是不灵活(第一没有序列化,第二泛型为<Object, Object>不是我们想要的类型) 所以自己实现RedisTemplate或StringRedisTemplate)
 * 2) 采用RedisCacheManager作为缓存管理器
 * @Author: linjinyu
 * @CreateDate: 2021/2/4 上午9:47
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/2/4 上午9:47
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {


    /**
     * redis配置属性读取
     */
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int    port;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.database}")
    private int    database;
    @Value("${spring.redis.jedis.pool.max-idle}")
    private int    maxIdle;
    @Value("${spring.redis.jedis.pool.max-wait}")
    private long   maxWaitMillis;
    @Value("${spring.redis.jedis.pool.max-active}")
    private int    maxActive;


    /**
     * JedisPoolConfig配置
     *
     * @return
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {

        log.info("\n初始化-JedisPoolConfig ... ");

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setMaxIdle(maxIdle);

        log.info("\n初始化-JedisPoolConfig\n{}", AnJsonUtils.toPrettyJson(jedisPoolConfig));

        return jedisPoolConfig;
    }

    /**
     * 注入RedisConnectionFactory
     *
     * @return
     */
    @Bean
    public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) {

        log.info("\n初始化-JedisConnectionFactory");

        /* 在Spring Boot 1.x中已经过时,采用RedisStandaloneConfiguration配置
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
        jedisConnectionFactory.setHostName(host);
        jedisConnectionFactory.setDatabase(database);*/

        // JedisConnectionFactory配置 hsot、database、password 等参数
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setDatabase(database);
        redisStandaloneConfiguration.setPassword(password);

        // JedisConnectionFactory 配置 jedisPoolConfig
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolConfigBuilder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
        jedisPoolConfigBuilder.poolConfig(jedisPoolConfig);

        log.info("\n初始化-JedisConnectionFactory\n{}", AnJsonUtils.toPrettyJson(redisStandaloneConfiguration));


        return new JedisConnectionFactory(redisStandaloneConfiguration);

    }

    /**
     * 采用RedisCacheManager作为缓存管理器
     *
     * @param redisConnectionFactory
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {

        RedisCacheManager redisCacheManager = RedisCacheManager.create(redisConnectionFactory);

        return redisCacheManager;
    }


    /**
     * 生成key的策略:根据类名+方法名+所有参数的值生成唯一的一个key 默认的
     *
     * @return
     */
    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return (Object target, Method method, Object... params) -> {

            StringBuilder sb = new StringBuilder();

            sb.append(target.getClass().getName());
            sb.append(method.getName());

            for (Object obj : params) {
                sb.append(obj.toString());
            }

            log.info("缓存 RedisConfig->keyGenerator(alias:default) key {}", sb.toString());

            return sb.toString();
        };
    }

    /**
     * 重新实现RedisTemplate:解决序列化问题
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    @SuppressWarnings({"rawtype", "unchecked"})
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<String, Object> template = new RedisTemplate();


        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        StringRedisSerializer       stringRedisSerializer       = new StringRedisSerializer();

        ObjectMapper objectMapper = new ObjectMapper();

        // 设置任何字段可见
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 设置不是final的属性可以转换
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        log.info("objectMapper: {}", objectMapper);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);


        template.setConnectionFactory(redisConnectionFactory);
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson序列化方式
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        template.setEnableTransactionSupport(true);

        return template;
    }

    /**
     * 重新实现StringRedisTmeplate:键值都是String的的数据
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {

        StringRedisTemplate template = new StringRedisTemplate();

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        StringRedisSerializer       stringRedisSerializer       = new StringRedisSerializer();

        template.setConnectionFactory(redisConnectionFactory);
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson序列化方式
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        return template;
    }


}
