package com.njcn.utils.redis;

import com.njcn.utils.SerializeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.util.List;


public class JedisManager {

    @Autowired
    private JedisPool jedisPool;

    public Jedis getJedis() {
        Jedis jedis;
        try {
            jedis = jedisPool.getResource();
        } catch (JedisConnectionException e) {
            String message = StringUtils.trim(e.getMessage());
            if ("Could not get a resource from the pool".equalsIgnoreCase(message)) {
                System.out.println("++++++++++请检查你的redis服务++++++++");
                System.out.println("|①.请检查是否安装redis服务，如果没安装");
                System.out.println("|②.请检查redis 服务是否启动。启动口诀[安装目录中的redis-server.exe，双击即可，如果有错误，请用CMD方式启动。");
                System.out.println("|③.请检查redis启动是否带配置文件启动，也就是是否有密码，是否端口有变化（默认6379）。解决方案，参考第二点。如果需要配置密码和改变端口，请修改applicationContext-redis.xml配置。");
            }
            throw new JedisConnectionException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jedis;
    }


    /**
     * 清除redis内部所有数据
     */
    public void flushAll() {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.flushAll();
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
    }


    /**
     * 放回jedis资源
     *
     * @param jedis jedis对象
     */
    public void returnResource(Jedis jedis) {
        if (jedis == null)
            return;
        jedis.close();
    }


    /**
     * 根据key和库索引获取内容字
     *
     * @param dbIndex 指定库索引
     * @param key     key值
     */
    public String getValueByKey(int dbIndex, String key) {
        Jedis jedis = null;
        String result;
        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            result = jedis.get(key);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 根据key和库索引删除数据
     *
     * @param dbIndex 指定库索引
     * @param key     key值
     */
    public void deleteByKey(int dbIndex, String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            jedis.del(key);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
    }

    /**
     * 根据key和库索引保存内容字节码格式
     *
     * @param dbIndex    指定库索引
     * @param key        key值
     * @param value      存储值
     * @param expireTime 生命周期时间 以秒为单位
     */
    public void saveValueByKey(int dbIndex, String key, String value, int expireTime) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            jedis.set(key, value);
            if (expireTime > 0)
                jedis.expire(key, expireTime);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
    }

    /***
     * 获取生命周期
     * @author hongawen
     * @date 2023/7/31 11:48
     * @return Long
     */
    public Long ttl(int dbIndex, String key) {
        Jedis jedis = null;
        long ttl = 1L;
        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            ttl = jedis.ttl(key);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
        return ttl;
    }


    public byte[] getValueByKey(int dbIndex, byte[] key) {
        Jedis jedis = null;
        byte[] result;
        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            result = jedis.get(key);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    public void deleteByKey(int dbIndex, byte[] key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            jedis.del(key);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
    }

    public void saveValueByKey(int dbIndex, byte[] key, byte[] value, int expireTime) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            jedis.set(key, value);
            if (expireTime > 0)
                jedis.expire(key, expireTime);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
    }

    public void hset(int dbIndex, byte[] key, byte[] field, byte[] value, int expireTime) {
        Jedis jedis = null;

        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            jedis.hset(key, field, value);

            if (expireTime > 0)
                jedis.expire(key, expireTime);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
    }

    public void hset(int dbIndex, String key, String field, String value, int expireTime) {
        Jedis jedis = null;

        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            jedis.hset(key, field, value);

            if (expireTime > 0)
                jedis.expire(key, expireTime);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
    }

    public String hget(int dbIndex, String key, String field) {
        Jedis jedis = null;
        String str = null;

        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            str = jedis.hget(key, field);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }

        return str;
    }

    public byte[] hget(int dbIndex, byte[] key, byte[] field) {
        Jedis jedis = null;
        byte[] bytes = null;

        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            bytes = jedis.hget(key, field);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }

        return bytes;
    }

    public void hdel(int dbIndex, byte[] key, byte[] field) {
        Jedis jedis = null;

        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            jedis.hdel(key, field);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
    }

    public void hdel(int dbIndex, String key, String field) {
        Jedis jedis = null;

        try {
            jedis = getJedis();
            jedis.select(dbIndex);
            jedis.hdel(key, field);
        } catch (Exception e) {
            throw e;
        } finally {
            returnResource(jedis);
        }
    }
}
