88 lines
3.9 KiB
C#
88 lines
3.9 KiB
C#
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.Core.Repositories.Interfaces;
|
||
using PARR.DAL.Contracts;
|
||
using PARR.Domain.Common.Roles;
|
||
using PARR.Domain.Enums;
|
||
|
||
namespace PARR.API.Controllers.V1
|
||
{
|
||
/// <summary>
|
||
/// Управление состоянием синхронизации шаблонов
|
||
/// </summary>
|
||
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
||
public class TemplateSyncStateController : BaseApiController
|
||
{
|
||
private readonly IMapper mapper;
|
||
private readonly ITemplateRepository templateService;
|
||
private readonly IRobotConfigurationRepository robotConfigurationService;
|
||
private readonly ILogger<TemplateController> logger;
|
||
|
||
public TemplateSyncStateController(
|
||
IMapper mapper,
|
||
ITemplateRepository templateService,
|
||
IRobotConfigurationRepository 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));
|
||
}
|
||
|
||
}
|
||
}
|