GetRobotTasksQueryHandler.cs
1.9 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
57
58
using AutoMapper;
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.DTOs;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
/// <summary>
/// 查询任务列表命令处理器
/// @author zzy
/// </summary>
public class GetRobotTasksQueryHandler : IConsumer<GetRobotTasksQuery>
{
private readonly ILogger<GetRobotTasksQueryHandler> _logger;
private readonly IRobotTaskRepository _robotTaskRepository;
private readonly IMapper _mapper;
public GetRobotTasksQueryHandler(
ILogger<GetRobotTasksQueryHandler> logger,
IRobotTaskRepository robotTaskRepository,
IMapper mapper)
{
_logger = logger;
_robotTaskRepository = robotTaskRepository;
_mapper = mapper;
}
public async Task Consume(ConsumeContext<GetRobotTasksQuery> context)
{
var query = context.Message;
var queryable = _robotTaskRepository.GetQueryable();
if (!string.IsNullOrWhiteSpace(query.FilterModel))
{
queryable = FilterHelper.ApplyFilters(queryable, query.FilterModel);
}
var totalCount = await queryable.CountAsync(context.CancellationToken);
var tasks = await queryable
.OrderByDescending(t => t.CreatedAt)
.Skip((query.PageNumber - 1) * query.PageSize)
.Take(query.PageSize)
.Include(t => t.Robot)
.Include(t => t.BeginLocation)
.Include(t => t.EndLocation)
.Include(t => t.TaskTemplate)
.ToListAsync(context.CancellationToken);
var taskDtos = _mapper.Map<List<RobotTaskListItemDto>>(tasks);
await context.RespondAsync(PagedResponse<RobotTaskListItemDto>.Successful(taskDtos, query.PageNumber, query.PageSize, totalCount));
}
}