using Edu.CacheManager.Base;
using Edu.Model.CacheModel;
using Edu.Model.ViewModel.WeChat;
using Edu.Repository.WeChat;
using System;
using System.Linq;

namespace Edu.Cache.WeChat
{
    /// <summary>
    /// redis缓存
    /// </summary>
    public class WeChatReidsCache
    {
        /// <summary>
        /// 使用redis第几号库
        /// </summary>
        public static readonly int REDIS_DB3 = 3;

        static readonly RedisHelper redis = new RedisHelper(REDIS_DB3);

        /// <summary>
        /// 账号仓储层对象
        /// </summary>
        static readonly RB_WeChat_ConfigRepository configRepository = new RB_WeChat_ConfigRepository();

        static readonly RB_WeChat_APPRepository appRepository = new RB_WeChat_APPRepository();



        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="token"></param>
        /// <param name="JwtExpirTime"></param>
        public static void TokenSet(string cacheKey, string token, int JwtExpirTime)
        {
            try
            {
                TimeSpan ts = GetExpirTime(JwtExpirTime);
                redis.StringSet(cacheKey, token, ts);
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "TokenSet缓存设置失败");
            }
        }

        /// <summary>
        /// 获取缓存时长
        /// </summary>
        /// <param name="JwtExpirTime"></param>
        /// <returns></returns>
        private static TimeSpan GetExpirTime(int JwtExpirTime)
        {
            DateTime dt = DateTime.Now;
            DateTime dt2 = DateTime.Now;
            TimeSpan ts = dt.AddSeconds(JwtExpirTime) - dt2;
            return ts;
        }

        /// <summary>
        /// 判断key是否存在
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public static bool Exists(string cacheKey)
        {
            return redis.KeyExists(cacheKey);
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="Data"></param>
        /// <param name="JwtExpirTime"></param>
        public static string GetToken(string cacheKey)
        {
            try
            {
                return redis.StringGet(cacheKey);
            }
            catch (Exception)
            {
                return "";
            }
        }

        #region 企业微信全局配置

        /// <summary>
        /// 设置配置缓存
        /// </summary>
        /// <param name="model"></param>
        public static void SetConfig(WorkChatConfig model, int JwtExpirTime)
        {
            try
            {
                TimeSpan ts = GetExpirTime(JwtExpirTime);
                redis.StringSet<WorkChatConfig>(CacheKey.QYWECHAT_GLOBAL_CONFIG_KEY+"_"+model.Group_Id, model, ts);
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "WorkChatConfig缓存设置失败");
            }
        }

        public static void SetAppConfig(RB_WeChat_APP_ViewModel model,string cacheKey, int JwtExpirTime)
        {
            try
            {
                TimeSpan ts = GetExpirTime(JwtExpirTime);
                redis.StringSet<RB_WeChat_APP_ViewModel>(cacheKey, model, ts);
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "SetAppConfig缓存设置失败");
            }
        }

        /// <summary>
        /// 更新全局配置
        /// </summary>
        /// <param name="id"></param>
        public static void UpdateConfig(int id)
        {
            var model = configRepository.GetEntity(id);
            if (model != null) 
            {
                var chatConfig = new WorkChatConfig
                {
                    Id = model.Id,
                    Group_Id = model.Group_Id,
                    Name = model.Name,
                    WX_CorpId = model.WX_CorpId,
                    Social_Code = model.Social_Code,
                    Employee_Secret = model.Employee_Secret,
                    Contact_Secret = model.Contact_Secret,
                    TelEvent_CallBack = model.TelEvent_CallBack,
                    Token = model.Token,
                    Encoding_Aes_Key = model.Encoding_Aes_Key,
                    EmpEvent_CallBack = model.EmpEvent_CallBack,
                    Enable = model.Enable,
                    DeptEmpEnable = model.DeptEmpEnable,
                    CreateBy = model.CreateBy,
                    CreateTime = model.CreateTime,
                    UpdateBy = model.UpdateBy,
                    UpdateTime = model.UpdateTime
                };
                SetConfig(chatConfig, Common.Config.JwtExpirTime);
            }
        }

        /// <summary>
        /// 获取应用配置信息
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static RB_WeChat_APP_ViewModel GetAppConfig(int groupId, string code)
        {
            RB_WeChat_APP_ViewModel model = null;
            var cropConfig = GetWorkChatConfig(groupId);
            string cacheKey = cropConfig.WX_CorpId + "_" + code;
            try
            {
                model = redis.StringGet<RB_WeChat_APP_ViewModel>(cacheKey);
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "GetAppConfig");
            }
            if (model == null)
            {
                model = appRepository.GetAPPConfigByCodeAndParent(cropConfig.Id, code);

                if (model != null)
                {
                    model.CropId = cropConfig.WX_CorpId;
                    SetAppConfig(model, cacheKey, 60 * 60);
                }
            }
            return model;
        }

        /// <summary>
        /// 获取配置换成
        /// </summary>
        /// <returns></returns>
        public static WorkChatConfig GetWorkChatConfig(int groupId)
        {
            WorkChatConfig chatConfig = null;
            string cacheKey = CacheKey.QYWECHAT_GLOBAL_CONFIG_KEY+"_"+groupId;
            try
            {
                chatConfig = redis.StringGet<WorkChatConfig>(cacheKey);
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "GeWorkChatConfig");
            }
            if (chatConfig == null)
            {
                var model = configRepository.GetList(new Model.ViewModel.WeChat.RB_WeChat_Config_ViewModel() { Group_Id= groupId }).FirstOrDefault();
                if (model != null)
                {
                    chatConfig = new WorkChatConfig
                    {
                        Id = model.Id,
                        Group_Id = model.Group_Id,
                        Name = model.Name,
                        WX_CorpId = model.WX_CorpId,
                        Social_Code = model.Social_Code,
                        Employee_Secret = model.Employee_Secret,
                        Contact_Secret = model.Contact_Secret,
                        TelEvent_CallBack = model.TelEvent_CallBack,
                        Token = model.Token,
                        Encoding_Aes_Key = model.Encoding_Aes_Key,
                        EmpEvent_CallBack = model.EmpEvent_CallBack,
                        Enable = model.Enable,
                        DeptEmpEnable = model.DeptEmpEnable,
                        CreateBy = model.CreateBy,
                        CreateTime = model.CreateTime,
                        UpdateBy = model.UpdateBy,
                        UpdateTime = model.UpdateTime
                    };
                    SetConfig(chatConfig, Common.Config.JwtExpirTime);
                }
            }
            
            return chatConfig;
        }

        #endregion

    }

    /// <summary>
    /// redis缓存【用户获取商城端缓存】
    /// </summary>
    public class WeChatReidsCache2
    {
        /// <summary>
        /// 使用redis第几号库
        /// </summary>
        public static readonly int REDIS_DB2 = 2;

        static readonly RedisHelper redis = new RedisHelper(REDIS_DB2);

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="value"></param>
        public static void Set(string cacheKey, string value)
        {
            try
            {
                TimeSpan ts = DateTime.Now.AddSeconds(7000) - DateTime.Now;
                redis.StringSet(cacheKey, value, ts);
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "WeChatReidsCache2_Set");
            }
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="Data"></param>
        /// <param name="JwtExpirTime"></param>
        public static string GetToken(string cacheKey)
        {
            try
            {
                return redis.StringGet(cacheKey);
            }
            catch (Exception)
            {
                return "";
            }
        }
    }

}