99 lines
4.7 KiB
C#
99 lines
4.7 KiB
C#
|
|
using AutoMapper;
|
|||
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using Microsoft.Extensions.Logging;
|
|||
|
|
using PARR.Core.Repositories.Interfaces;
|
|||
|
|
using PARR.Core.Repositories.Interfaces.TemplateRepositories;
|
|||
|
|
using PARR.Core.Services.RobotTaskRobotStatus.Interfaces;
|
|||
|
|
using PARR.Domain.DTOs.RobotTaskRobotStatus;
|
|||
|
|
using PARR.Domain.Entities.RobotEntities;
|
|||
|
|
using PARR.Domain.Enums;
|
|||
|
|
using PARR.Domain.Exceptions;
|
|||
|
|
|
|||
|
|
namespace PARR.Core.Services.RobotTaskRobotStatus.Implemetations
|
|||
|
|
{
|
|||
|
|
internal class RobotTaskRobotStatusService : IRobotTaskRobotStatusService
|
|||
|
|
{
|
|||
|
|
private readonly ILogger<RobotTaskRobotStatusService> _logger;
|
|||
|
|
private readonly IRobotConfigurationRepository _robotConfigurationRepository;
|
|||
|
|
private readonly IRobotHistoryRepository _robotHistoryRepository;
|
|||
|
|
private readonly ITemplateRenamePendingRepository _templateRenamePendingRepository;
|
|||
|
|
private readonly IMapper _mapper;
|
|||
|
|
|
|||
|
|
public RobotTaskRobotStatusService(
|
|||
|
|
ILogger<RobotTaskRobotStatusService> logger,
|
|||
|
|
IRobotConfigurationRepository robotConfigurationRepository,
|
|||
|
|
IRobotHistoryRepository robotHistoryRepository,
|
|||
|
|
ITemplateRenamePendingRepository templateRenamePendingRepository,
|
|||
|
|
IMapper mapper
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
_logger = logger;
|
|||
|
|
_robotConfigurationRepository = robotConfigurationRepository;
|
|||
|
|
_robotHistoryRepository = robotHistoryRepository;
|
|||
|
|
_templateRenamePendingRepository = templateRenamePendingRepository;
|
|||
|
|
_mapper = mapper;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public async Task<RobotConfigurationResult> ChangeStatusAsync(ChangeRobotStatus request)
|
|||
|
|
{
|
|||
|
|
var config = await _robotConfigurationRepository.Get()
|
|||
|
|
.FirstOrDefaultAsync(t => t.Id == request.TaskId);
|
|||
|
|
|
|||
|
|
if (config == null)
|
|||
|
|
throw new NotFoundException($"Не найдено задание с id: {request.TaskId}");
|
|||
|
|
|
|||
|
|
// изменение статуса робота
|
|||
|
|
_robotConfigurationRepository.ChangeRobotStatus(request.RobotStatusCode, config);
|
|||
|
|
|
|||
|
|
// если успех, изменяем статус задания на успех
|
|||
|
|
if (request.RobotStatusCode == RobotStatusEnum.Complete)
|
|||
|
|
{
|
|||
|
|
_robotConfigurationRepository.ChangeTaskStatus(TaskStatusEnum.Ok, config);
|
|||
|
|
// Тут нужно посмотреть, если этот шаблон был на переименование, удалить у него старое название, так как он успешно переименовался
|
|||
|
|
if (config.RobotCode == (int)RobotsEnum.TemplateOrder)
|
|||
|
|
{
|
|||
|
|
var renaming = await _templateRenamePendingRepository.Get().FirstOrDefaultAsync(t => t.TemplateId == config.TemplateId);
|
|||
|
|
if (renaming != null)
|
|||
|
|
{
|
|||
|
|
// Удаляем
|
|||
|
|
_templateRenamePendingRepository.Remove(renaming);
|
|||
|
|
_logger.LogInformation("Шаблон {TemplateId} успешно переименован. Запись TemplateRenamePending удалена.", config.TemplateId);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!await _robotConfigurationRepository.CommitAsync())
|
|||
|
|
throw new DbErrorException("Ошибка при сохранении в БД");
|
|||
|
|
|
|||
|
|
|
|||
|
|
//записываем в лог робота
|
|||
|
|
if (request.RobotStatusCode == RobotStatusEnum.InProgress || request.RobotStatusCode == RobotStatusEnum.Complete)
|
|||
|
|
{
|
|||
|
|
var historyLevel = request.RobotStatusCode == RobotStatusEnum.InProgress ? RobotHistoryLevelEnum.Start : RobotHistoryLevelEnum.Complete;
|
|||
|
|
|
|||
|
|
var history = new RobotHistory
|
|||
|
|
{
|
|||
|
|
Id = Guid.NewGuid(),
|
|||
|
|
HistoryLevel = (int)historyLevel,
|
|||
|
|
TaskStatusCode = config.TaskStatusCode,
|
|||
|
|
RobotConfigurationId = config.Id,
|
|||
|
|
RobotIp = request.RobotIp,
|
|||
|
|
RobotId = request.RobotId
|
|||
|
|
};
|
|||
|
|
await _robotHistoryRepository.CreateAsync(history);
|
|||
|
|
await _robotHistoryRepository.CommitAsync();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var configToResponse = await _robotConfigurationRepository.Get()
|
|||
|
|
.AsNoTracking()
|
|||
|
|
.Include(t => t.Robot)
|
|||
|
|
.Include(t => t.TaskStatus)
|
|||
|
|
.Include(t => t.RobotStatus)
|
|||
|
|
.FirstOrDefaultAsync(t => t.Id == request.TaskId);
|
|||
|
|
|
|||
|
|
return _mapper.Map<RobotConfigurationResult>(configToResponse);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|