2026-07-28 14:41:01 +10:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using PARR.Core.Repositories.Interfaces;
|
|
|
|
|
|
using PARR.Core.Services.RobotTaskDetailsServices.Interfaces;
|
|
|
|
|
|
using PARR.Domain.DTOs.RobotTaskDetails;
|
|
|
|
|
|
using PARR.Domain.DTOs.Shared;
|
|
|
|
|
|
using PARR.Domain.Enums;
|
2026-07-29 09:50:21 +10:00
|
|
|
|
using PARR.Domain.Exceptions;
|
2026-07-28 14:41:01 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.Core.Services.RobotTaskDetailsServices.Implementations
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class RobotTaskDetailsService : IRobotTaskDetailsService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<RobotTaskDetailsService> _logger;
|
|
|
|
|
|
private readonly IRobotConfigurationRepository _robotConfigurationRepository;
|
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
private readonly IRobotRepository _robotRepository;
|
|
|
|
|
|
private readonly ITaskStatusRepository _taskStatusRepository;
|
|
|
|
|
|
|
|
|
|
|
|
public RobotTaskDetailsService(
|
|
|
|
|
|
ILogger<RobotTaskDetailsService> logger,
|
|
|
|
|
|
IRobotConfigurationRepository robotConfigurationRepository,
|
|
|
|
|
|
IMapper mapper,
|
|
|
|
|
|
IRobotRepository robotRepository,
|
|
|
|
|
|
ITaskStatusRepository taskStatusRepository
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_robotConfigurationRepository = robotConfigurationRepository;
|
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
|
_robotRepository = robotRepository;
|
|
|
|
|
|
_taskStatusRepository = taskStatusRepository;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-29 09:50:21 +10:00
|
|
|
|
public async Task<RobotTaskDetailsResult> GetDetailsAsync(RobotsEnum robot, TaskStatusEnum task, CancellationToken cancellationToken = default)
|
2026-07-28 14:41:01 +10:00
|
|
|
|
{
|
2026-07-29 09:50:21 +10:00
|
|
|
|
// 1. Получаем группировку конфигураций
|
|
|
|
|
|
var groupedDetails = await _robotConfigurationRepository.Get()
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.Where(config => config.RobotCode == (int)robot && config.TaskStatusCode == (int)task)
|
|
|
|
|
|
.GroupBy(config => config.Template!.Job!.Group)
|
|
|
|
|
|
.Select(t => new
|
|
|
|
|
|
{
|
|
|
|
|
|
JobGroup = t.Key,
|
|
|
|
|
|
TemplatesCount = t.Count()
|
|
|
|
|
|
})
|
|
|
|
|
|
.ToListAsync(cancellationToken);
|
2026-07-28 14:41:01 +10:00
|
|
|
|
|
2026-07-29 09:50:21 +10:00
|
|
|
|
// 2. Получаем сущности робота и статуса задачи)
|
|
|
|
|
|
var robotEntity = await _robotRepository.Get()
|
2026-07-28 14:41:01 +10:00
|
|
|
|
.AsNoTracking()
|
2026-07-29 09:50:21 +10:00
|
|
|
|
.FirstOrDefaultAsync(r => r.Code == (int)robot, cancellationToken);
|
2026-07-28 14:41:01 +10:00
|
|
|
|
|
2026-07-29 09:50:21 +10:00
|
|
|
|
var taskStatusEntity = await _taskStatusRepository.Get()
|
2026-07-28 14:41:01 +10:00
|
|
|
|
.AsNoTracking()
|
2026-07-29 09:50:21 +10:00
|
|
|
|
.FirstOrDefaultAsync(t => t.Code == (int)task, cancellationToken);
|
2026-07-28 14:41:01 +10:00
|
|
|
|
|
2026-07-29 09:50:21 +10:00
|
|
|
|
|
|
|
|
|
|
if (robotEntity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogWarning("Робот с кодом {RobotCode} не найден в БД", robot);
|
|
|
|
|
|
throw new AppValidationException($"Робот с кодом {(int)robot} не найден");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (taskStatusEntity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogWarning("Статус задачи с кодом {TaskCode} не найден в БД", task);
|
|
|
|
|
|
throw new AppValidationException($"Статус задачи с кодом {(int)task} не найден");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Маппинг и сборка результирующего DTO
|
2026-07-28 14:41:01 +10:00
|
|
|
|
var result = new RobotTaskDetailsResult
|
|
|
|
|
|
{
|
2026-07-29 09:50:21 +10:00
|
|
|
|
Robot = _mapper.Map<RobotResult>(robotEntity),
|
|
|
|
|
|
Task = _mapper.Map<RobotTaskStatusResult>(taskStatusEntity),
|
|
|
|
|
|
Details = groupedDetails
|
|
|
|
|
|
.Select(t => new RobotTaskGroupDetailsResult
|
|
|
|
|
|
{
|
|
|
|
|
|
JobGroup = _mapper.Map<JobGroupShortResult>(t.JobGroup),
|
|
|
|
|
|
TemplatesCount = t.TemplatesCount
|
|
|
|
|
|
})
|
|
|
|
|
|
.OrderBy(d => d.JobGroup.GroupName)
|
|
|
|
|
|
.ToList()
|
2026-07-28 14:41:01 +10:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|