feat(templateGenerator,templateUpdater): Добавлена валидация входных UnitsInTemplate

This commit is contained in:
Mikhail Kuznetsov
2026-05-14 18:41:44 +10:00
parent 4dc89ffd2d
commit 2fea1ddfe6
2 changed files with 66 additions and 22 deletions

View File

@@ -1,9 +1,9 @@
// ValidatorService.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using PARR.Core.Repositories.Interfaces;
using PARR.Core.Repositories.Interfaces.Job;
using PARR.Core.Repositories.Interfaces.Unit;
namespace PARR.TemplateGeneratorWorker.Services
{
internal class ValidatorService : IValidatorService
@@ -12,46 +12,48 @@ namespace PARR.TemplateGeneratorWorker.Services
private readonly IJobRepository jobService;
private readonly IUnitRepository unitService;
private readonly ITemplateRepository templateService;
private readonly IUnitInValueRepository unitInValueRepo;
public ValidatorService(
ILogger<ValidatorService> logger,
IJobRepository jobService,
IUnitRepository unitService,
ITemplateRepository templateService
ITemplateRepository templateService,
IUnitInValueRepository unitInValueRepo
)
{
this.logger = logger;
this.jobService = jobService;
this.unitService = unitService;
this.templateService = templateService;
this.unitInValueRepo = unitInValueRepo;
}
public async Task<bool> IsValidAsync(Guid jobId, Guid unitId, int? index, List<Guid>? unitsInTemplate = null)
public async Task<bool> IsValidAsync(Guid jobId, Guid unitId, int? index, List<(Guid UnitId, Guid UnitFieldValueId)>? unitsInTemplate = null)
{
// Проверяем JobId
// 1. Проверяем JobId
var job = await jobService
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == jobId);
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == jobId);
if (job == null)
{
logger.LogError($"Не найдена регалментная работа {nameof(jobId)}: {jobId}");
logger.LogError("Не найдена регламентная работа JobId: {JobId}", jobId);
return false;
}
// Проверяем UnitId (UnitId - это ID регионального юнита)
// 2. Проверяем UnitId (основной юнит шаблона)
var unit = await unitService
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == unitId);
if (unit == null)
{
logger.LogError($"Не найден элемент конфигурации {nameof(unitId)}: {unitId}");
logger.LogError("Не найден элемент конфигурации UnitId: {UnitId}", unitId);
return false;
}
// Проверяем уникальность (JobId, UnitId, Index)
// 3. Проверяем уникальность (JobId, UnitId, Index)
var existingTemplate = await templateService
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.JobId == jobId && t.UnitId == unitId && t.Index == index);
@@ -62,19 +64,30 @@ namespace PARR.TemplateGeneratorWorker.Services
return false;
}
// Проверяем, что все UnitId в UnitsInTemplate существуют (если список не null и не пуст)
if (unitsInTemplate != null && unitsInTemplate.Any())
{
var unitIdsToCheck = unitsInTemplate.ToHashSet();
var existingUnitIdsCount = await unitService.Get()
.AsNoTracking()
.Where(u => unitIdsToCheck.Contains(u.Id))
.Select(u => u.Id)
.CountAsync();
var pairsToCheck = unitsInTemplate.Distinct().ToList();
var unitIdsToCheck = pairsToCheck.Select(p => p.UnitId).Distinct().ToList();
var valueIdsToCheck = pairsToCheck.Select(p => p.UnitFieldValueId).Distinct().ToList();
if (existingUnitIdsCount != unitIdsToCheck.Count)
// Загружаем только те связи, которые относятся к проверяемым ID
var existingLinks = await unitInValueRepo.Get()
.AsNoTracking()
.Where(uiv => unitIdsToCheck.Contains(uiv.UnitId) && valueIdsToCheck.Contains(uiv.ValueId))
.Select(uiv => new { uiv.UnitId, uiv.ValueId })
.ToListAsync();
// Формируем HashSet существующих пар для быстрой проверки в памяти
var existingPairsSet = existingLinks
.Select(x => (x.UnitId, UnitFieldValueId: x.ValueId))
.ToHashSet();
// Проверяем, что каждая запрошенная пара реально существует в БД
var allPairsValid = pairsToCheck.All(p => existingPairsSet.Contains((p.UnitId, p.UnitFieldValueId)));
if (!allPairsValid)
{
logger.LogError("Не все UnitId из UnitsInTemplate существуют в базе данных. Ожидается: {ExpectedCount}, Найдено: {FoundCount}", unitIdsToCheck.Count, existingUnitIdsCount);
logger.LogError("Не все пары (UnitId, UnitFieldValueId) из UnitsInTemplate являются валидными связями в UnitInValue.");
return false;
}
}