提交代码
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using HardwareControl.Core.SystemSetting;
|
||||
|
||||
namespace HardwareControl.Core.HardwareServiceImpl.HardwareService
|
||||
{
|
||||
/// <summary>
|
||||
/// 系统参数配置服务:对上层提供增删改查能力,屏蔽本地存储细节。
|
||||
/// </summary>
|
||||
public interface ISystemConfigService
|
||||
{
|
||||
/// <summary>读取全部配置,可按关键字过滤(分类、名称、值、描述)。</summary>
|
||||
IReadOnlyList<SystemConfig> GetAll(string? keyword = null);
|
||||
|
||||
/// <summary>按主键读取单条配置;不存在时返回 null。</summary>
|
||||
SystemConfig? GetById(int id);
|
||||
|
||||
/// <summary>新增配置,返回带主键的实体。</summary>
|
||||
SystemConfig Create(SystemConfig config);
|
||||
|
||||
/// <summary>更新配置;主键不存在时抛出 <see cref="HardwareException"/>。</summary>
|
||||
SystemConfig Update(SystemConfig config);
|
||||
|
||||
/// <summary>删除配置;返回是否成功删除。</summary>
|
||||
bool Delete(int id);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using LiteDB;
|
||||
|
||||
namespace HardwareControl.Core.SystemSetting
|
||||
{
|
||||
/// <summary>
|
||||
/// 系统参数配置的本地存储层(LiteDB)。
|
||||
/// 数据库文件由调用方指定,建议整个进程只注册一个实例。
|
||||
/// </summary>
|
||||
public sealed class SystemConfigRepository : IDisposable
|
||||
{
|
||||
private const string CollectionName = "system_config";
|
||||
|
||||
private readonly LiteDatabase _database;
|
||||
private readonly ILiteCollection<SystemConfig> _collection;
|
||||
|
||||
public SystemConfigRepository(string databasePath)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(databasePath))
|
||||
{
|
||||
throw new ArgumentException("数据库路径不能为空", nameof(databasePath));
|
||||
}
|
||||
|
||||
var fullPath = Path.GetFullPath(databasePath);
|
||||
var directory = Path.GetDirectoryName(fullPath);
|
||||
if (!string.IsNullOrWhiteSpace(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
_database = new LiteDatabase(new ConnectionString
|
||||
{
|
||||
Filename = fullPath,
|
||||
Connection = ConnectionType.Shared,
|
||||
});
|
||||
_collection = _database.GetCollection<SystemConfig>(CollectionName);
|
||||
}
|
||||
|
||||
/// <summary>读取全部配置,可按关键字过滤(分类、名称、值、描述)。</summary>
|
||||
public IReadOnlyList<SystemConfig> GetAll(string? keyword = null)
|
||||
{
|
||||
var items = _collection
|
||||
.FindAll()
|
||||
.OrderBy(x => x.ConfigClass)
|
||||
.ThenBy(x => x.ConfigName)
|
||||
.ToList();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
return items;
|
||||
}
|
||||
|
||||
var value = keyword.Trim();
|
||||
return items
|
||||
.Where(x =>
|
||||
Contains(x.ConfigClass, value) ||
|
||||
Contains(x.ConfigName, value) ||
|
||||
Contains(x.ConfigValue, value) ||
|
||||
Contains(x.ConfigDescript, value))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public SystemConfig? FindById(int id) => _collection.FindById(id);
|
||||
|
||||
public SystemConfig Insert(SystemConfig config)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(config);
|
||||
|
||||
config.ConfigId = 0;
|
||||
var id = _collection.Insert(config);
|
||||
config.ConfigId = id.AsInt32;
|
||||
return config;
|
||||
}
|
||||
|
||||
public bool Update(SystemConfig config)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(config);
|
||||
return _collection.Update(config);
|
||||
}
|
||||
|
||||
public bool Delete(int id) => _collection.Delete(id);
|
||||
|
||||
public void Dispose() => _database.Dispose();
|
||||
|
||||
private static bool Contains(string? source, string value) =>
|
||||
source is not null &&
|
||||
source.Contains(value, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user