diff --git a/src/X1.Application/Features/Devices/Commands/CreateDevice/CreateDeviceCommand.cs b/src/X1.Application/Features/Devices/Commands/CreateDevice/CreateDeviceCommand.cs index e34eb90..85ee15d 100644 --- a/src/X1.Application/Features/Devices/Commands/CreateDevice/CreateDeviceCommand.cs +++ b/src/X1.Application/Features/Devices/Commands/CreateDevice/CreateDeviceCommand.cs @@ -25,32 +25,30 @@ public class CreateDeviceCommand : IRequest - /// 备注 + /// 设备描述 /// - [MaxLength(500, ErrorMessage = "备注不能超过500个字符")] - public string Comment { get; set; } + [MaxLength(500, ErrorMessage = "设备描述不能超过500个字符")] + public string Description { get; set; } /// - /// 设备类型ID + /// 协议版本ID /// - [Required(ErrorMessage = "设备类型不能为空")] - public string DeviceTypeId { get; set; } - + [Required(ErrorMessage = "协议版本不能为空")] + public string ProtocolVersionId { get; set; } /// - /// 状态ID + /// IP地址 /// - [Required(ErrorMessage = "设备状态不能为空")] - public string StatusId { get; set; } + [MaxLength(45, ErrorMessage = "Agent端口不能为空")] + public string IpAddress { get; private set; } = null!; /// - /// 协议版本ID + /// Agent端口 /// - [Required(ErrorMessage = "协议版本不能为空")] - public string ProtocolVersionId { get; set; } + [Required(ErrorMessage = "Agent端口不能为空")] + public int AgentPort { get; set; } /// - /// 硬件版本 + /// 是否启用 /// - [MaxLength(50, ErrorMessage = "硬件版本不能超过50个字符")] - public string HardwareVersion { get; set; } + public bool IsEnabled { get; set; } = true; } \ No newline at end of file diff --git a/src/X1.Application/Features/Devices/Commands/CreateDevice/CreateDeviceCommandHandler.cs b/src/X1.Application/Features/Devices/Commands/CreateDevice/CreateDeviceCommandHandler.cs index faa4550..9647bdd 100644 --- a/src/X1.Application/Features/Devices/Commands/CreateDevice/CreateDeviceCommandHandler.cs +++ b/src/X1.Application/Features/Devices/Commands/CreateDevice/CreateDeviceCommandHandler.cs @@ -47,11 +47,11 @@ public class CreateDeviceCommandHandler : IRequestHandler - /// 设备类型 + /// 设备描述 /// - public string DeviceType { get; set; } + public string Description { get; set; } /// - /// 设备状态 + /// 协议版本 /// - public string Status { get; set; } + public string ProtocolVersion { get; set; } /// - /// 协议版本 + /// Agent端口 /// - public string ProtocolVersion { get; set; } + public int AgentPort { get; set; } /// - /// 固件版本 + /// 是否启用 /// - public string FirmwareVersion { get; set; } + public bool IsEnabled { get; set; } /// /// 创建时间 diff --git a/src/X1.Application/Features/Devices/Commands/UpdateDevice/UpdateDeviceCommand.cs b/src/X1.Application/Features/Devices/Commands/UpdateDevice/UpdateDeviceCommand.cs index ce75581..a96e27c 100644 --- a/src/X1.Application/Features/Devices/Commands/UpdateDevice/UpdateDeviceCommand.cs +++ b/src/X1.Application/Features/Devices/Commands/UpdateDevice/UpdateDeviceCommand.cs @@ -31,33 +31,30 @@ public class UpdateDeviceCommand : IRequest - /// 备注 + /// 设备描述 /// [MaxLength(500)] - public string Comment { get; set; } + public string Description { get; set; } /// - /// 设备类型ID + /// 协议版本ID /// [Required] - public string DeviceTypeId { get; set; } - + public string ProtocolVersionId { get; set; } /// - /// 设备状态ID + /// IP地址 /// [Required] - public string StatusId { get; set; } - + [MaxLength(45)] + public string IpAddress { get; private set; } = null!; /// - /// 协议版本ID + /// Agent端口 /// [Required] - public string ProtocolVersionId { get; set; } + public int AgentPort { get; set; } /// - /// 硬件版本 + /// 是否启用 /// - [Required] - [MaxLength(50)] - public string HardwareVersion { get; set; } + public bool IsEnabled { get; set; } = true; } \ No newline at end of file diff --git a/src/X1.Application/Features/Devices/Commands/UpdateDevice/UpdateDeviceCommandHandler.cs b/src/X1.Application/Features/Devices/Commands/UpdateDevice/UpdateDeviceCommandHandler.cs index 38d6a5d..02a610f 100644 --- a/src/X1.Application/Features/Devices/Commands/UpdateDevice/UpdateDeviceCommandHandler.cs +++ b/src/X1.Application/Features/Devices/Commands/UpdateDevice/UpdateDeviceCommandHandler.cs @@ -5,6 +5,7 @@ using CellularManagement.Domain.Entities.Device; using CellularManagement.Domain.Repositories; using CellularManagement.Application.Features.Devices.Commands.UpdateDevice; using CellularManagement.Domain.Repositories.Device; +using System.Net; namespace CellularManagement.Application.Features.Devices.Commands.UpdateDevice; @@ -59,14 +60,15 @@ public class UpdateDeviceCommandHandler : IRequestHandler - /// 设备类型 + /// 设备描述 /// - public string DeviceType { get; set; } + public string Description { get; set; } /// - /// 设备状态 + /// 协议版本 /// - public string Status { get; set; } + public string ProtocolVersion { get; set; } /// - /// 协议版本 + /// Agent端口 /// - public string ProtocolVersion { get; set; } + public int AgentPort { get; set; } /// - /// 固件版本 + /// 是否启用 /// - public string FirmwareVersion { get; set; } + public bool IsEnabled { get; set; } /// /// 更新时间 diff --git a/src/X1.Application/Features/Devices/Queries/GetDeviceById/GetDeviceByIdQueryHandler.cs b/src/X1.Application/Features/Devices/Queries/GetDeviceById/GetDeviceByIdQueryHandler.cs index 802479e..643deb4 100644 --- a/src/X1.Application/Features/Devices/Queries/GetDeviceById/GetDeviceByIdQueryHandler.cs +++ b/src/X1.Application/Features/Devices/Queries/GetDeviceById/GetDeviceByIdQueryHandler.cs @@ -48,10 +48,10 @@ public class GetDeviceByIdQueryHandler : IRequestHandler - /// 设备类型 + /// 设备描述 /// - public string DeviceType { get; set; } + public string Description { get; set; } /// - /// 设备状态 + /// 协议版本 /// - public string Status { get; set; } + public string ProtocolVersion { get; set; } /// - /// 协议版本 + /// Agent端口 /// - public string ProtocolVersion { get; set; } + public int AgentPort { get; set; } /// - /// 固件版本 + /// 是否启用 /// - public string FirmwareVersion { get; set; } + public bool IsEnabled { get; set; } /// /// 创建时间 diff --git a/src/X1.Application/Features/Devices/Queries/GetDevices/GetDevicesQueryHandler.cs b/src/X1.Application/Features/Devices/Queries/GetDevices/GetDevicesQueryHandler.cs index 2ee204d..6412509 100644 --- a/src/X1.Application/Features/Devices/Queries/GetDevices/GetDevicesQueryHandler.cs +++ b/src/X1.Application/Features/Devices/Queries/GetDevices/GetDevicesQueryHandler.cs @@ -52,8 +52,6 @@ public class GetDevicesQueryHandler : IRequestHandler +/// 创建协议版本命令 +/// +public class CreateProtocolVersionCommand : IRequest> +{ + /// + /// 版本名称 + /// + [Required] + [MaxLength(50)] + public string Name { get; set; } = null!; + + /// + /// 版本号 + /// + [Required] + [MaxLength(20)] + public string Version { get; set; } = null!; + + /// + /// 版本描述 + /// + [MaxLength(500)] + public string? Description { get; set; } + + /// + /// 是否启用 + /// + public bool IsEnabled { get; set; } = true; + + /// + /// 发布日期 + /// + public DateTime? ReleaseDate { get; set; } + + /// + /// 是否强制更新 + /// + public bool IsForceUpdate { get; set; } = false; + + /// + /// 最低支持版本 + /// + [MaxLength(20)] + public string? MinimumSupportedVersion { get; set; } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Commands/CreateProtocolVersion/CreateProtocolVersionCommandHandler.cs b/src/X1.Application/Features/ProtocolVersions/Commands/CreateProtocolVersion/CreateProtocolVersionCommandHandler.cs new file mode 100644 index 0000000..a941f68 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Commands/CreateProtocolVersion/CreateProtocolVersionCommandHandler.cs @@ -0,0 +1,83 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using CellularManagement.Domain.Common; +using CellularManagement.Domain.Entities.Device; +using CellularManagement.Domain.Repositories.Device; + +namespace CellularManagement.Application.Features.ProtocolVersions.Commands.CreateProtocolVersion; + +/// +/// 创建协议版本命令处理器 +/// +public class CreateProtocolVersionCommandHandler : IRequestHandler> +{ + private readonly IProtocolVersionRepository _protocolVersionRepository; + private readonly ILogger _logger; + + /// + /// 初始化命令处理器 + /// + public CreateProtocolVersionCommandHandler( + IProtocolVersionRepository protocolVersionRepository, + ILogger logger) + { + _protocolVersionRepository = protocolVersionRepository; + _logger = logger; + } + + /// + /// 处理创建协议版本命令 + /// + public async Task> Handle(CreateProtocolVersionCommand request, CancellationToken cancellationToken) + { + try + { + _logger.LogInformation("开始创建协议版本,版本名称: {Name}, 版本号: {Version}", + request.Name, request.Version); + + // 检查版本号是否已存在 + if (await _protocolVersionRepository.VersionExistsAsync(request.Version, cancellationToken)) + { + _logger.LogWarning("协议版本号已存在: {Version}", request.Version); + return OperationResult.CreateFailure($"协议版本号 {request.Version} 已存在"); + } + + // 创建协议版本实体 + var protocolVersion = ProtocolVersion.Create( + name: request.Name, + version: request.Version, + description: request.Description, + isEnabled: request.IsEnabled, + releaseDate: request.ReleaseDate, + isForceUpdate: request.IsForceUpdate, + minimumSupportedVersion: request.MinimumSupportedVersion); + + // 保存协议版本 + await _protocolVersionRepository.AddProtocolVersionAsync(protocolVersion, cancellationToken); + + // 构建响应 + var response = new CreateProtocolVersionResponse + { + ProtocolVersionId = protocolVersion.Id, + Name = protocolVersion.Name, + Version = protocolVersion.Version, + Description = protocolVersion.Description, + IsEnabled = protocolVersion.IsEnabled, + ReleaseDate = protocolVersion.ReleaseDate, + IsForceUpdate = protocolVersion.IsForceUpdate, + MinimumSupportedVersion = protocolVersion.MinimumSupportedVersion, + CreatedAt = protocolVersion.CreatedAt + }; + + _logger.LogInformation("协议版本创建成功,版本ID: {ProtocolVersionId}, 版本名称: {Name}, 版本号: {Version}", + protocolVersion.Id, protocolVersion.Name, protocolVersion.Version); + return OperationResult.CreateSuccess(response); + } + catch (Exception ex) + { + _logger.LogError(ex, "创建协议版本时发生错误,版本名称: {Name}, 版本号: {Version}", + request.Name, request.Version); + return OperationResult.CreateFailure($"创建协议版本时发生错误: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Commands/CreateProtocolVersion/CreateProtocolVersionResponse.cs b/src/X1.Application/Features/ProtocolVersions/Commands/CreateProtocolVersion/CreateProtocolVersionResponse.cs new file mode 100644 index 0000000..f48b8a5 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Commands/CreateProtocolVersion/CreateProtocolVersionResponse.cs @@ -0,0 +1,52 @@ +namespace CellularManagement.Application.Features.ProtocolVersions.Commands.CreateProtocolVersion; + +/// +/// 创建协议版本响应 +/// +public class CreateProtocolVersionResponse +{ + /// + /// 协议版本ID + /// + public string ProtocolVersionId { get; set; } = null!; + + /// + /// 版本名称 + /// + public string Name { get; set; } = null!; + + /// + /// 版本号 + /// + public string Version { get; set; } = null!; + + /// + /// 版本描述 + /// + public string? Description { get; set; } + + /// + /// 是否启用 + /// + public bool IsEnabled { get; set; } + + /// + /// 发布日期 + /// + public DateTime? ReleaseDate { get; set; } + + /// + /// 是否强制更新 + /// + public bool IsForceUpdate { get; set; } + + /// + /// 最低支持版本 + /// + public string? MinimumSupportedVersion { get; set; } + + /// + /// 创建时间 + /// + public DateTime CreatedAt { get; set; } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Commands/DeleteProtocolVersion/DeleteProtocolVersionCommand.cs b/src/X1.Application/Features/ProtocolVersions/Commands/DeleteProtocolVersion/DeleteProtocolVersionCommand.cs new file mode 100644 index 0000000..cd455bc --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Commands/DeleteProtocolVersion/DeleteProtocolVersionCommand.cs @@ -0,0 +1,17 @@ +using CellularManagement.Domain.Common; +using MediatR; +using System.ComponentModel.DataAnnotations; + +namespace CellularManagement.Application.Features.ProtocolVersions.Commands.DeleteProtocolVersion; + +/// +/// 删除协议版本命令 +/// +public class DeleteProtocolVersionCommand : IRequest> +{ + /// + /// 协议版本ID + /// + [Required] + public string ProtocolVersionId { get; set; } = null!; +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Commands/DeleteProtocolVersion/DeleteProtocolVersionCommandHandler.cs b/src/X1.Application/Features/ProtocolVersions/Commands/DeleteProtocolVersion/DeleteProtocolVersionCommandHandler.cs new file mode 100644 index 0000000..e908bd5 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Commands/DeleteProtocolVersion/DeleteProtocolVersionCommandHandler.cs @@ -0,0 +1,56 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using CellularManagement.Domain.Common; +using CellularManagement.Domain.Repositories.Device; + +namespace CellularManagement.Application.Features.ProtocolVersions.Commands.DeleteProtocolVersion; + +/// +/// 删除协议版本命令处理器 +/// +public class DeleteProtocolVersionCommandHandler : IRequestHandler> +{ + private readonly IProtocolVersionRepository _protocolVersionRepository; + private readonly ILogger _logger; + + /// + /// 初始化删除协议版本命令处理器 + /// + public DeleteProtocolVersionCommandHandler( + IProtocolVersionRepository protocolVersionRepository, + ILogger logger) + { + _protocolVersionRepository = protocolVersionRepository; + _logger = logger; + } + + /// + /// 处理删除协议版本命令 + /// + public async Task> Handle(DeleteProtocolVersionCommand request, CancellationToken cancellationToken) + { + try + { + _logger.LogInformation("开始处理删除协议版本命令,版本ID: {ProtocolVersionId}", request.ProtocolVersionId); + + // 检查协议版本是否存在 + var protocolVersion = await _protocolVersionRepository.GetProtocolVersionByIdAsync(request.ProtocolVersionId, cancellationToken); + if (protocolVersion == null) + { + _logger.LogWarning("未找到协议版本,版本ID: {ProtocolVersionId}", request.ProtocolVersionId); + return OperationResult.CreateFailure($"未找到ID为 {request.ProtocolVersionId} 的协议版本"); + } + + // 删除协议版本 + await _protocolVersionRepository.DeleteProtocolVersionAsync(request.ProtocolVersionId, cancellationToken); + + _logger.LogInformation("协议版本删除成功,版本ID: {ProtocolVersionId}", request.ProtocolVersionId); + return OperationResult.CreateSuccess("协议版本删除成功", true); + } + catch (Exception ex) + { + _logger.LogError(ex, "删除协议版本时发生错误,版本ID: {ProtocolVersionId}", request.ProtocolVersionId); + return OperationResult.CreateFailure($"删除协议版本时发生错误: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Commands/UpdateProtocolVersion/UpdateProtocolVersionCommand.cs b/src/X1.Application/Features/ProtocolVersions/Commands/UpdateProtocolVersion/UpdateProtocolVersionCommand.cs new file mode 100644 index 0000000..3b1fa47 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Commands/UpdateProtocolVersion/UpdateProtocolVersionCommand.cs @@ -0,0 +1,58 @@ +using CellularManagement.Domain.Common; +using MediatR; +using System.ComponentModel.DataAnnotations; + +namespace CellularManagement.Application.Features.ProtocolVersions.Commands.UpdateProtocolVersion; + +/// +/// 更新协议版本命令 +/// +public class UpdateProtocolVersionCommand : IRequest> +{ + /// + /// 协议版本ID + /// + [Required] + public string ProtocolVersionId { get; set; } = null!; + + /// + /// 版本名称 + /// + [Required] + [MaxLength(50)] + public string Name { get; set; } = null!; + + /// + /// 版本号 + /// + [Required] + [MaxLength(20)] + public string Version { get; set; } = null!; + + /// + /// 版本描述 + /// + [MaxLength(500)] + public string? Description { get; set; } + + /// + /// 是否启用 + /// + public bool IsEnabled { get; set; } = true; + + /// + /// 发布日期 + /// + public DateTime? ReleaseDate { get; set; } + + /// + /// 是否强制更新 + /// + public bool IsForceUpdate { get; set; } = false; + + /// + /// 最低支持版本 + /// + [MaxLength(20)] + public string? MinimumSupportedVersion { get; set; } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Commands/UpdateProtocolVersion/UpdateProtocolVersionCommandHandler.cs b/src/X1.Application/Features/ProtocolVersions/Commands/UpdateProtocolVersion/UpdateProtocolVersionCommandHandler.cs new file mode 100644 index 0000000..e757303 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Commands/UpdateProtocolVersion/UpdateProtocolVersionCommandHandler.cs @@ -0,0 +1,93 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using CellularManagement.Domain.Common; +using CellularManagement.Domain.Entities.Device; +using CellularManagement.Domain.Repositories.Device; + +namespace CellularManagement.Application.Features.ProtocolVersions.Commands.UpdateProtocolVersion; + +/// +/// 更新协议版本命令处理器 +/// +public class UpdateProtocolVersionCommandHandler : IRequestHandler> +{ + private readonly IProtocolVersionRepository _protocolVersionRepository; + private readonly ILogger _logger; + + /// + /// 初始化命令处理器 + /// + public UpdateProtocolVersionCommandHandler( + IProtocolVersionRepository protocolVersionRepository, + ILogger logger) + { + _protocolVersionRepository = protocolVersionRepository; + _logger = logger; + } + + /// + /// 处理更新协议版本命令 + /// + public async Task> Handle(UpdateProtocolVersionCommand request, CancellationToken cancellationToken) + { + try + { + _logger.LogInformation("开始更新协议版本,版本ID: {ProtocolVersionId}, 版本名称: {Name}", + request.ProtocolVersionId, request.Name); + + // 检查协议版本是否存在 + var existingProtocolVersion = await _protocolVersionRepository.GetProtocolVersionByIdAsync(request.ProtocolVersionId, cancellationToken); + if (existingProtocolVersion == null) + { + _logger.LogWarning("未找到协议版本,版本ID: {ProtocolVersionId}", request.ProtocolVersionId); + return OperationResult.CreateFailure($"未找到ID为 {request.ProtocolVersionId} 的协议版本"); + } + + // 如果版本号发生变化,检查新版本号是否已存在 + if (existingProtocolVersion.Version != request.Version) + { + if (await _protocolVersionRepository.VersionExistsAsync(request.Version, cancellationToken)) + { + _logger.LogWarning("协议版本号已存在: {Version}", request.Version); + return OperationResult.CreateFailure($"协议版本号 {request.Version} 已存在"); + } + } + + // 更新协议版本属性 + existingProtocolVersion.Update( + name: request.Name, + version: request.Version, + description: request.Description, + isEnabled: request.IsEnabled, + releaseDate: request.ReleaseDate, + isForceUpdate: request.IsForceUpdate, + minimumSupportedVersion: request.MinimumSupportedVersion); + + _protocolVersionRepository.UpdateProtocolVersion(existingProtocolVersion); + + // 构建响应 + var response = new UpdateProtocolVersionResponse + { + ProtocolVersionId = existingProtocolVersion.Id, + Name = existingProtocolVersion.Name, + Version = existingProtocolVersion.Version, + Description = existingProtocolVersion.Description, + IsEnabled = existingProtocolVersion.IsEnabled, + ReleaseDate = existingProtocolVersion.ReleaseDate, + IsForceUpdate = existingProtocolVersion.IsForceUpdate, + MinimumSupportedVersion = existingProtocolVersion.MinimumSupportedVersion, + UpdatedAt = DateTime.UtcNow + }; + + _logger.LogInformation("协议版本更新成功,版本ID: {ProtocolVersionId}, 版本名称: {Name}", + existingProtocolVersion.Id, existingProtocolVersion.Name); + return OperationResult.CreateSuccess(response); + } + catch (Exception ex) + { + _logger.LogError(ex, "更新协议版本时发生错误,版本ID: {ProtocolVersionId}, 版本名称: {Name}", + request.ProtocolVersionId, request.Name); + return OperationResult.CreateFailure($"更新协议版本时发生错误: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Commands/UpdateProtocolVersion/UpdateProtocolVersionResponse.cs b/src/X1.Application/Features/ProtocolVersions/Commands/UpdateProtocolVersion/UpdateProtocolVersionResponse.cs new file mode 100644 index 0000000..decf622 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Commands/UpdateProtocolVersion/UpdateProtocolVersionResponse.cs @@ -0,0 +1,52 @@ +namespace CellularManagement.Application.Features.ProtocolVersions.Commands.UpdateProtocolVersion; + +/// +/// 更新协议版本响应 +/// +public class UpdateProtocolVersionResponse +{ + /// + /// 协议版本ID + /// + public string ProtocolVersionId { get; set; } = null!; + + /// + /// 版本名称 + /// + public string Name { get; set; } = null!; + + /// + /// 版本号 + /// + public string Version { get; set; } = null!; + + /// + /// 版本描述 + /// + public string? Description { get; set; } + + /// + /// 是否启用 + /// + public bool IsEnabled { get; set; } + + /// + /// 发布日期 + /// + public DateTime? ReleaseDate { get; set; } + + /// + /// 是否强制更新 + /// + public bool IsForceUpdate { get; set; } + + /// + /// 最低支持版本 + /// + public string? MinimumSupportedVersion { get; set; } + + /// + /// 更新时间 + /// + public DateTime UpdatedAt { get; set; } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersionById/GetProtocolVersionByIdQuery.cs b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersionById/GetProtocolVersionByIdQuery.cs new file mode 100644 index 0000000..00a62ad --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersionById/GetProtocolVersionByIdQuery.cs @@ -0,0 +1,17 @@ +using CellularManagement.Domain.Common; +using MediatR; +using System.ComponentModel.DataAnnotations; + +namespace CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersionById; + +/// +/// 根据ID获取协议版本查询 +/// +public class GetProtocolVersionByIdQuery : IRequest> +{ + /// + /// 协议版本ID + /// + [Required] + public string ProtocolVersionId { get; set; } = null!; +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersionById/GetProtocolVersionByIdQueryHandler.cs b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersionById/GetProtocolVersionByIdQueryHandler.cs new file mode 100644 index 0000000..0f3a5a2 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersionById/GetProtocolVersionByIdQueryHandler.cs @@ -0,0 +1,68 @@ +using CellularManagement.Domain.Common; +using CellularManagement.Domain.Entities.Device; +using CellularManagement.Domain.Repositories.Device; +using MediatR; +using Microsoft.Extensions.Logging; + +namespace CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersionById; + +/// +/// 根据ID获取协议版本查询处理器 +/// +public class GetProtocolVersionByIdQueryHandler : IRequestHandler> +{ + private readonly IProtocolVersionRepository _protocolVersionRepository; + private readonly ILogger _logger; + + /// + /// 初始化查询处理器 + /// + public GetProtocolVersionByIdQueryHandler( + IProtocolVersionRepository protocolVersionRepository, + ILogger logger) + { + _protocolVersionRepository = protocolVersionRepository; + _logger = logger; + } + + /// + /// 处理查询请求 + /// + public async Task> Handle(GetProtocolVersionByIdQuery request, CancellationToken cancellationToken) + { + try + { + _logger.LogInformation("开始查询协议版本,版本ID: {ProtocolVersionId}", request.ProtocolVersionId); + + var protocolVersion = await _protocolVersionRepository.GetProtocolVersionByIdAsync(request.ProtocolVersionId, cancellationToken); + + if (protocolVersion == null) + { + _logger.LogWarning("未找到协议版本,版本ID: {ProtocolVersionId}", request.ProtocolVersionId); + return OperationResult.CreateFailure($"未找到ID为 {request.ProtocolVersionId} 的协议版本"); + } + + var response = new GetProtocolVersionByIdResponse + { + ProtocolVersionId = protocolVersion.Id, + Name = protocolVersion.Name, + Version = protocolVersion.Version, + Description = protocolVersion.Description, + IsEnabled = protocolVersion.IsEnabled, + ReleaseDate = protocolVersion.ReleaseDate, + IsForceUpdate = protocolVersion.IsForceUpdate, + MinimumSupportedVersion = protocolVersion.MinimumSupportedVersion, + CreatedAt = protocolVersion.CreatedAt, + UpdatedAt = protocolVersion.UpdatedAt + }; + + _logger.LogInformation("成功查询到协议版本,版本ID: {ProtocolVersionId}", request.ProtocolVersionId); + return OperationResult.CreateSuccess(response); + } + catch (Exception ex) + { + _logger.LogError(ex, "查询协议版本时发生错误,版本ID: {ProtocolVersionId}", request.ProtocolVersionId); + return OperationResult.CreateFailure($"查询协议版本时发生错误: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersionById/GetProtocolVersionByIdResponse.cs b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersionById/GetProtocolVersionByIdResponse.cs new file mode 100644 index 0000000..b0d2abe --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersionById/GetProtocolVersionByIdResponse.cs @@ -0,0 +1,57 @@ +namespace CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersionById; + +/// +/// 根据ID获取协议版本响应 +/// +public class GetProtocolVersionByIdResponse +{ + /// + /// 协议版本ID + /// + public string ProtocolVersionId { get; set; } = null!; + + /// + /// 版本名称 + /// + public string Name { get; set; } = null!; + + /// + /// 版本号 + /// + public string Version { get; set; } = null!; + + /// + /// 版本描述 + /// + public string? Description { get; set; } + + /// + /// 是否启用 + /// + public bool IsEnabled { get; set; } + + /// + /// 发布日期 + /// + public DateTime? ReleaseDate { get; set; } + + /// + /// 是否强制更新 + /// + public bool IsForceUpdate { get; set; } + + /// + /// 最低支持版本 + /// + public string? MinimumSupportedVersion { get; set; } + + /// + /// 创建时间 + /// + public DateTime CreatedAt { get; set; } + + /// + /// 更新时间 + /// + public DateTime? UpdatedAt { get; set; } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersions/GetProtocolVersionsQuery.cs b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersions/GetProtocolVersionsQuery.cs new file mode 100644 index 0000000..19ef593 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersions/GetProtocolVersionsQuery.cs @@ -0,0 +1,34 @@ +using CellularManagement.Domain.Common; +using MediatR; +using System.ComponentModel.DataAnnotations; + +namespace CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersions; + +/// +/// 获取协议版本列表查询 +/// +public class GetProtocolVersionsQuery : IRequest> +{ + /// + /// 页码 + /// + [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] + public int PageNumber { get; set; } = 1; + + /// + /// 每页数量 + /// + [Range(1, 100, ErrorMessage = "每页数量必须在1-100之间")] + public int PageSize { get; set; } = 10; + + /// + /// 搜索关键词 + /// + [MaxLength(100)] + public string? SearchTerm { get; set; } + + /// + /// 是否只获取启用的版本 + /// + public bool? IsEnabled { get; set; } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersions/GetProtocolVersionsQueryHandler.cs b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersions/GetProtocolVersionsQueryHandler.cs new file mode 100644 index 0000000..8f9f086 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersions/GetProtocolVersionsQueryHandler.cs @@ -0,0 +1,103 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using CellularManagement.Domain.Common; +using CellularManagement.Domain.Entities.Device; +using CellularManagement.Domain.Repositories; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersionById; +using CellularManagement.Domain.Repositories.Device; + +namespace CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersions; + +/// +/// 获取协议版本列表查询处理器 +/// +public class GetProtocolVersionsQueryHandler : IRequestHandler> +{ + private readonly IProtocolVersionRepository _protocolVersionRepository; + private readonly ILogger _logger; + + /// + /// 初始化查询处理器 + /// + public GetProtocolVersionsQueryHandler( + IProtocolVersionRepository protocolVersionRepository, + ILogger logger) + { + _protocolVersionRepository = protocolVersionRepository; + _logger = logger; + } + + /// + /// 处理获取协议版本列表查询 + /// + public async Task> Handle(GetProtocolVersionsQuery request, CancellationToken cancellationToken) + { + try + { + // 验证请求参数 + var validationContext = new ValidationContext(request); + var validationResults = new List(); + if (!Validator.TryValidateObject(request, validationContext, validationResults, true)) + { + var errorMessages = validationResults.Select(r => r.ErrorMessage).ToList(); + _logger.LogWarning("请求参数无效: {Errors}", string.Join(", ", errorMessages)); + return OperationResult.CreateFailure(errorMessages); + } + + _logger.LogInformation("开始获取协议版本列表,页码: {PageNumber}, 每页数量: {PageSize}, 搜索关键词: {SearchTerm}, 是否启用: {IsEnabled}", + request.PageNumber, request.PageSize, request.SearchTerm, request.IsEnabled); + + // 获取协议版本数据 + var protocolVersions = await _protocolVersionRepository.SearchProtocolVersionsAsync( + request.SearchTerm, + cancellationToken); + + // 如果指定了启用状态过滤 + if (request.IsEnabled.HasValue) + { + protocolVersions = protocolVersions.Where(pv => pv.IsEnabled == request.IsEnabled.Value).ToList(); + } + + // 计算分页 + int totalCount = protocolVersions.Count(); + var items = protocolVersions + .Skip((request.PageNumber - 1) * request.PageSize) + .Take(request.PageSize) + .ToList(); + + // 构建响应 + var response = new GetProtocolVersionsResponse + { + TotalCount = totalCount, + PageNumber = request.PageNumber, + PageSize = request.PageSize, + TotalPages = (int)Math.Ceiling(totalCount / (double)request.PageSize), + HasPreviousPage = request.PageNumber > 1, + HasNextPage = request.PageNumber < (int)Math.Ceiling(totalCount / (double)request.PageSize), + Items = items.Select(pv => new GetProtocolVersionByIdResponse + { + ProtocolVersionId = pv.Id, + Name = pv.Name, + Version = pv.Version, + Description = pv.Description, + IsEnabled = pv.IsEnabled, + ReleaseDate = pv.ReleaseDate, + IsForceUpdate = pv.IsForceUpdate, + MinimumSupportedVersion = pv.MinimumSupportedVersion, + CreatedAt = pv.CreatedAt, + UpdatedAt = pv.UpdatedAt + }).ToList() + }; + + _logger.LogInformation("成功获取协议版本列表,共 {Count} 条记录", items.Count); + return OperationResult.CreateSuccess(response); + } + catch (Exception ex) + { + _logger.LogError(ex, "获取协议版本列表时发生错误"); + return OperationResult.CreateFailure($"获取协议版本列表时发生错误: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersions/GetProtocolVersionsResponse.cs b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersions/GetProtocolVersionsResponse.cs new file mode 100644 index 0000000..21ff962 --- /dev/null +++ b/src/X1.Application/Features/ProtocolVersions/Queries/GetProtocolVersions/GetProtocolVersionsResponse.cs @@ -0,0 +1,44 @@ +using CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersionById; + +namespace CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersions; + +/// +/// 获取协议版本列表响应 +/// +public class GetProtocolVersionsResponse +{ + /// + /// 总数量 + /// + public int TotalCount { get; set; } + + /// + /// 当前页码 + /// + public int PageNumber { get; set; } + + /// + /// 每页数量 + /// + public int PageSize { get; set; } + + /// + /// 总页数 + /// + public int TotalPages { get; set; } + + /// + /// 是否有上一页 + /// + public bool HasPreviousPage { get; set; } + + /// + /// 是否有下一页 + /// + public bool HasNextPage { get; set; } + + /// + /// 协议版本列表 + /// + public List Items { get; set; } = new(); +} \ No newline at end of file diff --git a/src/X1.Domain/Entities/Device/CellularDevice.cs b/src/X1.Domain/Entities/Device/CellularDevice.cs index 0be741d..e6161ef 100644 --- a/src/X1.Domain/Entities/Device/CellularDevice.cs +++ b/src/X1.Domain/Entities/Device/CellularDevice.cs @@ -11,12 +11,6 @@ public class CellularDevice : AuditableEntity { private CellularDevice() { } - /// - /// 设备ID - /// - [Key] - public string Id { get; private set; } - /// /// 设备名称 /// @@ -37,30 +31,6 @@ public class CellularDevice : AuditableEntity [MaxLength(500)] public string Description { get; private set; } - /// - /// 设备类型ID - /// - [Required] - public string DeviceTypeId { get; private set; } - - /// - /// 设备类型 - /// - [ForeignKey(nameof(DeviceTypeId))] - public virtual DeviceType DeviceType { get; private set; } = null!; - - /// - /// 设备状态ID - /// - [Required] - public string StatusId { get; private set; } - - /// - /// 设备状态 - /// - [ForeignKey(nameof(StatusId))] - public virtual DeviceStatus Status { get; private set; } = null!; - /// /// 协议版本ID /// @@ -74,21 +44,22 @@ public class CellularDevice : AuditableEntity public virtual ProtocolVersion ProtocolVersion { get; private set; } = null!; /// - /// 当前固件版本 + /// Agent端口 /// [Required] - [MaxLength(50)] - public string FirmwareVersion { get; private set; } = null!; + public int AgentPort { get; private set; } /// - /// 创建时间 + /// IP地址 /// - public DateTime CreatedAt { get; private set; } + [Required] + [MaxLength(45)] + public string IpAddress { get; private set; } = null!; /// - /// 更新时间 + /// 是否启用 /// - public DateTime UpdatedAt { get; private set; } + public bool IsEnabled { get; private set; } = true; /// /// 创建设备 @@ -97,10 +68,10 @@ public class CellularDevice : AuditableEntity string name, string serialNumber, string description, - string deviceTypeId, - string statusId, string protocolVersionId, - string firmwareVersion) + int agentPort, + string ipAddress, + bool isEnabled = true) { var device = new CellularDevice { @@ -108,10 +79,10 @@ public class CellularDevice : AuditableEntity Name = name, SerialNumber = serialNumber, Description = description, - DeviceTypeId = deviceTypeId, - StatusId = statusId, ProtocolVersionId = protocolVersionId, - FirmwareVersion = firmwareVersion, + AgentPort = agentPort, + IpAddress = ipAddress, + IsEnabled = isEnabled, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; @@ -126,18 +97,36 @@ public class CellularDevice : AuditableEntity string name, string serialNumber, string description, - string deviceTypeId, - string statusId, string protocolVersionId, - string firmwareVersion) + int agentPort, + string ipAddress, + bool isEnabled = true) { Name = name; SerialNumber = serialNumber; Description = description; - DeviceTypeId = deviceTypeId; - StatusId = statusId; ProtocolVersionId = protocolVersionId; - FirmwareVersion = firmwareVersion; + AgentPort = agentPort; + IpAddress = ipAddress; + IsEnabled = isEnabled; + UpdatedAt = DateTime.UtcNow; + } + + /// + /// 启用设备 + /// + public void Enable() + { + IsEnabled = true; + UpdatedAt = DateTime.UtcNow; + } + + /// + /// 禁用设备 + /// + public void Disable() + { + IsEnabled = false; UpdatedAt = DateTime.UtcNow; } } \ No newline at end of file diff --git a/src/X1.Domain/Entities/Device/DeviceStatus.cs b/src/X1.Domain/Entities/Device/DeviceStatus.cs deleted file mode 100644 index 05534ab..0000000 --- a/src/X1.Domain/Entities/Device/DeviceStatus.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using CellularManagement.Domain.Entities.Common; - -namespace CellularManagement.Domain.Entities.Device; - -/// -/// 设备状态实体 -/// -public class DeviceStatus : AuditableEntity -{ - /// - /// 状态名称 - /// - [Required] - [MaxLength(50)] - public string Name { get; set; } = null!; - - /// - /// 状态代码 - /// - [Required] - [MaxLength(20)] - public string Code { get; set; } = null!; - - /// - /// 状态描述 - /// - [MaxLength(200)] - public string? Description { get; set; } - - /// - /// 状态颜色 - /// - [MaxLength(20)] - public string? Color { get; set; } - - /// - /// 是否启用 - /// - public bool IsEnabled { get; set; } = true; - - /// - /// 排序号 - /// - public int SortOrder { get; set; } -} \ No newline at end of file diff --git a/src/X1.Domain/Entities/Device/DeviceType.cs b/src/X1.Domain/Entities/Device/DeviceType.cs deleted file mode 100644 index 4ecf5c2..0000000 --- a/src/X1.Domain/Entities/Device/DeviceType.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using CellularManagement.Domain.Entities.Common; - -namespace CellularManagement.Domain.Entities.Device; - -/// -/// 设备类型实体 -/// -public class DeviceType : AuditableEntity -{ - /// - /// 类型名称 - /// - [Required] - [MaxLength(50)] - public string Name { get; set; } = null!; - - /// - /// 类型代码 - /// - [Required] - [MaxLength(20)] - public string Code { get; set; } = null!; - - /// - /// 类型描述 - /// - [MaxLength(200)] - public string? Description { get; set; } - - /// - /// 是否启用 - /// - public bool IsEnabled { get; set; } = true; - - /// - /// 排序号 - /// - public int SortOrder { get; set; } -} \ No newline at end of file diff --git a/src/X1.Domain/Entities/Device/ProtocolVersion.cs b/src/X1.Domain/Entities/Device/ProtocolVersion.cs index b847ccd..b627afa 100644 --- a/src/X1.Domain/Entities/Device/ProtocolVersion.cs +++ b/src/X1.Domain/Entities/Device/ProtocolVersion.cs @@ -8,51 +8,124 @@ namespace CellularManagement.Domain.Entities.Device; /// public class ProtocolVersion : AuditableEntity { + private ProtocolVersion() { } + /// /// 版本名称 /// [Required] [MaxLength(50)] - public string Name { get; set; } = null!; + public string Name { get; private set; } = null!; /// /// 版本号 /// [Required] [MaxLength(20)] - public string Version { get; set; } = null!; - - /// - /// 协议类型 - /// - [Required] - [MaxLength(50)] - public string ProtocolType { get; set; } = null!; + public string Version { get; private set; } = null!; /// /// 版本描述 /// [MaxLength(500)] - public string? Description { get; set; } + public string? Description { get; private set; } /// /// 是否启用 /// - public bool IsEnabled { get; set; } = true; + public bool IsEnabled { get; private set; } = true; /// /// 发布日期 /// - public DateTime? ReleaseDate { get; set; } + public DateTime? ReleaseDate { get; private set; } /// /// 是否强制更新 /// - public bool IsForceUpdate { get; set; } + public bool IsForceUpdate { get; private set; } /// /// 最低支持版本 /// [MaxLength(20)] - public string? MinimumSupportedVersion { get; set; } + public string? MinimumSupportedVersion { get; private set; } + + /// + /// 创建协议版本 + /// + public static ProtocolVersion Create( + string name, + string version, + string? description = null, + bool isEnabled = true, + DateTime? releaseDate = null, + bool isForceUpdate = false, + string? minimumSupportedVersion = null) + { + var protocolVersion = new ProtocolVersion + { + Id = Guid.NewGuid().ToString(), + Name = name, + Version = version, + Description = description, + IsEnabled = isEnabled, + ReleaseDate = releaseDate, + IsForceUpdate = isForceUpdate, + MinimumSupportedVersion = minimumSupportedVersion, + CreatedAt = DateTime.UtcNow, + UpdatedAt = DateTime.UtcNow + }; + + return protocolVersion; + } + + /// + /// 更新协议版本 + /// + public void Update( + string name, + string version, + string? description = null, + bool isEnabled = true, + DateTime? releaseDate = null, + bool isForceUpdate = false, + string? minimumSupportedVersion = null) + { + Name = name; + Version = version; + Description = description; + IsEnabled = isEnabled; + ReleaseDate = releaseDate; + IsForceUpdate = isForceUpdate; + MinimumSupportedVersion = minimumSupportedVersion; + UpdatedAt = DateTime.UtcNow; + } + + /// + /// 启用协议版本 + /// + public void Enable() + { + IsEnabled = true; + UpdatedAt = DateTime.UtcNow; + } + + /// + /// 禁用协议版本 + /// + public void Disable() + { + IsEnabled = false; + UpdatedAt = DateTime.UtcNow; + } + + /// + /// 设置强制更新 + /// + public void SetForceUpdate(bool isForceUpdate) + { + IsForceUpdate = isForceUpdate; + UpdatedAt = DateTime.UtcNow; + } } \ No newline at end of file diff --git a/src/X1.Domain/Repositories/Device/ICellularDeviceRepository.cs b/src/X1.Domain/Repositories/Device/ICellularDeviceRepository.cs index 9fd651c..97d133f 100644 --- a/src/X1.Domain/Repositories/Device/ICellularDeviceRepository.cs +++ b/src/X1.Domain/Repositories/Device/ICellularDeviceRepository.cs @@ -10,7 +10,6 @@ namespace CellularManagement.Domain.Repositories.Device; /// public interface ICellularDeviceRepository : IBaseRepository { - /// /// 添加蜂窝设备 /// @@ -19,28 +18,13 @@ public interface ICellularDeviceRepository : IBaseRepository /// /// 更新蜂窝设备 /// - Task UpdateDeviceAsync(CellularDevice device, CancellationToken cancellationToken = default); + void UpdateDevice(CellularDevice device); /// /// 删除蜂窝设备 /// Task DeleteDeviceAsync(string id, CancellationToken cancellationToken = default); - /// - /// 批量添加蜂窝设备 - /// - Task AddDevicesAsync(IEnumerable devices, CancellationToken cancellationToken = default); - - /// - /// 批量更新蜂窝设备 - /// - Task UpdateDevicesAsync(IEnumerable devices, CancellationToken cancellationToken = default); - - /// - /// 批量删除蜂窝设备 - /// - Task DeleteDevicesAsync(IEnumerable ids, CancellationToken cancellationToken = default); - /// /// 获取所有蜂窝设备 /// @@ -56,23 +40,11 @@ public interface ICellularDeviceRepository : IBaseRepository /// Task GetDeviceBySerialNumberAsync(string serialNumber, CancellationToken cancellationToken = default); - /// - /// 获取指定类型的蜂窝设备 - /// - Task> GetDevicesByTypeAsync(string deviceTypeId, CancellationToken cancellationToken = default); - - /// - /// 获取指定状态的蜂窝设备 - /// - Task> GetDevicesByStatusAsync(string statusId, CancellationToken cancellationToken = default); - /// /// 搜索蜂窝设备 /// Task> SearchDevicesAsync( string? keyword, - string? deviceTypeId, - string? statusId, CancellationToken cancellationToken = default); /// diff --git a/src/X1.Domain/Repositories/Device/IProtocolVersionRepository.cs b/src/X1.Domain/Repositories/Device/IProtocolVersionRepository.cs new file mode 100644 index 0000000..1163562 --- /dev/null +++ b/src/X1.Domain/Repositories/Device/IProtocolVersionRepository.cs @@ -0,0 +1,63 @@ +using CellularManagement.Domain.Entities; +using CellularManagement.Domain.Entities.Device; +using CellularManagement.Domain.Repositories.Base; + +namespace CellularManagement.Domain.Repositories.Device; + +/// +/// 协议版本仓储接口 +/// +public interface IProtocolVersionRepository : IBaseRepository +{ + /// + /// 添加协议版本 + /// + Task AddProtocolVersionAsync(ProtocolVersion protocolVersion, CancellationToken cancellationToken = default); + + /// + /// 更新协议版本 + /// + void UpdateProtocolVersion(ProtocolVersion protocolVersion); + + /// + /// 删除协议版本 + /// + Task DeleteProtocolVersionAsync(string id, CancellationToken cancellationToken = default); + + /// + /// 获取所有协议版本 + /// + Task> GetAllProtocolVersionsAsync(CancellationToken cancellationToken = default); + + /// + /// 根据ID获取协议版本 + /// + Task GetProtocolVersionByIdAsync(string id, CancellationToken cancellationToken = default); + + /// + /// 根据版本号获取协议版本 + /// + Task GetProtocolVersionByVersionAsync(string version, CancellationToken cancellationToken = default); + + /// + /// 搜索协议版本 + /// + Task> SearchProtocolVersionsAsync( + string? keyword, + CancellationToken cancellationToken = default); + + /// + /// 检查协议版本是否存在 + /// + Task ExistsAsync(string id, CancellationToken cancellationToken = default); + + /// + /// 检查版本号是否存在 + /// + Task VersionExistsAsync(string version, CancellationToken cancellationToken = default); + + /// + /// 获取启用的协议版本 + /// + Task> GetEnabledProtocolVersionsAsync(CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/src/X1.Infrastructure/Configurations/Device/CellularDeviceConfiguration.cs b/src/X1.Infrastructure/Configurations/Device/CellularDeviceConfiguration.cs index bcc4070..2faa370 100644 --- a/src/X1.Infrastructure/Configurations/Device/CellularDeviceConfiguration.cs +++ b/src/X1.Infrastructure/Configurations/Device/CellularDeviceConfiguration.cs @@ -13,8 +13,6 @@ public class CellularDeviceConfiguration : IEntityTypeConfiguration d.SerialNumber).IsUnique().HasDatabaseName("IX_CellularDevices_SerialNumber"); - builder.HasIndex(d => d.DeviceTypeId).HasDatabaseName("IX_CellularDevices_DeviceTypeId"); - builder.HasIndex(d => d.StatusId).HasDatabaseName("IX_CellularDevices_StatusId"); builder.HasIndex(d => d.ProtocolVersionId).HasDatabaseName("IX_CellularDevices_ProtocolVersionId"); // 配置属性 @@ -22,24 +20,13 @@ public class CellularDeviceConfiguration : IEntityTypeConfiguration d.Name).IsRequired().HasMaxLength(100).HasComment("设备名称"); builder.Property(d => d.SerialNumber).IsRequired().HasMaxLength(50).HasComment("序列号"); builder.Property(d => d.Description).HasMaxLength(500).HasComment("设备描述"); - builder.Property(d => d.DeviceTypeId).IsRequired().HasMaxLength(50).HasComment("设备类型ID"); - builder.Property(d => d.StatusId).IsRequired().HasMaxLength(50).HasComment("设备状态ID"); builder.Property(d => d.ProtocolVersionId).IsRequired().HasMaxLength(50).HasComment("协议版本ID"); - builder.Property(d => d.FirmwareVersion).HasMaxLength(50).HasComment("固件版本"); + builder.Property(d => d.AgentPort).IsRequired().HasComment("Agent端口"); + builder.Property(d => d.IsEnabled).IsRequired().HasComment("是否启用"); builder.Property(d => d.CreatedAt).IsRequired().HasComment("创建时间"); builder.Property(d => d.UpdatedAt).IsRequired().HasComment("更新时间"); - // 配置外键关系 - builder.HasOne(d => d.DeviceType) - .WithMany() - .HasForeignKey(d => d.DeviceTypeId) - .OnDelete(DeleteBehavior.Restrict); - - builder.HasOne(d => d.Status) - .WithMany() - .HasForeignKey(d => d.StatusId) - .OnDelete(DeleteBehavior.Restrict); - + // 配置关系 builder.HasOne(d => d.ProtocolVersion) .WithMany() .HasForeignKey(d => d.ProtocolVersionId) diff --git a/src/X1.Infrastructure/Configurations/Device/DeviceStatusConfiguration.cs b/src/X1.Infrastructure/Configurations/Device/DeviceStatusConfiguration.cs deleted file mode 100644 index fc5a16d..0000000 --- a/src/X1.Infrastructure/Configurations/Device/DeviceStatusConfiguration.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using CellularManagement.Domain.Entities.Device; - -namespace CellularManagement.Infrastructure.Configurations.Device; - -public class DeviceStatusConfiguration : IEntityTypeConfiguration -{ - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable("DeviceStatuses", t => t.HasComment("设备状态表")); - builder.HasKey(s => s.Id); - - // 配置属性 - builder.Property(s => s.Id).HasComment("状态ID"); - builder.Property(s => s.Name).IsRequired().HasMaxLength(50).HasComment("状态名称"); - builder.Property(s => s.Description).HasMaxLength(200).HasComment("状态描述"); - builder.Property(s => s.CreatedAt).IsRequired().HasComment("创建时间"); - builder.Property(s => s.UpdatedAt).IsRequired().HasComment("更新时间"); - } -} \ No newline at end of file diff --git a/src/X1.Infrastructure/Configurations/Device/DeviceTypeConfiguration.cs b/src/X1.Infrastructure/Configurations/Device/DeviceTypeConfiguration.cs deleted file mode 100644 index 6da0737..0000000 --- a/src/X1.Infrastructure/Configurations/Device/DeviceTypeConfiguration.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using CellularManagement.Domain.Entities.Device; - -namespace CellularManagement.Infrastructure.Configurations.Device; - -public class DeviceTypeConfiguration : IEntityTypeConfiguration -{ - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable("DeviceTypes", t => t.HasComment("设备类型表")); - builder.HasKey(t => t.Id); - - // 配置属性 - builder.Property(t => t.Id).HasComment("类型ID"); - builder.Property(t => t.Name).IsRequired().HasMaxLength(50).HasComment("类型名称"); - builder.Property(t => t.Description).HasMaxLength(200).HasComment("类型描述"); - builder.Property(t => t.CreatedAt).IsRequired().HasComment("创建时间"); - builder.Property(t => t.UpdatedAt).IsRequired().HasComment("更新时间"); - } -} \ No newline at end of file diff --git a/src/X1.Infrastructure/Configurations/Device/ProtocolVersionConfiguration.cs b/src/X1.Infrastructure/Configurations/Device/ProtocolVersionConfiguration.cs index c338707..b3afa3d 100644 --- a/src/X1.Infrastructure/Configurations/Device/ProtocolVersionConfiguration.cs +++ b/src/X1.Infrastructure/Configurations/Device/ProtocolVersionConfiguration.cs @@ -16,8 +16,13 @@ public class ProtocolVersionConfiguration : IEntityTypeConfiguration v.Id).HasComment("版本ID"); - builder.Property(v => v.Version).IsRequired().HasMaxLength(50).HasComment("版本号"); - builder.Property(v => v.Description).HasMaxLength(200).HasComment("版本描述"); + builder.Property(v => v.Name).IsRequired().HasMaxLength(50).HasComment("版本名称"); + builder.Property(v => v.Version).IsRequired().HasMaxLength(20).HasComment("版本号"); + builder.Property(v => v.Description).HasMaxLength(500).HasComment("版本描述"); + builder.Property(v => v.IsEnabled).IsRequired().HasComment("是否启用"); + builder.Property(v => v.ReleaseDate).HasComment("发布日期"); + builder.Property(v => v.IsForceUpdate).IsRequired().HasComment("是否强制更新"); + builder.Property(v => v.MinimumSupportedVersion).HasMaxLength(20).HasComment("最低支持版本"); builder.Property(v => v.CreatedAt).IsRequired().HasComment("创建时间"); builder.Property(v => v.UpdatedAt).IsRequired().HasComment("更新时间"); } diff --git a/src/X1.Infrastructure/Context/AppDbContext.cs b/src/X1.Infrastructure/Context/AppDbContext.cs index b94338a..88977b8 100644 --- a/src/X1.Infrastructure/Context/AppDbContext.cs +++ b/src/X1.Infrastructure/Context/AppDbContext.cs @@ -41,16 +41,6 @@ public class AppDbContext : IdentityDbContext /// public DbSet CellularDevices { get; set; } = null!; - /// - /// 设备类型集合 - /// - public DbSet DeviceTypes { get; set; } = null!; - - /// - /// 设备状态集合 - /// - public DbSet DeviceStatuses { get; set; } = null!; - /// /// 协议版本集合 /// diff --git a/src/X1.Infrastructure/DependencyInjection.cs b/src/X1.Infrastructure/DependencyInjection.cs index a4103c9..1313291 100644 --- a/src/X1.Infrastructure/DependencyInjection.cs +++ b/src/X1.Infrastructure/DependencyInjection.cs @@ -23,6 +23,8 @@ using CellularManagement.Infrastructure.Services.Authentication; using CellularManagement.Infrastructure.Services.Infrastructure; using CellularManagement.Infrastructure.Services.Security; using CellularManagement.Infrastructure.Services.UserManagement; +using CellularManagement.Domain.Repositories.Device; +using CellularManagement.Infrastructure.Repositories.Device; namespace CellularManagement.Infrastructure; @@ -170,6 +172,10 @@ public static class DependencyInjection services.AddScoped(); services.AddScoped(); + // 注册设备相关仓储 + services.AddScoped(); + services.AddScoped(); + return services; } diff --git a/src/X1.Infrastructure/Migrations/20250606094331_InitialCreate.Designer.cs b/src/X1.Infrastructure/Migrations/20250705165102_InitialCreate.Designer.cs similarity index 78% rename from src/X1.Infrastructure/Migrations/20250606094331_InitialCreate.Designer.cs rename to src/X1.Infrastructure/Migrations/20250705165102_InitialCreate.Designer.cs index 726f319..5dd955d 100644 --- a/src/X1.Infrastructure/Migrations/20250606094331_InitialCreate.Designer.cs +++ b/src/X1.Infrastructure/Migrations/20250705165102_InitialCreate.Designer.cs @@ -9,10 +9,10 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable -namespace CellularManagement.Infrastructure.Migrations +namespace X1.Infrastructure.Migrations { [DbContext(typeof(AppDbContext))] - [Migration("20250606094331_InitialCreate")] + [Migration("20250705165102_InitialCreate")] partial class InitialCreate { /// @@ -209,6 +209,10 @@ namespace CellularManagement.Infrastructure.Migrations .HasColumnType("text") .HasComment("设备ID"); + b.Property("AgentPort") + .HasColumnType("integer") + .HasComment("Agent端口"); + b.Property("CreatedAt") .HasColumnType("timestamp with time zone") .HasComment("创建时间"); @@ -223,21 +227,18 @@ namespace CellularManagement.Infrastructure.Migrations .HasColumnType("character varying(500)") .HasComment("设备描述"); - b.Property("DeviceTypeId") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("设备类型ID"); - - b.Property("FirmwareVersion") + b.Property("IpAddress") .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("固件版本"); + .HasMaxLength(45) + .HasColumnType("character varying(45)"); b.Property("IsDeleted") .HasColumnType("boolean"); + b.Property("IsEnabled") + .HasColumnType("boolean") + .HasComment("是否启用"); + b.Property("Name") .IsRequired() .HasMaxLength(100) @@ -256,13 +257,8 @@ namespace CellularManagement.Infrastructure.Migrations .HasColumnType("character varying(50)") .HasComment("序列号"); - b.Property("StatusId") + b.Property("UpdatedAt") .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("设备状态ID"); - - b.Property("UpdatedAt") .HasColumnType("timestamp with time zone") .HasComment("更新时间"); @@ -271,9 +267,6 @@ namespace CellularManagement.Infrastructure.Migrations b.HasKey("Id"); - b.HasIndex("DeviceTypeId") - .HasDatabaseName("IX_CellularDevices_DeviceTypeId"); - b.HasIndex("ProtocolVersionId") .HasDatabaseName("IX_CellularDevices_ProtocolVersionId"); @@ -281,129 +274,12 @@ namespace CellularManagement.Infrastructure.Migrations .IsUnique() .HasDatabaseName("IX_CellularDevices_SerialNumber"); - b.HasIndex("StatusId") - .HasDatabaseName("IX_CellularDevices_StatusId"); - b.ToTable("CellularDevices", null, t => { t.HasComment("蜂窝设备表"); }); }); - modelBuilder.Entity("CellularManagement.Domain.Entities.Device.DeviceStatus", b => - { - b.Property("Id") - .HasColumnType("text") - .HasComment("状态ID"); - - b.Property("Code") - .IsRequired() - .HasMaxLength(20) - .HasColumnType("character varying(20)"); - - b.Property("Color") - .HasMaxLength(20) - .HasColumnType("character varying(20)"); - - b.Property("CreatedAt") - .HasColumnType("timestamp with time zone") - .HasComment("创建时间"); - - b.Property("CreatedBy") - .IsRequired() - .HasColumnType("text"); - - b.Property("Description") - .HasMaxLength(200) - .HasColumnType("character varying(200)") - .HasComment("状态描述"); - - b.Property("IsDeleted") - .HasColumnType("boolean"); - - b.Property("IsEnabled") - .HasColumnType("boolean"); - - b.Property("Name") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("状态名称"); - - b.Property("SortOrder") - .HasColumnType("integer"); - - b.Property("UpdatedAt") - .IsRequired() - .HasColumnType("timestamp with time zone") - .HasComment("更新时间"); - - b.Property("UpdatedBy") - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("DeviceStatuses", null, t => - { - t.HasComment("设备状态表"); - }); - }); - - modelBuilder.Entity("CellularManagement.Domain.Entities.Device.DeviceType", b => - { - b.Property("Id") - .HasColumnType("text") - .HasComment("类型ID"); - - b.Property("Code") - .IsRequired() - .HasMaxLength(20) - .HasColumnType("character varying(20)"); - - b.Property("CreatedAt") - .HasColumnType("timestamp with time zone") - .HasComment("创建时间"); - - b.Property("CreatedBy") - .IsRequired() - .HasColumnType("text"); - - b.Property("Description") - .HasMaxLength(200) - .HasColumnType("character varying(200)") - .HasComment("类型描述"); - - b.Property("IsDeleted") - .HasColumnType("boolean"); - - b.Property("IsEnabled") - .HasColumnType("boolean"); - - b.Property("Name") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("类型名称"); - - b.Property("SortOrder") - .HasColumnType("integer"); - - b.Property("UpdatedAt") - .IsRequired() - .HasColumnType("timestamp with time zone") - .HasComment("更新时间"); - - b.Property("UpdatedBy") - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("DeviceTypes", null, t => - { - t.HasComment("设备类型表"); - }); - }); - modelBuilder.Entity("CellularManagement.Domain.Entities.Device.ProtocolVersion", b => { b.Property("Id") @@ -640,29 +516,13 @@ namespace CellularManagement.Infrastructure.Migrations modelBuilder.Entity("CellularManagement.Domain.Entities.Device.CellularDevice", b => { - b.HasOne("CellularManagement.Domain.Entities.Device.DeviceType", "DeviceType") - .WithMany() - .HasForeignKey("DeviceTypeId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - b.HasOne("CellularManagement.Domain.Entities.Device.ProtocolVersion", "ProtocolVersion") .WithMany() .HasForeignKey("ProtocolVersionId") .OnDelete(DeleteBehavior.Restrict) .IsRequired(); - b.HasOne("CellularManagement.Domain.Entities.Device.DeviceStatus", "Status") - .WithMany() - .HasForeignKey("StatusId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("DeviceType"); - b.Navigation("ProtocolVersion"); - - b.Navigation("Status"); }); modelBuilder.Entity("CellularManagement.Domain.Entities.Logging.LoginLog", b => diff --git a/src/X1.Infrastructure/Migrations/20250606094331_InitialCreate.cs b/src/X1.Infrastructure/Migrations/20250705165102_InitialCreate.cs similarity index 79% rename from src/X1.Infrastructure/Migrations/20250606094331_InitialCreate.cs rename to src/X1.Infrastructure/Migrations/20250705165102_InitialCreate.cs index 8a20569..0d13ec5 100644 --- a/src/X1.Infrastructure/Migrations/20250606094331_InitialCreate.cs +++ b/src/X1.Infrastructure/Migrations/20250705165102_InitialCreate.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations; #nullable disable -namespace CellularManagement.Infrastructure.Migrations +namespace X1.Infrastructure.Migrations { /// public partial class InitialCreate : Migration @@ -11,51 +11,6 @@ namespace CellularManagement.Infrastructure.Migrations /// protected override void Up(MigrationBuilder migrationBuilder) { - migrationBuilder.CreateTable( - name: "DeviceStatuses", - columns: table => new - { - Id = table.Column(type: "text", nullable: false, comment: "状态ID"), - Name = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, comment: "状态名称"), - Code = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), - Description = table.Column(type: "character varying(200)", maxLength: 200, nullable: true, comment: "状态描述"), - Color = table.Column(type: "character varying(20)", maxLength: 20, nullable: true), - IsEnabled = table.Column(type: "boolean", nullable: false), - SortOrder = table.Column(type: "integer", nullable: false), - CreatedAt = table.Column(type: "timestamp with time zone", nullable: false, comment: "创建时间"), - UpdatedAt = table.Column(type: "timestamp with time zone", nullable: false, comment: "更新时间"), - IsDeleted = table.Column(type: "boolean", nullable: false), - CreatedBy = table.Column(type: "text", nullable: false), - UpdatedBy = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_DeviceStatuses", x => x.Id); - }, - comment: "设备状态表"); - - migrationBuilder.CreateTable( - name: "DeviceTypes", - columns: table => new - { - Id = table.Column(type: "text", nullable: false, comment: "类型ID"), - Name = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, comment: "类型名称"), - Code = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), - Description = table.Column(type: "character varying(200)", maxLength: 200, nullable: true, comment: "类型描述"), - IsEnabled = table.Column(type: "boolean", nullable: false), - SortOrder = table.Column(type: "integer", nullable: false), - CreatedAt = table.Column(type: "timestamp with time zone", nullable: false, comment: "创建时间"), - UpdatedAt = table.Column(type: "timestamp with time zone", nullable: false, comment: "更新时间"), - IsDeleted = table.Column(type: "boolean", nullable: false), - CreatedBy = table.Column(type: "text", nullable: false), - UpdatedBy = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_DeviceTypes", x => x.Id); - }, - comment: "设备类型表"); - migrationBuilder.CreateTable( name: "Permissions", columns: table => new @@ -155,10 +110,10 @@ namespace CellularManagement.Infrastructure.Migrations Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false, comment: "设备名称"), SerialNumber = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, comment: "序列号"), Description = table.Column(type: "character varying(500)", maxLength: 500, nullable: false, comment: "设备描述"), - DeviceTypeId = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, comment: "设备类型ID"), - StatusId = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, comment: "设备状态ID"), ProtocolVersionId = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, comment: "协议版本ID"), - FirmwareVersion = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, comment: "固件版本"), + AgentPort = table.Column(type: "integer", nullable: false, comment: "Agent端口"), + IpAddress = table.Column(type: "character varying(45)", maxLength: 45, nullable: false), + IsEnabled = table.Column(type: "boolean", nullable: false, comment: "是否启用"), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false, comment: "创建时间"), UpdatedAt = table.Column(type: "timestamp with time zone", nullable: false, comment: "更新时间"), IsDeleted = table.Column(type: "boolean", nullable: false), @@ -168,18 +123,6 @@ namespace CellularManagement.Infrastructure.Migrations constraints: table => { table.PrimaryKey("PK_CellularDevices", x => x.Id); - table.ForeignKey( - name: "FK_CellularDevices_DeviceStatuses_StatusId", - column: x => x.StatusId, - principalTable: "DeviceStatuses", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "FK_CellularDevices_DeviceTypes_DeviceTypeId", - column: x => x.DeviceTypeId, - principalTable: "DeviceTypes", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_CellularDevices_ProtocolVersions_ProtocolVersionId", column: x => x.ProtocolVersionId, @@ -272,11 +215,6 @@ namespace CellularManagement.Infrastructure.Migrations }, comment: "用户角色关系表"); - migrationBuilder.CreateIndex( - name: "IX_CellularDevices_DeviceTypeId", - table: "CellularDevices", - column: "DeviceTypeId"); - migrationBuilder.CreateIndex( name: "IX_CellularDevices_ProtocolVersionId", table: "CellularDevices", @@ -288,11 +226,6 @@ namespace CellularManagement.Infrastructure.Migrations column: "SerialNumber", unique: true); - migrationBuilder.CreateIndex( - name: "IX_CellularDevices_StatusId", - table: "CellularDevices", - column: "StatusId"); - migrationBuilder.CreateIndex( name: "IX_LoginLogs_IpAddress", table: "LoginLogs", @@ -386,12 +319,6 @@ namespace CellularManagement.Infrastructure.Migrations migrationBuilder.DropTable( name: "UserRoles"); - migrationBuilder.DropTable( - name: "DeviceStatuses"); - - migrationBuilder.DropTable( - name: "DeviceTypes"); - migrationBuilder.DropTable( name: "ProtocolVersions"); diff --git a/src/X1.Infrastructure/Migrations/AppDbContextModelSnapshot.cs b/src/X1.Infrastructure/Migrations/AppDbContextModelSnapshot.cs index 7bb9ffd..594bacf 100644 --- a/src/X1.Infrastructure/Migrations/AppDbContextModelSnapshot.cs +++ b/src/X1.Infrastructure/Migrations/AppDbContextModelSnapshot.cs @@ -8,7 +8,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable -namespace CellularManagement.Infrastructure.Migrations +namespace X1.Infrastructure.Migrations { [DbContext(typeof(AppDbContext))] partial class AppDbContextModelSnapshot : ModelSnapshot @@ -206,6 +206,10 @@ namespace CellularManagement.Infrastructure.Migrations .HasColumnType("text") .HasComment("设备ID"); + b.Property("AgentPort") + .HasColumnType("integer") + .HasComment("Agent端口"); + b.Property("CreatedAt") .HasColumnType("timestamp with time zone") .HasComment("创建时间"); @@ -220,21 +224,18 @@ namespace CellularManagement.Infrastructure.Migrations .HasColumnType("character varying(500)") .HasComment("设备描述"); - b.Property("DeviceTypeId") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("设备类型ID"); - - b.Property("FirmwareVersion") + b.Property("IpAddress") .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("固件版本"); + .HasMaxLength(45) + .HasColumnType("character varying(45)"); b.Property("IsDeleted") .HasColumnType("boolean"); + b.Property("IsEnabled") + .HasColumnType("boolean") + .HasComment("是否启用"); + b.Property("Name") .IsRequired() .HasMaxLength(100) @@ -253,13 +254,8 @@ namespace CellularManagement.Infrastructure.Migrations .HasColumnType("character varying(50)") .HasComment("序列号"); - b.Property("StatusId") + b.Property("UpdatedAt") .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("设备状态ID"); - - b.Property("UpdatedAt") .HasColumnType("timestamp with time zone") .HasComment("更新时间"); @@ -268,9 +264,6 @@ namespace CellularManagement.Infrastructure.Migrations b.HasKey("Id"); - b.HasIndex("DeviceTypeId") - .HasDatabaseName("IX_CellularDevices_DeviceTypeId"); - b.HasIndex("ProtocolVersionId") .HasDatabaseName("IX_CellularDevices_ProtocolVersionId"); @@ -278,129 +271,12 @@ namespace CellularManagement.Infrastructure.Migrations .IsUnique() .HasDatabaseName("IX_CellularDevices_SerialNumber"); - b.HasIndex("StatusId") - .HasDatabaseName("IX_CellularDevices_StatusId"); - b.ToTable("CellularDevices", null, t => { t.HasComment("蜂窝设备表"); }); }); - modelBuilder.Entity("CellularManagement.Domain.Entities.Device.DeviceStatus", b => - { - b.Property("Id") - .HasColumnType("text") - .HasComment("状态ID"); - - b.Property("Code") - .IsRequired() - .HasMaxLength(20) - .HasColumnType("character varying(20)"); - - b.Property("Color") - .HasMaxLength(20) - .HasColumnType("character varying(20)"); - - b.Property("CreatedAt") - .HasColumnType("timestamp with time zone") - .HasComment("创建时间"); - - b.Property("CreatedBy") - .IsRequired() - .HasColumnType("text"); - - b.Property("Description") - .HasMaxLength(200) - .HasColumnType("character varying(200)") - .HasComment("状态描述"); - - b.Property("IsDeleted") - .HasColumnType("boolean"); - - b.Property("IsEnabled") - .HasColumnType("boolean"); - - b.Property("Name") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("状态名称"); - - b.Property("SortOrder") - .HasColumnType("integer"); - - b.Property("UpdatedAt") - .IsRequired() - .HasColumnType("timestamp with time zone") - .HasComment("更新时间"); - - b.Property("UpdatedBy") - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("DeviceStatuses", null, t => - { - t.HasComment("设备状态表"); - }); - }); - - modelBuilder.Entity("CellularManagement.Domain.Entities.Device.DeviceType", b => - { - b.Property("Id") - .HasColumnType("text") - .HasComment("类型ID"); - - b.Property("Code") - .IsRequired() - .HasMaxLength(20) - .HasColumnType("character varying(20)"); - - b.Property("CreatedAt") - .HasColumnType("timestamp with time zone") - .HasComment("创建时间"); - - b.Property("CreatedBy") - .IsRequired() - .HasColumnType("text"); - - b.Property("Description") - .HasMaxLength(200) - .HasColumnType("character varying(200)") - .HasComment("类型描述"); - - b.Property("IsDeleted") - .HasColumnType("boolean"); - - b.Property("IsEnabled") - .HasColumnType("boolean"); - - b.Property("Name") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasComment("类型名称"); - - b.Property("SortOrder") - .HasColumnType("integer"); - - b.Property("UpdatedAt") - .IsRequired() - .HasColumnType("timestamp with time zone") - .HasComment("更新时间"); - - b.Property("UpdatedBy") - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("DeviceTypes", null, t => - { - t.HasComment("设备类型表"); - }); - }); - modelBuilder.Entity("CellularManagement.Domain.Entities.Device.ProtocolVersion", b => { b.Property("Id") @@ -637,29 +513,13 @@ namespace CellularManagement.Infrastructure.Migrations modelBuilder.Entity("CellularManagement.Domain.Entities.Device.CellularDevice", b => { - b.HasOne("CellularManagement.Domain.Entities.Device.DeviceType", "DeviceType") - .WithMany() - .HasForeignKey("DeviceTypeId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - b.HasOne("CellularManagement.Domain.Entities.Device.ProtocolVersion", "ProtocolVersion") .WithMany() .HasForeignKey("ProtocolVersionId") .OnDelete(DeleteBehavior.Restrict) .IsRequired(); - b.HasOne("CellularManagement.Domain.Entities.Device.DeviceStatus", "Status") - .WithMany() - .HasForeignKey("StatusId") - .OnDelete(DeleteBehavior.Restrict) - .IsRequired(); - - b.Navigation("DeviceType"); - b.Navigation("ProtocolVersion"); - - b.Navigation("Status"); }); modelBuilder.Entity("CellularManagement.Domain.Entities.Logging.LoginLog", b => diff --git a/src/X1.Infrastructure/Repositories/Device/CellularDeviceRepository.cs b/src/X1.Infrastructure/Repositories/Device/CellularDeviceRepository.cs index 7c3ab78..9aafabf 100644 --- a/src/X1.Infrastructure/Repositories/Device/CellularDeviceRepository.cs +++ b/src/X1.Infrastructure/Repositories/Device/CellularDeviceRepository.cs @@ -19,8 +19,6 @@ namespace CellularManagement.Infrastructure.Repositories.Device; public class CellularDeviceRepository : BaseRepository, ICellularDeviceRepository { private readonly ILogger _logger; - private readonly IQueryRepository _deviceTypeQueryRepository; - private readonly IQueryRepository _deviceStatusQueryRepository; private readonly IQueryRepository _protocolVersionQueryRepository; /// @@ -29,20 +27,14 @@ public class CellularDeviceRepository : BaseRepository, ICellula public CellularDeviceRepository( ICommandRepository commandRepository, IQueryRepository queryRepository, - IQueryRepository deviceTypeQueryRepository, - IQueryRepository deviceStatusQueryRepository, IQueryRepository protocolVersionQueryRepository, ILogger logger) : base(commandRepository, queryRepository, logger) { _logger = logger; - _deviceTypeQueryRepository = deviceTypeQueryRepository; - _deviceStatusQueryRepository = deviceStatusQueryRepository; _protocolVersionQueryRepository = protocolVersionQueryRepository; } - #region ICellularDeviceCommandRepository 实现 - /// /// 添加蜂窝设备 /// @@ -54,7 +46,7 @@ public class CellularDeviceRepository : BaseRepository, ICellula /// /// 更新蜂窝设备 /// - public async Task UpdateDeviceAsync(CellularDevice device, CancellationToken cancellationToken = default) + public void UpdateDevice(CellularDevice device) { CommandRepository.Update(device); } @@ -67,34 +59,6 @@ public class CellularDeviceRepository : BaseRepository, ICellula await CommandRepository.DeleteByIdAsync(id, cancellationToken); } - /// - /// 批量添加蜂窝设备 - /// - public async Task AddDevicesAsync(IEnumerable devices, CancellationToken cancellationToken = default) - { - await CommandRepository.AddRangeAsync(devices, cancellationToken); - } - - /// - /// 批量更新蜂窝设备 - /// - public async Task UpdateDevicesAsync(IEnumerable devices, CancellationToken cancellationToken = default) - { - CommandRepository.UpdateRange(devices); - } - - /// - /// 批量删除蜂窝设备 - /// - public async Task DeleteDevicesAsync(IEnumerable ids, CancellationToken cancellationToken = default) - { - await CommandRepository.DeleteWhereAsync(d => ids.Contains(d.Id), cancellationToken); - } - - #endregion - - #region ICellularDeviceQueryRepository 实现 - /// /// 获取所有蜂窝设备 /// @@ -120,34 +84,14 @@ public class CellularDeviceRepository : BaseRepository, ICellula return await QueryRepository.FirstOrDefaultAsync(d => d.SerialNumber == serialNumber, cancellationToken); } - /// - /// 获取指定类型的蜂窝设备 - /// - public async Task> GetDevicesByTypeAsync(string deviceTypeId, CancellationToken cancellationToken = default) - { - var devices = await QueryRepository.FindAsync(d => d.DeviceTypeId == deviceTypeId, cancellationToken); - return devices.ToList(); - } - - /// - /// 获取指定状态的蜂窝设备 - /// - public async Task> GetDevicesByStatusAsync(string statusId, CancellationToken cancellationToken = default) - { - var devices = await QueryRepository.FindAsync(d => d.StatusId == statusId, cancellationToken); - return devices.ToList(); - } - /// /// 搜索蜂窝设备 /// public async Task> SearchDevicesAsync( string? keyword, - string? deviceTypeId, - string? statusId, CancellationToken cancellationToken = default) { - var query =await QueryRepository.FindAsync(d => true, cancellationToken); + var query = await QueryRepository.FindAsync(d => true, cancellationToken); if (!string.IsNullOrWhiteSpace(keyword)) { @@ -157,16 +101,6 @@ public class CellularDeviceRepository : BaseRepository, ICellula d.Description.Contains(keyword)); } - if (!string.IsNullOrWhiteSpace(deviceTypeId)) - { - query = query.Where(d => d.DeviceTypeId == deviceTypeId); - } - - if (!string.IsNullOrWhiteSpace(statusId)) - { - query = query.Where(d => d.StatusId == statusId); - } - var devices = query; return devices.ToList(); } @@ -186,6 +120,4 @@ public class CellularDeviceRepository : BaseRepository, ICellula { return await QueryRepository.AnyAsync(d => d.SerialNumber == serialNumber, cancellationToken); } - - #endregion } \ No newline at end of file diff --git a/src/X1.Infrastructure/Repositories/Device/ProtocolVersionRepository.cs b/src/X1.Infrastructure/Repositories/Device/ProtocolVersionRepository.cs new file mode 100644 index 0000000..4afd62a --- /dev/null +++ b/src/X1.Infrastructure/Repositories/Device/ProtocolVersionRepository.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using System.Linq; +using Microsoft.Extensions.Logging; +using CellularManagement.Domain.Entities; +using CellularManagement.Domain.Repositories; +using CellularManagement.Infrastructure.Repositories.Base; +using CellularManagement.Domain.Entities.Device; +using CellularManagement.Domain.Repositories.Base; +using CellularManagement.Domain.Repositories.Device; + +namespace CellularManagement.Infrastructure.Repositories.Device; + +/// +/// 协议版本仓储实现类 +/// +public class ProtocolVersionRepository : BaseRepository, IProtocolVersionRepository +{ + private readonly ILogger _logger; + + /// + /// 初始化仓储 + /// + public ProtocolVersionRepository( + ICommandRepository commandRepository, + IQueryRepository queryRepository, + ILogger logger) + : base(commandRepository, queryRepository, logger) + { + _logger = logger; + } + + /// + /// 添加协议版本 + /// + public async Task AddProtocolVersionAsync(ProtocolVersion protocolVersion, CancellationToken cancellationToken = default) + { + return await CommandRepository.AddAsync(protocolVersion, cancellationToken); + } + + /// + /// 更新协议版本 + /// + public void UpdateProtocolVersion(ProtocolVersion protocolVersion) + { + CommandRepository.Update(protocolVersion); + } + + /// + /// 删除协议版本 + /// + public async Task DeleteProtocolVersionAsync(string id, CancellationToken cancellationToken = default) + { + await CommandRepository.DeleteByIdAsync(id, cancellationToken); + } + + /// + /// 获取所有协议版本 + /// + public async Task> GetAllProtocolVersionsAsync(CancellationToken cancellationToken = default) + { + var protocolVersions = await QueryRepository.GetAllAsync(cancellationToken); + return protocolVersions.ToList(); + } + + /// + /// 根据ID获取协议版本 + /// + public async Task GetProtocolVersionByIdAsync(string id, CancellationToken cancellationToken = default) + { + return await QueryRepository.GetByIdAsync(id, cancellationToken); + } + + /// + /// 根据版本号获取协议版本 + /// + public async Task GetProtocolVersionByVersionAsync(string version, CancellationToken cancellationToken = default) + { + return await QueryRepository.FirstOrDefaultAsync(pv => pv.Version == version, cancellationToken); + } + + /// + /// 搜索协议版本 + /// + public async Task> SearchProtocolVersionsAsync( + string? keyword, + CancellationToken cancellationToken = default) + { + var query = await QueryRepository.FindAsync(pv => true, cancellationToken); + + if (!string.IsNullOrWhiteSpace(keyword)) + { + query = query.Where(pv => + pv.Name.Contains(keyword) || + pv.Version.Contains(keyword) || + pv.Description.Contains(keyword)); + } + + var protocolVersions = query; + return protocolVersions.ToList(); + } + + /// + /// 检查协议版本是否存在 + /// + public async Task ExistsAsync(string id, CancellationToken cancellationToken = default) + { + return await QueryRepository.AnyAsync(pv => pv.Id == id, cancellationToken); + } + + /// + /// 检查版本号是否存在 + /// + public async Task VersionExistsAsync(string version, CancellationToken cancellationToken = default) + { + return await QueryRepository.AnyAsync(pv => pv.Version == version, cancellationToken); + } + + /// + /// 获取启用的协议版本 + /// + public async Task> GetEnabledProtocolVersionsAsync(CancellationToken cancellationToken = default) + { + var protocolVersions = await QueryRepository.FindAsync(pv => pv.IsEnabled, cancellationToken); + return protocolVersions.ToList(); + } +} \ No newline at end of file diff --git a/src/X1.Presentation/Controllers/ProtocolVersionsController.cs b/src/X1.Presentation/Controllers/ProtocolVersionsController.cs new file mode 100644 index 0000000..aad30cf --- /dev/null +++ b/src/X1.Presentation/Controllers/ProtocolVersionsController.cs @@ -0,0 +1,135 @@ +using CellularManagement.Application.Features.ProtocolVersions.Commands.CreateProtocolVersion; +using CellularManagement.Application.Features.ProtocolVersions.Commands.DeleteProtocolVersion; +using CellularManagement.Application.Features.ProtocolVersions.Commands.UpdateProtocolVersion; +using CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersionById; +using CellularManagement.Application.Features.ProtocolVersions.Queries.GetProtocolVersions; +using CellularManagement.Domain.Common; +using CellularManagement.Presentation.Abstractions; +using MediatR; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace CellularManagement.Presentation.Controllers; + +/// +/// 协议版本管理控制器 +/// +[Route("api/protocolversions")] +[ApiController] +[Authorize] +public class ProtocolVersionsController : ApiController +{ + private readonly ILogger _logger; + + /// + /// 初始化协议版本控制器 + /// + public ProtocolVersionsController(IMediator mediator, ILogger logger) + : base(mediator) + { + _logger = logger; + } + + /// + /// 获取协议版本列表 + /// + [HttpGet] + public async Task> GetAll([FromQuery] GetProtocolVersionsQuery query) + { + _logger.LogInformation("开始获取协议版本列表,页码: {PageNumber}, 每页数量: {PageSize}, 搜索关键词: {SearchTerm}, 是否启用: {IsEnabled}", + query.PageNumber, query.PageSize, query.SearchTerm, query.IsEnabled); + + var result = await mediator.Send(query); + if (!result.IsSuccess) + { + _logger.LogWarning("获取协议版本列表失败: {Message}", result.ErrorMessages); + return result; + } + + _logger.LogInformation("成功获取协议版本列表,共 {Count} 条记录", result.Data?.TotalCount ?? 0); + return result; + } + + /// + /// 获取协议版本详情 + /// + [HttpGet("{id}")] + public async Task> GetById(string id) + { + _logger.LogInformation("开始获取协议版本详情,版本ID: {ProtocolVersionId}", id); + + var result = await mediator.Send(new GetProtocolVersionByIdQuery { ProtocolVersionId = id }); + if (!result.IsSuccess) + { + _logger.LogWarning("获取协议版本详情失败: {Message}", result.ErrorMessages); + return result; + } + + _logger.LogInformation("成功获取协议版本详情,版本ID: {ProtocolVersionId}", id); + return result; + } + + /// + /// 创建协议版本 + /// + [HttpPost] + public async Task> Create([FromBody] CreateProtocolVersionCommand command) + { + _logger.LogInformation("开始创建协议版本,版本名称: {Name}, 版本号: {Version}", command.Name, command.Version); + + var result = await mediator.Send(command); + if (!result.IsSuccess) + { + _logger.LogWarning("创建协议版本失败: {Message}", result.ErrorMessages); + return result; + } + + _logger.LogInformation("成功创建协议版本,版本ID: {ProtocolVersionId}", result.Data.ProtocolVersionId); + return result; + } + + /// + /// 更新协议版本 + /// + [HttpPut("{id}")] + public async Task> Update(string id, [FromBody] UpdateProtocolVersionCommand command) + { + _logger.LogInformation("开始更新协议版本,版本ID: {ProtocolVersionId}", id); + + if (id != command.ProtocolVersionId) + { + _logger.LogWarning("协议版本ID不匹配,路径ID: {PathId}, 命令ID: {CommandId}", id, command.ProtocolVersionId); + return OperationResult.CreateFailure("协议版本ID不匹配"); + } + + var result = await mediator.Send(command); + if (!result.IsSuccess) + { + _logger.LogWarning("更新协议版本失败: {Message}", result.ErrorMessages); + return result; + } + + _logger.LogInformation("成功更新协议版本,版本ID: {ProtocolVersionId}", id); + return result; + } + + /// + /// 删除协议版本 + /// + [HttpDelete("{id}")] + public async Task> Delete(string id) + { + _logger.LogInformation("开始删除协议版本,版本ID: {ProtocolVersionId}", id); + + var result = await mediator.Send(new DeleteProtocolVersionCommand { ProtocolVersionId = id }); + if (!result.IsSuccess) + { + _logger.LogWarning("删除协议版本失败: {Message}", result.ErrorMessages); + return result; + } + + _logger.LogInformation("成功删除协议版本,版本ID: {ProtocolVersionId}", id); + return result; + } +} \ No newline at end of file diff --git a/src/X1.WebAPI/ProtocolVersions.http b/src/X1.WebAPI/ProtocolVersions.http new file mode 100644 index 0000000..e39e19c --- /dev/null +++ b/src/X1.WebAPI/ProtocolVersions.http @@ -0,0 +1,73 @@ +### 协议版本 CRUD API 测试 + +### 1. 创建协议版本 +POST {{baseUrl}}/api/protocolversions +Content-Type: application/json +Authorization: Bearer {{token}} + +{ + "name": "HTTP/1.1", + "version": "1.1.0", + "description": "HTTP协议1.1版本", + "isEnabled": true, + "releaseDate": "1997-01-01T00:00:00Z", + "isForceUpdate": false, + "minimumSupportedVersion": "1.0.0" +} + +### 2. 创建另一个协议版本 +POST {{baseUrl}}/api/protocolversions +Content-Type: application/json +Authorization: Bearer {{token}} + +{ + "name": "HTTP/2.0", + "version": "2.0.0", + "description": "HTTP协议2.0版本", + "isEnabled": true, + "releaseDate": "2015-05-14T00:00:00Z", + "isForceUpdate": true, + "minimumSupportedVersion": "1.1.0" +} + +### 3. 获取协议版本列表 +GET {{baseUrl}}/api/protocolversions?pageNumber=1&pageSize=10 +Authorization: Bearer {{token}} + +### 4. 搜索协议版本 +GET {{baseUrl}}/api/protocolversions?searchTerm=HTTP&pageNumber=1&pageSize=10 +Authorization: Bearer {{token}} + +### 5. 获取启用的协议版本 +GET {{baseUrl}}/api/protocolversions?isEnabled=true&pageNumber=1&pageSize=10 +Authorization: Bearer {{token}} + +### 6. 根据ID获取协议版本 +GET {{baseUrl}}/api/protocolversions/{{protocolVersionId}} +Authorization: Bearer {{token}} + +### 7. 更新协议版本 +PUT {{baseUrl}}/api/protocolversions/{{protocolVersionId}} +Content-Type: application/json +Authorization: Bearer {{token}} + +{ + "protocolVersionId": "{{protocolVersionId}}", + "name": "HTTP/1.1 Updated", + "version": "1.1.1", + "description": "HTTP协议1.1版本(已更新)", + "isEnabled": true, + "releaseDate": "1997-01-01T00:00:00Z", + "isForceUpdate": true, + "minimumSupportedVersion": "1.0.0" +} + +### 8. 删除协议版本 +DELETE {{baseUrl}}/api/protocolversions/{{protocolVersionId}} +Authorization: Bearer {{token}} + +### 环境变量设置 +# 在 VS Code 的 REST Client 扩展中设置以下变量: +# baseUrl: http://localhost:5000 +# token: 你的JWT令牌 +# protocolVersionId: 从创建响应中获取的协议版本ID \ No newline at end of file