365 lines
15 KiB
C#
365 lines
15 KiB
C#
using YLErp.Abstract;
|
|
|
|
namespace YLErp.Modules.SystemModule
|
|
{
|
|
/// <summary>
|
|
/// 系统用户个性化配置服务
|
|
/// </summary>
|
|
public class SysUserConfigService : YLBaseService
|
|
{
|
|
public SysUserConfigService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
|
|
}
|
|
|
|
public SysUserConfigService(YLBaseService baseService) : base(baseService)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前用户个性化配置名称列表
|
|
/// </summary>
|
|
/// <param name="configType">配置类型</param>
|
|
public Dictionary<SysUserConfigType, List<string>> 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<SysUserConfigType, List<string>> 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<SysUserConfigType, List<string>>();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前用户个性化配置名称列表
|
|
/// </summary>
|
|
/// <param name="configType">配置类型</param>
|
|
public List<SysUserConfigDto> 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<SysUserConfigDto>().ToList() : new List<SysUserConfigDto>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前用户个性化配置名称列表
|
|
/// </summary>
|
|
/// <param name="configType">配置类型</param>
|
|
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<SysUserConfigDto>();
|
|
|
|
return sysUserConfigs.FirstOrDefault();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户个性化配置数据
|
|
/// </summary>
|
|
/// <param name="configType">配置类型</param>
|
|
public Dictionary<string, string> 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);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取用户个性化配置数据
|
|
/// </summary>
|
|
/// <param name="configType">配置类型</param>
|
|
public Dictionary<string, string> GetConfigData(SysUserConfigType configType)
|
|
{
|
|
return GetConfigData(UserId, configType);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户个性化配置数据
|
|
/// </summary>
|
|
/// <param name="configType">配置类型</param>
|
|
/// <param name="configName">配置名称</param>
|
|
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();
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户个性化配置数据
|
|
/// </summary>
|
|
/// <param name="configType">配置类型</param>
|
|
/// <param name="configName">配置名称</param>
|
|
public string GetConfigData(SysUserConfigType configType, string configName)
|
|
{
|
|
return GetConfigData(UserId, configType, configName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户个性化配置数据
|
|
/// </summary>
|
|
/// <param name="cacheProvider">缓存提供接口</param>
|
|
public T GetConfigData<T>(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<T>(configData) ?? new T();
|
|
|
|
if (cacheProvider != null)
|
|
{
|
|
cacheProvider.Set(cacheKey, config, TimeSpan.FromMinutes(30));
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户个性化配置数据
|
|
/// </summary>
|
|
/// <param name="cacheProvider">缓存提供接口</param>
|
|
public T GetConfigData<T>(ICacheProvider cacheProvider = null) where T : SysUserConfigModelBase, new()
|
|
{
|
|
return GetConfigData<T>(UserId, cacheProvider);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存用户配置数据
|
|
/// </summary>
|
|
public SysUserConfig SaveData<T>(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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存用户配置数据
|
|
/// </summary>
|
|
public SysUserConfig SaveData<T>(T config, ICacheProvider cacheProvider = null) where T : SysUserConfigModelBase
|
|
{
|
|
return SaveData(UserId, config, cacheProvider);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存用户配置数据
|
|
/// </summary>
|
|
/// <param name="configType">配置类型</param>
|
|
/// <param name="configName">配置名词</param>
|
|
/// <param name="configData">配置数据</param>
|
|
/// <param name="cacheProvider">缓存接口</param>
|
|
/// <param name="enableOverride">是否允许覆盖已有数据</param>
|
|
/// <param name="enableCommtemplate">是否为公共模板/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;
|
|
}
|
|
/// <summary>
|
|
/// 保存当前用户配置数据
|
|
/// </summary>
|
|
/// <param name="configType">配置类型</param>
|
|
/// <param name="configName">配置名词</param>
|
|
/// <param name="configData">配置数据</param>
|
|
/// <param name="cacheProvider">缓存接口</param>
|
|
/// <param name="enableOverride">是否允许覆盖已有数据</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除配置数据
|
|
/// </summary>
|
|
public int RemoveData(int userId, SysUserConfigType configType, IEnumerable<string> 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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 删除配置数据
|
|
/// </summary>
|
|
public int RemoveData(SysUserConfigType configType, IEnumerable<string> configNames, ICacheProvider cacheProvider = null)
|
|
{
|
|
return RemoveData(UserId, configType, configNames, cacheProvider);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否存在用户配置数据
|
|
/// </summary>
|
|
public bool Exists(int userId, SysUserConfigType configType, string configName)
|
|
{
|
|
return DbContext.SysUserConfig.Any(n => n.UserId == userId && n.ConfigType == configType && n.ConfigName == configName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否存在用户配置数据
|
|
/// </summary>
|
|
public bool Exists(SysUserConfigType configType, string configName)
|
|
{
|
|
return Exists(UserId, configType, configName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否存在用户配置数据
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否存在用户配置数据
|
|
/// </summary>
|
|
public bool Exists(SysUserConfigType configType, string configName, string configValue)
|
|
{
|
|
return Exists(UserId, configType, configName, configValue);
|
|
}
|
|
}
|
|
}
|