package com.dashitech.redis.core;

import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

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

    public static void main(String[] args) {
        JedisPoolFactory.getDefaultJedis();
        RedisListCmd redisList = new RedisListCmd();

        redisList.add("AAAAAAAAA", "A");
        redisList.add("AAAAAAAAA", "B");
        redisList.add("AAAAAAAAA", 0, "C");
        redisList.add("AAAAAAAAA", "A");

        List<String> list = new ArrayList<>();
        list.add("1111");
        list.add("2222");
        redisList.addAll("AAAAAAAAA", list);

        System.out.println(redisList.indexOf("AAAAAAAAA", "A"));
        System.out.println(redisList.contains("AAAAAAAAA", "B"));
        System.out.println(redisList.get("AAAAAAAAA", 1));
        System.out.println(redisList.get("AAAAAAAAA"));
        System.out.println(redisList.size("AAAAAAAAA"));

        redisList.remove("AAAAAAAAA", "B");

        redisList.drop("AAAAAAAAA");

    }

    /**
     * 新增，如果队列不存在，新建这个队列，如果存在插入值到队列最后面
     *
     * @param name  队列名称
     * @param value 值
     */
    public static void add(String name, String value) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            jedis.rpush(name, value);
        } finally {
            jedis.close();
        }
    }

    /**
     * 在指定下标插入值，注意！原下标值会被当前值覆盖！如果下标出错，会报错
     */
    public static void add(String name, int index, String value) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            jedis.lset(name, index, value);
        } finally {
            jedis.close();
        }
    }

    /**
     * 批量新增，如果队列不存在，新建这个队列，如果存在插入值到队列最后面
     */
    public static void addAll(String name, List<String> list) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            if (list != null && list.size() > 0) {
                String[] values = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    values[i] = list.get(i);
                }
                jedis.rpush(name, values);
            }
        } finally {
            jedis.close();
        }
    }

    /**
     * 删除所有和value值相等的对象
     */
    public static void remove(String name, String value) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            jedis.lrem(name, 0, value);
        } finally {
            jedis.close();
        }
    }

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

    /**
     * 根据下标获取值
     */
    public static String get(String name, int index) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            return jedis.lindex(name, index);
        } finally {
            jedis.close();
        }
    }

    /**
     * 获取所有值
     */
    public static List<String> get(String name) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            return jedis.lrange(name, 0, -1);
        } finally {
            jedis.close();
        }
    }

    /**
     * 直接比对返回下标
     */
    public static int indexOf(String name, String value) {
        List<String> list = get(name);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                String _value = list.get(i);
                if (_value.equals(value)) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 获取并移除列表第一个值，如果队列空了，返回null
     */
    public static String pop(String name) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            return jedis.lpop(name);
        } finally {
            jedis.close();
        }
    }


    /**
     * 直接返回是否包含
     */
    public static boolean contains(String name, String value) {
        int index = indexOf(name, value);
        return index > -1;
    }

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

}
