using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.RedisRepository.RedisAsyncHelper
{
    /// <summary>
    /// 异步方法接口  
    /// 存入数据均为方法内部序列化后的byte,所以取数据的时候需要反序列化时，请指定正确的数据类型
    /// </summary>
    public partial interface IRedisHelper
    {
        #region Redis数据类型—String 

        /// <summary>
        /// 将任何数据以redis string存储
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? timeout = null);

        /// <summary>
        /// 对数值进行减法操作，默认-1
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>操作后的结果</returns>
        Task<long> StringDecrementAsync(string key, long value = 1L);

        /// <summary>
        /// 对数值进行加法操作，默认+1
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>操作后的结果</returns>
        Task<long> StringIncrementAsync(string key, long value = 1L);

        /// <summary>
        /// 从redis string中以指定类型取出
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<T> StringGetAsync<T>(string key);

        #endregion

        #region Redis数据类型—Hash

        /// <summary>
        /// 向Hash key中存储任意类型任意值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns>是否成功</returns>
        Task<bool> HashSetAsync<T>(string key, string field, T value);

        /// <summary>
        /// 批量 向Hash key中存储任意类型任意值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashFields"></param>
        /// <returns>无返回值</returns>
        Task HashMultiSetAsync<T>(string key, Dictionary<string, T> hashFields);

        /// <summary>
        /// 对指定hash key中制定field做数量增加操作 默认自增1 
        /// 如果此操作前key不存在 则创建。 如果此操作前该field不存在或者非数字 则先被置0，再被继续操作
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="incrCount"></param>
        /// <returns>操作后的结果</returns>
        Task<long> HashIncrementAsync(string key, string field, long incrCount = 1);

        /// <summary>
        /// 对指定hash key中制定field做数量增加操作 默认自减1 
        /// 如果此操作前key不存在 则创建。 如果此操作前该field不存在或者非数字 则先被置0，再被继续操作
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="decrCount"></param>
        /// <returns>操作后的结果</returns>
        Task<long> HashDecrementAsync(string key, string field, long decrCount = 1);

        /// <summary>
        /// 从指定Hash中 删除指定field
        /// 如果key或者field不存在，则false
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns>是否成功</returns>
        Task<bool> HashDeleteFieldAsync(string key, string field);

        /// <summary>
        /// 从指定Hash key中 批量删除指定field
        /// 如果key或者field不存在，则false
        /// </summary>
        /// <param name="key"></param>
        /// <param name="fields"></param>
        /// <returns>移除数量</returns>
        Task<long> HashMultiDeleteFieldAsync(string key, List<string> fields);

        /// <summary>
        /// 从指定Hash key中获取指定field值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        Task<T> HashGetAsync<T>(string key, string field);

        /// <summary>
        /// 从指定Hash key中判断field是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        Task<bool> HashFieldExistAsync(string key, string field);

        /// <summary>
        /// 获取指定Hash key中的所有field的值 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<List<T>> HashValuesAsync<T>(string key);

        /// <summary>
        /// 获取指定Hash key中所有 field名称及其Value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<Dictionary<string, T>> HashGetAllAsync<T>(string key);

        /// <summary>
        /// 获取指定Hash key中所有field
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<List<string>> HashFieldsAsync(string key);

        #endregion

        #region Redis数据类型—List     

        /// <summary>
        /// 在指定pivot后插入value, 如果pivot不存在，则返回-1, 如果key不存在，则返回0
        /// 如果存在多个相同指定的的pivot,则插入第一个指定pivot后面.
        /// 即链表从左向右查找，遇到指定pivot，则确定位置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="pivot">list中的一个值</param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task<long> ListInsertAfterAsync<T>(string key, string pivot, T value);

        /// <summary>
        /// 在指定pivot前插入value, 如果pivot不存在，则返回-1, 如果key不存在，则返回0
        /// 如果存在多个相同指定的的pivot,则插入第一个指定pivot前面.
        /// 即链表从左向右查找，遇到指定pivot，则确定位置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="pivot"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task<long> ListInsertBeforeAsync<T>(string key, string pivot, T value);

        /// <summary>
        /// 从链表左侧弹出第一个元素（弹出能获取到该元素并且被删除）
        /// 如果key不存在 或者链表为空 则为null
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<T> ListLeftPopAsync<T>(string key);

        /// <summary>
        /// 从链表左侧增加一个元素,key不存在则被创建
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>返回操作后的链表长度</returns>
        Task<long> ListLeftPushAsync<T>(string key, T value);

        /// <summary>
        /// 从链表左侧批量增加元素，如果 a b c  则c会在链表左侧第一位  b第二位  a第三位
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="values"></param>
        /// <returns>返回操作后的链表长度</returns>
        Task<long> ListLeftMultiPushAsync<T>(string key, List<T> values);

        /// <summary>
        /// 获取链表长度，不存在key则为0
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<long> ListLengthAsync<T>(string key);

        /// <summary>
        /// 获取链表中所有数据，从左侧start开始到stop结束，从0—-1则认为获取全部，默认获取全部
        /// start为负数则代表从链表右侧开始，-1为右侧第一位，-2为右侧第二位
        /// start要小于stop,否则返回null
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <returns></returns>
        Task<List<T>> ListRangeAsync<T>(string key, long start = 0L, long stop = -1L);

        /// <summary>
        /// 从链表中一处count数量的value. count大于0则从左至右，count小于0则从右至左，count=0则移除全部
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        Task<long> ListRemoveAsync<T>(string key, T value, long count = 0L);

        /// <summary>
        /// 从右侧弹出第一个元素（弹出能获取到该元素并且被删除）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<T> ListRightPopAsync<T>(string key);


        /// <summary>
        /// 从链表右侧加入元素，如果 rpush a b c 则c为右侧第一位 b第二位 c第三位
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task<long> ListRightPushAsync<T>(string key, T value);

        /// <summary>
        /// 从右侧批量插入，和左侧相反
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        Task<long> ListRightMultiPushAsync<T>(string key, List<T> values);

        /// <summary>
        /// 在链表指定索引处，插入元素
        /// 正数索引从0开始，代表左侧。负数从-1开始 代表从右侧。-1为右侧第一位
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="index"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task ListSetByIndexAsync<T>(string key, int index, T value);

        /// <summary>
        /// 留下start到stop之间的数据。负数代表从右侧寻找  -1为右侧第一位
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <returns></returns>
        Task ListTrimAsync(string key, long start, long stop);

        /// <summary>
        /// 获取指定index的值，负数代表从右侧寻找  -1为右侧第一位
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        Task<T> ListGetByIndexAsync<T>(string key, long index);

        #endregion

        #region Redis数据类型—Set

        /// <summary>
        /// 向指定集合中增加一个元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task<bool> SetAddAsync<T>(string key, T value);

        ///// <summary>
        ///// 指定集合计算操作operation枚举，指定计算结果将存的目标destKey，指定需要参与计算的多个key
        ///// </summary>
        ///// <param name="operation"></param>
        ///// <param name="destKey"></param>
        ///// <param name="combineKeys"></param>
        ///// <returns></returns>
        //Task<long> SetCombineAndStoreAsync(SetOperation operation, string destKey, List<string> combineKeys);

        ///// <summary>
        ///// 指定集合计算操作operation枚举，指定需要参与计算的多个key
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="operation"></param>
        ///// <param name="combineKeys"></param>
        ///// <returns></returns>
        //Task<List<T>> SetCombineAsync<T>(SetOperation operation, List<string> combineKeys);

        ///// <summary>
        ///// 指定值是否存在于指定集合中
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="key"></param>
        ///// <param name="value"></param>
        ///// <returns></returns>
        //Task<bool> SetContainsAsync<T>(string key, T value);

        ///// <summary>
        ///// 获取指定集合中元素个数
        ///// </summary>
        ///// <param name="key"></param>
        ///// <returns></returns>
        //Task<long> SetLengthAsync(string key);

        ///// <summary>
        ///// 获取指定集合中的所有元素
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="key"></param>
        ///// <param name="value"></param>
        ///// <returns></returns>
        //Task<List<T>> SetMembersAsync<T>(string key, T value);

        ///// <summary>
        ///// 从sourceKey移除指定value到目标distKey集合当中
        ///// 如果sourceKey存在指定value则返回true，否则不做任何操作返回false
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="sourcekey"></param>
        ///// <param name="distKey"></param>
        ///// <param name="value"></param>
        ///// <returns></returns>
        //Task<bool> SetMoveAsync<T>(string sourcekey, string distKey, T value);

        ///// <summary>
        ///// 从指定集合当中随机取出一个元素
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="key"></param>
        ///// <returns></returns>
        //Task<T> SetRandomMemberAsync<T>(string key);

        ///// <summary>
        ///// 从指定集合随机弹出（删除并获取）一个元素
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="key"></param>
        ///// <returns></returns>
        //Task<T> SetPopAsync<T>(string key);

        ///// <summary>
        ///// 从集合中随机弹出（删除并获取）多个元素
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="key"></param>
        ///// <returns></returns>
        //Task<List<T>> SetRandomMembersAsync<T>(string key);

        ///// <summary>
        ///// 从集合中移除指定元素
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="key"></param>
        ///// <param name="value"></param>
        ///// <returns></returns>
        //Task<bool> SetRemoveAsync<T>(string key, T value);

        ///// <summary>
        ///// 从集合中批量移除元素
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="key"></param>
        ///// <param name="values"></param>
        ///// <returns></returns>
        //Task<long> SetMultiRemoveAsync<T>(string key, List<T> values);

        #endregion

        #region Redis数据类型—SortSet



        #endregion

        #region Redis Key操作

        /// <summary>
        /// 删除指定key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<bool> KeyDeleteAsync(string key);

        /// <summary>
        /// 设置key过期时间具体DateTime
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expireAt"></param>
        /// <returns></returns>
        //Task<bool> KeyExpireAtAsync(string key, DateTime expireAt);

        /// <summary>
        /// 设置key在将来的timeout后过期（TimeSpan）
        /// </summary>
        /// <param name="key"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        Task<bool> KeyExpireInAsync(string key, TimeSpan timeout);

        /// <summary>
        /// key重命名
        /// </summary>
        /// <param name="key"></param>
        /// <param name="newKey"></param>
        /// <returns></returns>
        Task<bool> KeyRenameAsync(string key, string newKey);

        /// <summary>
        /// 判断key是否已存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<bool> KeyExistsAsync(string key);

        #endregion

        #region Redis Transcation

        /// <summary>
        /// 在事务中执行一系列redis命令。注意：在委托中的一系列命令的所有 值  都需要进行字节数组序列化
        /// </summary>
        /// <param name="ranOperations"></param>
        /// <returns></returns>
        //Task<bool> DoInTranscationAsync(Action<ITransaction> ranOperations);

        #endregion

    }
}
