package net.northking.cctp.designer.ws;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 */
@Component
public class RedisUtil {

    private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);


    @SuppressWarnings("rawtypes")
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    @SuppressWarnings("unchecked")
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    @SuppressWarnings("unchecked")
    public void remove(final String key) {

        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }


    /**
     * 读取缓存
     *
     * @param key
     * @return
     */

    public Map<String, List<String[]>> getHashList(final String key) {
        Map<String, List<String[]>> result=null;
        result = redisTemplate.opsForHash().entries(key);
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean setHash(final String key, Map<String,Object> value, Long expireTime) {
        boolean result = false;
        try {
            redisTemplate.opsForHash().putAll(key,value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            logger.error("",e);
        }
        return result;
    }
    @SuppressWarnings("unchecked")
    public boolean setHashList(final String key, Map<String,List<String[]>> value, Long expireTime) {
        boolean result = false;
        try {
            redisTemplate.opsForHash().putAll(key,value);
            result = true;
        } catch (Exception e) {
            logger.error("",e);
        }
        return result;
    }


    public Map<String,Object>  getHash(final String key) {
        Map<String,Object> result = null;
        try {
            result=redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
            logger.error("",e);
        }
        return result;
    }

    /**
     * 读取缓存
     *
     * @param token
     * @return
     */
    @SuppressWarnings("unchecked")
    public Object getByToken(final String token) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        Object result = operations.get(token);
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            logger.error("",e);
        }
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            logger.error("",e);
        }
        return result;
    }


    /**设置key的过期时间
     *
     *
     */
    public void setExpire(String key ,Long expireTime){
        redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
    }


    /**
     * 获取所有符合条件key
     *
     * @param pattern
     */
    public Set<Serializable> keys(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        return keys;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean setPlatform(final String key, Object value) {
        boolean result = false;
        try {
            LettuceConnectionFactory jedisConnectionFactory = (LettuceConnectionFactory)redisTemplate.getConnectionFactory();
            jedisConnectionFactory.setDatabase(1);
            redisTemplate.setConnectionFactory(jedisConnectionFactory);
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            logger.error("",e);
        }
        return result;
    }

    /**
     * 还原
     * @return
     */
    @SuppressWarnings("unchecked")
    public void init() {
        LettuceConnectionFactory jedisConnectionFactory = (LettuceConnectionFactory)redisTemplate.getConnectionFactory();
        jedisConnectionFactory.setDatabase(0);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
    }
}
