feat(api): метод сброса ошибок синхронизации шаблонов

This commit is contained in:
Mikhail Trubnikov
2025-12-09 12:20:27 +10:00
parent cd886ee2fd
commit 362da02233
7 changed files with 111 additions and 93 deletions

View File

@@ -0,0 +1,87 @@
using AutoMapper;
using InfluxDB.Client.Api.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.Constants;
using PARR.DAL.Contracts;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1
{
/// <summary>
/// Управление состоянием синхронизации шаблонов
/// </summary>
[Authorize(Roles = ParrRoles.Administrator.Role)]
public class TemplateSyncStateController : BaseApiController
{
private readonly IMapper mapper;
private readonly ITemplateService templateService;
private readonly IRobotConfigurationService robotConfigurationService;
private readonly ILogger<TemplateController> logger;
public TemplateSyncStateController(
IMapper mapper,
ITemplateService templateService,
IRobotConfigurationService robotConfigurationService,
ILogger<TemplateController> logger
)
{
this.mapper = mapper;
this.templateService = templateService;
this.robotConfigurationService = robotConfigurationService;
this.logger = logger;
}
/// <summary>
/// Сбросить ошибки синхронизации шаблона по ИД
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost(ApiRoutes.TemplateSyncState.ResetSyncErrors)]
public async Task<IActionResult> ResetSyncErrors([FromRoute] Guid id)
{
var template = await templateService.Get()
.Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
.Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
.Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
.FirstOrDefaultAsync(t => t.Id == id);
if (template == null)
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблона с id: {id}" } }));
var isChanged = false;
// смотрим RobotStatusCode, если есть завершенный с ошибкой, то сбросим ошибку и кол-во попыток
foreach (var config in template.RobotConfigurations)
{
if (config.RobotStatusCode == (int)RobotStatusEnum.Error)
{
robotConfigurationService.ChangeRobotStatus(RobotStatusEnum.Wait, config);
isChanged = true;
logger.LogDebug($"Для шаблона {id} изменен статус ОШИБКА для робота robotCode: {config.RobotCode}, новый статус: {RobotStatusEnum.Wait.ToString()}");
}
}
if (!isChanged)
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Для данного шаблона нет ошибочных статусов." } }));
if (!await templateService.CommitAsync())
{
logger.LogError($"Ошибка при сбросе статуса ошибки синхрнизации для шаблона {id}");
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при сбросе ошибок синхронизации." } }));
}
var response = mapper.Map<List<RobotConfigurationResponse>>(template.RobotConfigurations);
return Ok(new Response<List<RobotConfigurationResponse>>(response, true));
}
}
}