using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests.Queries;
using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.API.Services.Interfaces;
using PARR.API.Settings;
using PARR.Core.Services.RobotTask.Interfaces;
using PARR.Domain.Common.Roles;
using PARR.Domain.Entities.Base.History;
using PARR.Domain.Enums;
using PARR.Domain.Exceptions;
namespace PARR.API.Controllers.V1
{
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class RobotTaskController : BaseApiController
{
private readonly IRobotTaskService _robotTaskService;
private readonly CommonSettings _commonSettings;
private readonly IMapper _mapper;
private readonly IClientService _clientService;
public RobotTaskController(
IMapper mapper,
IClientService clientService,
IRobotTaskService robotTaskService,
CommonSettings commonSettings
)
{
_robotTaskService = robotTaskService;
_commonSettings = commonSettings;
_mapper = mapper;
_clientService = clientService;
}
///
/// Получить задание для робота по коду робота и по статусу задания
///
///
///
///
///
[HttpGet(ApiRoutes.RobotTask.GetByRobotAndStatusTask)]
public async Task GetByRobotAndStatusTask([FromRoute] RobotsEnum robotCode, [FromRoute] TaskStatusEnum taskStatusCode, [FromQuery] RobotTaskQuery requestQuery)
{
switch (robotCode)
{
case RobotsEnum.TemplateOrder:
var templateTask = await _robotTaskService.GetTemplateTaskAsync(
taskStatusCode,
requestQuery.SetInProgressStatus ?? false,
_clientService.GetClientIp()?.ToString(),
requestQuery.RobotId
);
var templateResponse = _mapper.Map(templateTask);
return Ok(new Response(templateResponse, true));
case RobotsEnum.ScheduleOrder:
var historyIniciator = new HistoryInitiator
{
InitiatorComment = "Задание роботу, расписание.",
InitiatorIp = _clientService.GetClientIp()?.ToString(),
InitiatorParrComponentId = ParrComponentsEnum.Api
};
var scheduleTask = await _robotTaskService.GetScheduleTaskAsync(
taskStatusCode,
requestQuery.SetInProgressStatus ?? false,
historyIniciator.InitiatorIp,
requestQuery.RobotId,
historyIniciator,
_commonSettings.ScheduleCooldownDuration
);
var scheduleResponse = _mapper.Map(scheduleTask);
return Ok(new Response(scheduleResponse, true));
default:
throw new AppValidationException("Некорректное значение robotCode");
}
}
}
}