feat(api): Новый контроллер JobUnitPreview, для получения предпросмотра затрагиваемых ЭК работой

This commit is contained in:
Mikhail Kuznetsov
2025-09-24 10:08:50 +10:00
parent 6034b137d2
commit 09f0f1e063
9 changed files with 130 additions and 62 deletions

View File

@@ -125,10 +125,10 @@ namespace PARR.API.Controllers.V1
.Include(t => t.Tnk)
.Include(t => t.Group)
.Include(t => t.UnitFilters)
.ThenInclude(t => t.FieldFilters.OrderBy(o=>o.UnitField!.AihitName))
.ThenInclude(t => t.FieldFilters)
.ThenInclude(t => t.UnitField)
.Include(t => t.UnitFilters)
.ThenInclude(t => t.RelationshipFilters.OrderBy(o => o.UnitField!.AihitName))
.ThenInclude(t => t.RelationshipFilters)
.ThenInclude(t => t.UnitField)
.FirstOrDefaultAsync(t => t.Id == id);
@@ -354,8 +354,8 @@ namespace PARR.API.Controllers.V1
orig.UnitFilters.Add(newUnitFilter);
}
/*//Сразу удаляем UnitFilter которых нет
/* Писал полноценный апдейт, но Миша сказал что нахрен это - просто все удаляем, а потом создаём заново
//Сразу удаляем UnitFilter которых нет
var toDelete = orig.UnitFilters.Where(t => !mappedRequest.UnitFilters.Any(e => e.Id == t.Id));
foreach (var item in toDelete)
orig.UnitFilters.Remove(item);
@@ -529,38 +529,6 @@ namespace PARR.API.Controllers.V1
return NoContent();
}
/// <summary>
/// Получить список ЭК для которых будут созданы шаблоны
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost(ApiRoutes.Job.Preview)]
public async Task<IActionResult> Preview([FromBody] JobRequest request)
{
#region Валидация
var jobValidateResult = await jobValidator.ValidateAsync(request);//Валидация параметров самого задания
if (!jobValidateResult.IsValid)
return BadRequest(new Response(jobValidateResult.Errors));
#endregion
var job = mapper.Map<Job>(request);
var group = await jobGroupService.GetAsync(request.GroupId);
job.Group = group;
var unitIds = await unitFilterService.GetUnitsIdByJobFilterAsync(job);
if (unitIds == null)
return NotFound();
var units = await unitService.Get().Where(t => unitIds.Any(a => a == t.Id)).Take(1000).ToListAsync();
var response = mapper.Map<List<UnitResponse>>(units);
return Ok(response);
}
/// <summary>
/// Загрузка статистики
@@ -599,6 +567,7 @@ namespace PARR.API.Controllers.V1
}
public class JobStatModel
{
public required TemplateStats TemplateStatistics { get; set; }

View File

@@ -0,0 +1,76 @@
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.Responses;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.Constants;
using PARR.DAL.DomainServices.Interfaces;
using PARR.DAL.Models.Job;
using PARR.DAL.Services.Interfaces.Unit;
namespace PARR.API.Controllers.V1
{
/// <summary>
/// Получение предварительной информации о работах
/// </summary>
[Authorize(Roles = ParrRoles.Administrator.Role)]
public class JobUnitPreviewController : BaseApiController
{
private readonly ILogger<JobUnitPreviewController> logger;
private readonly IUnitFilterService unitFilterService;
private readonly IValidator<JobRequest> jobValidator;
private readonly IUnitService unitService;
private readonly IMapper mapper;
public JobUnitPreviewController(
ILogger<JobUnitPreviewController> logger,
IUnitFilterService unitFilterService,
IValidator<JobRequest> jobValidator,
IUnitService unitService,
IMapper mapper
)
{
this.logger = logger;
this.unitFilterService = unitFilterService;
this.jobValidator = jobValidator;
this.unitService = unitService;
this.mapper = mapper;
}
/// <summary>
/// Получить список ЭК по предполагаемой работе перед сохранением
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost(ApiRoutes.JobUnitPreview.Preview)]
public async Task<IActionResult> Preview([FromBody] JobRequest request)
{
#region Валидация
var jobValidateResult = await jobValidator.ValidateAsync(request);//Валидация параметров самого задания
if (!jobValidateResult.IsValid)
return BadRequest(new Response(jobValidateResult.Errors));
#endregion
var job = mapper.Map<Job>(request);
var unitIds = await unitFilterService.GetUnitsIdByJobFilterAsync(job, 100);
if (unitIds == null || !unitIds.Any())
return NoContent();
var query = unitService.Get().Where(t => unitIds.Any(x => x == t.Id)).OrderBy(o => o.Name);
var units = await query.ToListAsync();
var unitResponse = mapper.Map<List<UnitBaseResponse>>(units);
return Ok(new Response<List<UnitBaseResponse>>(unitResponse, true));
}
}
}

View File

@@ -1,10 +1,17 @@
using Microsoft.AspNetCore.Authorization;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
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.Extensions;
using PARR.API.Services.Interfaces;
using PARR.DAL.CacheServices;
using PARR.DAL.DomainModels;
using PARR.DAL.DomainServices.Interfaces;
using PARR.DAL.Models.Job;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Services.Interfaces.Job;
@@ -24,6 +31,8 @@ namespace PARR.API.Controllers.V1
private readonly IFieldFilterService fieldFilterService;
private readonly IJobService jobService;
private readonly IJobUnitFilterService jobUnitFilterService;
private readonly IUnitFilterService unitFilterService;
private readonly IMapper mapper;
public TestController(
IClientService clientService,
@@ -33,7 +42,9 @@ namespace PARR.API.Controllers.V1
IUnitService unitService,
IFieldFilterService fieldFilterService,
IJobService jobService,
IJobUnitFilterService jobUnitFilterService
IJobUnitFilterService jobUnitFilterService,
IUnitFilterService unitFilterService,
IMapper mapper
)
{
this.clientService = clientService;
@@ -44,6 +55,8 @@ namespace PARR.API.Controllers.V1
this.fieldFilterService = fieldFilterService;
this.jobService = jobService;
this.jobUnitFilterService = jobUnitFilterService;
this.unitFilterService = unitFilterService;
this.mapper = mapper;
}
@@ -174,8 +187,6 @@ namespace PARR.API.Controllers.V1
// return Ok(result);
//}
}
public class CaheRequestTest