feat(api,core,domain): Изменен контроллер RobotTaskRobotStatusController, метод изменения статуса задания вынесен в сервисы. Добавлена логика успрешного переименования шаблона.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using AutoMapper;
|
||||
using InfluxDB.Client.Api.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.BLL.Helpers;
|
||||
@@ -9,7 +8,6 @@ using PARR.Core.Services.NextRunServices;
|
||||
using PARR.Core.Services.RobotTask.Interfaces;
|
||||
using PARR.Core.Services.RobotTask.Models;
|
||||
using PARR.Core.Services.Shortcodes;
|
||||
using PARR.Domain.Common.Template;
|
||||
using PARR.Domain.DTOs.RobotTask;
|
||||
using PARR.Domain.Entities;
|
||||
using PARR.Domain.Entities.Base.History;
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using PARR.Domain.DTOs.RobotTaskRobotStatus;
|
||||
|
||||
namespace PARR.Core.Services.RobotTaskRobotStatus.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Сервис по изменению статуса выполнения задания роботами
|
||||
/// </summary>
|
||||
public interface IRobotTaskRobotStatusService
|
||||
{
|
||||
/// <summary>
|
||||
/// Изменить статус выполнения задания роботом по ИД задания
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
Task<RobotConfigurationResult> ChangeStatusAsync(ChangeRobotStatus request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user