// ValidatorService.cs 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 { private readonly ILogger logger; private readonly IJobRepository jobService; private readonly IUnitRepository unitService; private readonly ITemplateRepository templateService; public ValidatorService( ILogger logger, IJobRepository jobService, IUnitRepository unitService, ITemplateRepository templateService ) { this.logger = logger; this.jobService = jobService; this.unitService = unitService; this.templateService = templateService; } public async Task IsValidAsync(Guid jobId, Guid unitId, int? index, List? unitsInTemplate = null) { // Проверяем JobId var job = await jobService .Get().AsNoTracking() .FirstOrDefaultAsync(t => t.Id == jobId); if (job == null) { logger.LogError($"Не найдена регалментная работа {nameof(jobId)}: {jobId}"); return false; } // Проверяем UnitId (UnitId - это ID регионального юнита) var unit = await unitService .Get().AsNoTracking() .FirstOrDefaultAsync(t => t.Id == unitId); if (unit == null) { logger.LogError($"Не найден элемент конфигурации {nameof(unitId)}: {unitId}"); return false; } // Проверяем уникальность (JobId, UnitId, Index) var existingTemplate = await templateService .Get().AsNoTracking() .FirstOrDefaultAsync(t => t.JobId == jobId && t.UnitId == unitId && t.Index == index); if (existingTemplate != null) { logger.LogError("Шаблон с JobId={JobId}, UnitId={UnitId}, Index={Index} уже существует.", jobId, unitId, index); 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(); if (existingUnitIdsCount != unitIdsToCheck.Count) { logger.LogError("Не все UnitId из UnitsInTemplate существуют в базе данных. Ожидается: {ExpectedCount}, Найдено: {FoundCount}", unitIdsToCheck.Count, existingUnitIdsCount); return false; } } return true; } } }