174 lines
7.7 KiB
C#
174 lines
7.7 KiB
C#
using AutoMapper;
|
||
using FluentValidation;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using PARR.API.Contracts.V1;
|
||
using PARR.API.Contracts.V1.Requests;
|
||
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.Extensions;
|
||
using PARR.API.Services.Interfaces;
|
||
using PARR.Core.Repositories.Interfaces;
|
||
using PARR.Domain.Common.Pagination;
|
||
using PARR.Domain.Common.Roles;
|
||
using PARR.Domain.Entities;
|
||
|
||
namespace PARR.API.Controllers.V1
|
||
{
|
||
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
|
||
public class RobotHistoryController : BaseApiController
|
||
{
|
||
private readonly IValidator<RobotHistoryRequest> validator;
|
||
|
||
private readonly IMapper mapper;
|
||
private readonly IUriService uriService;
|
||
private readonly IRobotHistoryRepository robotHistoryService;
|
||
private readonly IRobotConfigurationRepository robotConfigurationService;
|
||
private readonly IClientService clientService;
|
||
private readonly IUserRepository userService;
|
||
|
||
public RobotHistoryController(
|
||
IValidator<RobotHistoryRequest> validator,
|
||
IMapper mapper,
|
||
IUriService uriService,
|
||
IRobotHistoryRepository robotHistoryService,
|
||
IRobotConfigurationRepository robotConfigurationService,
|
||
IClientService clientService,
|
||
IUserRepository userService
|
||
)
|
||
{
|
||
this.validator = validator;
|
||
this.mapper = mapper;
|
||
this.uriService = uriService;
|
||
this.robotHistoryService = robotHistoryService;
|
||
this.robotConfigurationService = robotConfigurationService;
|
||
this.clientService = clientService;
|
||
this.userService = userService;
|
||
}
|
||
|
||
|
||
/// <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);
|
||
|
||
IQueryable<RobotHistory> query = robotHistoryService.Get()
|
||
.AsNoTracking()
|
||
.Include(t => t.RobotConfiguration)
|
||
.ThenInclude(t => t!.Template)
|
||
.Include(t => t.RobotConfiguration)
|
||
.ThenInclude(t => t!.Robot)
|
||
.Include(t => t.RobotHistoryLevel)
|
||
.Include(t => t.StatusTask)
|
||
.OrderByDescending(t => t.DateCreated);
|
||
|
||
if (request.TemplateId.HasValue)
|
||
query = query.Where(t => t.RobotConfiguration!.TemplateId == request.TemplateId.Value);
|
||
|
||
if (request.RobotCode.HasValue)
|
||
query = query.Where(t => t.RobotConfiguration!.RobotCode == (int)request.RobotCode);
|
||
|
||
if (request.HistoryLevel.HasValue)
|
||
query = query.Where(t => t.HistoryLevel == (int)request.HistoryLevel);
|
||
|
||
if (request.DateFrom.HasValue)
|
||
{
|
||
var startDateUtc = request.DateFrom.Value.ToUniversalTime();
|
||
query = query.Where(t => t.DateCreated >= startDateUtc);
|
||
}
|
||
|
||
if (request.DateTo.HasValue)
|
||
{
|
||
var endDateUtc = request.DateTo.Value.ToUniversalTime();
|
||
query = query.Where(t => t.DateCreated < endDateUtc);
|
||
}
|
||
|
||
var history = await robotHistoryService.GetPage(query, paginationFilter).ToListAsync();
|
||
|
||
if (history.Count == 0)
|
||
return NoContent();
|
||
|
||
var robotsIp = history.Where(t => !string.IsNullOrEmpty(t.RobotIp)).Select(t => t.RobotIp).Distinct().ToList();
|
||
var usersDictionary = await userService.Get()
|
||
.AsNoTracking()
|
||
.Where(t => robotsIp.Contains(t.Ip))
|
||
.ToDictionaryAsync(t => t.Ip, t => t);
|
||
|
||
var response = mapper.Map<List<RobotHistoryResponse>>(history);
|
||
foreach (var item in response)
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(item.RobotIp) && usersDictionary.TryGetValue(item.RobotIp, out var user))
|
||
{
|
||
item.User = mapper.Map<UserBaseResponse>(user);
|
||
}
|
||
}
|
||
|
||
var paginationResponse = new PagedResponse<RobotHistoryResponse>(response, true).GetPaginatedProps(paginationFilter, query);
|
||
|
||
return Ok(paginationResponse);
|
||
}
|
||
|
||
|
||
/// <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,
|
||
RobotConfigurationId = request.TaskId,
|
||
RobotIp = clientService.GetClientIp()?.ToString(),
|
||
RobotId = request.RobotId
|
||
};
|
||
|
||
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)
|
||
.ThenInclude(t => t!.Template)
|
||
.Include(t => t.RobotConfiguration)
|
||
.ThenInclude(t => t!.Robot)
|
||
.Include(t => t.RobotHistoryLevel)
|
||
.Include(t => t.StatusTask)
|
||
.FirstOrDefaultAsync(t => t.Id == history.Id);
|
||
|
||
var response = mapper.Map<RobotHistoryResponse>(createdObj);
|
||
response.User = mapper.Map<UserBaseResponse>(await userService.Get().FirstOrDefaultAsync(t => t.Ip == response.RobotIp));
|
||
|
||
//todo: createdUri
|
||
//var createdUri = uriService.GetAllUri(ApiRoutes.RobotTemplateHistory.GetAll) + $"?{nameof(RobotTemplateHistoryQuery.IdSeries)}={createdObj!.IdSeries}";
|
||
var createdUri = "";
|
||
|
||
return Created(createdUri, new Response<RobotHistoryResponse>(response, true));
|
||
}
|
||
|
||
}
|
||
}
|