package com.neusoft.smart.pos.framework.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neusoft.smart.pos.framework.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.io.Serializable;
import java.lang.reflect.Method;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

	@Value("${spring.redis.database}")
    private Integer database;
	
	@Value("${spring.redis.host}")
	private String host;
	
	@Value("${spring.redis.port}")
	private Integer port;
	
	@Value("${spring.redis.password}")
	private String password;
	
	@Value("${spring.redis.maxWait}")
	private Long maxWait;
	
	@Value("${spring.redis.maxIdle}")
	private Integer maxIdle;
	
	@Value("${spring.redis.minIdle}")
	private Integer minIdle;
	
	@Value("${spring.redis.maxTotal}")
	private Integer maxTotal;
	
	@Value("${spring.redis.timeout}")
	private Integer timeout;
	
	@Bean
    public JedisConnectionFactory jedisConnectionFactory() {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setDatabase(database);
		jedisConnectionFactory.setHostName(host);
		jedisConnectionFactory.setPort(port);
		jedisConnectionFactory.setPassword(password);
		jedisConnectionFactory.setTimeout(timeout);
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
        return jedisConnectionFactory;
    }
	
    @Bean
    public RedisTemplate<Serializable, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<Serializable, Object> redisTemplate = new RedisTemplate<Serializable, Object>();
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;
    }
    
    @Bean
    public JedisPoolConfig jedisPoolConfig(){
    	JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    	jedisPoolConfig.setMaxIdle(maxIdle);
    	jedisPoolConfig.setMinIdle(minIdle);
    	jedisPoolConfig.setMaxWaitMillis(maxWait);
    	jedisPoolConfig.setMaxTotal(maxTotal);
    	return jedisPoolConfig;
    }
    
    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                	if(null != obj){
                		sb.append(obj.toString());
                	}
                }
                System.out.println("缓存key："+ MD5Utils.encoderByMd5With32Bit(sb.toString()));
                return MD5Utils.encoderByMd5With32Bit(sb.toString());
            }
        };
    }

}
