namespace Admin.NET.Core.Service;
///
/// 系统参数配置服务
///
[ApiDescriptionSettings(Order = 440)]
public class SysConfigService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _sysConfigRep;
private readonly SysCacheService _sysCacheService;
public SysConfigService(SqlSugarRepository sysConfigRep,
SysCacheService sysCacheService)
{
_sysConfigRep = sysConfigRep;
_sysCacheService = sysCacheService;
}
///
/// 获取参数配置分页列表
///
///
///
[DisplayName("获取参数配置分页列表")]
public async Task> 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);
}
///
/// 获取参数配置列表
///
///
[DisplayName("获取参数配置列表")]
public async Task> GetList()
{
return await _sysConfigRep.GetListAsync();
}
///
/// 增加参数配置
///
///
///
[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);
await _sysConfigRep.InsertAsync(input.Adapt());
}
///
/// 更新参数配置
///
///
///
[ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("更新参数配置")]
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();
await _sysConfigRep.AsUpdateable(config).IgnoreColumns(true).ExecuteCommandAsync();
_sysCacheService.Remove(config.Code);
}
///
/// 删除参数配置
///
///
///
[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);
_sysCacheService.Remove(config.Code);
}
///
/// 批量删除参数配置
///
///
///
[ApiDescriptionSettings(Name = "BatchDelete"), HttpPost]
[DisplayName("批量删除参数配置")]
public async Task BatchDeleteConfig(List 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);
_sysCacheService.Remove(config.Code);
}
}
///
/// 获取参数配置详情
///
///
///
[DisplayName("获取参数配置详情")]
public async Task GetDetail([FromQuery] ConfigInput input)
{
return await _sysConfigRep.GetFirstAsync(u => u.Id == input.Id);
}
///
/// 获取参数配置值
///
///
///
[ApiDescriptionSettings(false)]
public async Task GetConfigValue(string code)
{
var value = _sysCacheService.Get(code);
if (string.IsNullOrEmpty(value))
{
var config = await _sysConfigRep.GetFirstAsync(u => u.Code == code);
value = config != null ? config.Value : default;
_sysCacheService.Set(code, value);
}
if (string.IsNullOrWhiteSpace(value)) return default;
return (T)Convert.ChangeType(value, typeof(T));
}
///
/// 获取分组列表
///
///
[DisplayName("获取分组列表")]
public async Task> GetGroupList()
{
return await _sysConfigRep.AsQueryable().GroupBy(u => u.GroupCode).Select(u => u.GroupCode).ToListAsync();
}
///
/// 获取 Token 过期时间
///
///
[ApiDescriptionSettings(false)]
public async Task GetTokenExpire()
{
var tokenExpireStr = await GetConfigValue(CommonConst.SysTokenExpire);
_ = int.TryParse(tokenExpireStr, out var tokenExpire);
return tokenExpire == 0 ? 20 : tokenExpire;
}
///
/// 获取 RefreshToken 过期时间
///
///
[ApiDescriptionSettings(false)]
public async Task GetRefreshTokenExpire()
{
var refreshTokenExpireStr = await GetConfigValue(CommonConst.SysRefreshTokenExpire);
_ = int.TryParse(refreshTokenExpireStr, out var refreshTokenExpire);
return refreshTokenExpire == 0 ? 40 : refreshTokenExpire;
}
}