CompletionRobotTaskCommandHandler.cs
1.79 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
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>
/// 删除任务命令处理器
/// @author zzy
/// </summary>
public class CompletionRobotTaskCommandHandler : IConsumer<CompletionRobotTaskCommand>
{
private readonly ILogger<CompletionRobotTaskCommandHandler> _logger;
private readonly IRobotTaskRepository _robotTaskRepository;
private readonly IRobotRepository _robotRepository;
public CompletionRobotTaskCommandHandler(
ILogger<CompletionRobotTaskCommandHandler> logger,
IRobotTaskRepository robotTaskRepository,
IRobotRepository robotRepository)
{
_logger = logger;
_robotTaskRepository = robotTaskRepository;
_robotRepository = robotRepository;
}
public async Task Consume(ConsumeContext<CompletionRobotTaskCommand> context)
{
var command = context.Message;
try
{
var task = await _robotTaskRepository.GetByRelationAsync(command.task_id, context.CancellationToken);
if (task == null)
{
await context.RespondAsync(ApiResponse.Failed($"未找到关联单据为 {command.task_id} 的任务"));
return;
}
var robot = await _robotRepository.GetBySerialNumberAsync(command.robot_code, context.CancellationToken);
task.Completed(robot?.RobotId);
await _robotTaskRepository.UpdateAsync(task, context.CancellationToken);
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
}