2023-10-06 13:58:10 +10:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using FluentValidation;
|
2023-12-04 10:16:00 +10:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-10-06 13:58:10 +10:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using PARR.API.Contracts.V1;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Requests;
|
2023-10-13 15:28:17 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Requests.Queries;
|
2023-10-06 13:58:10 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Responses;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Responses.Base;
|
|
|
|
|
|
using PARR.API.Controllers.V1.Base;
|
2023-10-13 15:28:17 +10:00
|
|
|
|
using PARR.API.Extensions;
|
2023-10-06 13:58:10 +10:00
|
|
|
|
using PARR.API.Services.Interfaces;
|
2023-12-04 10:16:00 +10:00
|
|
|
|
using PARR.Constants;
|
2023-10-13 15:28:17 +10:00
|
|
|
|
using PARR.DAL.DomainModels;
|
2023-10-06 13:58:10 +10:00
|
|
|
|
using PARR.DAL.Models;
|
|
|
|
|
|
using PARR.DAL.Services.Interfaces;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PARR.API.Controllers.V1
|
|
|
|
|
|
{
|
2023-12-04 10:16:00 +10:00
|
|
|
|
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
|
2023-10-06 13:58:10 +10:00
|
|
|
|
public class RobotHistoryController : BaseApiController
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IValidator<RobotHistoryRequest> validator;
|
2023-10-06 14:24:58 +10:00
|
|
|
|
|
2023-10-06 13:58:10 +10:00
|
|
|
|
private readonly IMapper mapper;
|
|
|
|
|
|
private readonly IUriService uriService;
|
|
|
|
|
|
private readonly IRobotHistoryService robotHistoryService;
|
|
|
|
|
|
private readonly IRobotConfigurationService robotConfigurationService;
|
2024-07-22 14:48:44 +10:00
|
|
|
|
private readonly IClientService clientService;
|
2024-07-22 15:54:07 +10:00
|
|
|
|
private readonly IUserService userService;
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
|
|
|
|
|
public RobotHistoryController(
|
|
|
|
|
|
IValidator<RobotHistoryRequest> validator,
|
|
|
|
|
|
IMapper mapper,
|
|
|
|
|
|
IUriService uriService,
|
|
|
|
|
|
IRobotHistoryService robotHistoryService,
|
2024-07-22 14:48:44 +10:00
|
|
|
|
IRobotConfigurationService robotConfigurationService,
|
2024-07-22 15:54:07 +10:00
|
|
|
|
IClientService clientService,
|
|
|
|
|
|
IUserService userService
|
2023-10-06 13:58:10 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.validator = validator;
|
|
|
|
|
|
this.mapper = mapper;
|
|
|
|
|
|
this.uriService = uriService;
|
|
|
|
|
|
this.robotHistoryService = robotHistoryService;
|
|
|
|
|
|
this.robotConfigurationService = robotConfigurationService;
|
2024-07-22 14:48:44 +10:00
|
|
|
|
this.clientService = clientService;
|
2024-07-22 15:54:07 +10:00
|
|
|
|
this.userService = userService;
|
2023-10-06 13:58:10 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-13 15:28:17 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Просмотр истории работы робота
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="paginationQuery"></param>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet(ApiRoutes.RobotHistory.GetAll)]
|
|
|
|
|
|
public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] RobotHistoryQuery request)
|
|
|
|
|
|
{
|
|
|
|
|
|
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
2023-10-13 15:28:17 +10:00
|
|
|
|
IQueryable<RobotHistory> query = robotHistoryService.Get()
|
|
|
|
|
|
.Include(t => t.RobotConfiguration)
|
2023-12-04 10:16:00 +10:00
|
|
|
|
.ThenInclude(t => t!.Template)
|
2024-01-26 12:27:59 +10:00
|
|
|
|
.Include(t => t.RobotConfiguration)
|
|
|
|
|
|
.ThenInclude(t => t!.Robot)
|
2023-10-13 15:28:17 +10:00
|
|
|
|
.Include(t => t.RobotHistoryLevel)
|
|
|
|
|
|
.Include(t => t.StatusTask)
|
|
|
|
|
|
.OrderByDescending(t => t.DateCreated);
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
2023-10-13 15:28:17 +10:00
|
|
|
|
if (request.TemplateId.HasValue)
|
|
|
|
|
|
query = query.Where(t => t.RobotConfiguration!.TemplateId == request.TemplateId.Value);
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
2023-10-13 15:28:17 +10:00
|
|
|
|
if (request.RobotCode.HasValue)
|
|
|
|
|
|
query = query.Where(t => t.RobotConfiguration!.RobotCode == (int)request.RobotCode);
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
2023-10-13 15:28:17 +10:00
|
|
|
|
if (request.HistoryLevel.HasValue)
|
|
|
|
|
|
query = query.Where(t => t.HistoryLevel == (int)request.HistoryLevel);
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
2023-10-13 15:28:17 +10:00
|
|
|
|
var history = await robotHistoryService.GetPage(query, paginationFilter).ToListAsync();
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
2023-10-13 15:28:17 +10:00
|
|
|
|
if (!history.Any())
|
|
|
|
|
|
return NoContent();
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
2024-07-22 15:54:07 +10:00
|
|
|
|
var robotsIp = history.Where(t => !string.IsNullOrEmpty(t.RobotIp)).Select(t => t.RobotIp).Distinct().ToList();
|
|
|
|
|
|
var users = await userService.Get().Where(t => robotsIp.Any(x => x == t.Ip)).Distinct().ToListAsync();
|
|
|
|
|
|
|
2023-10-13 15:28:17 +10:00
|
|
|
|
var response = mapper.Map<List<RobotHistoryResponse>>(history);
|
2024-07-22 15:54:07 +10:00
|
|
|
|
response.ForEach(item =>
|
|
|
|
|
|
{
|
|
|
|
|
|
item.User = mapper.Map<UserBaseResponse>(users.FirstOrDefault(t => t.Ip == item.RobotIp));
|
|
|
|
|
|
});
|
2023-10-13 15:28:17 +10:00
|
|
|
|
var paginationResponse = new PagedResponse<RobotHistoryResponse>(response, true).GetPaginatedProps(paginationFilter, query);
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
2023-10-13 15:28:17 +10:00
|
|
|
|
return Ok(paginationResponse);
|
|
|
|
|
|
}
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Добавить историю работы робота
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPost(ApiRoutes.RobotHistory.Create)]
|
|
|
|
|
|
public async Task<IActionResult> Create([FromBody] RobotHistoryRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
var resultValidate = await validator.ValidateAsync(request);
|
|
|
|
|
|
if (!resultValidate.IsValid)
|
|
|
|
|
|
return BadRequest(new Response(resultValidate.Errors));
|
|
|
|
|
|
|
|
|
|
|
|
var config = await robotConfigurationService.GetAsync(request.TaskId);
|
|
|
|
|
|
if (config == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найдено задание с id: {request.TaskId}" } }));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var history = new RobotHistory
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
|
HistoryLevel = (int)request.HistoryLevel,
|
|
|
|
|
|
RobotMessage = request.RobotMessage,
|
|
|
|
|
|
EsppMessage = request.EsppMessage,
|
|
|
|
|
|
TaskStatusCode = config.TaskStatusCode,
|
2024-07-22 14:48:44 +10:00
|
|
|
|
RobotConfigurationId = request.TaskId,
|
|
|
|
|
|
RobotIp = clientService.GetClientIp()?.ToString()
|
2023-10-06 13:58:10 +10:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (!await robotHistoryService.CreateAsync(history) || !await robotHistoryService.CommitAsync())
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при добавлении записи в историю" } }));
|
|
|
|
|
|
|
|
|
|
|
|
var createdObj = await robotHistoryService.Get()
|
|
|
|
|
|
.Include(t => t.RobotConfiguration)
|
2023-10-13 15:28:17 +10:00
|
|
|
|
.ThenInclude(t => t!.Template)
|
2024-01-26 12:27:59 +10:00
|
|
|
|
.Include(t => t.RobotConfiguration)
|
|
|
|
|
|
.ThenInclude(t => t!.Robot)
|
2023-10-06 13:58:10 +10:00
|
|
|
|
.Include(t => t.RobotHistoryLevel)
|
2023-10-13 15:28:17 +10:00
|
|
|
|
.Include(t => t.StatusTask)
|
2023-10-06 13:58:10 +10:00
|
|
|
|
.FirstOrDefaultAsync(t => t.Id == history.Id);
|
|
|
|
|
|
|
|
|
|
|
|
var response = mapper.Map<RobotHistoryResponse>(createdObj);
|
2024-07-22 15:54:07 +10:00
|
|
|
|
response.User = mapper.Map<UserBaseResponse>(userService.Get().FirstOrDefaultAsync(t => t.Ip == response.RobotIp));
|
2023-10-06 13:58:10 +10:00
|
|
|
|
|
|
|
|
|
|
//todo: createdUri
|
|
|
|
|
|
//var createdUri = uriService.GetAllUri(ApiRoutes.RobotTemplateHistory.GetAll) + $"?{nameof(RobotTemplateHistoryQuery.IdSeries)}={createdObj!.IdSeries}";
|
|
|
|
|
|
var createdUri = "";
|
|
|
|
|
|
|
|
|
|
|
|
return Created(createdUri, new Response<RobotHistoryResponse>(response, true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|