Files
parr_api/PARR.API/Controllers/V1/TemplateSyncStateController.cs

87 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AutoMapper;
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));
}
}
}