feat: initialize Evergreen Health backend
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core.Service;
|
||||
|
||||
public class ConfigInput : BaseIdInput
|
||||
{
|
||||
}
|
||||
|
||||
public class PageConfigInput : BasePageInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 编码
|
||||
/// </summary>
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 分组编码
|
||||
/// </summary>
|
||||
public string GroupCode { get; set; }
|
||||
}
|
||||
|
||||
public class AddConfigInput : SysConfig
|
||||
{
|
||||
}
|
||||
|
||||
public class UpdateConfigInput : AddConfigInput
|
||||
{
|
||||
}
|
||||
|
||||
public class DeleteConfigInput : BaseIdInput
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量配置参数输入
|
||||
/// </summary>
|
||||
public class BatchConfigInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 编码
|
||||
/// </summary>
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 属性值
|
||||
/// </summary>
|
||||
public string Value { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core.Service;
|
||||
|
||||
/// <summary>
|
||||
/// 系统信息保存输入参数
|
||||
/// </summary>
|
||||
public class InfoSaveInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 系统图标(Data URI scheme base64 编码)
|
||||
/// </summary>
|
||||
public string LogoBase64 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 系统图标文件名
|
||||
/// </summary>
|
||||
public string LogoFileName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 水印内容
|
||||
/// </summary>
|
||||
public string Watermark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 系统主标题
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "系统主标题不能为空")]
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 系统副标题
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "系统副标题不能为空")]
|
||||
public string ViceTitle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 系统描述
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "系统描述不能为空")]
|
||||
public string ViceDesc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版权说明
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "版权说明不能为空")]
|
||||
public string Copyright { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// ICP备案号
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "ICP备案号不能为空")]
|
||||
public string Icp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// ICP地址
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "ICP地址不能为空")]
|
||||
public string IcpUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 启用注册功能
|
||||
/// </summary>
|
||||
public YesNoEnum EnableReg { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 登录二次验证
|
||||
/// </summary>
|
||||
public YesNoEnum SecondVer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 图形验证码
|
||||
/// </summary>
|
||||
public YesNoEnum Captcha { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 默认注册方案Id
|
||||
/// </summary>
|
||||
public virtual long RegWayId { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,307 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
using NewLife.Reflection;
|
||||
|
||||
namespace Admin.NET.Core.Service;
|
||||
|
||||
/// <summary>
|
||||
/// 平台参数配置服务 🧩
|
||||
/// </summary>
|
||||
[ApiDescriptionSettings(Order = 440)]
|
||||
public class SysConfigService : IDynamicApiController, ITransient
|
||||
{
|
||||
private static readonly SysTenantService SysTenantService = App.GetService<SysTenantService>();
|
||||
private readonly SqlSugarRepository<SysConfig> _sysConfigRep;
|
||||
private readonly SqlSugarRepository<SysTenant> _sysTenantRep;
|
||||
private readonly SysCacheService _sysCacheService;
|
||||
private readonly UserManager _userManager;
|
||||
|
||||
public SysConfigService(
|
||||
SqlSugarRepository<SysTenant> sysTenantRep,
|
||||
SqlSugarRepository<SysConfig> sysConfigRep,
|
||||
SysCacheService sysCacheService,
|
||||
UserManager userManager)
|
||||
{
|
||||
_sysCacheService = sysCacheService;
|
||||
_sysConfigRep = sysConfigRep;
|
||||
_sysTenantRep = sysTenantRep;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取参数配置分页列表 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取参数配置分页列表")]
|
||||
public async Task<SqlSugarPagedList<SysConfig>> Page(PageConfigInput input)
|
||||
{
|
||||
return await _sysConfigRep.AsQueryable()
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Name?.Trim()), u => u.Name.Contains(input.Name))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Code?.Trim()), u => u.Code.Contains(input.Code))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupCode?.Trim()), u => u.GroupCode.Equals(input.GroupCode))
|
||||
.OrderBuilder(input)
|
||||
.ToPagedListAsync(input.Page, input.PageSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取参数配置列表 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取参数配置列表")]
|
||||
public async Task<List<SysConfig>> List(PageConfigInput input)
|
||||
{
|
||||
return await _sysConfigRep.AsQueryable()
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupCode?.Trim()), u => u.GroupCode.Equals(input.GroupCode))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加参数配置 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Add"), HttpPost]
|
||||
[DisplayName("增加参数配置")]
|
||||
public async Task AddConfig(AddConfigInput input)
|
||||
{
|
||||
if (input.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3010);
|
||||
|
||||
var isExist = await _sysConfigRep.IsAnyAsync(u => u.Name == input.Name || u.Code == input.Code);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D9000);
|
||||
|
||||
await _sysConfigRep.InsertAsync(input.Adapt<SysConfig>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新参数配置 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Update"), HttpPost]
|
||||
[DisplayName("更新参数配置")]
|
||||
public async Task UpdateConfig(UpdateConfigInput input)
|
||||
{
|
||||
if (input.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3010);
|
||||
|
||||
var isExist = await _sysConfigRep.IsAnyAsync(u => (u.Name == input.Name || u.Code == input.Code) && u.Id != input.Id);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D9000);
|
||||
|
||||
var config = input.Adapt<SysConfig>();
|
||||
await _sysConfigRep.AsUpdateable(config).IgnoreColumns(true).ExecuteCommandAsync();
|
||||
|
||||
Remove(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除参数配置 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Delete"), HttpPost]
|
||||
[DisplayName("删除参数配置")]
|
||||
public async Task DeleteConfig(DeleteConfigInput input)
|
||||
{
|
||||
var config = await _sysConfigRep.GetFirstAsync(u => u.Id == input.Id);
|
||||
|
||||
// 禁止删除系统参数
|
||||
if (config.SysFlag == YesNoEnum.Y) throw Oops.Oh(ErrorCodeEnum.D9001);
|
||||
|
||||
await _sysConfigRep.DeleteAsync(config);
|
||||
|
||||
Remove(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除参数配置 🔖
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "BatchDelete"), HttpPost]
|
||||
[DisplayName("批量删除参数配置")]
|
||||
public async Task BatchDeleteConfig(List<long> ids)
|
||||
{
|
||||
foreach (var id in ids)
|
||||
{
|
||||
var config = await _sysConfigRep.GetFirstAsync(u => u.Id == id);
|
||||
|
||||
// 禁止删除系统参数
|
||||
if (config.SysFlag == YesNoEnum.Y) continue;
|
||||
|
||||
await _sysConfigRep.DeleteAsync(config);
|
||||
|
||||
Remove(config);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取参数配置详情 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取参数配置详情")]
|
||||
public async Task<SysConfig> GetDetail([FromQuery] ConfigInput input)
|
||||
{
|
||||
return await _sysConfigRep.GetFirstAsync(u => u.Id == input.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取参数配置值
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<T> GetConfigValue<T>(string code)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(code)) return default;
|
||||
|
||||
var value = _sysCacheService.Get<string>($"{CacheConst.KeyConfig}{code}");
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
value = (await _sysConfigRep.AsQueryable().FirstAsync(u => u.Code == code))?.Value;
|
||||
_sysCacheService.Set($"{CacheConst.KeyConfig}{code}", value);
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(value)) return default;
|
||||
return (T)Convert.ChangeType(value, typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新参数配置值
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task UpdateConfigValue(string code, string value)
|
||||
{
|
||||
var config = await _sysConfigRep.AsQueryable().FirstAsync(u => u.Code == code);
|
||||
if (config == null) return;
|
||||
|
||||
config.Value = value;
|
||||
await _sysConfigRep.AsUpdateable(config).ExecuteCommandAsync();
|
||||
|
||||
Remove(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取分组列表 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取分组列表")]
|
||||
public async Task<List<string>> GetGroupList()
|
||||
{
|
||||
return await _sysConfigRep.AsQueryable()
|
||||
.GroupBy(u => u.GroupCode)
|
||||
.Select(u => u.GroupCode).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 Token 过期时间
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<int> GetTokenExpire()
|
||||
{
|
||||
var tokenExpireStr = await GetConfigValue<string>(ConfigConst.SysTokenExpire);
|
||||
_ = int.TryParse(tokenExpireStr, out var tokenExpire);
|
||||
return tokenExpire == 0 ? 20 : tokenExpire;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 RefreshToken 过期时间
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<int> GetRefreshTokenExpire()
|
||||
{
|
||||
var refreshTokenExpireStr = await GetConfigValue<string>(ConfigConst.SysRefreshTokenExpire);
|
||||
_ = int.TryParse(refreshTokenExpireStr, out var refreshTokenExpire);
|
||||
return refreshTokenExpire == 0 ? 40 : refreshTokenExpire;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新参数配置值
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "BatchUpdate"), HttpPost]
|
||||
[DisplayName("批量更新参数配置值")]
|
||||
public async Task BatchUpdateConfig(List<BatchConfigInput> input)
|
||||
{
|
||||
foreach (var config in input)
|
||||
{
|
||||
var info = await _sysConfigRep.AsQueryable().FirstAsync(c => c.Code == config.Code);
|
||||
if (info == null || info.SysFlag == YesNoEnum.Y) continue;
|
||||
|
||||
await _sysConfigRep.AsUpdateable().SetColumns(u => u.Value == config.Value).Where(u => u.Code == config.Code).ExecuteCommandAsync();
|
||||
Remove(info);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取系统信息 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[SuppressMonitor]
|
||||
[AllowAnonymous]
|
||||
[DisplayName("获取系统信息")]
|
||||
public async Task<dynamic> GetSysInfo()
|
||||
{
|
||||
var tenant = await SysTenantService.GetCurrentTenantSysInfo();
|
||||
var wayList = await _sysConfigRep.Context.Queryable<SysUserRegWay>()
|
||||
.Where(u => u.TenantId == tenant.Id)
|
||||
.Select(u => new { Label = u.Name, Value = u.Id })
|
||||
.ToListAsync();
|
||||
|
||||
var captcha = await GetConfigValue<bool>(ConfigConst.SysCaptcha);
|
||||
var secondVer = await GetConfigValue<bool>(ConfigConst.SysSecondVer);
|
||||
var hideTenantForLogin = await GetConfigValue<bool>(ConfigConst.SysHideTenantLogin);
|
||||
return new
|
||||
{
|
||||
tenant.Logo,
|
||||
tenant.Title,
|
||||
tenant.ViceTitle,
|
||||
tenant.ViceDesc,
|
||||
tenant.Watermark,
|
||||
tenant.Copyright,
|
||||
tenant.Icp,
|
||||
tenant.IcpUrl,
|
||||
tenant.RegWayId,
|
||||
tenant.EnableReg,
|
||||
SecondVer = secondVer ? YesNoEnum.Y : YesNoEnum.N,
|
||||
Captcha = captcha ? YesNoEnum.Y : YesNoEnum.N,
|
||||
HideTenantForLogin = hideTenantForLogin,
|
||||
WayList = wayList
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存系统信息 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[UnitOfWork]
|
||||
[DisplayName("保存系统信息")]
|
||||
public async Task SaveSysInfo(InfoSaveInput input)
|
||||
{
|
||||
var tenant = await _sysTenantRep.GetFirstAsync(u => u.Id == _userManager.TenantId) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
|
||||
if (!string.IsNullOrEmpty(input.LogoBase64)) SysTenantService.SetLogoUrl(tenant, input.LogoBase64, input.LogoFileName);
|
||||
// await UpdateConfigValue(ConfigConst.SysCaptcha, (input.Captcha == YesNoEnum.Y).ToString());
|
||||
// await UpdateConfigValue(ConfigConst.SysSecondVer, (input.SecondVer == YesNoEnum.Y).ToString());
|
||||
|
||||
tenant.Copy(input);
|
||||
tenant.RegWayId = input.EnableReg == YesNoEnum.Y ? input.RegWayId : null;
|
||||
await _sysTenantRep.Context.Updateable(tenant).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
private void Remove(SysConfig config)
|
||||
{
|
||||
_sysCacheService.Remove($"{CacheConst.KeyConfig}Value:{config.Code}");
|
||||
_sysCacheService.Remove($"{CacheConst.KeyConfig}Remark:{config.Code}");
|
||||
_sysCacheService.Remove($"{CacheConst.KeyConfig}{config.GroupCode}:GroupWithCache");
|
||||
_sysCacheService.Remove($"{CacheConst.KeyConfig}{config.Code}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core.Service;
|
||||
|
||||
/// <summary>
|
||||
/// 系统租户配置参数服务 🧩
|
||||
/// </summary>
|
||||
[ApiDescriptionSettings(Order = 440)]
|
||||
public class SysTenantConfigService : IDynamicApiController, ITransient
|
||||
{
|
||||
private readonly SysCacheService _sysCacheService;
|
||||
private readonly SqlSugarRepository<SysTenantConfig> _sysConfigRep;
|
||||
private readonly SqlSugarRepository<SysTenantConfigData> _sysConfigDataRep;
|
||||
public readonly ISugarQueryable<SysConfig> VSysConfig;
|
||||
private readonly UserManager _userManager;
|
||||
|
||||
public SysTenantConfigService(SysCacheService sysCacheService,
|
||||
SqlSugarRepository<SysTenantConfig> sysConfigRep,
|
||||
SqlSugarRepository<SysTenantConfigData> sysConfigDataRep,
|
||||
UserManager userManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_sysCacheService = sysCacheService;
|
||||
_sysConfigRep = sysConfigRep;
|
||||
_sysConfigDataRep = sysConfigDataRep;
|
||||
VSysConfig = _sysConfigRep.AsQueryable()
|
||||
.InnerJoin(
|
||||
_sysConfigDataRep.AsQueryable().WhereIF(!_userManager.SuperAdmin, cv => cv.TenantId == _userManager.TenantId),
|
||||
(c, cv) => c.Id == cv.ConfigId
|
||||
)
|
||||
//.Select<SysConfig>().MergeTable();
|
||||
// 解决PostgreSQL下并启用驼峰转下划线时,报字段不存在,SqlSugar在Select后生成的sql, as后没转下划线导致.
|
||||
.SelectMergeTable((c, cv) => new SysConfig { Id = c.Id.SelectAll(), Value = cv.Value });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数分页列表 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取配置参数分页列表")]
|
||||
public async Task<SqlSugarPagedList<SysConfig>> Page(PageConfigInput input)
|
||||
{
|
||||
return await VSysConfig
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Name?.Trim()), u => u.Name.Contains(input.Name))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Code?.Trim()), u => u.Code.Contains(input.Code))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupCode?.Trim()), u => u.GroupCode.Equals(input.GroupCode))
|
||||
.OrderBuilder(input)
|
||||
.ToPagedListAsync(input.Page, input.PageSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数列表 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取配置参数列表")]
|
||||
public async Task<List<SysConfig>> List(PageConfigInput input)
|
||||
{
|
||||
return await VSysConfig
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupCode?.Trim()), u => u.GroupCode.Equals(input.GroupCode))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Add"), HttpPost]
|
||||
[DisplayName("增加配置参数")]
|
||||
public async Task AddConfig(AddConfigInput input)
|
||||
{
|
||||
var isExist = await _sysConfigRep.IsAnyAsync(u => u.Name == input.Name || u.Code == input.Code);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D9000);
|
||||
|
||||
var configId = _sysConfigRep.InsertReturnSnowflakeId(input.Adapt<SysTenantConfig>());
|
||||
await _sysConfigDataRep.InsertAsync(new SysTenantConfigData()
|
||||
{
|
||||
ConfigId = configId,
|
||||
Value = input.Value
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Update"), HttpPost]
|
||||
[DisplayName("更新配置参数")]
|
||||
[UnitOfWork]
|
||||
public async Task UpdateConfig(UpdateConfigInput input)
|
||||
{
|
||||
var isExist = await _sysConfigRep.IsAnyAsync(u => (u.Name == input.Name || u.Code == input.Code) && u.Id != input.Id);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D9000);
|
||||
|
||||
var config = input.Adapt<SysTenantConfig>();
|
||||
await _sysConfigRep.AsUpdateable(config).IgnoreColumns(true).ExecuteCommandAsync();
|
||||
var configData = await _sysConfigDataRep.GetFirstAsync(cv => cv.ConfigId == input.Id);
|
||||
if (configData == null)
|
||||
await _sysConfigDataRep.AsInsertable(new SysTenantConfigData() { ConfigId = input.Id, Value = input.Value }).ExecuteCommandAsync();
|
||||
else
|
||||
{
|
||||
configData.Value = input.Value;
|
||||
await _sysConfigDataRep.AsUpdateable(configData).IgnoreColumns(true).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
RemoveConfigCache(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Delete"), HttpPost]
|
||||
[DisplayName("删除配置参数")]
|
||||
public async Task DeleteConfig(DeleteConfigInput input)
|
||||
{
|
||||
var config = await _sysConfigRep.GetByIdAsync(input.Id);
|
||||
// 禁止删除系统参数
|
||||
if (config.SysFlag == YesNoEnum.Y) throw Oops.Oh(ErrorCodeEnum.D9001);
|
||||
|
||||
await _sysConfigRep.DeleteAsync(config);
|
||||
await _sysConfigDataRep.DeleteAsync(it => it.TenantId == _userManager.TenantId && it.ConfigId == config.Id);
|
||||
|
||||
RemoveConfigCache(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "BatchDelete"), HttpPost]
|
||||
[DisplayName("批量删除配置参数")]
|
||||
public async Task BatchDeleteConfig(List<long> ids)
|
||||
{
|
||||
foreach (var id in ids)
|
||||
{
|
||||
var config = await _sysConfigRep.GetByIdAsync(id);
|
||||
// 禁止删除系统参数
|
||||
if (config.SysFlag == YesNoEnum.Y) continue;
|
||||
|
||||
await _sysConfigRep.DeleteAsync(config);
|
||||
await _sysConfigDataRep.DeleteAsync(it => it.TenantId == _userManager.TenantId && it.ConfigId == config.Id);
|
||||
|
||||
RemoveConfigCache(config);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数详情 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取配置参数详情")]
|
||||
public async Task<SysConfig> GetDetail([FromQuery] ConfigInput input)
|
||||
{
|
||||
return await VSysConfig.FirstAsync(u => u.Id == input.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据Code获取配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<SysConfig> GetConfig(string code)
|
||||
{
|
||||
return await VSysConfig.FirstAsync(u => u.Code == code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据Code获取配置参数值 🔖
|
||||
/// </summary>
|
||||
/// <param name="code">编码</param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("根据Code获取配置参数值")]
|
||||
public async Task<string> GetConfigValueByCode(string code)
|
||||
{
|
||||
return await GetConfigValueByCode<string>(code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数值
|
||||
/// </summary>
|
||||
/// <param name="code">编码</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<string> GetConfigValueByCode(string code, string defaultValue = default)
|
||||
{
|
||||
return await GetConfigValueByCode<string>(code, defaultValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数值
|
||||
/// </summary>
|
||||
/// <typeparam name="T">类型</typeparam>
|
||||
/// <param name="code">编码</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<T> GetConfigValueByCode<T>(string code, T defaultValue = default)
|
||||
{
|
||||
return await GetConfigValueByCode<T>(code, _userManager.TenantId, defaultValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数值
|
||||
/// </summary>
|
||||
/// <typeparam name="T">类型</typeparam>
|
||||
/// <param name="code">编码</param>
|
||||
/// <param name="tenantId">租户Id</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<T> GetConfigValueByCode<T>(string code, long tenantId, T defaultValue = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(code)) return defaultValue;
|
||||
|
||||
var value = _sysCacheService.Get<string>($"{CacheConst.KeyTenantConfig}{tenantId}{code}");
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
value = (await VSysConfig.FirstAsync(u => u.Code == code))?.Value;
|
||||
_sysCacheService.Set($"{CacheConst.KeyTenantConfig}{tenantId}{code}", value);
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(value)) return defaultValue;
|
||||
return (T)Convert.ChangeType(value, typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新配置参数值
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task UpdateConfigValue(string code, string value)
|
||||
{
|
||||
var config = await _sysConfigRep.GetFirstAsync(u => u.Code == code);
|
||||
if (config == null) return;
|
||||
|
||||
await _sysConfigDataRep.AsUpdateable().SetColumns(it => it.Value == value).Where(it => it.TenantId == _userManager.TenantId && it.ConfigId == config.Id).ExecuteCommandAsync();
|
||||
|
||||
RemoveConfigCache(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取分组列表 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取分组列表")]
|
||||
public async Task<List<string>> GetGroupList()
|
||||
{
|
||||
return await VSysConfig
|
||||
.GroupBy(u => u.GroupCode)
|
||||
.Select(u => u.GroupCode).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新配置参数值 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "BatchUpdate"), HttpPost]
|
||||
[DisplayName("批量更新配置参数值")]
|
||||
public async Task BatchUpdateConfig(List<BatchConfigInput> input)
|
||||
{
|
||||
foreach (var config in input)
|
||||
{
|
||||
await UpdateConfigValue(config.Code, config.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除配置缓存
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
private void RemoveConfigCache(SysTenantConfig config)
|
||||
{
|
||||
_sysCacheService.Remove($"{CacheConst.KeyTenantConfig}{_userManager.TenantId}{config.Code}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core.Service;
|
||||
|
||||
/// <summary>
|
||||
/// 系统用户配置参数服务 🧩
|
||||
/// </summary>
|
||||
[ApiDescriptionSettings(Order = 440)]
|
||||
public class SysUserConfigService : IDynamicApiController, ITransient
|
||||
{
|
||||
private readonly SysCacheService _sysCacheService;
|
||||
private readonly SqlSugarRepository<SysUserConfig> _sysConfigRep;
|
||||
private readonly SqlSugarRepository<SysUserConfigData> _sysConfigDataRep;
|
||||
public readonly ISugarQueryable<SysConfig> VSysConfig;
|
||||
private readonly UserManager _userManager;
|
||||
|
||||
public SysUserConfigService(SysCacheService sysCacheService,
|
||||
SqlSugarRepository<SysUserConfig> sysConfigRep,
|
||||
SqlSugarRepository<SysUserConfigData> sysConfigDataRep,
|
||||
UserManager userManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_sysCacheService = sysCacheService;
|
||||
_sysConfigRep = sysConfigRep;
|
||||
_sysConfigDataRep = sysConfigDataRep;
|
||||
VSysConfig = _sysConfigRep.AsQueryable()
|
||||
.LeftJoin(
|
||||
_sysConfigDataRep.AsQueryable().WhereIF(_userManager.SuperAdmin, cv => cv.UserId == _userManager.UserId),
|
||||
(c, cv) => c.Id == cv.ConfigId
|
||||
)
|
||||
//.Select<SysConfig>().MergeTable();
|
||||
// 解决PostgreSQL下并启用驼峰转下划线时,报字段不存在,SqlSugar在Select后生成的sql, as后没转下划线导致.
|
||||
.SelectMergeTable((c, cv) => new SysConfig { Id = c.Id.SelectAll(), Value = cv.Value });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数分页列表 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取配置参数分页列表")]
|
||||
public async Task<SqlSugarPagedList<SysConfig>> Page(PageConfigInput input)
|
||||
{
|
||||
return await VSysConfig
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Name?.Trim()), u => u.Name.Contains(input.Name))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Code?.Trim()), u => u.Code.Contains(input.Code))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupCode?.Trim()), u => u.GroupCode.Equals(input.GroupCode))
|
||||
.OrderBuilder(input)
|
||||
.ToPagedListAsync(input.Page, input.PageSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数列表 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取配置参数列表")]
|
||||
public async Task<List<SysConfig>> List(PageConfigInput input)
|
||||
{
|
||||
return await VSysConfig
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupCode?.Trim()), u => u.GroupCode.Equals(input.GroupCode))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Add"), HttpPost]
|
||||
[DisplayName("增加配置参数")]
|
||||
public async Task AddConfig(AddConfigInput input)
|
||||
{
|
||||
var isExist = await _sysConfigRep.IsAnyAsync(u => u.Name == input.Name || u.Code == input.Code);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D9000);
|
||||
|
||||
var configId = _sysConfigRep.InsertReturnSnowflakeId(input.Adapt<SysUserConfig>());
|
||||
await _sysConfigDataRep.InsertAsync(new SysUserConfigData()
|
||||
{
|
||||
UserId = _userManager.UserId,
|
||||
ConfigId = configId,
|
||||
Value = input.Value
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Update"), HttpPost]
|
||||
[DisplayName("更新配置参数")]
|
||||
[UnitOfWork]
|
||||
public async Task UpdateConfig(UpdateConfigInput input)
|
||||
{
|
||||
var isExist = await _sysConfigRep.IsAnyAsync(u => (u.Name == input.Name || u.Code == input.Code) && u.Id != input.Id);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D9000);
|
||||
|
||||
var config = input.Adapt<SysUserConfig>();
|
||||
await _sysConfigRep.AsUpdateable(config).IgnoreColumns(true).ExecuteCommandAsync();
|
||||
var configData = await _sysConfigDataRep.GetFirstAsync(cv => cv.ConfigId == input.Id);
|
||||
if (configData == null)
|
||||
await _sysConfigDataRep.AsInsertable(new SysUserConfigData() { UserId = _userManager.UserId, ConfigId = input.Id, Value = input.Value }).ExecuteCommandAsync();
|
||||
else
|
||||
{
|
||||
configData.Value = input.Value;
|
||||
await _sysConfigDataRep.AsUpdateable(configData).IgnoreColumns(true).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
RemoveConfigCache(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "Delete"), HttpPost]
|
||||
[DisplayName("删除配置参数")]
|
||||
public async Task DeleteConfig(DeleteConfigInput input)
|
||||
{
|
||||
var config = await _sysConfigRep.GetByIdAsync(input.Id);
|
||||
// 禁止删除系统参数
|
||||
if (config.SysFlag == YesNoEnum.Y) throw Oops.Oh(ErrorCodeEnum.D9001);
|
||||
|
||||
await _sysConfigRep.DeleteAsync(config);
|
||||
await _sysConfigDataRep.DeleteAsync(it => it.UserId == _userManager.UserId && it.ConfigId == config.Id);
|
||||
|
||||
RemoveConfigCache(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "BatchDelete"), HttpPost]
|
||||
[DisplayName("批量删除配置参数")]
|
||||
public async Task BatchDeleteConfig(List<long> ids)
|
||||
{
|
||||
foreach (var id in ids)
|
||||
{
|
||||
var config = await _sysConfigRep.GetByIdAsync(id);
|
||||
// 禁止删除系统参数
|
||||
if (config.SysFlag == YesNoEnum.Y) continue;
|
||||
|
||||
await _sysConfigRep.DeleteAsync(config);
|
||||
await _sysConfigDataRep.DeleteAsync(it => it.UserId == _userManager.UserId && it.ConfigId == config.Id);
|
||||
|
||||
RemoveConfigCache(config);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数详情 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取配置参数详情")]
|
||||
public async Task<SysConfig> GetDetail([FromQuery] ConfigInput input)
|
||||
{
|
||||
return await VSysConfig.FirstAsync(u => u.Id == input.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据Code获取配置参数 🔖
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<SysConfig> GetConfig(string code)
|
||||
{
|
||||
return await VSysConfig.FirstAsync(u => u.Code == code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据Code获取配置参数值 🔖
|
||||
/// </summary>
|
||||
/// <param name="code">编码</param>
|
||||
/// <returns></returns>
|
||||
[DisplayName("根据Code获取配置参数值")]
|
||||
public async Task<string> GetConfigValueByCode(string code)
|
||||
{
|
||||
return await GetConfigValueByCode<string>(code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数值
|
||||
/// </summary>
|
||||
/// <param name="code">编码</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<string> GetConfigValueByCode(string code, string defaultValue = default)
|
||||
{
|
||||
return await GetConfigValueByCode<string>(code, defaultValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数值
|
||||
/// </summary>
|
||||
/// <typeparam name="T">类型</typeparam>
|
||||
/// <param name="code">编码</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<T> GetConfigValueByCode<T>(string code, T defaultValue = default)
|
||||
{
|
||||
return await GetConfigValueByCode<T>(code, _userManager.UserId, defaultValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置参数值
|
||||
/// </summary>
|
||||
/// <typeparam name="T">类型</typeparam>
|
||||
/// <param name="code">编码</param>
|
||||
/// <param name="userId">用户Id</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<T> GetConfigValueByCode<T>(string code, long userId, T defaultValue = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(code)) return defaultValue;
|
||||
|
||||
var value = _sysCacheService.Get<string>($"{CacheConst.KeyUserConfig}{userId}{code}");
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
value = (await VSysConfig.FirstAsync(u => u.Code == code))?.Value;
|
||||
_sysCacheService.Set($"{CacheConst.KeyUserConfig}{userId}{code}", value);
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(value)) return defaultValue;
|
||||
return (T)Convert.ChangeType(value, typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新配置参数值
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task UpdateConfigValue(string code, string value)
|
||||
{
|
||||
var config = await _sysConfigRep.GetFirstAsync(u => u.Code == code);
|
||||
if (config == null) return;
|
||||
|
||||
await _sysConfigDataRep.AsUpdateable().SetColumns(it => it.Value == value).Where(it => it.UserId == _userManager.UserId && it.ConfigId == config.Id).ExecuteCommandAsync();
|
||||
|
||||
RemoveConfigCache(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取分组列表 🔖
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("获取分组列表")]
|
||||
public async Task<List<string>> GetGroupList()
|
||||
{
|
||||
return await _sysConfigRep.AsQueryable()
|
||||
.GroupBy(u => u.GroupCode)
|
||||
.Select(u => u.GroupCode).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新配置参数值 🔖
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[ApiDescriptionSettings(Name = "BatchUpdate"), HttpPost]
|
||||
[DisplayName("批量更新配置参数值")]
|
||||
public async Task BatchUpdateConfig(List<BatchConfigInput> input)
|
||||
{
|
||||
foreach (var config in input)
|
||||
{
|
||||
await UpdateConfigValue(config.Code, config.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除配置缓存
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
private void RemoveConfigCache(SysUserConfig config)
|
||||
{
|
||||
_sysCacheService.Remove($"{CacheConst.KeyUserConfig}{_userManager.UserId}{config.Code}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user