using YLErp.Abstract; namespace YLErp.Modules.SystemModule { /// /// 系统用户个性化配置服务 /// public class SysUserConfigService : YLBaseService { public SysUserConfigService(OptUserInfo userInfo) : base(userInfo) { } public SysUserConfigService(YLBaseService baseService) : base(baseService) { } /// /// 获取当前用户个性化配置名称列表 /// /// 配置类型 public Dictionary> GetConfigNames(bool useCommonTemplate = true) { var query = DbContext.SysUserConfig.AsNoTracking() .Where(n => (n.ConfigType == SysUserConfigType.PricingTemplateV2) && n.UserId == UserId);//获取个人模板 if (useCommonTemplate) { query = query.Union(DbContext.SysUserConfig.AsNoTracking().Where(b => b.ConfigType == SysUserConfigType.CommonTemplate));//获取公共模板 } query = query.OrderByDescending(n => n.UpdateTime); Dictionary> result = null; if (query.Any()) { result = query.AsEnumerable().GroupBy(O => O.ConfigType).ToDictionary(K => K.Key, V => V.Select(O => O.ConfigName).ToList()); } else { result = new Dictionary>(); } return result; } /// /// 获取当前用户个性化配置名称列表 /// /// 配置类型 public List GetConfigInfos(bool useCommonTemplate = true) { var sysUserConfigs = DbContext.SysUserConfig.AsNoTracking() .Where(n => (n.ConfigType == SysUserConfigType.PricingTemplateV2) && n.UserId == UserId);//获取个人模板 if (useCommonTemplate) { sysUserConfigs = sysUserConfigs.Union(DbContext.SysUserConfig.AsNoTracking().Where(b => b.ConfigType == SysUserConfigType.CommonTemplate));//获取公共模板 } sysUserConfigs = sysUserConfigs.OrderByDescending(n => n.UpdateTime); return sysUserConfigs.Any() ? sysUserConfigs.ProjectTo().ToList() : new List(); } /// /// 获取当前用户个性化配置名称列表 /// /// 配置类型 public SysUserConfigDto GetConfigInfos(int id) { var sysUserConfigs = DbContext.SysUserConfig.AsNoTracking() .Where(n => (n.ConfigType == SysUserConfigType.PricingTemplateV2 && n.UserId == UserId && n.id == id) || (n.ConfigType == SysUserConfigType.CommonTemplate && n.id == id)) .ProjectTo(); return sysUserConfigs.FirstOrDefault(); } /// /// 获取用户个性化配置数据 /// /// 配置类型 public Dictionary GetConfigData(int userId, SysUserConfigType configType) { var datas = DbContext.SysUserConfig.Where(n => n.UserId == userId && n.ConfigType == configType).Select(n => new { n.ConfigName, n.ConfigData }).ToArray(); return datas.ToDictionary(n => n.ConfigName, m => m.ConfigData); } /// /// 获取用户个性化配置数据 /// /// 配置类型 public Dictionary GetConfigData(SysUserConfigType configType) { return GetConfigData(UserId, configType); } /// /// 获取用户个性化配置数据 /// /// 配置类型 /// 配置名称 public string GetConfigData(int userId, SysUserConfigType configType, string configName) { if (string.IsNullOrWhiteSpace(configName)) { throw new ArgumentException("不能为空", nameof(configName)); } if (configType == SysUserConfigType.CommonTemplate) { return DbContext.SysUserConfig.Where(n => n.ConfigType == configType && n.ConfigName == configName) .Select(n => n.ConfigData).FirstOrDefault(); } else { return DbContext.SysUserConfig.Where(n => n.UserId == userId && n.ConfigType == configType && n.ConfigName == configName) .Select(n => n.ConfigData).FirstOrDefault(); } } /// /// 获取用户个性化配置数据 /// /// 配置类型 /// 配置名称 public string GetConfigData(SysUserConfigType configType, string configName) { return GetConfigData(UserId, configType, configName); } /// /// 获取用户个性化配置数据 /// /// 缓存提供接口 public T GetConfigData(int userId, ICacheProvider cacheProvider = null) where T : SysUserConfigModelBase, new() { var obj = new T(); var configType = obj.GetConfigType(); var configName = obj.GetConfigName(); if (string.IsNullOrWhiteSpace(configName)) { throw new ArgumentException("不能为空", nameof(configName)); } string cacheKey = null; if (cacheProvider != null) { cacheKey = $"{configType}^{configName}^{userId}"; if (cacheProvider.Get(cacheKey) is T userConfig) { return userConfig; } } var configData = DbContext.SysUserConfig.Where(n => n.UserId == userId && n.ConfigType == configType && n.ConfigName == configName) .Select(n => n.ConfigData).FirstOrDefault(); var config = JsonHelper.Deserialize(configData) ?? new T(); if (cacheProvider != null) { cacheProvider.Set(cacheKey, config, TimeSpan.FromMinutes(30)); } return config; } /// /// 获取用户个性化配置数据 /// /// 缓存提供接口 public T GetConfigData(ICacheProvider cacheProvider = null) where T : SysUserConfigModelBase, new() { return GetConfigData(UserId, cacheProvider); } /// /// 保存用户配置数据 /// public SysUserConfig SaveData(int userId, T config, ICacheProvider cacheProvider = null) where T : SysUserConfigModelBase { if (config is null) { throw new ArgumentNullException(nameof(config)); } var configType = config.GetConfigType(); var configName = config.GetConfigName(); var configData = JsonHelper.Serialize(config); return SaveData(userId, configType, configName, configData, cacheProvider); } /// /// 保存用户配置数据 /// public SysUserConfig SaveData(T config, ICacheProvider cacheProvider = null) where T : SysUserConfigModelBase { return SaveData(UserId, config, cacheProvider); } /// /// 保存用户配置数据 /// /// 配置类型 /// 配置名词 /// 配置数据 /// 缓存接口 /// 是否允许覆盖已有数据 /// 是否为公共模板/param> public SysUserConfig SaveData(int userId, SysUserConfigType configType, string configName, string configData, ICacheProvider cacheProvider = null, bool enableOverride = true, bool enableCommtemplate = false) { if (string.IsNullOrWhiteSpace(configName)) { throw new ArgumentException("不能为空", nameof(configName)); } configType = enableCommtemplate ? SysUserConfigType.CommonTemplate : configType;//公共模板类型 if (enableCommtemplate) { var dbmodel = DbContext.SysUserConfig.Where(n => n.ConfigType == configType && n.ConfigName == configName).FirstOrDefault(); return SysUserConfigDataHandle(dbmodel, userId, configType, configName, configData, enableOverride, cacheProvider); } else { var dbmodel = DbContext.SysUserConfig.Where(n => n.ConfigType == configType && n.ConfigName == configName && n.UserId == userId).FirstOrDefault(); return SysUserConfigDataHandle(dbmodel, userId, configType, configName, configData, enableOverride, cacheProvider); } } private SysUserConfig SysUserConfigDataHandle(SysUserConfig dbmodel, int userId, SysUserConfigType configType, string configName, string configData, bool enableOverride, ICacheProvider cacheProvider) { if (dbmodel == null) { dbmodel = new SysUserConfig { UserId = userId,//公共模板 SysUserConfigType.CommonTemplate ConfigType = configType, ConfigName = configName, CreateTime = DateTime.Now, }; DbContext.SysUserConfig.Add(dbmodel); } else if (enableOverride) { dbmodel.UserId = userId;//公共模板 SysUserConfigType.CommonTemplate dbmodel.ConfigType = configType; dbmodel.ConfigName = configName; dbmodel.CreateTime = DateTime.Now; } else { throw new ServiceException("不允许覆盖"); } dbmodel.ConfigData = configData; dbmodel.UpdateTime = DateTime.Now; var changes = DbContext.SaveChanges(); //设置缓存 if (cacheProvider != null) { var cacheKey = $"{configType}^{configName}^{userId}"; cacheProvider.Set(cacheKey, dbmodel, TimeSpan.FromMinutes(30)); } return dbmodel; } /// /// 保存当前用户配置数据 /// /// 配置类型 /// 配置名词 /// 配置数据 /// 缓存接口 /// 是否允许覆盖已有数据 public SysUserConfig SaveData(SysUserConfigType configType, string configName, string configData, ICacheProvider cacheProvider = null, bool enableOverride = true, bool enableCommtemplate = false) { return SaveData(UserId, configType, configName, configData, cacheProvider, enableOverride, enableCommtemplate); } /// /// 删除配置数据 /// public int RemoveData(int userId, SysUserConfigType configType, IEnumerable configNames, ICacheProvider cacheProvider = null) { if (configNames == null || !configNames.Any(n => !string.IsNullOrEmpty(n))) { return -1; } configNames = configNames.Where(n => !string.IsNullOrEmpty(n)).ToArray(); var dbmodels = DbContext.SysUserConfig.Where(n => n.UserId == userId && n.ConfigType == configType && configNames.Contains(n.ConfigName)).ToArray(); DbContext.SysUserConfig.RemoveRange(dbmodels); var changes = DbContext.SaveChanges(); //设置缓存 if (cacheProvider != null) { foreach (var name in configNames) { cacheProvider.Remove($"{configType}^{name}^{userId}"); } } return changes; } public int RemoveData(int sysUserConfigId, SysUserConfigType configType, ICacheProvider cacheProvider = null) { var sysUserConfig = DbContext.SysUserConfig.Where(a => a.id == sysUserConfigId).FirstOrDefault(); if (sysUserConfig != null) { DbContext.SysUserConfig.Remove(sysUserConfig); if (cacheProvider != null) { cacheProvider.Remove($"{configType}^{sysUserConfig.ConfigName}^{sysUserConfig.UserId}"); } } var changes = DbContext.SaveChanges(); return changes; } /// /// 删除配置数据 /// public int RemoveData(SysUserConfigType configType, IEnumerable configNames, ICacheProvider cacheProvider = null) { return RemoveData(UserId, configType, configNames, cacheProvider); } /// /// 是否存在用户配置数据 /// public bool Exists(int userId, SysUserConfigType configType, string configName) { return DbContext.SysUserConfig.Any(n => n.UserId == userId && n.ConfigType == configType && n.ConfigName == configName); } /// /// 是否存在用户配置数据 /// public bool Exists(SysUserConfigType configType, string configName) { return Exists(UserId, configType, configName); } /// /// 是否存在用户配置数据 /// public bool Exists(int userId, SysUserConfigType configType, string configName, string configValue) { return DbContext.SysUserConfig.Any(n => n.UserId == userId && n.ConfigType == configType && n.ConfigName == configName && n.ConfigData == configValue); } /// /// 是否存在用户配置数据 /// public bool Exists(SysUserConfigType configType, string configName, string configValue) { return Exists(UserId, configType, configName, configValue); } } }