using System;
using System.Collections.Generic;
using Library;

namespace MySystem
{
    public class BothdisDbconn
    {
        public readonly static BothdisDbconn Instance = new BothdisDbconn();

        public void SendMq(string key, object obj)
        {
            SetRedisDataList data = new SetRedisDataList()
            {
                key = key,
                val = Newtonsoft.Json.JsonConvert.SerializeObject(obj),
            };
            // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(data), "SetRedisDataList");
            RedisDbconn.Instance.AddList("SetRedisDataList", Newtonsoft.Json.JsonConvert.SerializeObject(data));
        }

        #region 设置单个字段
        public void Set(string key, object value)
        {
            try
            {
                if (RedisDbconn.Instance.Set(key, value))
                {
                    RedisDbconn.Instance.Clear(key);
                }
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:Set:Error");
            }
        }
        #endregion

        #region 整数累加
        public void AddInt(string key, long value = 1)
        {
            try
            {
                if (RedisDbconn.Instance.AddInt(key, value) > 0)
                {
                    RedisDbconn.Instance.Clear(key);
                }
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddInt:Error");
            }
        }
        #endregion

        #region 数字累加
        public void AddNumber(string key, decimal value = 1)
        {
            try
            {
                if (RedisDbconn.Instance.AddNumber(key, value) > 0)
                {
                    RedisDbconn.Instance.Clear(key);
                }
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddNumber:Error");
            }
        }
        #endregion

        #region 获取单个字段
        public T Get<T>(string key)
        {
            if (RedisDbconn.Instance.Exists(key))
            {
                T obj = RedisDbconn.Instance.Get<T>(key);
                if (obj != null)
                {
                    return obj;
                }
            }
            T newobj = TendisDbconn.Instance.Get<T>(key);
            if (newobj != null)
            {
                RedisDbconn.Instance.Set(key, newobj);
                RedisDbconn.Instance.SetExpire(key, function.get_Random(1800, 5400));
            }
            return newobj;
        }
        public decimal GetNumber(string key)
        {
            if (RedisDbconn.Instance.Exists(key))
            {
                decimal obj = RedisDbconn.Instance.Get<decimal>(key);
                if (obj > 0)
                {
                    return obj;
                }
            }
            decimal newobj = TendisDbconn.Instance.Get<decimal>(key);
            if (newobj > 0)
            {
                RedisDbconn.Instance.Clear(key);
                RedisDbconn.Instance.AddNumber(key, newobj);
                RedisDbconn.Instance.SetExpire(key, function.get_Random(1800, 5400));
            }
            return 0;
        }
        public int GetInt(string key)
        {
            if (RedisDbconn.Instance.Exists(key))
            {
                int obj = RedisDbconn.Instance.Get<int>(key);
                if (obj > 0)
                {
                    return obj;
                }
            }
            int newobj = TendisDbconn.Instance.Get<int>(key);
            if (newobj > 0)
            {
                RedisDbconn.Instance.Clear(key);
                RedisDbconn.Instance.AddInt(key, newobj);
                RedisDbconn.Instance.SetExpire(key, function.get_Random(1800, 5400));
            }
            return 0;
        }
        #endregion

        #region 设置散列字段
        public void HSet(string key, string field, object value)
        {
            try
            {
                RedisDbconn.Instance.HSet(key, field, value);
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:HSet:Error");
            }
            RedisDbconn.Instance.HSet(key, field, value);
        }
        #endregion

        #region 散列整数累加
        public void HAddInt(string key, string field, long value = 1)
        {
            try
            {
                RedisDbconn.Instance.HAddInt(key, field, value);
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:HAddInt:Error");
            }
            RedisDbconn.Instance.HAddInt(key, field, value);
        }
        #endregion

        #region 散列数字累加
        public void HAddNumber(string key, string field, decimal value = 1)
        {
            try
            {
                RedisDbconn.Instance.HAddNumber(key, field, value);
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:HAddNumber:Error");
            }
            RedisDbconn.Instance.HAddNumber(key, field, value);
        }
        #endregion

        #region 获取散列元素
        public T HGet<T>(string key, string field)
        {
            if (RedisDbconn.Instance.HExists(key, field))
            {
                T obj = RedisDbconn.Instance.HGet<T>(key, field);
                if (obj != null)
                {
                    return obj;
                }
            }
            T newobj = RedisDbconn.Instance.HGet<T>(key, field);
            // if (newobj != null)
            // {
            //     RedisDbconn.Instance.HSet(key, field, newobj);
            // }
            return newobj;
        }
        #endregion

        #region 获取散列所有元素
        public Dictionary<string, T> HGetAll<T>(string key)
        {
            if (RedisDbconn.Instance.Exists(key))
            {
                Dictionary<string, T> obj = RedisDbconn.Instance.HGetAll<T>(key);
                if (obj != null)
                {
                    return obj;
                }
            }
            Dictionary<string, T> newobj = RedisDbconn.Instance.HGetAll<T>(key);
            // if (newobj != null)
            // {
            //     foreach (string sub in newobj.Keys)
            //     {
            //         RedisDbconn.Instance.HSet(key, sub, newobj[sub]);
            //     }
            // }
            return newobj;
        }
        #endregion

        #region 添加集合对象
        public void SAdd(string key, object value)
        {
            try
            {
                RedisDbconn.Instance.SAdd(key, value);
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:SAdd:Error");
            }
            RedisDbconn.Instance.SAdd(key, value);
        }
        public void SAdd(string key, object[] value)
        {
            try
            {
                RedisDbconn.Instance.SAdd(key, value);
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:SAdd:Error");
            }
            RedisDbconn.Instance.SAdd(key, value);
        }
        #endregion

        #region 获取集合对象
        public T[] SGetList<T>(string key)
        {
            if (RedisDbconn.Instance.Exists(key))
            {
                T[] obj = RedisDbconn.Instance.SGetList<T>(key);
                if (obj != null)
                {
                    if (obj.Length > 0)
                    {
                        return obj;
                    }
                }
            }
            T[] newobj = RedisDbconn.Instance.SGetList<T>(key);
            // if (newobj != null)
            // {
            //     foreach (T sub in newobj)
            //     {
            //         RedisDbconn.Instance.SAdd(key, sub);
            //     }
            // }
            return newobj;
        }
        #endregion

        #region 添加列表对象
        public void AddList(string key, object value)
        {
            try
            {
                if (RedisDbconn.Instance.AddList(key, value) > 0)
                { 
                    RedisDbconn.Instance.Clear(key);
                }
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddList:Error");
            }
        }
        public void AddList(string key, object[] value)
        {
            try
            {
                if (RedisDbconn.Instance.AddList(key, value) > 0)
                {
                    RedisDbconn.Instance.Clear(key);
                }
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddList:Error");
            }
        }
        #endregion

        #region 获取列表
        public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
        {
            if (RedisDbconn.Instance.Exists(key))
            {
                List<T> list = RedisDbconn.Instance.GetList<T>(key, pageNum, pageSize);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            List<T> tlist = RedisDbconn.Instance.GetList<T>(key, pageNum, pageSize);
            if (tlist.Count > 0)
            {
                RedisDbconn.Instance.Clear(key);
                object[] tmplist = new object[tlist.Count];
                for (int i = 0; i < tlist.Count; i++)
                {
                    tmplist[i] = tlist[i];
                }
                RedisDbconn.Instance.AddList(key, tmplist);
            }
            return tlist;
        }
        #endregion

        #region 添加排序列表对象
        public void AddSort(string key, object value, decimal score)
        {
            try
            {
                RedisDbconn.Instance.AddSort(key, value, score);
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    value = value,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddSort:Error");
            }
            RedisDbconn.Instance.AddSort(key, value, score);
            int Expired = 60 * 60 * 24 * 180;
            RedisDbconn.Instance.SetExpire(key, Expired);
        }
        #endregion

        #region 获取排序列表
        public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
        {
            if (RedisDbconn.Instance.Exists(key))
            {
                List<T> list = RedisDbconn.Instance.GetSort<T>(key, pageNum, pageSize);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            return RedisDbconn.Instance.GetSort<T>(key, pageNum, pageSize);
        }
        public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
        {
            if (RedisDbconn.Instance.Exists(key))
            {
                List<T> list = RedisDbconn.Instance.GetSortDesc<T>(key, pageNum, pageSize);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            return RedisDbconn.Instance.GetSortDesc<T>(key, pageNum, pageSize);
        }
        #endregion

        public void Remove(string key, long start, long end)
        {
            try
            {
                RedisDbconn.Instance.AddSort(key, start, end);
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = key,
                    start = start,
                    end = end,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:Remove:Error");
            }
            RedisDbconn.Instance.Remove(key, start, end);
        }

        public void RemoveTop(string key, long count)
        {
            Remove(key, count, RedisDbconn.Instance.Count(key) - 1); ;
        }

        public void Clear(string pattern)
        {
            try
            {
                RedisDbconn.Instance.Clear(pattern);
                RedisDbconn.Instance.Clear(pattern);
            }
            catch (Exception ex)
            {
                TendisErr err = new TendisErr()
                {
                    key = pattern,
                    errMsg = ex.ToString(),
                };
                function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:Clear:Error");
            }
        }
    }
}
