89 lines
3.9 KiB
C#
89 lines
3.9 KiB
C#
using AutoMapper;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.Logging;
|
||
using PARR.Core.Repositories.Interfaces;
|
||
using PARR.Core.Services.RobotStatusDetails.Interfaces;
|
||
using PARR.Domain.DTOs.RobotStatusDetails;
|
||
using PARR.Domain.DTOs.Shared;
|
||
using PARR.Domain.Enums;
|
||
using PARR.Domain.Exceptions;
|
||
|
||
namespace PARR.Core.Services.RobotStatusDetails.Implementations
|
||
{
|
||
internal class RobotStatusDetailsService : IRobotStatusDetailsService
|
||
{
|
||
private readonly ILogger<RobotStatusDetailsService> _logger;
|
||
private readonly IRobotConfigurationRepository _robotConfigurationRepository;
|
||
private readonly IMapper _mapper;
|
||
private readonly IRobotRepository _robotRepository;
|
||
private readonly IRobotStatusRepository _robotStatusRepository;
|
||
|
||
public RobotStatusDetailsService(
|
||
ILogger<RobotStatusDetailsService> logger,
|
||
IRobotConfigurationRepository robotConfigurationRepository,
|
||
IMapper mapper,
|
||
IRobotRepository robotRepository,
|
||
IRobotStatusRepository robotStatusRepository
|
||
)
|
||
{
|
||
_logger = logger;
|
||
_robotConfigurationRepository = robotConfigurationRepository;
|
||
_mapper = mapper;
|
||
_robotRepository = robotRepository;
|
||
_robotStatusRepository = robotStatusRepository;
|
||
}
|
||
|
||
|
||
public async Task<RobotStatusDetailsResult> GetDetailsAsync(RobotsEnum robot, RobotStatusEnum status, CancellationToken cancellationToken = default)
|
||
{
|
||
var groupedDetails = await _robotConfigurationRepository.Get()
|
||
.AsNoTracking()
|
||
.Where(config => config.RobotCode == (int)robot && config.RobotStatusCode == (int)status)
|
||
.GroupBy(config => config.Template!.Job!.Group)
|
||
.Select(t => new
|
||
{
|
||
JobGroup = t.Key,
|
||
TemplatesCount = t.Count()
|
||
})
|
||
.ToListAsync(cancellationToken);
|
||
|
||
var robotEntity = await _robotRepository.Get()
|
||
.AsNoTracking()
|
||
.FirstOrDefaultAsync(r => r.Code == (int)robot, cancellationToken);
|
||
|
||
var robotStatusEntity = await _robotStatusRepository.Get()
|
||
.AsNoTracking()
|
||
.FirstOrDefaultAsync(t => t.Code == (int)status, cancellationToken);
|
||
|
||
if (robotEntity == null)
|
||
{
|
||
_logger.LogWarning("Робот с кодом {RobotCode} не найден в БД", robot);
|
||
throw new AppValidationException($"Робот с кодом {(int)robot} не найден");
|
||
}
|
||
|
||
if (robotStatusEntity == null)
|
||
{
|
||
_logger.LogWarning("Статус робота с кодом {StatusCode} не найден в БД", status);
|
||
throw new AppValidationException($"Статус робота с кодом {(int)status} не найден");
|
||
}
|
||
|
||
var result = new RobotStatusDetailsResult
|
||
{
|
||
Robot = _mapper.Map<RobotResult>(robotEntity),
|
||
Status = _mapper.Map<RobotStatusResult>(robotStatusEntity),
|
||
Details = groupedDetails
|
||
.Select(t => new RobotStatusGroupDetailsResult
|
||
{
|
||
JobGroup = _mapper.Map<JobGroupShortResult>(t.JobGroup),
|
||
TemplatesCount = t.TemplatesCount
|
||
}).OrderBy(t => t.JobGroup.GroupName)
|
||
.ToList()
|
||
};
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
}
|
||
}
|