DeleteRobotCommandHandler.cs
1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
/// <summary>
/// 删除机器人命令处理器
/// </summary>
public class DeleteRobotCommandHandler : IConsumer<DeleteRobotCommand>
{
private readonly ILogger<DeleteRobotCommandHandler> _logger;
private readonly IRobotRepository _robotRepository;
public DeleteRobotCommandHandler(
ILogger<DeleteRobotCommandHandler> logger,
IRobotRepository robotRepository)
{
_logger = logger;
_robotRepository = robotRepository;
}
public async Task Consume(ConsumeContext<DeleteRobotCommand> context)
{
var command = context.Message;
try
{
// 获取机器人
var robot = await _robotRepository.GetByIdAsync(
command.RobotId,
context.CancellationToken);
if (robot == null)
{
throw new InvalidOperationException($"机器人ID {command.RobotId} 不存在");
}
// 检查机器人状态,如果正在忙碌则不允许删除
if (robot.Status == Domain.Entities.RobotStatus.Busy)
{
throw new InvalidOperationException($"机器人 {robot.RobotCode} 正在忙碌,不允许删除");
}
await _robotRepository.DeleteByIdAsync(command.RobotId);
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
}