feat(api): Контроллер RobotTaskRobotStatusController управления статусами работы робота

This commit is contained in:
Mikhail Trubnikov
2023-10-06 11:38:33 +10:00
parent 1934f7c645
commit d334e46780
21 changed files with 4313 additions and 20 deletions

View File

@@ -99,12 +99,19 @@
public static class RobotTask
{
public const string GetByRobotAndStatusTask = Base + "/robot-tasks/" + robotCode + "/statuses/" + taskStatusCode;
public const string GetByRobotAndStatusTask = Base + "/robot-tasks/robots/" + robotCode + "/statuses/" + taskStatusCode;
public const string robotCode = "{robotCode}";
public const string taskStatusCode = "{taskStatusCode}";
}
public static class RobotTaskRobotStatus
{
public const string ChangeRobotStatus = Base + "/robot-tasks/" + taskId + "/robot-status";
public const string taskId = "{taskId}";
}
//public static class Layer
//{

View File

@@ -0,0 +1,12 @@
using PARR.DAL.Contracts;
namespace PARR.API.Contracts.V1.Requests
{
public class RobotTaskChangeRobotStatusRequest
{
/// <summary>
/// Статус робота
/// </summary>
public RobotStatusEnum RobotStatusCode { get; set; }
}
}

View File

@@ -1,5 +1,6 @@
namespace PARR.API.Contracts.V1.Requests
{
//todo: delete
public class TemplateRobotStatusRequest
{
public int Code { get; set; }

View File

@@ -2,6 +2,21 @@
{
public class RobotConfigurationResponse
{
public Guid Id { get; set; }
public DateTimeOffset DateCreated { get; set; }
public Guid TemplateId { get; set; }
public RobotResponse? Robot { get; set; }
public TaskStatusResponse? TaskStatus { get; set; }
public RobotStatusResponse? RobotStatus { get; set; }
public int AttemptsNumber { get; set; }
public DateTimeOffset? LastRobotStatusUpdated { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace PARR.API.Contracts.V1.Responses
{
public class RobotResponse
{
public int Code { get; set; }
public required string Name { get; set; }
public required string Description { get; set; }
}
}

View File

@@ -2,6 +2,12 @@
{
public class RobotTaskScheduleResponse
{
/// <summary>
/// RobotConfigurationId
/// </summary>
public Guid TaskId { get; set; }
//TODO:
}
}

View File

@@ -2,6 +2,11 @@
{
public class RobotTaskTemplateResponse
{
/// <summary>
/// RobotConfigurationId
/// </summary>
public Guid TaskId { get; set; }
public bool IsActive { get; set; }
public required string ClosingCode { get; set; }
public required string FullDescription { get; set; }
@@ -27,5 +32,7 @@
public required string WorkName { get; set; }
public required string WorkEsppId { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace PARR.API.Contracts.V1.Responses
{
public class TaskStatusResponse
{
public int Code { get; set; }
public required string Name { get; set; }
public required string Description { get; set; }
}
}

View File

@@ -82,7 +82,7 @@ namespace PARR.API.Controllers.V1
task = await query.FirstOrDefaultAsync(t =>
t.RobotStatusCode == (int)RobotStatusEnum.InProgress
&& t.AttemptsNumber < settingsFromDb.RobotAttemptsNumber
&& t.LastStatusUpdated < endDate
&& t.LastRobotStatusUpdated < endDate
);
}

View File

@@ -0,0 +1,86 @@
using AutoMapper;
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;
using PARR.DAL.Contracts;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1
{
public class RobotTaskRobotStatusController : BaseApiController
{
private readonly IRobotConfigurationService robotConfigurationService;
private readonly IRobotHistoryService robotHistoryService;
private readonly IMapper mapper;
public RobotTaskRobotStatusController(
IRobotConfigurationService robotConfigurationService,
IRobotHistoryService robotHistoryService,
IMapper mapper
)
{
this.robotConfigurationService = robotConfigurationService;
this.robotHistoryService = robotHistoryService;
this.mapper = mapper;
}
/// <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}" } }));
//изменение статуса робота
robotConfigurationService.ChangeRobotStatus(request.RobotStatusCode, ref config);
//если успех, изменяем статус задания на успех
if (request.RobotStatusCode == RobotStatusEnum.Complete)
robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Ok, ref config);
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,
RobotConfigurationId = config.Id
};
await robotHistoryService.CreateAsync(history);
await robotHistoryService.CommitAsync();
}
var configToResonse = await robotConfigurationService.Get()
.Include(t => t.Robot)
.Include(t => t.StatusTask)
.Include(t => t.RobotStatus)
.FirstOrDefaultAsync(t => t.Id == taskId);
var response = mapper.Map<RobotConfigurationResponse>(configToResonse);
return Ok(new Response<RobotConfigurationResponse>(response, true));
}
}
}

View File

@@ -114,12 +114,24 @@ namespace PARR.API.MappingProfiles
.ForMember(d => d.TnkName, o => o.MapFrom(s => s.Template!.ApplicationsInWork!.Work!.Tnk!.Name))
.ForMember(d => d.TnkEsppId, o => o.MapFrom(s => s.Template!.ApplicationsInWork!.Work!.Tnk!.EsppId))
.ForMember(d => d.WorkName, o => o.MapFrom(s => s.Template!.ApplicationsInWork!.Work!.Name))
.ForMember(d => d.WorkEsppId, o => o.MapFrom(s => s.Template!.ApplicationsInWork!.Work!.EsppId));
.ForMember(d => d.WorkEsppId, o => o.MapFrom(s => s.Template!.ApplicationsInWork!.Work!.EsppId))
.ForMember(d => d.TaskId, o => o.MapFrom(s => s.Id));
//TODO: сделать маппинг!!!
CreateMap<RobotConfiguration, RobotTaskScheduleResponse>();
CreateMap<RobotConfiguration, RobotTaskScheduleResponse>()
.ForMember(d => d.TaskId, o => o.MapFrom(s => s.Id));
// === RobotConfiguration ===
CreateMap<Robot, RobotResponse>();
CreateMap<DAL.Models.TaskStatus, TaskStatusResponse>();
CreateMap<RobotConfiguration, RobotConfigurationResponse>()
.ForMember(d => d.Robot, o => o.MapFrom(s => s.Robot))
.ForMember(d => d.TaskStatus, o => o.MapFrom(s => s.StatusTask))
.ForMember(d => d.RobotStatus, o => o.MapFrom(s => s.RobotStatus));
}
}
}