GetRobotTaskHistoriesQueryHandler.cs
3.47 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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.Infrastructure.DB.MsSql;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
/// <summary>
/// 查询历史主任务列表命令处理器
/// </summary>
public class GetRobotTaskHistoriesQueryHandler : IConsumer<GetRobotTaskHistoriesQuery>
{
private readonly ILogger<GetRobotTaskHistoriesQueryHandler> _logger;
private readonly AppDbContext _dbContext;
private readonly IMapper _mapper;
public GetRobotTaskHistoriesQueryHandler(
ILogger<GetRobotTaskHistoriesQueryHandler> logger,
AppDbContext dbContext,
IMapper mapper)
{
_logger = logger;
_dbContext = dbContext;
_mapper = mapper;
}
public async Task Consume(ConsumeContext<GetRobotTaskHistoriesQuery> context)
{
var query = context.Message;
var queryable = _dbContext.RobotTaskHistories.AsQueryable();
if (!string.IsNullOrWhiteSpace(query.FilterModel))
{
queryable = FilterHelper.ApplyFilters(queryable, query.FilterModel);
}
if (!string.IsNullOrWhiteSpace(query.TaskCode))
{
queryable = queryable.Where(t => t.TaskCode.Contains(query.TaskCode));
}
if (!string.IsNullOrWhiteSpace(query.TaskName))
{
queryable = queryable.Where(t => t.TaskName != null && t.TaskName.Contains(query.TaskName));
}
if (!string.IsNullOrWhiteSpace(query.RobotCode))
{
queryable = queryable.Where(t => t.RobotCode != null && t.RobotCode.Contains(query.RobotCode));
}
if (query.Status.HasValue)
{
queryable = queryable.Where(t => (int)t.Status == query.Status.Value);
}
if (query.Priority.HasValue)
{
queryable = queryable.Where(t => t.Priority == query.Priority.Value);
}
if (!string.IsNullOrWhiteSpace(query.BeginLocationCode))
{
queryable = queryable.Where(t =>
t.BeginLocationCode != null && t.BeginLocationCode.Contains(query.BeginLocationCode));
}
if (!string.IsNullOrWhiteSpace(query.EndLocationCode))
{
queryable = queryable.Where(t =>
t.EndLocationCode != null && t.EndLocationCode.Contains(query.EndLocationCode));
}
if (!string.IsNullOrWhiteSpace(query.Source))
{
queryable = queryable.Where(t => t.Source != null && t.Source.Contains(query.Source));
}
if (!string.IsNullOrWhiteSpace(query.Relation))
{
queryable = queryable.Where(t => t.Relation != null && t.Relation.Contains(query.Relation));
}
var totalCount = await queryable.CountAsync(context.CancellationToken);
var histories = await queryable
.OrderByDescending(t => t.ArchivedAt)
.ThenByDescending(t => t.CreatedAt)
.Skip((query.PageNumber - 1) * query.PageSize)
.Take(query.PageSize)
.ToListAsync(context.CancellationToken);
var historyDtos = _mapper.Map<List<RobotTaskHistoryListItemDto>>(histories);
await context.RespondAsync(
PagedResponse<RobotTaskHistoryListItemDto>.Successful(
historyDtos,
query.PageNumber,
query.PageSize,
totalCount));
}
}