using System;
using StackExchange.Redis;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Collections;

namespace RMYY_CallCenter_Api.Utility
{
    public class RedisHelper
    {
        #region 
        //执行顺序---静态字段---静态构造函数---构造函数
        private static ConnectionMultiplexer redis;
        private static IDatabase database;
        static RedisHelper()
        {
            if (redis == null || !redis.IsConnected)
            {
                var config = new ConfigurationOptions
                {
                    AbortOnConnectFail = false,
                    AllowAdmin = true,
                    ConnectTimeout = 15000,
                    SyncTimeout = 5000,
                    ResponseTimeout = 15000,
                    Password = "",//Redis数据库密码
                    EndPoints = { ConfigHelper.GetValue("Redis_Server") + ":" + ConfigHelper.GetValue("Redis_Port") }// connectionString 为IP:Port 如”192.168.2.110:6379”
                };
                redis = ConnectionMultiplexer.Connect(config);
                //redis = ConnectionMultiplexer.Connect(Configs.GetValue("Redis_Server")+":"+Configs.GetValue("Redis_Port"));
                //redis = ConnectionMultiplexer.Connect("192.168.4.18, abortConnect=false");

                database = redis.GetDatabase();
            }
        }

        #endregion

        #region redis 字符串（string）操作

        /// <summary>
        /// 设置指定键的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool StringSet(string key, string value)
        {
            return database.StringSet(key, value);
        }

        /// <summary>
        /// 获取指定键的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object StringGet(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return null;

            }
            else
            {
                var rt = database.StringGet(key);

                if (rt.IsNull)
                {
                    return null ;
                }
                else
                {
                    return rt;
                }
            }
          
        }

        /// <summary>
        /// 获取存储在键上的字符串的子字符串
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static object StringGet(string key, int start, int end)
        {
            var rt = database.StringGetRange(key, start, end);
            if (rt.IsNull)
            {
                return null;
            }
            else
            {
                return rt;
            }
        }

        /// <summary>
        /// 设置键的字符串值并返回其旧值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object StringGetAndSet(string key, string value)
        {
            var rt = database.StringGetSet(key, value);
            if (rt.IsNull)
            {
                return null;
            }
            else
            {
                return rt;
            }
        }

        /// <summary>
        /// 返回在键处存储的字符串值中偏移处的位值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public static bool StringGetBit(string key, long offset)
        {
            return database.StringGetBit(key, offset);
        }

        /// <summary>
        /// 获取所有给定键的值
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static List<object> StringMultiGet(string[] keys)
        {
            List<object> list = new List<object>();
            for (int i = 0; i < keys.Length; i++)
            {
                list.Add(database.StringGet(keys[i]));
            }
            return list;
        }

        /// <summary>
        /// 存储在键上的字符串值中设置或清除偏移处的位
        /// </summary>
        /// <param name="key"></param>
        /// <param name="offset"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool StringSetBit(string key, long offset)
        {
            return database.StringSetBit(key, offset, true);
        }

        /// <summary>
        /// 使用键和到期时间来设置值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public static bool StringSet(string key, string value, TimeSpan expiry)
        {
            return database.StringSet(key, value, expiry);
        }

        /// <summary>
        /// 设置键的值，仅当键不存在时
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static void StringSetIfAbsent(string key, string value)
        {
            if (database.StringGet(key) == RedisValue.Null)
            {
                database.StringSet(key, value);
            }
        }

        /// <summary>
        /// 在指定偏移处开始的键处覆盖字符串的一部分
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="value">值</param>
        /// <param name="offset">偏移量</param>
        /// <returns></returns>
        public static object StringSet(string key, long offset, string value)
        {
            return database.StringSetRange(key, offset, value);
        }

        /// <summary>
        /// 获取存储在键中的值的长度
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static long StringSize(string key)
        {
            return database.StringLength(key);
        }

        /// <summary>
        /// 为多个键分别设置它们的值
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static void StringMultiSet(Dictionary<string, string> dic)
        {
            foreach (KeyValuePair<string, string> key in dic)
            {
                database.StringSet(key.Key, key.Value);
            }
        }

        /// <summary>
        /// 为多个键分别设置它们的值，仅当键不存在时
        /// </summary>
        /// <param name="keys">键值集合</param>
        /// <returns></returns>
        public static void StringMultiSetIfAbsent(Dictionary<string, string> dic)
        {
            foreach (KeyValuePair<string, string> key in dic)
            {
                //判断键值是否存在
                if (database.StringGet(key.Key) == RedisValue.Null)
                {
                    database.StringSet(key.Key, key.Value);
                }
            }
        }

        /// <summary>
        /// 将键的整数值按给定的数值增加
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="value">给定的数值</param>
        /// <returns></returns>
        public static double StringIncrement(string key, double value)
        {
            return database.StringIncrement(key, value);
        }

        /// <summary>
        /// 在key键对应值的右面追加值value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static long StringAppend(string key, string value)
        {
            return database.StringAppend(key, value);
        }

        /// <summary>
        /// 删除某个键值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool StringDelete(string key)
        {
            return false;
        }

        #endregion

        #region redis 哈希/散列/字典（Hash）操作

        /// <summary>
        /// 删除指定的哈希字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public static bool HashDelete(string key, string field)
        {
            return database.HashDelete(key, field);
        }

        /// <summary>
        /// 判断是否存在散列字段
        /// </summary>
        /// <param name=""></param>
        /// <param name=""></param>
        /// <returns></returns>
        public static bool HashHasKey(string key, string field)
        {
            return database.HashExists(key, field);
        }

        /// <summary>
        /// 获取存储在指定键的哈希字段的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public static object HashGet(string key, string field)
        {
            return database.HashGet(key, field);
        }

        /// <summary>
        /// 获取存储在指定键的哈希中的所有字段和值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Dictionary<string, object> HashGetAll(string key)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            var collection = database.HashGetAll(key);
            foreach (var item in collection)
            {
                dic.Add(item.Name, item.Value);
            }
            return dic;
        }

        /// <summary>
        /// 将哈希字段的浮点值按给定数值增加
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="value">给定的数值</param>
        /// <returns></returns>
        public static double HashIncrement(string key, string field, double value)
        {
            return database.HashIncrement(key, field, value);
        }

        /// <summary>
        /// 获取哈希中的所有字段
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string[] HashKeys(string key)
        {
            return database.HashKeys(key).ToStringArray();
        }

        /// <summary>
        /// 获取散列中的字段数量
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static long HashSize(string key)
        {
            return database.HashLength(key);
        }

        /// <summary>
        /// 获取所有给定哈希字段的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashKeys"></param>
        /// <returns></returns>
        public static List<object> HashMultiGet(string key, List<string> hashKeys)
        {
            List<object> result = new List<object>();
            foreach (string field in hashKeys)
            {
                result.Add(database.HashGet(key, field));
            }
            return result;
        }

        /// <summary>
        /// 为多个哈希字段分别设置它们的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static void HashPutAll(string key, Dictionary<string, string> dic)
        {
            List<HashEntry> list = new List<HashEntry>();
            for (int i = 0; i < dic.Count; i++)
            {
                KeyValuePair<string, string> param = dic.ElementAt(i);
                list.Add(new HashEntry(param.Key, param.Value));
            }
            database.HashSet(key, list.ToArray());
        }

        /// <summary>
        /// 设置散列字段的字符串值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static void HashPut(string key, string field, string value)
        {
            database.HashSet(key, new HashEntry[] { new HashEntry(field, value) });
        }

        /// <summary>
        /// 仅当字段不存在时，才设置散列字段的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="fiels"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static void HashPutIfAbsent(string key, string field, string value)
        {
            if (!HashHasKey(key, field))
            {
                database.HashSet(key, new HashEntry[] { new HashEntry(field, value) });
            }
        }

        /// <summary>
        /// 获取哈希中的所有值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string[] HashValues(string key)
        {
            return database.HashValues(key).ToStringArray();
        }

        /// <summary>
        /// redis中获取指定键的值并返回对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetHashValue<T>(string key)
        {
            HashEntry[] array = database.HashGetAll(key);
            Dictionary<string, object> dic = new Dictionary<string, object>();
            for (int i = 0; i < array.Length; i++)
            {
                dic.Add(array[i].Name, array[i].Value);
            }
            if (dic.Count > 0)
            {
                string strJson = JsonConvert.SerializeObject(dic);
                return JsonConvert.DeserializeObject<T>(strJson);
            }
            else
            {
                return default(T);
            }
        }

        /// <summary>
        /// 把指定对象存储在键值为key的redis中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="key"></param>
        public static void SetHashValue<T>(T t, string key)
        {
            string strJson = JsonConvert.SerializeObject(t);
            Dictionary<string, string> param = JsonConvert.DeserializeObject<Dictionary<string, string>>(strJson);
            HashPutAll(key, param);
        }

        #endregion

        #region redis 列表（List）操作

        /// <summary>
        /// 从左向右存压栈
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static long ListLeftPush(string key, string value)
        {
            return database.ListLeftPush(key, value);
        }

        /// <summary>
        /// 从左出栈
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object ListLeftPop(string key)
        {
            return database.ListLeftPop(key);
        }

        /// <summary>
        /// 队/栈长
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static long ListSize(string key)
        {
            return database.ListLength(key);
        }

        /// <summary>
        /// 范围检索,返回List
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static string[] ListRange(string key, int start, int end)
        {
            return database.ListRange(key, start, end).ToStringArray();
        }

        /// <summary>
        /// 移除key中值为value的i个,返回删除的个数；如果没有这个元素则返回0 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="i"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static long ListRemove(string key, string value)
        {
            return database.ListRemove(key, value);
        }

        /// <summary>
        /// 检索
        /// </summary>
        /// <param name="key"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static object ListIndex(string key, long index)
        {
            return database.ListGetByIndex(key, index);
        }

        /// <summary>
        /// 赋值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="index"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static void ListSet(string key, int index, string value)
        {
            database.ListSetByIndex(key, index, value);
        }

        /// <summary>
        /// 裁剪,删除除了[start,end]以外的所有元素 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static void ListTrim(string key, int start, int end)
        {
            database.ListTrim(key, start, end);
        }

        /// <summary>
        /// 将源key的队列的右边的一个值删除，然后塞入目标key的队列的左边，返回这个值
        /// </summary>
        /// <param name="sourceKey"></param>
        /// <param name="destinationKey"></param>
        /// <returns></returns>
        public static object ListRightPopAndLeftPush(string sourceKey, string destinationKey)
        {
            return database.ListRightPopLeftPush(sourceKey, destinationKey);
        }

        #endregion

        #region redis 集合（Set）操作

        /// <summary>
        /// 集合添加元素
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetAdd(string key, string value)
        {
            database.SetAdd(key, value);
        }

        /// <summary>
        /// 集合组合操作
        /// </summary>
        /// <param name="point">操作标示：0--并集；1--交集；2--差集</param>
        /// <param name="firstKey">第一个集合的键值</param>
        /// <param name="secondKey">第二个集合的键值</param>
        public static string[] SetCombine(int point, string firstKey, string secondKey)
        {
            RedisValue[] array;
            switch (point)
            {
                case 0:
                    array = database.SetCombine(SetOperation.Union, firstKey, secondKey);
                    break;
                case 1:
                    array = database.SetCombine(SetOperation.Intersect, firstKey, secondKey);
                    break;
                case 2:
                    array = database.SetCombine(SetOperation.Difference, firstKey, secondKey);
                    break;
                default:
                    array = new RedisValue[0];
                    break;
            }
            return array.ToStringArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetContains(string key, string value)
        {
            return database.SetContains(key, value);
        }

        /// <summary>
        /// 返回对应键值集合的长度
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static long SetLength(string key)
        {
            return database.SetLength(key);
        }

        /// <summary>
        /// 根据键值返回集合中所有的value
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string[] SetMembers(string key)
        {
            return database.SetMembers(key).ToStringArray();
        }

        /// <summary>
        /// 将成员从源集移动到目标集
        /// </summary>
        /// <param name="sourceKey">源集key</param>
        /// <param name="destinationKey">目标集key</param>
        /// <param name="value"></param>
        public static bool SetMove(string sourceKey, string destinationKey, string value)
        {
            return database.SetMove(sourceKey, destinationKey, value);
        }

        /// <summary>
        /// 移除集合中指定键值随机元素
        /// </summary>
        /// <param name="key"></param>
        public static string SetPop(string key)
        {
            return database.SetPop(key);
        }

        /// <summary>
        /// 返回集合中指定键值随机元素
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string SetRandomMember(string key)
        {
            return database.SetRandomMember(key);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="count"></param>
        public static string[] SetRandomMembers(string key, long count)
        {
            return database.SetRandomMembers(key, count).ToStringArray();
        }

        /// <summary>
        /// 移除集合中指定key值和value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetRemove(string key, string value)
        {
            database.SetRemove(key, value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public static void SetScan(string key)
        {
            database.SetScan(key);
        }

        #endregion

        #region redis 有序集合（sorted set）操作

        public static void Method(string key, string value, double score)
        {
            database.SortedSetAdd(key, new SortedSetEntry[] { new SortedSetEntry(value, score) });
        }

        #endregion

        #region redis 键（Key）操作

        /// <summary>
        /// 获取 Key
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        //public static IEnumerable<RedisKey> GetKeyList(string redisKey)
        //{
        //    var server = redis.GetServer(Configs.GetValue("Redis_Server"), Configs.GetValue("Redis_Port"));
        //    return server.Keys(pattern: "*"+ redisKey + "*");
        //}
        public static List<string> GetKeyList(string redisKey)
        {
            var server = redis.GetServer(ConfigHelper.GetValue("Redis_Server"), Int32.Parse(ConfigHelper.GetValue("Redis_Port")));
            List<string> keylist = new List<string>();
            var redisenum = server.Keys(pattern: "*" + redisKey + "*");
            foreach (var r in redisenum.ToList())
            {
                keylist.Add(r.ToString());
            }
            return keylist;
        }

        /// <summary>
        /// 移除指定 Key
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public static bool KeyDelete(string redisKey)
        {
            if (KeyExists(redisKey))
                return database.KeyDelete(redisKey);
            else
                return true;
        }

        /// <summary>
        /// 移除指定 Key
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        //public static long KeysDelete(IEnumerable<RedisKey> redisKeys)
        //{
        //    return database.KeyDelete(redisKeys.ToArray());
        //}
        public static long KeysDelete(List<string> redisKeys)
        {
            int n = 0;
            foreach (var r in redisKeys)
            {
                if (database.KeyDelete(r))
                {
                    n++;
                }
            }
            return n;
        }

        /// <summary>
        /// 校验 Key 是否存在
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public static bool KeyExists(string redisKey)
        {
            return database.KeyExists(redisKey);
        }

        /// <summary>
        /// 重命名 Key
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisNewKey"></param>
        /// <returns></returns>
        public static bool KeyRename(string redisKey, string redisNewKey)
        {
            return database.KeyRename(redisKey, redisNewKey);
        }

        /// <summary>
        /// 设置 Key 的时间
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public static bool KeyExpire(string redisKey, TimeSpan? expiry)
        {
            return database.KeyExpire(redisKey, expiry);
        }

        #endregion

        /// <summary>
        /// 获取key过期时间
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public static string GetKeyOutTime(string redisKey)
        {
            var server = redis.GetServer(ConfigHelper.GetValue("Redis_Server"), Int32.Parse(ConfigHelper.GetValue("Redis_Port")));
            var timeNow = server.Time().ToUniversalTime();
            var time = database.KeyTimeToLive(redisKey);
            var expire = time == null ? (DateTime?)null : timeNow.Add(time.Value); //返回UTC时间。
            return expire == null ? "" : expire.Value.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss");
        }
    }
}
