package cn.hmsoft.helper;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import cn.hmsoft.application.web.RedisConfigHelper;
import cn.hmsoft.log.LogHelper;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**   
* @Description: redis-操作帮助类
* @version: v1.0.0
* @date: 2021年11月17日 下午2:22:34 
*/
public class RedisHelper {
	
	private static JedisPool jedisPool = null;
	
	private RedisHelper() {
	}
	
	static {
		if (jedisPool == null) {
			synchronized (RedisHelper.class) {
				if (jedisPool == null) {
					final GenericObjectPoolConfig<Object> jedisPoolConfig = new GenericObjectPoolConfig<Object>();
					jedisPoolConfig.setMaxTotal(RedisConfigHelper.PoolMaxTotal);
					jedisPoolConfig.setMaxIdle(RedisConfigHelper.PoolMaxIdle);
					jedisPoolConfig.setMinIdle(RedisConfigHelper.PoolMinIdle);
					jedisPoolConfig.setMaxWaitMillis(RedisConfigHelper.PoolMaxWait);
					jedisPoolConfig.setTestOnBorrow(true);
					if (StringHelper.isNotEmpty(RedisConfigHelper.Password))
						jedisPool = new JedisPool(jedisPoolConfig, RedisConfigHelper.Host, RedisConfigHelper.Port,
								RedisConfigHelper.Timeout, RedisConfigHelper.Password);
					else
						jedisPool = new JedisPool(jedisPoolConfig, RedisConfigHelper.Host, RedisConfigHelper.Port,
								RedisConfigHelper.Timeout);
				}
			}
		}
	}
	
	/**
	 *  redis set String key和value
	 * @param key
	 * @param seconds:过期时间
	 * @param value
	 */
	public static void set(String key, int seconds, String value) {
		Jedis jedis = jedisPool.getResource();
		try {
			jedis.setex(key, seconds, value);
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}
	
	public static String get(String key) {
		String ret = "";
		Jedis jedis = jedisPool.getResource();
		try {
			if (jedis.exists(key)) {
				ret = jedis.get(key);
			}
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return ret;
	}
	
	public static void del(String... key) {
		if (key == null || key.length == 0)
			return;
		Jedis jedis = jedisPool.getResource();
		try {
				jedis.del(key);
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}
	
	/**
	 *  key剩余的过期时间
	 * @param key
	 * @return
	 */
	public static long ttl(String key) {
		long ttl = 0;
		Jedis jedis = jedisPool.getResource();
		try {
			if (jedis.exists(key)) {
				ttl = jedis.ttl(key);
			}
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return ttl;
	}
	
	public static Map<String, String> hgetAll(String key) {
		Jedis jedis = jedisPool.getResource();
		try {
			Map<String, String> map = jedis.hgetAll(key);
			return map;
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return new HashMap<String, String>();
	}
	
	public static String hset(String key, String field, String value) {
		String ret = "";
		Jedis jedis = jedisPool.getResource();
		try {
			jedis.hset(key, field, value);
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return ret;
	}
	
	public static String hget(String key, String field) {
		String ret = "";
		Jedis jedis = jedisPool.getResource();
		try {
			ret = jedis.hget(key, field);
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return ret;
	}
	
	public static boolean hexists(String key, String field, String value) {
		Jedis jedis = jedisPool.getResource();
		try {
			return jedis.hexists(key, field);
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return false;
	}
	
	public static long expire(String key, int seconds) {
		Jedis jedis = jedisPool.getResource();
		try {
			return jedis.expire(key, seconds);
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return 0;
	}
	
	/**
	 * 模糊查询 左匹配
	 * @param key
	 * @return
	 */
	public static Set<String> keys(String key) {
		if (key==null || "".equals(key)) {
			return null;
		}
		Jedis jedis = jedisPool.getResource();
		try {
			return jedis.keys(key+"*");
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}
	
	public static long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		try {
			return jedis.incr(key);
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return 0;
	}
	
	public static boolean exists(String key) {
		Jedis jedis = jedisPool.getResource();
		try {
			return jedis.exists(key);
		} catch(Exception e) {
			LogHelper.error(e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return false;
	}
	
}
