2023-10-06 11:38:33 +10:00
|
|
|
|
using AutoMapper;
|
2023-12-04 10:16:00 +10:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-10-06 11:38:33 +10:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using PARR.API.Contracts.V1;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Requests;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Responses;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Responses.Base;
|
|
|
|
|
|
using PARR.API.Controllers.V1.Base;
|
2024-07-22 14:48:44 +10:00
|
|
|
|
using PARR.API.Services.Interfaces;
|
2026-04-30 10:35:31 +10:00
|
|
|
|
using PARR.Core.Repositories.Interfaces;
|
2026-04-14 12:01:49 +10:00
|
|
|
|
using PARR.Domain.Common.Roles;
|
2026-04-30 10:35:31 +10:00
|
|
|
|
using PARR.Domain.Entities;
|
2026-07-21 11:48:21 +10:00
|
|
|
|
using PARR.Domain.Entities.RobotEntities;
|
2026-04-13 16:58:30 +10:00
|
|
|
|
using PARR.Domain.Enums;
|
2023-10-06 11:38:33 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.API.Controllers.V1
|
|
|
|
|
|
{
|
2023-12-04 10:16:00 +10:00
|
|
|
|
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
|
2023-10-06 11:38:33 +10:00
|
|
|
|
public class RobotTaskRobotStatusController : BaseApiController
|
|
|
|
|
|
{
|
2026-04-30 10:35:31 +10:00
|
|
|
|
private readonly IRobotConfigurationRepository robotConfigurationService;
|
|
|
|
|
|
private readonly IRobotHistoryRepository robotHistoryService;
|
2023-10-06 11:38:33 +10:00
|
|
|
|
private readonly IMapper mapper;
|
2024-07-22 14:48:44 +10:00
|
|
|
|
private readonly IClientService clientService;
|
2023-10-06 11:38:33 +10:00
|
|
|
|
|
|
|
|
|
|
public RobotTaskRobotStatusController(
|
2026-04-30 10:35:31 +10:00
|
|
|
|
IRobotConfigurationRepository robotConfigurationService,
|
|
|
|
|
|
IRobotHistoryRepository robotHistoryService,
|
2024-07-22 14:48:44 +10:00
|
|
|
|
IMapper mapper,
|
|
|
|
|
|
IClientService clientService
|
2023-10-06 11:38:33 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.robotConfigurationService = robotConfigurationService;
|
|
|
|
|
|
this.robotHistoryService = robotHistoryService;
|
|
|
|
|
|
this.mapper = mapper;
|
2024-07-22 14:48:44 +10:00
|
|
|
|
this.clientService = clientService;
|
2023-10-06 11:38:33 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Изменить статус выполнения задания роботом по ИД задания
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="taskId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPut(ApiRoutes.RobotTaskRobotStatus.ChangeRobotStatus)]
|
|
|
|
|
|
public async Task<IActionResult> ChangeStatus([FromRoute] Guid taskId, [FromBody] RobotTaskChangeRobotStatusRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
var config = await robotConfigurationService.Get()
|
|
|
|
|
|
.FirstOrDefaultAsync(t => t.Id == taskId);
|
|
|
|
|
|
|
|
|
|
|
|
if (config == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найдено задание с id: {taskId}" } }));
|
|
|
|
|
|
|
|
|
|
|
|
//изменение статуса робота
|
2025-12-09 12:20:27 +10:00
|
|
|
|
robotConfigurationService.ChangeRobotStatus(request.RobotStatusCode, config);
|
2023-10-06 11:38:33 +10:00
|
|
|
|
|
|
|
|
|
|
//если успех, изменяем статус задания на успех
|
|
|
|
|
|
if (request.RobotStatusCode == RobotStatusEnum.Complete)
|
2025-11-26 14:34:03 +10:00
|
|
|
|
robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Ok, config);
|
2023-10-06 11:38:33 +10:00
|
|
|
|
|
|
|
|
|
|
if (!await robotConfigurationService.CommitAsync())
|
|
|
|
|
|
return BadRequest("Ошибка при изменении статуса работы робота.");
|
|
|
|
|
|
|
|
|
|
|
|
//записываем в лог робота
|
|
|
|
|
|
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,
|
2024-07-22 14:48:44 +10:00
|
|
|
|
RobotConfigurationId = config.Id,
|
2026-05-26 12:24:44 +10:00
|
|
|
|
RobotIp = clientService.GetClientIp()?.ToString(),
|
|
|
|
|
|
RobotId = request.RobotId
|
2023-10-06 11:38:33 +10:00
|
|
|
|
};
|
|
|
|
|
|
await robotHistoryService.CreateAsync(history);
|
|
|
|
|
|
await robotHistoryService.CommitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-01 14:21:28 +10:00
|
|
|
|
var configToResponse = await robotConfigurationService.Get()
|
2023-10-06 11:38:33 +10:00
|
|
|
|
.Include(t => t.Robot)
|
2023-10-06 15:13:40 +10:00
|
|
|
|
.Include(t => t.TaskStatus)
|
2023-10-06 11:38:33 +10:00
|
|
|
|
.Include(t => t.RobotStatus)
|
|
|
|
|
|
.FirstOrDefaultAsync(t => t.Id == taskId);
|
|
|
|
|
|
|
2024-08-01 14:21:28 +10:00
|
|
|
|
var response = mapper.Map<RobotConfigurationResponse>(configToResponse);
|
2023-10-06 11:38:33 +10:00
|
|
|
|
|
|
|
|
|
|
return Ok(new Response<RobotConfigurationResponse>(response, true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|