GetRobotTaskHistoriesQueryHandler.cs 3.47 KB
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));
    }
}