51 lines
1.8 KiB
C#
51 lines
1.8 KiB
C#
using SqlSugar;
|
|
using YY.Admin.Core;
|
|
using YY.Admin.Core.Const;
|
|
|
|
namespace YY.Admin.Services.Service.Config
|
|
{
|
|
public class SysConfigService : ISysConfigService, ISingletonDependency
|
|
{
|
|
private readonly ISysCacheService _sysCacheService;
|
|
private readonly ISqlSugarClient _dbContext;
|
|
public SysConfigService(
|
|
ISysCacheService sysCacheService,
|
|
ISqlSugarClient dbContext)
|
|
{
|
|
_sysCacheService= sysCacheService;
|
|
_dbContext= dbContext;
|
|
}
|
|
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 _dbContext.Queryable<SysConfig>().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));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<(bool ok, string message)> SetConfigValueAsync(string code, string value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(code))
|
|
return (false, "配置编码无效");
|
|
|
|
var n = await _dbContext.Updateable<SysConfig>()
|
|
.SetColumns(c => new SysConfig { Value = value, UpdateTime = DateTime.Now })
|
|
.Where(c => c.Code == code)
|
|
.ExecuteCommandAsync();
|
|
|
|
if (n <= 0)
|
|
return (false, "未找到对应配置项或无需更新");
|
|
|
|
_sysCacheService.Remove($"{CacheConst.KeyConfig}{code}");
|
|
return (true, "保存成功");
|
|
}
|
|
}
|
|
}
|