package com.platform.common.utils;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;

/**
 * Redis工具类
 */
@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;
    /**  默认过期时长，单位：秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1;
    private final static Gson gson = new Gson();
    
    public Set<String> keys(String key) {
        return redisTemplate.keys(key);
    }

    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    public void set(String key, Object value, long expire){
        valueOperations.set(key, toJson(value));
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    public void set(String key, Object value){
        set(key, value, DEFAULT_EXPIRE);
    }

    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);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }
	
    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }
    
	/**
	 * 从hash中删除指定的存储
	 * @param String key
	 * @param String  field 存储的名字
	 * */
   public void hdel(String key, String field) {
	   redisTemplate.opsForHash().delete(key,field);
   }
   /**
    * 将数据放入set缓存
    * @param key
    * @param field
    * @param value
    */
   public void hset(String key, String field, Object value){
	   hashOperations.put(key, field, toJson(value));
   }
   public void hset(String key, String field, Object value, long expire){
	   hashOperations.put(key, field, toJson(value));
       if(expire != NOT_EXPIRE){
           redisTemplate.expire(key, expire, TimeUnit.SECONDS);
       }
   }

   public void hsetall(String key, Map<String, Object> map, long expire) {
       hashOperations.putAll(key, map);
       if(expire != NOT_EXPIRE){
           redisTemplate.expire(key, expire, TimeUnit.SECONDS);
       }
   }

   public <T> T hget(String key, String field, Class<T> clazz, long expire) {
	   Object value = hashOperations.get(key, field);
       if(expire != NOT_EXPIRE){
           redisTemplate.expire(key, expire, TimeUnit.SECONDS);
       }
       return value == null ? null : fromJson(toJson(value),clazz);
   }
   
   public <T> T hget(String key, String field, Class<T> clazz) {
	   Object value = hget(key,field);
	   return value == null ? null : fromJson(toJson(value),clazz);
   }

   public Object hget(String key, String field) {
       return hashOperations.get(key, field);
   }

   public Map<String, Object> hentries(String key) {
       return hashOperations.entries(key);
   }
   
   public  Set<String> hkeys(String key) {
	   Set<String> set = hashOperations.keys(key);
	   return set;
   }

   public List<Object> hmget(String key, List<String> fields) {
       return hashOperations.multiGet(key, fields);
   }
   
   public long hlen(String key) {
	   return hashOperations.size(key);
   }
   /**
    * 判断hash中指定的存储是否存在
    * @param String key
    * @param String  field 存储的名字
    * @return true存在，false不存在
    * */
   public boolean hexists(String key, String field) {
	   boolean s = hashOperations.hasKey(key, field);
	   return s;
   }
    

    /**
     * Object转成JSON数据
     */
    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 gson.toJson(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz){
        return gson.fromJson(json, clazz);
    }
}
