package com.dxhy.common.util.redis;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import com.alibaba.fastjson.JSON;

/**
 * Redis工具类
 *
 * @author jiaohongyang
 */
@Configuration

public class RedisUtils {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private ValueOperations<String, String> valueOperations;
    /**
     * 默认过期时长，单位：秒
     */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**
     * 不设置过期时长
     */
    public final static long NOT_EXPIRE = -1;

    /**
     * 根据可以保存value
     *
     * @param key
     *            key
     * @param value
     *            value
     * @param expire
     *            过期时间
     */
    public void set(String key, Object value, long expire) {
        valueOperations.set(key, toJson(value));
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    /**
     * 根据可以保存value
     *
     * @param key
     *            key
     * @param value
     *            value
     */
    public void set(String key, Object value) {
        set(key, value, DEFAULT_EXPIRE);
    }

    /**
     * 根据key 获取value 返回实体
     *
     * @param key
     *            key
     * @param clazz
     *            实体class
     * @param expire
     *            过期
     * @param <T>
     *            实体class
     * @return 返回
     */
    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    /**
     * 根据key 获取value 返回实体
     *
     * @param key
     *            key
     * @param clazz
     *            实体class
     * @param <T>实体class
     * @return 返回
     */
    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    /**
     * 根据key 获取value
     *
     * @param key
     *            key
     * @param expire
     *            过期时间
     * @return 返回
     */
    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    /**
     * 根据key 获取 value
     *
     * @param key
     *            key
     * @return 返回
     */
    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    /**
     * 根据 key 删除
     *
     * @param key
     *            key
     */
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * Object转成JSON数据
     *
     * @param object
     *            实体
     * @return 返回
     */
    private String toJson(Object object) {
        if (object instanceof Integer || object instanceof Long || object instanceof Float || object instanceof Double
            || object instanceof Boolean || object instanceof String) {
            return String.valueOf(object);
        }
        return JSON.toJSONString(object);
    }

    /**
     * JSON数据，转成Object
     *
     * @param json
     *            json
     * @param clazz
     *            class
     * @param <T>
     *            返回
     * @return 返回
     */
    private <T> T fromJson(String json, Class<T> clazz) {
        return JSON.parseObject(json, clazz);
    }

    /**
     * 模糊搜索所有key
     *
     * @param key
     *            key值
     * @return 返回
     */
    public Set<String> keys(String key) {
        return redisTemplate.keys(key + "*");
    }

    /**
     * redis hasKey
     *
     * @param key
     *            key值
     * @return 返回
     */
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
}
