提交代码
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using HardwareControl.Core.SystemSetting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace HardwareControl.Core.HardwareServiceImpl
|
||||
{
|
||||
/// <summary>
|
||||
/// 系统参数配置服务实现。负责入参校验与日志,持久化委托给 <see cref="SystemConfigRepository"/>。
|
||||
/// </summary>
|
||||
public sealed class SystemConfigService : ISystemConfigService
|
||||
{
|
||||
private readonly SystemConfigRepository _repository;
|
||||
private readonly ILogger<SystemConfigService> _logger;
|
||||
|
||||
public SystemConfigService(
|
||||
SystemConfigRepository repository,
|
||||
ILogger<SystemConfigService> logger)
|
||||
{
|
||||
_repository = repository;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public IReadOnlyList<SystemConfig> GetAll(string? keyword = null) =>
|
||||
_repository.GetAll(keyword);
|
||||
|
||||
public SystemConfig? GetById(int id) => _repository.FindById(id);
|
||||
|
||||
public SystemConfig Create(SystemConfig config)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(config);
|
||||
Normalize(config);
|
||||
|
||||
var created = _repository.Insert(config);
|
||||
_logger.LogInformation(
|
||||
"新增系统参数:{ConfigClass}/{ConfigName}(Id={ConfigId})",
|
||||
created.ConfigClass,
|
||||
created.ConfigName,
|
||||
created.ConfigId);
|
||||
|
||||
return created;
|
||||
}
|
||||
|
||||
public SystemConfig Update(SystemConfig config)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(config);
|
||||
|
||||
if (config.ConfigId <= 0)
|
||||
{
|
||||
throw new HardwareException(
|
||||
HardwareErrorKind.InvalidArgument, "系统参数 ID 无效");
|
||||
}
|
||||
|
||||
Normalize(config);
|
||||
|
||||
if (!_repository.Update(config))
|
||||
{
|
||||
throw new HardwareException(
|
||||
HardwareErrorKind.InvalidArgument,
|
||||
$"系统参数 {config.ConfigId} 不存在");
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"更新系统参数:{ConfigClass}/{ConfigName}(Id={ConfigId})",
|
||||
config.ConfigClass,
|
||||
config.ConfigName,
|
||||
config.ConfigId);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
public bool Delete(int id)
|
||||
{
|
||||
if (id <= 0)
|
||||
{
|
||||
throw new HardwareException(
|
||||
HardwareErrorKind.InvalidArgument, "系统参数 ID 无效");
|
||||
}
|
||||
|
||||
var deleted = _repository.Delete(id);
|
||||
if (deleted)
|
||||
{
|
||||
_logger.LogInformation("删除系统参数:Id={ConfigId}", id);
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
private static void Normalize(SystemConfig config)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(config.ConfigName))
|
||||
{
|
||||
throw new HardwareException(
|
||||
HardwareErrorKind.InvalidArgument, "配置名称不能为空");
|
||||
}
|
||||
|
||||
config.ConfigClass = config.ConfigClass?.Trim() ?? string.Empty;
|
||||
config.ConfigName = config.ConfigName.Trim();
|
||||
config.ConfigValue = config.ConfigValue?.Trim() ?? string.Empty;
|
||||
config.ConfigDescript = config.ConfigDescript?.Trim() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user