|
|
@ -5,6 +5,9 @@ using CellularManagement.Domain.Entities.Device; |
|
|
|
using CellularManagement.Domain.Repositories.Device; |
|
|
|
using CellularManagement.Domain.Repositories.Base; |
|
|
|
using CellularManagement.Domain.Services; |
|
|
|
using X1.DynamicClientCore.Features; |
|
|
|
using X1.DynamicClientCore.Models; |
|
|
|
using Microsoft.EntityFrameworkCore.Metadata; |
|
|
|
|
|
|
|
namespace CellularManagement.Application.Features.DeviceRuntimes.Commands.StopDeviceRuntime; |
|
|
|
|
|
|
@ -17,6 +20,8 @@ public class StopDeviceRuntimeCommandHandler : IRequestHandler<StopDeviceRuntime |
|
|
|
private readonly ILogger<StopDeviceRuntimeCommandHandler> _logger; |
|
|
|
private readonly IUnitOfWork _unitOfWork; |
|
|
|
private readonly ICurrentUserService _currentUserService; |
|
|
|
private readonly IInstrumentProtocolClient _protocolClient; |
|
|
|
private readonly ICellularDeviceRuntimeDetailRepository _detailRepository; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 初始化命令处理器
|
|
|
@ -25,12 +30,16 @@ public class StopDeviceRuntimeCommandHandler : IRequestHandler<StopDeviceRuntime |
|
|
|
ICellularDeviceRuntimeRepository deviceRuntimeRepository, |
|
|
|
ILogger<StopDeviceRuntimeCommandHandler> logger, |
|
|
|
IUnitOfWork unitOfWork, |
|
|
|
ICurrentUserService currentUserService) |
|
|
|
ICurrentUserService currentUserService, |
|
|
|
IInstrumentProtocolClient protocolClient, |
|
|
|
ICellularDeviceRuntimeDetailRepository detailRepository) |
|
|
|
{ |
|
|
|
_deviceRuntimeRepository = deviceRuntimeRepository; |
|
|
|
_logger = logger; |
|
|
|
_unitOfWork = unitOfWork; |
|
|
|
_currentUserService = currentUserService; |
|
|
|
_protocolClient = protocolClient; |
|
|
|
_detailRepository = detailRepository; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
@ -38,60 +47,396 @@ public class StopDeviceRuntimeCommandHandler : IRequestHandler<StopDeviceRuntime |
|
|
|
/// </summary>
|
|
|
|
public async Task<OperationResult<StopDeviceRuntimeResponse>> Handle(StopDeviceRuntimeCommand request, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
var requestId = Guid.NewGuid().ToString("N")[..8]; // 生成8位请求ID用于跟踪
|
|
|
|
var startTime = DateTime.UtcNow; |
|
|
|
|
|
|
|
_logger.LogInformation("[{RequestId}] 开始处理停止设备运行时状态命令,设备数量: {DeviceCount}", |
|
|
|
requestId, request.DeviceCodes.Length); |
|
|
|
|
|
|
|
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.LogDebug("[{RequestId}] 开始验证请求参数", requestId); |
|
|
|
if (!ValidateRequest(request)) |
|
|
|
{ |
|
|
|
_logger.LogWarning("设备未在运行中,设备编号: {DeviceCode}, 当前状态: {RuntimeStatus}", |
|
|
|
request.DeviceCode, deviceRuntime.RuntimeStatus); |
|
|
|
return OperationResult<StopDeviceRuntimeResponse>.CreateFailure($"设备编号 {request.DeviceCode} 未在运行中,当前状态: {deviceRuntime.RuntimeStatus}"); |
|
|
|
_logger.LogWarning("[{RequestId}] 请求参数验证失败", requestId); |
|
|
|
return OperationResult<StopDeviceRuntimeResponse>.CreateFailure("请求参数无效"); |
|
|
|
} |
|
|
|
_logger.LogDebug("[{RequestId}] 请求参数验证通过", requestId); |
|
|
|
|
|
|
|
// 获取当前用户
|
|
|
|
var currentUser = _currentUserService.GetCurrentUserId(); |
|
|
|
_logger.LogDebug("[{RequestId}] 开始获取当前用户信息", requestId); |
|
|
|
var currentUser = GetCurrentUser(); |
|
|
|
if (string.IsNullOrEmpty(currentUser)) |
|
|
|
{ |
|
|
|
_logger.LogWarning("当前用户未登录"); |
|
|
|
_logger.LogWarning("[{RequestId}] 用户未登录,停止处理", requestId); |
|
|
|
return OperationResult<StopDeviceRuntimeResponse>.CreateFailure("用户未登录"); |
|
|
|
} |
|
|
|
_logger.LogDebug("[{RequestId}] 当前用户: {CurrentUser}", requestId, currentUser); |
|
|
|
|
|
|
|
// 获取设备运行时状态
|
|
|
|
_logger.LogDebug("[{RequestId}] 开始获取设备运行时状态,设备编号: [{DeviceCodes}]", |
|
|
|
requestId, string.Join(", ", request.DeviceCodes)); |
|
|
|
var runtimeMap = await GetDeviceRuntimesAsync(request.DeviceCodes, cancellationToken); |
|
|
|
_logger.LogInformation("[{RequestId}] 获取设备运行时状态完成,找到 {FoundCount} 个运行中的设备", |
|
|
|
requestId, runtimeMap.Count); |
|
|
|
|
|
|
|
// 停止设备
|
|
|
|
deviceRuntime.Stop(); |
|
|
|
// 处理设备停止
|
|
|
|
_logger.LogDebug("[{RequestId}] 开始处理设备停止", requestId); |
|
|
|
var processingResults = ProcessDeviceStop(request.DeviceCodes, runtimeMap, currentUser); |
|
|
|
var successCount = processingResults.Count(r => r.Success); |
|
|
|
var failureCount = processingResults.Count(r => !r.Success); |
|
|
|
_logger.LogInformation("[{RequestId}] 设备停止处理完成,成功: {SuccessCount}, 失败: {FailureCount}", |
|
|
|
requestId, successCount, failureCount); |
|
|
|
|
|
|
|
// 更新到数据库
|
|
|
|
_deviceRuntimeRepository.UpdateRuntime(deviceRuntime); |
|
|
|
await _unitOfWork.SaveChangesAsync(cancellationToken); |
|
|
|
// 并行停止网络连接
|
|
|
|
_logger.LogDebug("[{RequestId}] 开始并行停止网络连接", requestId); |
|
|
|
var stopResults = await StopNetworkConnectionsAsync(processingResults, cancellationToken); |
|
|
|
var networkSuccessCount = stopResults.Count(kvp => kvp.Value); |
|
|
|
var networkFailureCount = stopResults.Count(kvp => !kvp.Value); |
|
|
|
_logger.LogInformation("[{RequestId}] 网络连接停止完成,成功: {NetworkSuccessCount}, 失败: {NetworkFailureCount}", |
|
|
|
requestId, networkSuccessCount, networkFailureCount); |
|
|
|
|
|
|
|
// 构建结果
|
|
|
|
_logger.LogDebug("[{RequestId}] 开始构建响应结果", requestId); |
|
|
|
var (deviceResults, summary) = BuildResults(processingResults, stopResults); |
|
|
|
_logger.LogDebug("[{RequestId}] 响应结果构建完成,设备结果数量: {DeviceResultsCount}", |
|
|
|
requestId, deviceResults.Count); |
|
|
|
|
|
|
|
_logger.LogInformation("设备停止成功,设备编号: {DeviceCode}", deviceRuntime.DeviceCode); |
|
|
|
// 保存数据
|
|
|
|
_logger.LogDebug("[{RequestId}] 开始保存数据到数据库", requestId); |
|
|
|
await SaveDataAsync(processingResults, cancellationToken); |
|
|
|
_logger.LogDebug("[{RequestId}] 数据保存完成", requestId); |
|
|
|
|
|
|
|
// 构建响应
|
|
|
|
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 |
|
|
|
DeviceResults = deviceResults, |
|
|
|
Summary = summary, |
|
|
|
UpdatedAt = DateTime.UtcNow |
|
|
|
}; |
|
|
|
|
|
|
|
var totalTime = DateTime.UtcNow - startTime; |
|
|
|
_logger.LogInformation("[{RequestId}] 批量停止设备完成,总耗时: {TotalTime:F2}ms, 总数: {TotalCount}, 成功: {SuccessCount}, 失败: {FailureCount}, 成功率: {SuccessRate:F2}%", |
|
|
|
requestId, totalTime.TotalMilliseconds, summary.TotalCount, summary.SuccessCount, summary.FailureCount, summary.SuccessRate); |
|
|
|
|
|
|
|
return OperationResult<StopDeviceRuntimeResponse>.CreateSuccess(response); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "停止设备运行时状态失败,设备编号: {DeviceCode}", request.DeviceCode); |
|
|
|
return OperationResult<StopDeviceRuntimeResponse>.CreateFailure($"停止设备运行时状态失败: {ex.Message}"); |
|
|
|
var totalTime = DateTime.UtcNow - startTime; |
|
|
|
_logger.LogError(ex, "[{RequestId}] 批量停止设备运行时状态失败,总耗时: {TotalTime:F2}ms, 错误: {ErrorMessage}", |
|
|
|
requestId, totalTime.TotalMilliseconds, ex.Message); |
|
|
|
return OperationResult<StopDeviceRuntimeResponse>.CreateFailure($"批量停止设备运行时状态失败: {ex.Message}"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 验证请求参数
|
|
|
|
/// </summary>
|
|
|
|
private bool ValidateRequest(StopDeviceRuntimeCommand request) |
|
|
|
{ |
|
|
|
if (!request.IsValid()) |
|
|
|
{ |
|
|
|
_logger.LogWarning("请求参数无效"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取当前用户
|
|
|
|
/// </summary>
|
|
|
|
private string GetCurrentUser() |
|
|
|
{ |
|
|
|
var currentUser = _currentUserService.GetCurrentUserId(); |
|
|
|
if (string.IsNullOrEmpty(currentUser)) |
|
|
|
{ |
|
|
|
_logger.LogWarning("当前用户未登录"); |
|
|
|
} |
|
|
|
return currentUser; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取设备运行时状态
|
|
|
|
/// </summary>
|
|
|
|
private async Task<Dictionary<string, CellularDeviceRuntime>> GetDeviceRuntimesAsync(string[] deviceCodes, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
var allRuntimes = await _deviceRuntimeRepository.GetRuntimesByDeviceCodesAsync( |
|
|
|
deviceCodes.ToList(), |
|
|
|
new[] { DeviceRuntimeStatus.Running }, |
|
|
|
cancellationToken); |
|
|
|
|
|
|
|
return allRuntimes.ToDictionary(r => r.DeviceCode, r => r); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 处理设备停止
|
|
|
|
/// </summary>
|
|
|
|
private List<DeviceProcessingResult> ProcessDeviceStop( |
|
|
|
string[] deviceCodes, |
|
|
|
Dictionary<string, CellularDeviceRuntime> runtimeMap, |
|
|
|
string currentUser) |
|
|
|
{ |
|
|
|
var processingResults = new List<DeviceProcessingResult>(); |
|
|
|
|
|
|
|
foreach (var deviceCode in deviceCodes) |
|
|
|
{ |
|
|
|
var result = ProcessSingleDevice(deviceCode, runtimeMap, currentUser); |
|
|
|
processingResults.Add(result); |
|
|
|
} |
|
|
|
|
|
|
|
return processingResults; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 处理单个设备停止
|
|
|
|
/// </summary>
|
|
|
|
private DeviceProcessingResult ProcessSingleDevice( |
|
|
|
string deviceCode, |
|
|
|
Dictionary<string, CellularDeviceRuntime> runtimeMap, |
|
|
|
string currentUser) |
|
|
|
{ |
|
|
|
// 检查设备运行时状态是否存在
|
|
|
|
if (!runtimeMap.TryGetValue(deviceCode, out var deviceRuntime)) |
|
|
|
{ |
|
|
|
_logger.LogInformation("跳过设备停止:设备运行时状态不存在,设备编号: {DeviceCode}", deviceCode); |
|
|
|
return new DeviceProcessingResult |
|
|
|
{ |
|
|
|
DeviceCode = deviceCode, |
|
|
|
Success = false, |
|
|
|
Runtime = null, |
|
|
|
Detail = null, |
|
|
|
ErrorMessage = "设备运行时状态不存在" |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
// 停止设备
|
|
|
|
deviceRuntime.Stop(); |
|
|
|
|
|
|
|
// 创建运行时详情
|
|
|
|
var detail = CellularDeviceRuntimeDetail.Create( |
|
|
|
deviceCode, |
|
|
|
deviceRuntime.RuntimeCode ?? throw new InvalidOperationException($"设备 {deviceCode} 的运行编码为空"), |
|
|
|
deviceRuntime.NetworkStackCode ?? throw new InvalidOperationException($"设备 {deviceCode} 的网络栈配置编码为空"), |
|
|
|
currentUser); |
|
|
|
|
|
|
|
_logger.LogDebug("设备停止处理完成,设备编号: {DeviceCode}", deviceRuntime.DeviceCode); |
|
|
|
|
|
|
|
return new DeviceProcessingResult |
|
|
|
{ |
|
|
|
DeviceCode = deviceCode, |
|
|
|
Success = true, |
|
|
|
Runtime = deviceRuntime, |
|
|
|
Detail = detail, |
|
|
|
ErrorMessage = null |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 并行停止网络连接
|
|
|
|
/// </summary>
|
|
|
|
private async Task<Dictionary<string, bool>> StopNetworkConnectionsAsync( |
|
|
|
List<DeviceProcessingResult> processingResults, |
|
|
|
CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
_logger.LogInformation("开始并行停止网络连接,处理结果数量: {ProcessingResultsCount}", processingResults.Count); |
|
|
|
|
|
|
|
// 过滤有效的处理结果
|
|
|
|
var validResults = processingResults |
|
|
|
.Where(r => r.Success && r.Runtime != null && !string.IsNullOrEmpty(r.Runtime.RuntimeCode)) |
|
|
|
.ToList(); |
|
|
|
|
|
|
|
_logger.LogInformation("有效处理结果数量: {ValidResultsCount}", validResults.Count); |
|
|
|
|
|
|
|
if (!validResults.Any()) |
|
|
|
{ |
|
|
|
_logger.LogWarning("没有有效的处理结果需要停止网络连接"); |
|
|
|
return new Dictionary<string, bool>(); |
|
|
|
} |
|
|
|
|
|
|
|
// 记录每个设备的详细信息
|
|
|
|
foreach (var result in validResults) |
|
|
|
{ |
|
|
|
_logger.LogDebug("准备停止网络连接,设备编号: {DeviceCode}, 运行编码: {RuntimeCode}, 网络栈编码: {NetworkStackCode}", |
|
|
|
result.DeviceCode, result.Runtime!.RuntimeCode, result.Runtime.NetworkStackCode); |
|
|
|
} |
|
|
|
|
|
|
|
var stopTasks = validResults |
|
|
|
.Select(async result => |
|
|
|
{ |
|
|
|
var deviceCode = result.DeviceCode; |
|
|
|
var runtimeCode = result.Runtime!.RuntimeCode!; |
|
|
|
|
|
|
|
_logger.LogDebug("开始停止网络连接,设备编号: {DeviceCode}, 运行编码: {RuntimeCode}", deviceCode, runtimeCode); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
var stopRequest = new StopCellularNetworkRequest |
|
|
|
{ |
|
|
|
// 根据实际需要设置停止请求参数
|
|
|
|
Key = runtimeCode |
|
|
|
}; |
|
|
|
|
|
|
|
_logger.LogDebug("发送停止网络请求,设备编号: {DeviceCode}, 请求Key: {RequestKey}", deviceCode, stopRequest.Key); |
|
|
|
|
|
|
|
var stopSuccess = await _protocolClient.StopNetworkAsync( |
|
|
|
deviceCode, |
|
|
|
stopRequest, |
|
|
|
cancellationToken: cancellationToken); |
|
|
|
|
|
|
|
_logger.LogDebug("收到停止网络响应,设备编号: {DeviceCode}, 停止成功: {StopSuccess}", deviceCode, stopSuccess); |
|
|
|
|
|
|
|
if (!stopSuccess) |
|
|
|
{ |
|
|
|
_logger.LogWarning("协议客户端停止网络连接失败,设备编号: {DeviceCode}", deviceCode); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
_logger.LogInformation("协议客户端停止网络连接成功,设备编号: {DeviceCode}", deviceCode); |
|
|
|
} |
|
|
|
|
|
|
|
return new { DeviceCode = deviceCode, StopSuccess = stopSuccess }; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "协议客户端停止网络连接异常,设备编号: {DeviceCode}, 运行编码: {RuntimeCode}", deviceCode, runtimeCode); |
|
|
|
return new { DeviceCode = deviceCode, StopSuccess = false }; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
_logger.LogInformation("启动 {TaskCount} 个并行停止网络连接任务", stopTasks.Count()); |
|
|
|
|
|
|
|
var stopResults = await Task.WhenAll(stopTasks); |
|
|
|
|
|
|
|
_logger.LogInformation("所有停止网络连接任务完成,结果数量: {ResultCount}", stopResults.Length); |
|
|
|
|
|
|
|
var resultDictionary = stopResults.ToDictionary(r => r.DeviceCode, r => r.StopSuccess); |
|
|
|
|
|
|
|
// 统计成功和失败数量
|
|
|
|
var successCount = resultDictionary.Count(kvp => kvp.Value); |
|
|
|
var failureCount = resultDictionary.Count(kvp => !kvp.Value); |
|
|
|
|
|
|
|
_logger.LogInformation("网络连接停止统计 - 成功: {SuccessCount}, 失败: {FailureCount}, 成功率: {SuccessRate:F2}%", |
|
|
|
successCount, failureCount, successCount * 100.0 / resultDictionary.Count); |
|
|
|
|
|
|
|
return resultDictionary; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 构建结果
|
|
|
|
/// </summary>
|
|
|
|
private (List<DeviceStopResult> deviceResults, BatchOperationSummary summary) BuildResults( |
|
|
|
List<DeviceProcessingResult> processingResults, |
|
|
|
Dictionary<string, bool> stopResults) |
|
|
|
{ |
|
|
|
var deviceResults = new List<DeviceStopResult>(); |
|
|
|
var successCount = 0; |
|
|
|
var failureCount = 0; |
|
|
|
|
|
|
|
foreach (var result in processingResults) |
|
|
|
{ |
|
|
|
if (result.Success && result.Runtime != null && result.Detail != null) |
|
|
|
{ |
|
|
|
// 检查网络停止结果
|
|
|
|
var networkStopSuccess = stopResults.TryGetValue(result.DeviceCode, out var stopSuccess) && stopSuccess; |
|
|
|
|
|
|
|
deviceResults.Add(new DeviceStopResult |
|
|
|
{ |
|
|
|
DeviceCode = result.Runtime.DeviceCode, |
|
|
|
Id = result.Runtime.Id, |
|
|
|
RuntimeStatus = result.Runtime.RuntimeStatus.ToString(), |
|
|
|
RuntimeCode = result.Runtime.RuntimeCode, |
|
|
|
NetworkStackCode = result.Runtime.NetworkStackCode, |
|
|
|
UpdatedAt = result.Runtime.UpdatedAt ?? DateTime.UtcNow, |
|
|
|
IsSuccess = networkStopSuccess, |
|
|
|
ErrorMessage = networkStopSuccess ? null : "网络连接停止失败" |
|
|
|
}); |
|
|
|
|
|
|
|
if (networkStopSuccess) |
|
|
|
successCount++; |
|
|
|
else |
|
|
|
failureCount++; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// 处理失败的情况
|
|
|
|
var errorMessage = result.ErrorMessage ?? "设备运行时状态不存在"; |
|
|
|
|
|
|
|
// 如果有Runtime对象但缺少必要字段,提供更具体的错误信息
|
|
|
|
if (result.Runtime != null) |
|
|
|
{ |
|
|
|
if (string.IsNullOrEmpty(result.Runtime.RuntimeCode)) |
|
|
|
errorMessage = "设备运行编码为空"; |
|
|
|
else if (string.IsNullOrEmpty(result.Runtime.NetworkStackCode)) |
|
|
|
errorMessage = "设备网络栈配置编码为空"; |
|
|
|
} |
|
|
|
|
|
|
|
deviceResults.Add(new DeviceStopResult |
|
|
|
{ |
|
|
|
DeviceCode = result.DeviceCode, |
|
|
|
Id = result.Runtime?.Id ?? string.Empty, |
|
|
|
RuntimeStatus = result.Runtime?.RuntimeStatus.ToString() ?? "Error", |
|
|
|
RuntimeCode = result.Runtime?.RuntimeCode, |
|
|
|
NetworkStackCode = result.Runtime?.NetworkStackCode, |
|
|
|
UpdatedAt = DateTime.UtcNow, |
|
|
|
IsSuccess = false, |
|
|
|
ErrorMessage = errorMessage |
|
|
|
}); |
|
|
|
failureCount++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var summary = new BatchOperationSummary |
|
|
|
{ |
|
|
|
TotalCount = processingResults.Count, |
|
|
|
SuccessCount = successCount, |
|
|
|
FailureCount = failureCount |
|
|
|
}; |
|
|
|
|
|
|
|
return (deviceResults, summary); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 保存数据
|
|
|
|
/// </summary>
|
|
|
|
private async Task SaveDataAsync(List<DeviceProcessingResult> processingResults, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
var runtimesToUpdate = processingResults |
|
|
|
.Where(r => r.Success && r.Runtime != null) |
|
|
|
.Select(r => r.Runtime!) |
|
|
|
.ToList(); |
|
|
|
|
|
|
|
var runtimeDetails = processingResults |
|
|
|
.Where(r => r.Success && r.Detail != null) |
|
|
|
.Select(r => r.Detail!) |
|
|
|
.ToList(); |
|
|
|
|
|
|
|
// 批量保存运行时详情
|
|
|
|
if (runtimeDetails.Any()) |
|
|
|
{ |
|
|
|
_logger.LogDebug("保存运行时详情,详情数量: {DetailCount}", runtimeDetails.Count); |
|
|
|
await _detailRepository.AddRangeAsync(runtimeDetails); |
|
|
|
} |
|
|
|
|
|
|
|
// 批量更新数据库
|
|
|
|
if (runtimesToUpdate.Any()) |
|
|
|
{ |
|
|
|
_deviceRuntimeRepository.UpdateRuntimes(runtimesToUpdate); |
|
|
|
await _unitOfWork.SaveChangesAsync(cancellationToken); |
|
|
|
_logger.LogInformation("批量更新数据库完成,更新数量: {UpdateCount}", runtimesToUpdate.Count); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 设备处理结果
|
|
|
|
/// </summary>
|
|
|
|
private class DeviceProcessingResult |
|
|
|
{ |
|
|
|
public string DeviceCode { get; set; } = string.Empty; |
|
|
|
public bool Success { get; set; } |
|
|
|
public CellularDeviceRuntime? Runtime { get; set; } |
|
|
|
public CellularDeviceRuntimeDetail? Detail { get; set; } |
|
|
|
public string? ErrorMessage { get; set; } |
|
|
|
} |
|
|
|
} |