Browse Source
- 修复SearchDevicesWithRuntimeAsync方法,添加DeviceCode搜索支持 - 为GetDeviceById添加运行时状态支持,新增GetDeviceByIdWithRuntimeAsync方法 - 统一所有设备搜索方法,确保支持按设备编码搜索 - 更新查询处理器,返回设备运行时状态信息 - 完善导航属性查询,正确处理CellularDevice和CellularDeviceRuntime的一对一关系feature/x1-web-request
44 changed files with 2555 additions and 630 deletions
@ -0,0 +1,46 @@ |
|||
using CellularManagement.Domain.Common; |
|||
using MediatR; |
|||
using System.ComponentModel.DataAnnotations; |
|||
|
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Commands.StartDeviceRuntime; |
|||
|
|||
/// <summary>
|
|||
/// 启动设备运行时状态命令
|
|||
/// </summary>
|
|||
public class StartDeviceRuntimeCommand : IRequest<OperationResult<StartDeviceRuntimeResponse>> |
|||
{ |
|||
/// <summary>
|
|||
/// 设备启动请求列表
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "设备启动请求列表不能为空")] |
|||
public List<DeviceStartRequest> DeviceRequests { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 验证命令参数
|
|||
/// </summary>
|
|||
public bool IsValid() |
|||
{ |
|||
return DeviceRequests != null && DeviceRequests.Any() && |
|||
DeviceRequests.All(d => !string.IsNullOrEmpty(d.DeviceCode) && !string.IsNullOrEmpty(d.NetworkStackCode)); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 设备启动请求
|
|||
/// </summary>
|
|||
public class DeviceStartRequest |
|||
{ |
|||
/// <summary>
|
|||
/// 设备编号
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "设备编号不能为空")] |
|||
[MaxLength(50, ErrorMessage = "设备编号不能超过50个字符")] |
|||
public string DeviceCode { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 网络栈配置编号
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "网络栈配置编号不能为空")] |
|||
[MaxLength(50, ErrorMessage = "网络栈配置编号不能超过50个字符")] |
|||
public string NetworkStackCode { get; set; } = null!; |
|||
} |
@ -0,0 +1,208 @@ |
|||
using MediatR; |
|||
using Microsoft.Extensions.Logging; |
|||
using CellularManagement.Domain.Common; |
|||
using CellularManagement.Domain.Entities.Device; |
|||
using CellularManagement.Domain.Repositories.Device; |
|||
using CellularManagement.Domain.Repositories.Base; |
|||
using CellularManagement.Domain.Services; |
|||
using X1.DynamicClientCore.Features; |
|||
|
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Commands.StartDeviceRuntime; |
|||
|
|||
/// <summary>
|
|||
/// 启动设备运行时状态命令处理器
|
|||
/// </summary>
|
|||
public class StartDeviceRuntimeCommandHandler : IRequestHandler<StartDeviceRuntimeCommand, OperationResult<StartDeviceRuntimeResponse>> |
|||
{ |
|||
private readonly ICellularDeviceRuntimeRepository _deviceRuntimeRepository; |
|||
private readonly ICellularDeviceRepository _deviceRepository; |
|||
private readonly ILogger<StartDeviceRuntimeCommandHandler> _logger; |
|||
private readonly IUnitOfWork _unitOfWork; |
|||
private readonly ICurrentUserService _currentUserService; |
|||
private readonly IInstrumentProtocolClient _protocolClient; |
|||
|
|||
/// <summary>
|
|||
/// 初始化命令处理器
|
|||
/// </summary>
|
|||
public StartDeviceRuntimeCommandHandler( |
|||
ICellularDeviceRuntimeRepository deviceRuntimeRepository, |
|||
ICellularDeviceRepository deviceRepository, |
|||
ILogger<StartDeviceRuntimeCommandHandler> logger, |
|||
IUnitOfWork unitOfWork, |
|||
ICurrentUserService currentUserService, |
|||
IInstrumentProtocolClient protocolClient) |
|||
{ |
|||
_deviceRuntimeRepository = deviceRuntimeRepository; |
|||
_deviceRepository = deviceRepository; |
|||
_logger = logger; |
|||
_unitOfWork = unitOfWork; |
|||
_currentUserService = currentUserService; |
|||
_protocolClient= protocolClient; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 处理启动设备运行时状态命令
|
|||
/// </summary>
|
|||
public async Task<OperationResult<StartDeviceRuntimeResponse>> Handle(StartDeviceRuntimeCommand request, CancellationToken cancellationToken) |
|||
{ |
|||
try |
|||
{ |
|||
_logger.LogInformation("开始批量启动设备运行时状态,设备数量: {DeviceCount}", request.DeviceRequests.Count); |
|||
|
|||
// 验证命令参数
|
|||
if (!request.IsValid()) |
|||
{ |
|||
_logger.LogWarning("命令参数验证失败"); |
|||
return OperationResult<StartDeviceRuntimeResponse>.CreateFailure("命令参数验证失败"); |
|||
} |
|||
|
|||
// 获取当前用户
|
|||
var currentUser = _currentUserService.GetCurrentUserId(); |
|||
if (string.IsNullOrEmpty(currentUser)) |
|||
{ |
|||
_logger.LogWarning("当前用户未登录"); |
|||
return OperationResult<StartDeviceRuntimeResponse>.CreateFailure("用户未登录"); |
|||
} |
|||
|
|||
var deviceResults = new List<DeviceStartResult>(); |
|||
var successCount = 0; |
|||
var failureCount = 0; |
|||
|
|||
// 批量处理设备启动
|
|||
foreach (var deviceRequest in request.DeviceRequests) |
|||
{ |
|||
try |
|||
{ |
|||
_logger.LogInformation("开始启动设备,设备编号: {DeviceCode}, 网络栈配置编号: {NetworkStackCode}", |
|||
deviceRequest.DeviceCode, deviceRequest.NetworkStackCode); |
|||
|
|||
// 获取设备运行时状态
|
|||
var deviceRuntime = await _deviceRuntimeRepository.GetRuntimeByDeviceCodeAsync(deviceRequest.DeviceCode, cancellationToken); |
|||
if (deviceRuntime == null) |
|||
{ |
|||
_logger.LogWarning("设备运行时状态不存在,设备编号: {DeviceCode}", deviceRequest.DeviceCode); |
|||
deviceResults.Add(new DeviceStartResult |
|||
{ |
|||
DeviceCode = deviceRequest.DeviceCode, |
|||
IsSuccess = false, |
|||
ErrorMessage = $"设备编号 {deviceRequest.DeviceCode} 的运行时状态不存在" |
|||
}); |
|||
failureCount++; |
|||
continue; |
|||
} |
|||
|
|||
// 检查设备是否已经在运行
|
|||
if (deviceRuntime.RuntimeStatus == DeviceRuntimeStatus.Running) |
|||
{ |
|||
_logger.LogWarning("设备已经在运行中,设备编号: {DeviceCode}", deviceRequest.DeviceCode); |
|||
deviceResults.Add(new DeviceStartResult |
|||
{ |
|||
DeviceCode = deviceRequest.DeviceCode, |
|||
IsSuccess = false, |
|||
ErrorMessage = $"设备编号 {deviceRequest.DeviceCode} 已经在运行中" |
|||
}); |
|||
failureCount++; |
|||
continue; |
|||
} |
|||
|
|||
// 生成运行编码
|
|||
var runtimeCode = await GenerateRuntimeCodeAsync(deviceRequest.DeviceCode, cancellationToken); |
|||
|
|||
// 启动设备
|
|||
deviceRuntime.Start(deviceRequest.NetworkStackCode, runtimeCode); |
|||
|
|||
// 更新到数据库
|
|||
_deviceRuntimeRepository.UpdateRuntime(deviceRuntime); |
|||
|
|||
_logger.LogInformation("设备启动成功,设备编号: {DeviceCode}, 运行编码: {RuntimeCode}", |
|||
deviceRuntime.DeviceCode, deviceRuntime.RuntimeCode); |
|||
|
|||
// 添加到结果列表
|
|||
deviceResults.Add(new DeviceStartResult |
|||
{ |
|||
DeviceCode = deviceRuntime.DeviceCode, |
|||
Id = deviceRuntime.Id, |
|||
RuntimeStatus = deviceRuntime.RuntimeStatus.ToString(), |
|||
NetworkStackCode = deviceRuntime.NetworkStackCode, |
|||
UpdatedAt = deviceRuntime.UpdatedAt ?? DateTime.UtcNow, |
|||
IsSuccess = true |
|||
}); |
|||
successCount++; |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "启动设备失败,设备编号: {DeviceCode}", deviceRequest.DeviceCode); |
|||
deviceResults.Add(new DeviceStartResult |
|||
{ |
|||
DeviceCode = deviceRequest.DeviceCode, |
|||
IsSuccess = false, |
|||
ErrorMessage = $"启动设备失败: {ex.Message}" |
|||
}); |
|||
failureCount++; |
|||
} |
|||
} |
|||
|
|||
// 保存所有更改
|
|||
await _unitOfWork.SaveChangesAsync(cancellationToken); |
|||
|
|||
// 构建响应
|
|||
var response = new StartDeviceRuntimeResponse |
|||
{ |
|||
DeviceResults = deviceResults, |
|||
Summary = new BatchOperationSummary |
|||
{ |
|||
TotalCount = request.DeviceRequests.Count, |
|||
SuccessCount = successCount, |
|||
FailureCount = failureCount |
|||
} |
|||
}; |
|||
|
|||
_logger.LogInformation("批量启动设备完成,总数量: {TotalCount}, 成功: {SuccessCount}, 失败: {FailureCount}", |
|||
response.Summary.TotalCount, response.Summary.SuccessCount, response.Summary.FailureCount); |
|||
|
|||
return OperationResult<StartDeviceRuntimeResponse>.CreateSuccess(response); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "批量启动设备运行时状态失败"); |
|||
return OperationResult<StartDeviceRuntimeResponse>.CreateFailure($"批量启动设备运行时状态失败: {ex.Message}"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 生成运行编码
|
|||
/// </summary>
|
|||
private async Task<string> GenerateRuntimeCodeAsync(string deviceCode, CancellationToken cancellationToken) |
|||
{ |
|||
// 获取当前设备运行时总数
|
|||
var runtimeCount = await _deviceRuntimeRepository.GetRuntimeCountAsync(cancellationToken); |
|||
var nextNumber = runtimeCount + 1; |
|||
|
|||
// 计算需要的位数,确保至少3位数
|
|||
var digitCount = CalculateRequiredDigits(nextNumber); |
|||
|
|||
// 格式化序号,动态补0,确保从000开始
|
|||
var formattedNumber = nextNumber.ToString($"D{digitCount}"); |
|||
|
|||
// 生成运行编码格式:RT-000-DEVCODE, RT-001-DEVCODE 等
|
|||
var runtimeCode = $"RT-{formattedNumber}-{deviceCode}"; |
|||
|
|||
_logger.LogDebug("生成运行编码: {RuntimeCode}, 运行时总数: {RuntimeCount}, 位数: {DigitCount}", |
|||
runtimeCode, runtimeCount, digitCount); |
|||
|
|||
return runtimeCode; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 计算需要的位数
|
|||
/// </summary>
|
|||
private int CalculateRequiredDigits(int number) |
|||
{ |
|||
if (number <= 0) return 3; // 从000开始,至少3位数
|
|||
|
|||
// 计算位数:确保至少3位数,从000开始
|
|||
// 1-999用3位,1000-9999用4位,10000-99999用5位,以此类推
|
|||
var calculatedDigits = (int)Math.Floor(Math.Log10(number)) + 1; |
|||
return Math.Max(calculatedDigits, 3); // 确保至少3位数
|
|||
} |
|||
} |
@ -0,0 +1,113 @@ |
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Commands.StartDeviceRuntime; |
|||
|
|||
/// <summary>
|
|||
/// 启动设备运行时状态响应
|
|||
/// </summary>
|
|||
public class StartDeviceRuntimeResponse |
|||
{ |
|||
/// <summary>
|
|||
/// 运行时状态ID(单个设备启动时使用)
|
|||
/// </summary>
|
|||
public string? Id { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 设备编号(单个设备启动时使用)
|
|||
/// </summary>
|
|||
public string? DeviceCode { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 运行时状态(单个设备启动时使用)
|
|||
/// </summary>
|
|||
public string? RuntimeStatus { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 网络栈配置编号
|
|||
/// </summary>
|
|||
public string? NetworkStackCode { get; set; } |
|||
|
|||
|
|||
|
|||
/// <summary>
|
|||
/// 更新时间
|
|||
/// </summary>
|
|||
public DateTime UpdatedAt { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 设备启动结果列表(批量启动时使用)
|
|||
/// </summary>
|
|||
public List<DeviceStartResult>? DeviceResults { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 批量操作统计
|
|||
/// </summary>
|
|||
public BatchOperationSummary? Summary { get; set; } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 设备启动结果
|
|||
/// </summary>
|
|||
public class DeviceStartResult |
|||
{ |
|||
/// <summary>
|
|||
/// 设备编号
|
|||
/// </summary>
|
|||
public string DeviceCode { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行时状态ID
|
|||
/// </summary>
|
|||
public string Id { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行时状态
|
|||
/// </summary>
|
|||
public string RuntimeStatus { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 网络栈配置编号
|
|||
/// </summary>
|
|||
public string? NetworkStackCode { get; set; } |
|||
|
|||
|
|||
|
|||
/// <summary>
|
|||
/// 更新时间
|
|||
/// </summary>
|
|||
public DateTime UpdatedAt { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 是否成功
|
|||
/// </summary>
|
|||
public bool IsSuccess { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 错误信息
|
|||
/// </summary>
|
|||
public string? ErrorMessage { get; set; } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 批量操作统计
|
|||
/// </summary>
|
|||
public class BatchOperationSummary |
|||
{ |
|||
/// <summary>
|
|||
/// 总设备数
|
|||
/// </summary>
|
|||
public int TotalCount { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 成功数量
|
|||
/// </summary>
|
|||
public int SuccessCount { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 失败数量
|
|||
/// </summary>
|
|||
public int FailureCount { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 成功率
|
|||
/// </summary>
|
|||
public double SuccessRate => TotalCount > 0 ? (double)SuccessCount / TotalCount * 100 : 0; |
|||
} |
@ -0,0 +1,18 @@ |
|||
using CellularManagement.Domain.Common; |
|||
using MediatR; |
|||
using System.ComponentModel.DataAnnotations; |
|||
|
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Commands.StopDeviceRuntime; |
|||
|
|||
/// <summary>
|
|||
/// 停止设备运行时状态命令
|
|||
/// </summary>
|
|||
public class StopDeviceRuntimeCommand : IRequest<OperationResult<StopDeviceRuntimeResponse>> |
|||
{ |
|||
/// <summary>
|
|||
/// 设备编号
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "设备编号不能为空")] |
|||
[MaxLength(50, ErrorMessage = "设备编号不能超过50个字符")] |
|||
public string DeviceCode { get; set; } = null!; |
|||
} |
@ -0,0 +1,97 @@ |
|||
using MediatR; |
|||
using Microsoft.Extensions.Logging; |
|||
using CellularManagement.Domain.Common; |
|||
using CellularManagement.Domain.Entities.Device; |
|||
using CellularManagement.Domain.Repositories.Device; |
|||
using CellularManagement.Domain.Repositories.Base; |
|||
using CellularManagement.Domain.Services; |
|||
|
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Commands.StopDeviceRuntime; |
|||
|
|||
/// <summary>
|
|||
/// 停止设备运行时状态命令处理器
|
|||
/// </summary>
|
|||
public class StopDeviceRuntimeCommandHandler : IRequestHandler<StopDeviceRuntimeCommand, OperationResult<StopDeviceRuntimeResponse>> |
|||
{ |
|||
private readonly ICellularDeviceRuntimeRepository _deviceRuntimeRepository; |
|||
private readonly ILogger<StopDeviceRuntimeCommandHandler> _logger; |
|||
private readonly IUnitOfWork _unitOfWork; |
|||
private readonly ICurrentUserService _currentUserService; |
|||
|
|||
/// <summary>
|
|||
/// 初始化命令处理器
|
|||
/// </summary>
|
|||
public StopDeviceRuntimeCommandHandler( |
|||
ICellularDeviceRuntimeRepository deviceRuntimeRepository, |
|||
ILogger<StopDeviceRuntimeCommandHandler> logger, |
|||
IUnitOfWork unitOfWork, |
|||
ICurrentUserService currentUserService) |
|||
{ |
|||
_deviceRuntimeRepository = deviceRuntimeRepository; |
|||
_logger = logger; |
|||
_unitOfWork = unitOfWork; |
|||
_currentUserService = currentUserService; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 处理停止设备运行时状态命令
|
|||
/// </summary>
|
|||
public async Task<OperationResult<StopDeviceRuntimeResponse>> Handle(StopDeviceRuntimeCommand request, CancellationToken cancellationToken) |
|||
{ |
|||
try |
|||
{ |
|||
_logger.LogInformation("开始停止设备运行时状态,设备编号: {DeviceCode}", request.DeviceCode); |
|||
|
|||
// 获取设备运行时状态
|
|||
var deviceRuntime = await _deviceRuntimeRepository.GetRuntimeByDeviceCodeAsync(request.DeviceCode, cancellationToken); |
|||
if (deviceRuntime == null) |
|||
{ |
|||
_logger.LogWarning("设备运行时状态不存在,设备编号: {DeviceCode}", request.DeviceCode); |
|||
return OperationResult<StopDeviceRuntimeResponse>.CreateFailure($"设备编号 {request.DeviceCode} 的运行时状态不存在"); |
|||
} |
|||
|
|||
// 检查设备是否已经在停止或未运行
|
|||
if (deviceRuntime.RuntimeStatus != DeviceRuntimeStatus.Running) |
|||
{ |
|||
_logger.LogWarning("设备未在运行中,设备编号: {DeviceCode}, 当前状态: {RuntimeStatus}", |
|||
request.DeviceCode, deviceRuntime.RuntimeStatus); |
|||
return OperationResult<StopDeviceRuntimeResponse>.CreateFailure($"设备编号 {request.DeviceCode} 未在运行中,当前状态: {deviceRuntime.RuntimeStatus}"); |
|||
} |
|||
|
|||
// 获取当前用户
|
|||
var currentUser = _currentUserService.GetCurrentUserId(); |
|||
if (string.IsNullOrEmpty(currentUser)) |
|||
{ |
|||
_logger.LogWarning("当前用户未登录"); |
|||
return OperationResult<StopDeviceRuntimeResponse>.CreateFailure("用户未登录"); |
|||
} |
|||
|
|||
// 停止设备
|
|||
deviceRuntime.Stop(); |
|||
|
|||
// 更新到数据库
|
|||
_deviceRuntimeRepository.UpdateRuntime(deviceRuntime); |
|||
await _unitOfWork.SaveChangesAsync(cancellationToken); |
|||
|
|||
_logger.LogInformation("设备停止成功,设备编号: {DeviceCode}", deviceRuntime.DeviceCode); |
|||
|
|||
// 构建响应
|
|||
var response = new StopDeviceRuntimeResponse |
|||
{ |
|||
Id = deviceRuntime.Id, |
|||
DeviceCode = deviceRuntime.DeviceCode, |
|||
RuntimeStatus = deviceRuntime.RuntimeStatus.ToString(), |
|||
RuntimeCode = deviceRuntime.RuntimeCode, |
|||
NetworkStackCode = deviceRuntime.NetworkStackCode, |
|||
UpdatedAt = deviceRuntime.UpdatedAt ?? DateTime.UtcNow |
|||
}; |
|||
|
|||
return OperationResult<StopDeviceRuntimeResponse>.CreateSuccess(response); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "停止设备运行时状态失败,设备编号: {DeviceCode}", request.DeviceCode); |
|||
return OperationResult<StopDeviceRuntimeResponse>.CreateFailure($"停止设备运行时状态失败: {ex.Message}"); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,39 @@ |
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Commands.StopDeviceRuntime; |
|||
|
|||
/// <summary>
|
|||
/// 停止设备运行时状态响应
|
|||
/// </summary>
|
|||
public class StopDeviceRuntimeResponse |
|||
{ |
|||
/// <summary>
|
|||
/// 运行时状态ID
|
|||
/// </summary>
|
|||
public string Id { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 设备编号
|
|||
/// </summary>
|
|||
public string DeviceCode { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行时状态
|
|||
/// </summary>
|
|||
public string RuntimeStatus { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行编码
|
|||
/// </summary>
|
|||
public string? RuntimeCode { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 网络栈配置编号
|
|||
/// </summary>
|
|||
public string? NetworkStackCode { get; set; } |
|||
|
|||
|
|||
|
|||
/// <summary>
|
|||
/// 更新时间
|
|||
/// </summary>
|
|||
public DateTime UpdatedAt { get; set; } |
|||
} |
@ -0,0 +1,26 @@ |
|||
using CellularManagement.Domain.Common; |
|||
using MediatR; |
|||
using System.ComponentModel.DataAnnotations; |
|||
|
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Queries.GetDeviceRuntimeStatus; |
|||
|
|||
/// <summary>
|
|||
/// 获取设备运行时状态查询
|
|||
/// </summary>
|
|||
public class GetDeviceRuntimeStatusQuery : IRequest<OperationResult<GetDeviceRuntimeStatusResponse>> |
|||
{ |
|||
/// <summary>
|
|||
/// 设备编号
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "设备编号不能为空")] |
|||
[MaxLength(50, ErrorMessage = "设备编号不能超过50个字符")] |
|||
public string DeviceCode { get; } |
|||
|
|||
/// <summary>
|
|||
/// 初始化查询
|
|||
/// </summary>
|
|||
public GetDeviceRuntimeStatusQuery(string deviceCode) |
|||
{ |
|||
DeviceCode = deviceCode; |
|||
} |
|||
} |
@ -0,0 +1,66 @@ |
|||
using MediatR; |
|||
using Microsoft.Extensions.Logging; |
|||
using CellularManagement.Domain.Common; |
|||
using CellularManagement.Domain.Entities.Device; |
|||
using CellularManagement.Domain.Repositories.Device; |
|||
|
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Queries.GetDeviceRuntimeStatus; |
|||
|
|||
/// <summary>
|
|||
/// 获取设备运行时状态查询处理器
|
|||
/// </summary>
|
|||
public class GetDeviceRuntimeStatusQueryHandler : IRequestHandler<GetDeviceRuntimeStatusQuery, OperationResult<GetDeviceRuntimeStatusResponse>> |
|||
{ |
|||
private readonly ICellularDeviceRuntimeRepository _deviceRuntimeRepository; |
|||
private readonly ILogger<GetDeviceRuntimeStatusQueryHandler> _logger; |
|||
|
|||
/// <summary>
|
|||
/// 初始化查询处理器
|
|||
/// </summary>
|
|||
public GetDeviceRuntimeStatusQueryHandler( |
|||
ICellularDeviceRuntimeRepository deviceRuntimeRepository, |
|||
ILogger<GetDeviceRuntimeStatusQueryHandler> logger) |
|||
{ |
|||
_deviceRuntimeRepository = deviceRuntimeRepository; |
|||
_logger = logger; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 处理查询请求
|
|||
/// </summary>
|
|||
public async Task<OperationResult<GetDeviceRuntimeStatusResponse>> Handle(GetDeviceRuntimeStatusQuery request, CancellationToken cancellationToken) |
|||
{ |
|||
try |
|||
{ |
|||
_logger.LogInformation("开始查询设备运行时状态,设备编号: {DeviceCode}", request.DeviceCode); |
|||
|
|||
// 获取设备运行时状态
|
|||
var deviceRuntime = await _deviceRuntimeRepository.GetRuntimeByDeviceCodeAsync(request.DeviceCode, cancellationToken); |
|||
|
|||
if (deviceRuntime == null) |
|||
{ |
|||
_logger.LogWarning("未找到设备运行时状态,设备编号: {DeviceCode}", request.DeviceCode); |
|||
return OperationResult<GetDeviceRuntimeStatusResponse>.CreateFailure($"未找到设备编号为 {request.DeviceCode} 的运行时状态"); |
|||
} |
|||
|
|||
// 构建响应
|
|||
var response = new GetDeviceRuntimeStatusResponse |
|||
{ |
|||
DeviceCode = deviceRuntime.DeviceCode, |
|||
RuntimeStatus = deviceRuntime.RuntimeStatus.ToString(), |
|||
RuntimeCode = deviceRuntime.RuntimeCode, |
|||
NetworkStackCode = deviceRuntime.NetworkStackCode |
|||
}; |
|||
|
|||
_logger.LogInformation("查询设备运行时状态成功,设备编号: {DeviceCode}, 运行时状态: {RuntimeStatus}", |
|||
deviceRuntime.DeviceCode, deviceRuntime.RuntimeStatus); |
|||
|
|||
return OperationResult<GetDeviceRuntimeStatusResponse>.CreateSuccess(response); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "查询设备运行时状态失败,设备编号: {DeviceCode}", request.DeviceCode); |
|||
return OperationResult<GetDeviceRuntimeStatusResponse>.CreateFailure($"查询设备运行时状态失败: {ex.Message}"); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,27 @@ |
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Queries.GetDeviceRuntimeStatus; |
|||
|
|||
/// <summary>
|
|||
/// 获取设备运行时状态响应
|
|||
/// </summary>
|
|||
public class GetDeviceRuntimeStatusResponse |
|||
{ |
|||
/// <summary>
|
|||
/// 设备编号
|
|||
/// </summary>
|
|||
public string DeviceCode { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行时状态
|
|||
/// </summary>
|
|||
public string RuntimeStatus { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行编码
|
|||
/// </summary>
|
|||
public string? RuntimeCode { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 网络栈配置编号
|
|||
/// </summary>
|
|||
public string? NetworkStackCode { get; set; } |
|||
} |
@ -0,0 +1,40 @@ |
|||
using CellularManagement.Domain.Common; |
|||
using MediatR; |
|||
using System.ComponentModel.DataAnnotations; |
|||
|
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Queries.GetDeviceRuntimes; |
|||
|
|||
/// <summary>
|
|||
/// 获取设备运行时状态列表查询
|
|||
/// </summary>
|
|||
public class GetDeviceRuntimesQuery : IRequest<OperationResult<GetDeviceRuntimesResponse>> |
|||
{ |
|||
/// <summary>
|
|||
/// 页码,从1开始
|
|||
/// </summary>
|
|||
[Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] |
|||
public int PageNumber { get; set; } = 1; |
|||
|
|||
/// <summary>
|
|||
/// 每页记录数
|
|||
/// </summary>
|
|||
[Range(1, 100, ErrorMessage = "每页记录数必须在1-100之间")] |
|||
public int PageSize { get; set; } = 10; |
|||
|
|||
/// <summary>
|
|||
/// 搜索关键词
|
|||
/// </summary>
|
|||
[MaxLength(100, ErrorMessage = "搜索关键词不能超过100个字符")] |
|||
public string? SearchTerm { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 运行时状态过滤
|
|||
/// </summary>
|
|||
public string? RuntimeStatus { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 网络栈配置编号过滤
|
|||
/// </summary>
|
|||
[MaxLength(50, ErrorMessage = "网络栈配置编号不能超过50个字符")] |
|||
public string? NetworkStackCode { get; set; } |
|||
} |
@ -0,0 +1,97 @@ |
|||
using MediatR; |
|||
using Microsoft.Extensions.Logging; |
|||
using CellularManagement.Domain.Common; |
|||
using CellularManagement.Domain.Entities.Device; |
|||
using CellularManagement.Domain.Repositories.Device; |
|||
|
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Queries.GetDeviceRuntimes; |
|||
|
|||
/// <summary>
|
|||
/// 获取设备运行时状态列表查询处理器
|
|||
/// </summary>
|
|||
public class GetDeviceRuntimesQueryHandler : IRequestHandler<GetDeviceRuntimesQuery, OperationResult<GetDeviceRuntimesResponse>> |
|||
{ |
|||
private readonly ICellularDeviceRuntimeRepository _deviceRuntimeRepository; |
|||
private readonly ILogger<GetDeviceRuntimesQueryHandler> _logger; |
|||
|
|||
/// <summary>
|
|||
/// 初始化查询处理器
|
|||
/// </summary>
|
|||
public GetDeviceRuntimesQueryHandler( |
|||
ICellularDeviceRuntimeRepository deviceRuntimeRepository, |
|||
ILogger<GetDeviceRuntimesQueryHandler> logger) |
|||
{ |
|||
_deviceRuntimeRepository = deviceRuntimeRepository; |
|||
_logger = logger; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 处理查询请求
|
|||
/// </summary>
|
|||
public async Task<OperationResult<GetDeviceRuntimesResponse>> Handle(GetDeviceRuntimesQuery request, CancellationToken cancellationToken) |
|||
{ |
|||
try |
|||
{ |
|||
_logger.LogInformation("开始查询设备运行时状态列表,页码: {PageNumber}, 每页数量: {PageSize}, 搜索关键词: {SearchTerm}, 运行时状态: {RuntimeStatus}, 网络栈配置编号: {NetworkStackCode}", |
|||
request.PageNumber, request.PageSize, request.SearchTerm, request.RuntimeStatus, request.NetworkStackCode); |
|||
|
|||
// 执行分页查询
|
|||
var (totalCount, items) = await _deviceRuntimeRepository.SearchRuntimesAsync( |
|||
request.SearchTerm, |
|||
request.PageNumber, |
|||
request.PageSize, |
|||
cancellationToken); |
|||
|
|||
// 计算总页数
|
|||
var totalPages = (int)Math.Ceiling((double)totalCount / request.PageSize); |
|||
|
|||
// 构建响应
|
|||
var response = new GetDeviceRuntimesResponse |
|||
{ |
|||
TotalCount = totalCount, |
|||
PageNumber = request.PageNumber, |
|||
PageSize = request.PageSize, |
|||
TotalPages = totalPages, |
|||
Items = items.Select(MapToDto).ToList() |
|||
}; |
|||
|
|||
_logger.LogInformation("查询设备运行时状态列表成功,总记录数: {TotalCount}, 总页数: {TotalPages}", totalCount, totalPages); |
|||
|
|||
return OperationResult<GetDeviceRuntimesResponse>.CreateSuccess(response); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "查询设备运行时状态列表失败"); |
|||
return OperationResult<GetDeviceRuntimesResponse>.CreateFailure($"查询设备运行时状态列表失败: {ex.Message}"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 映射到数据传输对象
|
|||
/// </summary>
|
|||
private static DeviceRuntimeDto MapToDto(CellularDeviceRuntime runtime) |
|||
{ |
|||
return new DeviceRuntimeDto |
|||
{ |
|||
Id = runtime.Id, |
|||
DeviceCode = runtime.DeviceCode, |
|||
RuntimeStatus = runtime.RuntimeStatus.ToString(), |
|||
RuntimeCode = runtime.RuntimeCode, |
|||
NetworkStackCode = runtime.NetworkStackCode, |
|||
|
|||
CreatedAt = runtime.CreatedAt, |
|||
UpdatedAt = runtime.UpdatedAt ?? DateTime.UtcNow, |
|||
Device = runtime.Device != null ? new DeviceInfoDto |
|||
{ |
|||
Id = runtime.Device.Id, |
|||
Name = runtime.Device.Name, |
|||
SerialNumber = runtime.Device.SerialNumber, |
|||
DeviceCode = runtime.Device.DeviceCode, |
|||
Description = runtime.Device.Description, |
|||
IpAddress = runtime.Device.IpAddress, |
|||
AgentPort = runtime.Device.AgentPort, |
|||
IsEnabled = runtime.Device.IsEnabled |
|||
} : null |
|||
}; |
|||
} |
|||
} |
@ -0,0 +1,126 @@ |
|||
namespace CellularManagement.Application.Features.DeviceRuntimes.Queries.GetDeviceRuntimes; |
|||
|
|||
/// <summary>
|
|||
/// 获取设备运行时状态列表响应
|
|||
/// </summary>
|
|||
public class GetDeviceRuntimesResponse |
|||
{ |
|||
/// <summary>
|
|||
/// 总记录数
|
|||
/// </summary>
|
|||
public int TotalCount { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 当前页码
|
|||
/// </summary>
|
|||
public int PageNumber { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 每页记录数
|
|||
/// </summary>
|
|||
public int PageSize { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 总页数
|
|||
/// </summary>
|
|||
public int TotalPages { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 设备运行时状态列表
|
|||
/// </summary>
|
|||
public List<DeviceRuntimeDto> Items { get; set; } = new(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 设备运行时状态数据传输对象
|
|||
/// </summary>
|
|||
public class DeviceRuntimeDto |
|||
{ |
|||
/// <summary>
|
|||
/// 运行时状态ID
|
|||
/// </summary>
|
|||
public string Id { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 设备编号
|
|||
/// </summary>
|
|||
public string DeviceCode { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行时状态
|
|||
/// </summary>
|
|||
public string RuntimeStatus { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行编码
|
|||
/// </summary>
|
|||
public string? RuntimeCode { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 网络栈配置编号
|
|||
/// </summary>
|
|||
public string? NetworkStackCode { get; set; } |
|||
|
|||
|
|||
|
|||
/// <summary>
|
|||
/// 创建时间
|
|||
/// </summary>
|
|||
public DateTime CreatedAt { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 更新时间
|
|||
/// </summary>
|
|||
public DateTime UpdatedAt { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 设备信息
|
|||
/// </summary>
|
|||
public DeviceInfoDto? Device { get; set; } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 设备信息数据传输对象
|
|||
/// </summary>
|
|||
public class DeviceInfoDto |
|||
{ |
|||
/// <summary>
|
|||
/// 设备ID
|
|||
/// </summary>
|
|||
public string Id { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 设备名称
|
|||
/// </summary>
|
|||
public string Name { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 设备序列号
|
|||
/// </summary>
|
|||
public string SerialNumber { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 设备编码
|
|||
/// </summary>
|
|||
public string DeviceCode { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 设备描述
|
|||
/// </summary>
|
|||
public string Description { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// IP地址
|
|||
/// </summary>
|
|||
public string IpAddress { get; set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// Agent端口
|
|||
/// </summary>
|
|||
public int AgentPort { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 是否启用
|
|||
/// </summary>
|
|||
public bool IsEnabled { get; set; } |
|||
} |
@ -0,0 +1,84 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.ComponentModel.DataAnnotations.Schema; |
|||
using CellularManagement.Domain.Entities.Common; |
|||
|
|||
namespace CellularManagement.Domain.Entities.Device; |
|||
|
|||
/// <summary>
|
|||
/// 蜂窝设备运行时状态实体
|
|||
/// </summary>
|
|||
public class CellularDeviceRuntime : BaseEntity |
|||
{ |
|||
private CellularDeviceRuntime() { } |
|||
|
|||
/// <summary>
|
|||
/// 设备编号
|
|||
/// </summary>
|
|||
[Required] |
|||
[MaxLength(50)] |
|||
public string DeviceCode { get; private set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行时状态
|
|||
/// </summary>
|
|||
public DeviceRuntimeStatus RuntimeStatus { get; private set; } = DeviceRuntimeStatus.Initializing; |
|||
|
|||
/// <summary>
|
|||
/// 运行编码(仅在Running状态时生成)
|
|||
/// </summary>
|
|||
[MaxLength(50)] |
|||
public string? RuntimeCode { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// 网络栈配置编号
|
|||
/// </summary>
|
|||
[MaxLength(50)] |
|||
public string? NetworkStackCode { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// 设备实例
|
|||
/// </summary>
|
|||
public virtual CellularDevice Device { get; private set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 创建设备运行时状态
|
|||
/// </summary>
|
|||
public static CellularDeviceRuntime Create( |
|||
string deviceCode, |
|||
string createdBy, |
|||
DeviceRuntimeStatus runtimeStatus = DeviceRuntimeStatus.Initializing, |
|||
string? networkStackCode = null) |
|||
{ |
|||
var runtime = new CellularDeviceRuntime |
|||
{ |
|||
Id = Guid.NewGuid().ToString(), |
|||
DeviceCode = deviceCode, |
|||
RuntimeStatus = runtimeStatus, |
|||
RuntimeCode = null, // 运行编码由业务层设置
|
|||
NetworkStackCode = networkStackCode, |
|||
CreatedAt = DateTime.UtcNow, |
|||
UpdatedAt = DateTime.UtcNow |
|||
}; |
|||
|
|||
return runtime; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 启动设备
|
|||
/// </summary>
|
|||
public void Start(string networkStackCode, string runtimeCode) |
|||
{ |
|||
RuntimeStatus = DeviceRuntimeStatus.Running; |
|||
NetworkStackCode = networkStackCode; |
|||
RuntimeCode = runtimeCode; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 停止设备
|
|||
/// </summary>
|
|||
public void Stop() |
|||
{ |
|||
RuntimeStatus = DeviceRuntimeStatus.Stopping; |
|||
RuntimeCode = null; |
|||
} |
|||
} |
@ -0,0 +1,55 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
using CellularManagement.Domain.Abstractions; |
|||
|
|||
namespace CellularManagement.Domain.Entities.Device; |
|||
|
|||
/// <summary>
|
|||
/// 蜂窝设备运行时明细表实体
|
|||
/// </summary>
|
|||
public class CellularDeviceRuntimeDetail : Entity |
|||
{ |
|||
private CellularDeviceRuntimeDetail() { } |
|||
|
|||
/// <summary>
|
|||
/// 运行编码
|
|||
/// </summary>
|
|||
[Required] |
|||
[MaxLength(50)] |
|||
public string RuntimeCode { get; private set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 运行时状态(true=运行中,false=已停止)
|
|||
/// </summary>
|
|||
public bool RuntimeStatus { get; private set; } = false; |
|||
|
|||
/// <summary>
|
|||
/// 创建人ID
|
|||
/// </summary>
|
|||
[MaxLength(450)] |
|||
public string? CreatedBy { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// 创建时间
|
|||
/// </summary>
|
|||
public DateTime CreatedAt { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// 创建设备运行时明细记录
|
|||
/// </summary>
|
|||
public static CellularDeviceRuntimeDetail Create( |
|||
string runtimeCode, |
|||
string createdBy, |
|||
bool runtimeStatus = false) |
|||
{ |
|||
var detail = new CellularDeviceRuntimeDetail |
|||
{ |
|||
Id = Guid.NewGuid().ToString(), |
|||
RuntimeCode = runtimeCode, |
|||
RuntimeStatus = runtimeStatus, |
|||
CreatedBy = createdBy, |
|||
CreatedAt = DateTime.UtcNow |
|||
}; |
|||
|
|||
return detail; |
|||
} |
|||
} |
@ -0,0 +1,106 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.ComponentModel.DataAnnotations.Schema; |
|||
using CellularManagement.Domain.Entities.Common; |
|||
|
|||
namespace CellularManagement.Domain.Entities.Device; |
|||
|
|||
/// <summary>
|
|||
/// 蜂窝设备运行时历史记录实体
|
|||
/// </summary>
|
|||
public class CellularDeviceRuntimeHistory : BaseEntity |
|||
{ |
|||
private CellularDeviceRuntimeHistory() { } |
|||
|
|||
/// <summary>
|
|||
/// 运行时ID(外键)
|
|||
/// </summary>
|
|||
[Required] |
|||
[MaxLength(450)] |
|||
public string RuntimeId { get; private set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 设备编号
|
|||
/// </summary>
|
|||
[Required] |
|||
[MaxLength(50)] |
|||
public string DeviceCode { get; private set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 操作前状态
|
|||
/// </summary>
|
|||
public DeviceRuntimeStatus PreviousStatus { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// 操作后状态
|
|||
/// </summary>
|
|||
public DeviceRuntimeStatus NewStatus { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// 运行编码
|
|||
/// </summary>
|
|||
[MaxLength(50)] |
|||
public string? RuntimeCode { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// 网络栈配置编号
|
|||
/// </summary>
|
|||
[MaxLength(50)] |
|||
public string? NetworkStackCode { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// 操作人ID
|
|||
/// </summary>
|
|||
[Required] |
|||
[MaxLength(450)] |
|||
public string OperatorId { get; private set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 操作描述
|
|||
/// </summary>
|
|||
[Required] |
|||
[MaxLength(200)] |
|||
public string OperationDescription { get; private set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 操作时间
|
|||
/// </summary>
|
|||
public DateTime OperationAt { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// 运行时实例
|
|||
/// </summary>
|
|||
public virtual CellularDeviceRuntime Runtime { get; private set; } = null!; |
|||
|
|||
/// <summary>
|
|||
/// 创建设备运行时历史记录
|
|||
/// </summary>
|
|||
public static CellularDeviceRuntimeHistory Create( |
|||
string runtimeId, |
|||
string deviceCode, |
|||
DeviceRuntimeStatus previousStatus, |
|||
DeviceRuntimeStatus newStatus, |
|||
string? runtimeCode, |
|||
string? networkStackCode, |
|||
string operatorId, |
|||
string operationDescription, |
|||
DateTime operationAt) |
|||
{ |
|||
var history = new CellularDeviceRuntimeHistory |
|||
{ |
|||
Id = Guid.NewGuid().ToString(), |
|||
RuntimeId = runtimeId, |
|||
DeviceCode = deviceCode, |
|||
PreviousStatus = previousStatus, |
|||
NewStatus = newStatus, |
|||
RuntimeCode = runtimeCode, |
|||
NetworkStackCode = networkStackCode, |
|||
OperatorId = operatorId, |
|||
OperationDescription = operationDescription, |
|||
OperationAt = operationAt, |
|||
CreatedAt = DateTime.UtcNow, |
|||
UpdatedAt = DateTime.UtcNow |
|||
}; |
|||
|
|||
return history; |
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
namespace CellularManagement.Domain.Entities.Device; |
|||
|
|||
/// <summary>
|
|||
/// 设备连接状态枚举
|
|||
/// </summary>
|
|||
public enum DeviceConnectionStatus |
|||
{ |
|||
/// <summary>
|
|||
/// 初始化
|
|||
/// </summary>
|
|||
Initializing = 0, |
|||
|
|||
/// <summary>
|
|||
/// 未连接
|
|||
/// </summary>
|
|||
Disconnected = 1, |
|||
|
|||
/// <summary>
|
|||
/// 已连接
|
|||
/// </summary>
|
|||
Connected = 2 |
|||
} |
@ -0,0 +1,22 @@ |
|||
namespace CellularManagement.Domain.Entities.Device; |
|||
|
|||
/// <summary>
|
|||
/// 设备运行时状态枚举
|
|||
/// </summary>
|
|||
public enum DeviceRuntimeStatus |
|||
{ |
|||
/// <summary>
|
|||
/// 初始化
|
|||
/// </summary>
|
|||
Initializing = 0, |
|||
|
|||
/// <summary>
|
|||
/// 运行中
|
|||
/// </summary>
|
|||
Running = 1, |
|||
|
|||
/// <summary>
|
|||
/// 停止中
|
|||
/// </summary>
|
|||
Stopping = 2 |
|||
} |
@ -0,0 +1,71 @@ |
|||
using CellularManagement.Domain.Entities.Device; |
|||
using CellularManagement.Domain.Repositories.Base; |
|||
|
|||
namespace CellularManagement.Domain.Repositories.Device; |
|||
|
|||
/// <summary>
|
|||
/// 蜂窝设备运行时明细仓储接口
|
|||
/// </summary>
|
|||
public interface ICellularDeviceRuntimeDetailRepository : IBaseRepository<CellularDeviceRuntimeDetail> |
|||
{ |
|||
/// <summary>
|
|||
/// 根据运行编码获取运行时明细列表
|
|||
/// </summary>
|
|||
/// <param name="runtimeCode">运行编码</param>
|
|||
/// <param name="cancellationToken">取消令牌</param>
|
|||
/// <returns>运行时明细列表</returns>
|
|||
Task<List<CellularDeviceRuntimeDetail>> GetDetailsByRuntimeCodeAsync(string runtimeCode, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 根据创建人获取运行时明细列表
|
|||
/// </summary>
|
|||
/// <param name="createdBy">创建人ID</param>
|
|||
/// <param name="cancellationToken">取消令牌</param>
|
|||
/// <returns>运行时明细列表</returns>
|
|||
Task<List<CellularDeviceRuntimeDetail>> GetDetailsByCreatedByAsync(string createdBy, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 根据运行时状态获取运行时明细列表
|
|||
/// </summary>
|
|||
/// <param name="runtimeStatus">运行时状态(true=运行中,false=已停止)</param>
|
|||
/// <param name="cancellationToken">取消令牌</param>
|
|||
/// <returns>运行时明细列表</returns>
|
|||
Task<List<CellularDeviceRuntimeDetail>> GetDetailsByRuntimeStatusAsync(bool runtimeStatus, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 分页查询运行时明细
|
|||
/// </summary>
|
|||
/// <param name="runtimeCode">运行编码(可选)</param>
|
|||
/// <param name="createdBy">创建人(可选)</param>
|
|||
/// <param name="runtimeStatus">运行时状态(可选,true=运行中,false=已停止)</param>
|
|||
/// <param name="startDate">开始日期(可选)</param>
|
|||
/// <param name="endDate">结束日期(可选)</param>
|
|||
/// <param name="pageNumber">页码</param>
|
|||
/// <param name="pageSize">每页数量</param>
|
|||
/// <param name="cancellationToken">取消令牌</param>
|
|||
/// <returns>分页结果</returns>
|
|||
Task<(int TotalCount, List<CellularDeviceRuntimeDetail> Items)> SearchDetailsAsync( |
|||
string? runtimeCode = null, |
|||
string? createdBy = null, |
|||
bool? runtimeStatus = null, |
|||
DateTime? startDate = null, |
|||
DateTime? endDate = null, |
|||
int pageNumber = 1, |
|||
int pageSize = 10, |
|||
CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 获取运行时明细总数
|
|||
/// </summary>
|
|||
/// <param name="cancellationToken">取消令牌</param>
|
|||
/// <returns>总数</returns>
|
|||
Task<int> GetDetailCountAsync(CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 批量添加运行时明细
|
|||
/// </summary>
|
|||
/// <param name="details">明细列表</param>
|
|||
/// <param name="cancellationToken">取消令牌</param>
|
|||
/// <returns>添加后的明细列表</returns>
|
|||
Task<IEnumerable<CellularDeviceRuntimeDetail>> AddDetailsRangeAsync(IEnumerable<CellularDeviceRuntimeDetail> details, CancellationToken cancellationToken = default); |
|||
} |
@ -0,0 +1,107 @@ |
|||
using CellularManagement.Domain.Entities.Device; |
|||
using CellularManagement.Domain.Repositories.Base; |
|||
|
|||
namespace CellularManagement.Domain.Repositories.Device; |
|||
|
|||
/// <summary>
|
|||
/// 蜂窝设备运行时状态仓储接口
|
|||
/// 组合命令和查询仓储接口
|
|||
/// </summary>
|
|||
public interface ICellularDeviceRuntimeRepository : IBaseRepository<CellularDeviceRuntime> |
|||
{ |
|||
/// <summary>
|
|||
/// 添加设备运行时状态
|
|||
/// </summary>
|
|||
Task<CellularDeviceRuntime> AddRuntimeAsync(CellularDeviceRuntime runtime, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 更新设备运行时状态
|
|||
/// </summary>
|
|||
void UpdateRuntime(CellularDeviceRuntime runtime); |
|||
|
|||
/// <summary>
|
|||
/// 删除设备运行时状态
|
|||
/// </summary>
|
|||
Task DeleteRuntimeAsync(string id, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 获取所有设备运行时状态
|
|||
/// </summary>
|
|||
Task<IList<CellularDeviceRuntime>> GetAllRuntimesAsync(CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 根据ID获取设备运行时状态
|
|||
/// </summary>
|
|||
Task<CellularDeviceRuntime?> GetRuntimeByIdAsync(string id, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 根据设备编号获取运行时状态
|
|||
/// </summary>
|
|||
Task<CellularDeviceRuntime?> GetRuntimeByDeviceCodeAsync(string deviceCode, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 根据设备编号获取运行时状态(包含设备信息)
|
|||
/// </summary>
|
|||
Task<CellularDeviceRuntime?> GetRuntimeByDeviceCodeWithDeviceAsync(string deviceCode, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 根据运行编码获取运行时状态
|
|||
/// </summary>
|
|||
Task<CellularDeviceRuntime?> GetRuntimeByRuntimeCodeAsync(string runtimeCode, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 获取正在运行的设备运行时状态
|
|||
/// </summary>
|
|||
Task<IList<CellularDeviceRuntime>> GetRunningRuntimesAsync(CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 根据运行时状态获取设备运行时状态
|
|||
/// </summary>
|
|||
Task<IList<CellularDeviceRuntime>> GetRuntimesByStatusAsync(DeviceRuntimeStatus runtimeStatus, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 根据网络栈配置编号获取运行时状态
|
|||
/// </summary>
|
|||
Task<IList<CellularDeviceRuntime>> GetRuntimesByNetworkStackCodeAsync(string networkStackCode, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 搜索设备运行时状态
|
|||
/// </summary>
|
|||
Task<IList<CellularDeviceRuntime>> SearchRuntimesAsync( |
|||
string? keyword, |
|||
CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 搜索设备运行时状态(分页)
|
|||
/// </summary>
|
|||
Task<(int TotalCount, IList<CellularDeviceRuntime> Items)> SearchRuntimesAsync( |
|||
string? keyword, |
|||
int pageNumber, |
|||
int pageSize, |
|||
CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 检查设备运行时状态是否存在
|
|||
/// </summary>
|
|||
Task<bool> ExistsAsync(string id, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 检查设备编号是否有运行时状态
|
|||
/// </summary>
|
|||
Task<bool> DeviceCodeExistsAsync(string deviceCode, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 检查运行编码是否存在
|
|||
/// </summary>
|
|||
Task<bool> RuntimeCodeExistsAsync(string runtimeCode, CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 获取运行时状态总数
|
|||
/// </summary>
|
|||
Task<int> GetRuntimeCountAsync(CancellationToken cancellationToken = default); |
|||
|
|||
/// <summary>
|
|||
/// 根据设备编号删除运行时状态
|
|||
/// </summary>
|
|||
Task DeleteRuntimeByDeviceCodeAsync(string deviceCode, CancellationToken cancellationToken = default); |
|||
} |
@ -0,0 +1,66 @@ |
|||
using CellularManagement.Domain.Entities.Device; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|||
|
|||
namespace CellularManagement.Infrastructure.Configurations.Device; |
|||
|
|||
/// <summary>
|
|||
/// 蜂窝设备运行时状态配置
|
|||
/// </summary>
|
|||
public class CellularDeviceRuntimeConfiguration : IEntityTypeConfiguration<CellularDeviceRuntime> |
|||
{ |
|||
public void Configure(EntityTypeBuilder<CellularDeviceRuntime> builder) |
|||
{ |
|||
builder.ToTable("CellularDeviceRuntimes"); |
|||
|
|||
builder.HasKey(x => x.Id); |
|||
|
|||
// 基础属性配置
|
|||
builder.Property(x => x.DeviceCode) |
|||
.IsRequired() |
|||
.HasMaxLength(50); |
|||
|
|||
builder.Property(x => x.RuntimeStatus) |
|||
.IsRequired() |
|||
.HasConversion<int>() |
|||
.HasDefaultValue(DeviceRuntimeStatus.Initializing); |
|||
|
|||
builder.Property(x => x.RuntimeCode) |
|||
.HasMaxLength(50); |
|||
|
|||
builder.Property(x => x.NetworkStackCode) |
|||
.HasMaxLength(50); |
|||
|
|||
|
|||
|
|||
// BaseEntity 属性配置
|
|||
builder.Property(x => x.CreatedAt) |
|||
.IsRequired(); |
|||
|
|||
builder.Property(x => x.UpdatedAt); |
|||
|
|||
builder.Property(x => x.IsDeleted) |
|||
.HasDefaultValue(false); |
|||
|
|||
// 配置与 CellularDevice 的关系
|
|||
builder.HasOne(x => x.Device) |
|||
.WithOne(x => x.Runtime) |
|||
.HasForeignKey<CellularDeviceRuntime>(x => x.DeviceCode) |
|||
.HasPrincipalKey<CellularDevice>(x => x.DeviceCode) |
|||
.OnDelete(DeleteBehavior.Cascade); |
|||
|
|||
// 创建索引
|
|||
builder.HasIndex(x => x.DeviceCode) |
|||
.IsUnique(); |
|||
|
|||
// 复合索引:优化按设备编号和创建时间排序的查询
|
|||
builder.HasIndex(x => new { x.DeviceCode, x.CreatedAt }) |
|||
.HasDatabaseName("IX_CellularDeviceRuntimes_DeviceCode_CreatedAt"); |
|||
|
|||
builder.HasIndex(x => x.RuntimeStatus); |
|||
builder.HasIndex(x => x.RuntimeCode); |
|||
builder.HasIndex(x => x.NetworkStackCode); |
|||
|
|||
builder.HasIndex(x => x.IsDeleted); |
|||
} |
|||
} |
@ -0,0 +1,55 @@ |
|||
using CellularManagement.Domain.Entities.Device; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|||
|
|||
namespace CellularManagement.Infrastructure.Configurations.Device; |
|||
|
|||
/// <summary>
|
|||
/// 蜂窝设备运行时明细配置
|
|||
/// </summary>
|
|||
public class CellularDeviceRuntimeDetailConfiguration : IEntityTypeConfiguration<CellularDeviceRuntimeDetail> |
|||
{ |
|||
public void Configure(EntityTypeBuilder<CellularDeviceRuntimeDetail> builder) |
|||
{ |
|||
builder.ToTable("CellularDeviceRuntimeDetails"); |
|||
|
|||
builder.HasKey(x => x.Id); |
|||
|
|||
// 基础属性配置
|
|||
builder.Property(x => x.RuntimeCode) |
|||
.IsRequired() |
|||
.HasMaxLength(50); |
|||
|
|||
builder.Property(x => x.RuntimeStatus) |
|||
.IsRequired() |
|||
.HasDefaultValue(false); |
|||
|
|||
builder.Property(x => x.CreatedBy) |
|||
.HasMaxLength(450); |
|||
|
|||
// Entity 属性配置
|
|||
builder.Property(x => x.Id) |
|||
.IsRequired() |
|||
.HasMaxLength(450); |
|||
|
|||
// 创建时间配置
|
|||
builder.Property(x => x.CreatedAt) |
|||
.IsRequired(); |
|||
|
|||
// 创建索引
|
|||
builder.HasIndex(x => x.RuntimeCode) |
|||
.HasDatabaseName("IX_CellularDeviceRuntimeDetails_RuntimeCode"); |
|||
|
|||
builder.HasIndex(x => x.CreatedBy) |
|||
.HasDatabaseName("IX_CellularDeviceRuntimeDetails_CreatedBy"); |
|||
|
|||
builder.HasIndex(x => x.RuntimeStatus) |
|||
.HasDatabaseName("IX_CellularDeviceRuntimeDetails_RuntimeStatus"); |
|||
|
|||
// 复合索引:优化按创建时间排序的查询
|
|||
builder.HasIndex(x => x.CreatedAt) |
|||
.HasDatabaseName("IX_CellularDeviceRuntimeDetails_CreatedAt"); |
|||
|
|||
|
|||
} |
|||
} |
@ -0,0 +1,232 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.Extensions.Logging; |
|||
using System.Text.RegularExpressions; |
|||
using CellularManagement.Domain.Entities.Device; |
|||
using CellularManagement.Domain.Repositories.Device; |
|||
using CellularManagement.Domain.Repositories.Base; |
|||
using CellularManagement.Infrastructure.Repositories.Base; |
|||
|
|||
namespace CellularManagement.Infrastructure.Repositories.Device; |
|||
|
|||
/// <summary>
|
|||
/// 蜂窝设备运行时状态仓储实现类
|
|||
/// </summary>
|
|||
public class CellularDeviceRuntimeRepository : BaseRepository<CellularDeviceRuntime>, ICellularDeviceRuntimeRepository |
|||
{ |
|||
private readonly ILogger<CellularDeviceRuntimeRepository> _logger; |
|||
|
|||
/// <summary>
|
|||
/// 初始化仓储
|
|||
/// </summary>
|
|||
public CellularDeviceRuntimeRepository( |
|||
ICommandRepository<CellularDeviceRuntime> commandRepository, |
|||
IQueryRepository<CellularDeviceRuntime> queryRepository, |
|||
ILogger<CellularDeviceRuntimeRepository> logger) |
|||
: base(commandRepository, queryRepository, logger) |
|||
{ |
|||
_logger = logger; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 添加设备运行时状态
|
|||
/// </summary>
|
|||
public async Task<CellularDeviceRuntime> AddRuntimeAsync(CellularDeviceRuntime runtime, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await CommandRepository.AddAsync(runtime, cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 更新设备运行时状态
|
|||
/// </summary>
|
|||
public void UpdateRuntime(CellularDeviceRuntime runtime) |
|||
{ |
|||
CommandRepository.Update(runtime); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 删除设备运行时状态
|
|||
/// </summary>
|
|||
public async Task DeleteRuntimeAsync(string id, CancellationToken cancellationToken = default) |
|||
{ |
|||
await CommandRepository.DeleteByIdAsync(id, cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取所有设备运行时状态
|
|||
/// </summary>
|
|||
public async Task<IList<CellularDeviceRuntime>> GetAllRuntimesAsync(CancellationToken cancellationToken = default) |
|||
{ |
|||
var runtimes = await QueryRepository.GetAllAsync(cancellationToken: cancellationToken); |
|||
return runtimes.ToList(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据ID获取设备运行时状态
|
|||
/// </summary>
|
|||
public async Task<CellularDeviceRuntime?> GetRuntimeByIdAsync(string id, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await QueryRepository.GetByIdAsync(id, cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据设备编号获取运行时状态(最新一条按创建时间排序)
|
|||
/// </summary>
|
|||
public async Task<CellularDeviceRuntime?> GetRuntimeByDeviceCodeAsync(string deviceCode, CancellationToken cancellationToken = default) |
|||
{ |
|||
// 使用queryRepository获取所有匹配的记录,然后在内存中排序获取最新一条
|
|||
// 注意:由于queryRepository的限制,我们无法直接在数据库层面进行排序
|
|||
// 但通过添加的复合索引(DeviceCode, CreatedAt)可以提升查询性能
|
|||
var runtimes = await QueryRepository.FindAsync(r => r.DeviceCode == deviceCode, cancellationToken: cancellationToken); |
|||
return runtimes.OrderByDescending(r => r.CreatedAt).FirstOrDefault(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据设备编号获取运行时状态(包含设备信息)
|
|||
/// </summary>
|
|||
public async Task<CellularDeviceRuntime?> GetRuntimeByDeviceCodeWithDeviceAsync(string deviceCode, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await QueryRepository.FirstOrDefaultAsync( |
|||
r => r.DeviceCode == deviceCode, |
|||
include: q => q.Include(r => r.Device), |
|||
cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据运行编码获取运行时状态
|
|||
/// </summary>
|
|||
public async Task<CellularDeviceRuntime?> GetRuntimeByRuntimeCodeAsync(string runtimeCode, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await QueryRepository.FirstOrDefaultAsync( |
|||
r => r.RuntimeCode == runtimeCode, |
|||
include: q => q.Include(r => r.Device), |
|||
cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取正在运行的设备运行时状态
|
|||
/// </summary>
|
|||
public async Task<IList<CellularDeviceRuntime>> GetRunningRuntimesAsync(CancellationToken cancellationToken = default) |
|||
{ |
|||
var runtimes = await QueryRepository.FindAsync( |
|||
r => r.RuntimeStatus == DeviceRuntimeStatus.Running, |
|||
include: q => q.Include(r => r.Device), |
|||
cancellationToken: cancellationToken); |
|||
return runtimes.ToList(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据运行时状态获取设备运行时状态
|
|||
/// </summary>
|
|||
public async Task<IList<CellularDeviceRuntime>> GetRuntimesByStatusAsync(DeviceRuntimeStatus runtimeStatus, CancellationToken cancellationToken = default) |
|||
{ |
|||
var runtimes = await QueryRepository.FindAsync( |
|||
r => r.RuntimeStatus == runtimeStatus, |
|||
include: q => q.Include(r => r.Device), |
|||
cancellationToken: cancellationToken); |
|||
return runtimes.ToList(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据网络栈配置编号获取运行时状态
|
|||
/// </summary>
|
|||
public async Task<IList<CellularDeviceRuntime>> GetRuntimesByNetworkStackCodeAsync(string networkStackCode, CancellationToken cancellationToken = default) |
|||
{ |
|||
var runtimes = await QueryRepository.FindAsync( |
|||
r => r.NetworkStackCode == networkStackCode, |
|||
include: q => q.Include(r => r.Device), |
|||
cancellationToken: cancellationToken); |
|||
return runtimes.ToList(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 搜索设备运行时状态
|
|||
/// </summary>
|
|||
public async Task<IList<CellularDeviceRuntime>> SearchRuntimesAsync( |
|||
string? keyword, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
var query = await QueryRepository.FindAsync(r => true, cancellationToken: cancellationToken); |
|||
|
|||
if (!string.IsNullOrWhiteSpace(keyword)) |
|||
{ |
|||
query = query.Where(r => |
|||
r.DeviceCode.Contains(keyword) || |
|||
(r.RuntimeCode != null && r.RuntimeCode.Contains(keyword)) || |
|||
(r.NetworkStackCode != null && r.NetworkStackCode.Contains(keyword))); |
|||
} |
|||
|
|||
var runtimes = query; |
|||
return runtimes.ToList(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 搜索设备运行时状态(分页)
|
|||
/// </summary>
|
|||
public async Task<(int TotalCount, IList<CellularDeviceRuntime> Items)> SearchRuntimesAsync( |
|||
string? keyword, |
|||
int pageNumber, |
|||
int pageSize, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
// 构建查询条件
|
|||
Expression<Func<CellularDeviceRuntime, bool>> predicate = r => true; |
|||
|
|||
if (!string.IsNullOrWhiteSpace(keyword)) |
|||
{ |
|||
predicate = r => r.DeviceCode.Contains(keyword) || |
|||
(r.RuntimeCode != null && r.RuntimeCode.Contains(keyword)) || |
|||
(r.NetworkStackCode != null && r.NetworkStackCode.Contains(keyword)); |
|||
} |
|||
|
|||
// 执行分页查询
|
|||
var result = await QueryRepository.GetPagedAsync(predicate, pageNumber, pageSize, null, cancellationToken); |
|||
|
|||
return (result.TotalCount, result.Items.ToList()); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 检查设备运行时状态是否存在
|
|||
/// </summary>
|
|||
public async Task<bool> ExistsAsync(string id, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await QueryRepository.AnyAsync(r => r.Id == id, cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 检查设备编号是否有运行时状态
|
|||
/// </summary>
|
|||
public async Task<bool> DeviceCodeExistsAsync(string deviceCode, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await QueryRepository.AnyAsync(r => r.DeviceCode == deviceCode, cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 检查运行编码是否存在
|
|||
/// </summary>
|
|||
public async Task<bool> RuntimeCodeExistsAsync(string runtimeCode, CancellationToken cancellationToken = default) |
|||
{ |
|||
return await QueryRepository.AnyAsync(r => r.RuntimeCode == runtimeCode, cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取运行时状态总数
|
|||
/// </summary>
|
|||
public async Task<int> GetRuntimeCountAsync(CancellationToken cancellationToken = default) |
|||
{ |
|||
return await QueryRepository.CountAsync(r => true, cancellationToken: cancellationToken); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据设备编号删除运行时状态
|
|||
/// </summary>
|
|||
public async Task DeleteRuntimeByDeviceCodeAsync(string deviceCode, CancellationToken cancellationToken = default) |
|||
{ |
|||
await CommandRepository.DeleteWhereAsync(r => r.DeviceCode == deviceCode, cancellationToken); |
|||
} |
|||
} |
@ -0,0 +1,127 @@ |
|||
using CellularManagement.Application.Features.DeviceRuntimes.Commands.StartDeviceRuntime; |
|||
using CellularManagement.Application.Features.DeviceRuntimes.Commands.StopDeviceRuntime; |
|||
using CellularManagement.Application.Features.DeviceRuntimes.Queries.GetDeviceRuntimes; |
|||
using CellularManagement.Application.Features.DeviceRuntimes.Queries.GetDeviceRuntimeStatus; |
|||
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; |
|||
|
|||
/// <summary>
|
|||
/// 设备运行时状态管理控制器
|
|||
/// </summary>
|
|||
[Route("api/device-runtimes")] |
|||
[ApiController] |
|||
[Authorize] |
|||
public class DeviceRuntimesController : ApiController |
|||
{ |
|||
private readonly ILogger<DeviceRuntimesController> _logger; |
|||
|
|||
/// <summary>
|
|||
/// 初始化设备运行时状态控制器
|
|||
/// </summary>
|
|||
public DeviceRuntimesController(IMediator mediator, ILogger<DeviceRuntimesController> logger) |
|||
: base(mediator) |
|||
{ |
|||
_logger = logger; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取设备运行时状态列表
|
|||
/// </summary>
|
|||
[HttpGet] |
|||
public async Task<OperationResult<GetDeviceRuntimesResponse>> GetAll([FromQuery] GetDeviceRuntimesQuery query) |
|||
{ |
|||
_logger.LogInformation("开始获取设备运行时状态列表,页码: {PageNumber}, 每页数量: {PageSize}, 搜索关键词: {SearchTerm}", |
|||
query.PageNumber, query.PageSize, query.SearchTerm); |
|||
|
|||
var result = await mediator.Send(query); |
|||
|
|||
if (result.IsSuccess) |
|||
{ |
|||
_logger.LogInformation("获取设备运行时状态列表成功,总记录数: {TotalCount}", result.Data?.TotalCount); |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogWarning("获取设备运行时状态列表失败: {Error}", result.ErrorMessages?.FirstOrDefault()); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据设备编号获取设备运行时状态
|
|||
/// </summary>
|
|||
[HttpGet("{deviceCode}")] |
|||
public async Task<OperationResult<GetDeviceRuntimeStatusResponse>> GetByDeviceCode(string deviceCode) |
|||
{ |
|||
_logger.LogInformation("开始获取设备运行时状态,设备编号: {DeviceCode}", deviceCode); |
|||
|
|||
var query = new GetDeviceRuntimeStatusQuery(deviceCode); |
|||
var result = await mediator.Send(query); |
|||
|
|||
if (result.IsSuccess) |
|||
{ |
|||
_logger.LogInformation("获取设备运行时状态成功,设备编号: {DeviceCode}, 运行时状态: {RuntimeStatus}", |
|||
deviceCode, result.Data?.RuntimeStatus); |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogWarning("获取设备运行时状态失败,设备编号: {DeviceCode}, 错误: {Error}", |
|||
deviceCode, result.ErrorMessages?.FirstOrDefault()); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 启动设备
|
|||
/// </summary>
|
|||
[HttpPost("start")] |
|||
public async Task<OperationResult<StartDeviceRuntimeResponse>> StartDevices([FromBody] StartDeviceRuntimeCommand command) |
|||
{ |
|||
_logger.LogInformation("开始批量启动设备,设备数量: {DeviceCount}", command.DeviceRequests.Count); |
|||
|
|||
var result = await mediator.Send(command); |
|||
|
|||
if (result.IsSuccess) |
|||
{ |
|||
_logger.LogInformation("批量启动设备成功,总数量: {TotalCount}, 成功: {SuccessCount}, 失败: {FailureCount}", |
|||
result.Data?.Summary?.TotalCount, result.Data?.Summary?.SuccessCount, result.Data?.Summary?.FailureCount); |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogWarning("批量启动设备失败: {Error}", result.ErrorMessages?.FirstOrDefault()); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 停止设备
|
|||
/// </summary>
|
|||
[HttpPost("{deviceCode}/stop")] |
|||
public async Task<OperationResult<StopDeviceRuntimeResponse>> StopDevice(string deviceCode) |
|||
{ |
|||
_logger.LogInformation("开始停止设备,设备编号: {DeviceCode}", deviceCode); |
|||
|
|||
var command = new StopDeviceRuntimeCommand { DeviceCode = deviceCode }; |
|||
var result = await mediator.Send(command); |
|||
|
|||
if (result.IsSuccess) |
|||
{ |
|||
_logger.LogInformation("设备停止成功,设备编号: {DeviceCode}", deviceCode); |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogWarning("设备停止失败,设备编号: {DeviceCode}, 错误: {Error}", |
|||
deviceCode, result.ErrorMessages?.FirstOrDefault()); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
File diff suppressed because it is too large
Loading…
Reference in new issue