You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.2 KiB

using CellularManagement.Domain.Repositories.Terminal;
using CellularManagement.Domain.Repositories.Base;
using MediatR;
using Microsoft.Extensions.Logging;
using CellularManagement.Domain.Common;
namespace CellularManagement.Application.Features.AdbOperations.Commands.DeleteAdbOperation;
/// <summary>
/// 删除ADB操作命令处理器
/// </summary>
public class DeleteAdbOperationCommandHandler : IRequestHandler<DeleteAdbOperationCommand, OperationResult<bool>>
{
private readonly IAdbOperationRepository _adbOperationRepository;
private readonly ILogger<DeleteAdbOperationCommandHandler> _logger;
private readonly IUnitOfWork _unitOfWork;
public DeleteAdbOperationCommandHandler(
IAdbOperationRepository adbOperationRepository,
ILogger<DeleteAdbOperationCommandHandler> logger,
IUnitOfWork unitOfWork)
{
_adbOperationRepository = adbOperationRepository;
_logger = logger;
_unitOfWork = unitOfWork;
}
public async Task<OperationResult<bool>> Handle(
DeleteAdbOperationCommand request,
CancellationToken cancellationToken)
{
_logger.LogInformation("开始删除ADB操作,ID: {Id}", request.Id);
try
{
// 检查ADB操作是否存在
var existingOperation = await _adbOperationRepository.GetOperationByIdAsync(request.Id, cancellationToken);
if (existingOperation == null)
{
_logger.LogWarning("ADB操作不存在,ID: {Id}", request.Id);
return OperationResult<bool>.CreateFailure("ADB操作不存在");
}
// 删除ADB操作
await _adbOperationRepository.DeleteOperationAsync(request.Id, cancellationToken);
// 确保数据被持久化到数据库
await _unitOfWork.SaveChangesAsync(cancellationToken);
_logger.LogInformation("ADB操作删除成功,ID: {Id}", request.Id);
return OperationResult<bool>.CreateSuccess(true);
}
catch (Exception ex)
{
_logger.LogError(ex, "ADB操作删除失败,ID: {Id}", request.Id);
return OperationResult<bool>.CreateFailure("删除ADB操作时发生错误");
}
}
}