package com.dashitech.redis.core;

import redis.clients.jedis.Jedis;

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

/**
 * Redis键值队列，元素key不可重复，注意并发问题
 * Created by CX on 2018/8/20 0020.
 */
public class RedisHashCmd {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("A", "1");
        map.put("B", "2");

        JedisPoolFactory.getDefaultJedis();
        RedisHashCmd redisMap = new RedisHashCmd();
        redisMap.putAll("MapA", map);


        redisMap.put("MapA", "C", "3");
        redisMap.put("MapA", "C", "4");

        System.out.println(redisMap.containsKey("MapA", "C"));
        System.out.println(redisMap.get("MapA", "C"));
        System.out.println(redisMap.values("MapA"));
        redisMap.remove("MapA", "A");

        redisMap.drop("MapA");
    }

    /**
     * 新增，如果队列不存在，新建这个队列，如果存在直接覆盖键和值
     *
     * @param name  队列名称
     * @param key   主键
     * @param value 值
     */
    public static void put(String name, String key, String value) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            jedis.hset(name, key, value);
        } finally {
            jedis.close();
        }
    }

    /**
     * 批量新增，如果队列不存在，新建这个队列，如果存在直接覆盖键和值
     */
    public static void putAll(String name, Map<String, String> map) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            jedis.hmset(name, map);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            jedis.close();
        }
    }

    /**
     * 删除key和对应值
     */
    public static void remove(String name, String key) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            jedis.hdel(name, key);
        }catch (Exception ex) {
            ex.printStackTrace();
        }finally {
            jedis.close();
        }
    }

    /**
     * 直接删除队列本身
     */
    public static void drop(String name) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            jedis.del(name);
        } finally {
            jedis.close();
        }
    }

    /**
     * 获取key对应值
     */
    public static String get(String name, String key) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            return jedis.hget(name, key);
        } finally {
            jedis.close();
        }
    }

    /**
     * key集合
     */
    public static Set<String> keySet(String name) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            return jedis.hkeys(name);
        } finally {
            jedis.close();
        }
    }

    /**
     * value集合
     */
    public static List<String> values(String name) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            return jedis.hvals(name);
        } finally {
            jedis.close();
        }
    }

    public static Map<String, String> getAll(String name) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            return jedis.hgetAll(name);
        } finally {
            jedis.close();
        }
    }

    /**
     * 是否存在这个key
     */
    public static boolean containsKey(String name, String key) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            return jedis.hexists(name, key);
        } finally {
            jedis.close();
        }
    }

    /**
     * 队列大小
     */
    public static Long size(String name) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            return jedis.hlen(name);
        } finally {
            jedis.close();
        }
    }

}
