feat(api,core,domain): Изменен контроллер RobotTaskRobotStatusController, метод изменения статуса задания вынесен в сервисы. Добавлена логика успрешного переименования шаблона.

This commit is contained in:
Mikhail Trubnikov
2026-07-23 14:39:55 +10:00
parent 58b4d98b16
commit b3879062a6
17 changed files with 313 additions and 55 deletions

View File

@@ -12,6 +12,8 @@ using PARR.Core.Services.RobotMetrics;
using PARR.Core.Services.RobotSnapshotServices;
using PARR.Core.Services.RobotTask.Implementations;
using PARR.Core.Services.RobotTask.Interfaces;
using PARR.Core.Services.RobotTaskRobotStatus.Implemetations;
using PARR.Core.Services.RobotTaskRobotStatus.Interfaces;
using PARR.Core.Services.Shortcodes;
using PARR.Core.Services.Shortcodes.Handlers;
using PARR.Core.Services.Snapshots.Implementations;
@@ -111,6 +113,7 @@ namespace PARR.Core
services.AddScoped<IRobotTaskService, RobotTaskService>();
services.AddScoped<IRobotSnapshotService, RobotSnapshotService>();
services.AddScoped<IRobotTaskRobotStatusService, RobotTaskRobotStatusService>();
services.AddScoped<IUnitService, UnitService>();
services.AddScoped<UnitCacheService>();

View File

@@ -0,0 +1,17 @@
using AutoMapper;
using PARR.Domain.DTOs.RobotTaskRobotStatus;
using PARR.Domain.Entities;
namespace PARR.Core.Infrastructure.Mapping.RobotTaskRobotStatus
{
internal class RobotConfigurationResultMappingProfile : Profile
{
public RobotConfigurationResultMappingProfile()
{
CreateMap<RobotConfiguration, RobotConfigurationResult>()
.ForMember(d => d.Robot, o => o.MapFrom(s => s.Robot))
.ForMember(d => d.TaskStatus, o => o.MapFrom(s => s.TaskStatus))
.ForMember(d => d.RobotStatus, o => o.MapFrom(s => s.RobotStatus));
}
}
}

View File

@@ -0,0 +1,14 @@
using AutoMapper;
using PARR.Domain.DTOs.Shared;
using PARR.Domain.Entities.RobotEntities;
namespace PARR.Core.Infrastructure.Mapping.Shared
{
public class RobotDtoMappingProfile : Profile
{
public RobotDtoMappingProfile()
{
CreateMap<Robot, RobotDto>();
}
}
}

View File

@@ -0,0 +1,14 @@
using AutoMapper;
using PARR.Domain.DTOs.Shared;
using PARR.Domain.Entities.RobotEntities;
namespace PARR.Core.Infrastructure.Mapping.Shared
{
public class RobotStatusDtoMappingProfile : Profile
{
public RobotStatusDtoMappingProfile()
{
CreateMap<RobotStatus, RobotStatusDto>();
}
}
}

View File

@@ -0,0 +1,13 @@
using AutoMapper;
using PARR.Domain.DTOs.Shared;
namespace PARR.Core.Infrastructure.Mapping.Shared
{
public class RobotTaskStatusDtoMappingProfile : Profile
{
public RobotTaskStatusDtoMappingProfile()
{
CreateMap<PARR.Domain.Entities.RobotEntities.TaskStatus, RobotTaskStatusDto>();
}
}
}

View File

@@ -6,5 +6,6 @@ namespace PARR.Core.Repositories.Interfaces.TemplateRepositories
{
Task<bool> CreateAsync(TemplateRenamePending obj);
IQueryable<TemplateRenamePending> Get();
void Remove(TemplateRenamePending obj);
}
}

View File

@@ -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;

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}