2026-04-28 10:23:58 +08:00
|
|
|
|
using SqlSugar;
|
2026-04-30 15:28:20 +08:00
|
|
|
|
using YY.Admin.Core;
|
|
|
|
|
|
using YY.Admin.Core.Const;
|
2026-05-20 12:29:10 +08:00
|
|
|
|
using YY.Admin.Core.SeedData;
|
2026-04-28 10:23:58 +08:00
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
}
|
2026-04-30 15:28:20 +08:00
|
|
|
|
|
|
|
|
|
|
/// <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)
|
2026-05-20 12:29:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 旧库可能缺少新增配置项,按种子模板补插后再写入
|
|
|
|
|
|
var seed = new SysConfigSeedData().HasData()
|
|
|
|
|
|
.FirstOrDefault(c => string.Equals(c.Code, code, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
if (seed == null)
|
|
|
|
|
|
return (false, "未找到对应配置项或无需更新");
|
|
|
|
|
|
|
|
|
|
|
|
var row = new SysConfig
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = seed.Id,
|
|
|
|
|
|
Name = seed.Name,
|
|
|
|
|
|
Code = seed.Code,
|
|
|
|
|
|
Value = value,
|
|
|
|
|
|
SysFlag = seed.SysFlag,
|
|
|
|
|
|
GroupCode = seed.GroupCode,
|
|
|
|
|
|
OrderNo = seed.OrderNo,
|
|
|
|
|
|
Remark = seed.Remark,
|
|
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
|
|
UpdateTime = DateTime.Now,
|
|
|
|
|
|
};
|
|
|
|
|
|
await _dbContext.Insertable(row).ExecuteCommandAsync();
|
|
|
|
|
|
}
|
2026-04-30 15:28:20 +08:00
|
|
|
|
|
|
|
|
|
|
_sysCacheService.Remove($"{CacheConst.KeyConfig}{code}");
|
|
|
|
|
|
return (true, "保存成功");
|
|
|
|
|
|
}
|
2026-04-28 10:23:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|