feat(dal): Реализован сервис UnitFilterService для получения Unit, для которых требуется создать или уже создан шаблон по Job.Id

This commit is contained in:
Mikhail Kuznetsov
2025-08-12 11:50:29 +10:00
parent 97af546654
commit 1272ce92ec
12 changed files with 189 additions and 114 deletions

View File

@@ -1,10 +1,11 @@
using Microsoft.EntityFrameworkCore;
using PARR.DAL.Contracts;
using PARR.DAL.DomainServices.Interfaces;
using PARR.DAL.Services.Interfaces.Job;
using PARR.DAL.Services.Interfaces.Unit;
using System.Text.RegularExpressions;
namespace PARR.DAL.DomainServices
namespace PARR.DAL.DomainServices.Implementations
{
internal class ShortcodesService : IShortcodesService
{

View File

@@ -1,10 +1,11 @@
using Microsoft.EntityFrameworkCore;
using PARR.DAL.Contracts;
using PARR.DAL.DomainServices.Interfaces;
using PARR.DAL.Services.Interfaces.Job;
using PARR.DAL.Services.Interfaces.Unit;
using System.Text.RegularExpressions;
namespace PARR.DAL.DomainServices
namespace PARR.DAL.DomainServices.Implementations
{
internal class TemplateNameGeneratorService : ITemplateNameGeneratorService
{

View File

@@ -0,0 +1,133 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.DAL.DomainServices.Interfaces;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Services.Interfaces.Job;
using PARR.DAL.Services.Interfaces.Unit;
namespace PARR.DAL.DomainServices.Implementations
{
internal class UnitFilterService : IUnitFilterService
{
private readonly ILogger<UnitFilterService> logger;
private readonly IJobService jobService;
private readonly IUnitService unitService;
private readonly ITemplateService templateService;
public UnitFilterService(
ILogger<UnitFilterService> logger,
IJobService jobService,
IUnitService unitService,
ITemplateService templateService
)
{
this.logger = logger;
this.jobService = jobService;
this.unitService = unitService;
this.templateService = templateService;
}
public async Task<IEnumerable<Guid>?> GetUnitsIdByJobFilterAsync(Guid jobId)
{
var result = new List<Guid>();
var job = await jobService
.Get().AsNoTracking()
.Include(t => t.UnitFilters)
.ThenInclude(t => t.FieldFilters)
.Include(t => t.UnitFilters)
.ThenInclude(t => t.RelationshipFilters)
.FirstOrDefaultAsync(t => t.Id == jobId);
if (job == null)
return null;
var baseQuery = unitService
.Get().AsNoTracking()
.Include(t => t.UnitValues)
.ThenInclude(t => t.Field)
.Include(t => t.UnitValues)
.ThenInclude(t => t.Value)
.Include(t => t.ParentUnits)
.ThenInclude(t => t.ParentUnit)
.ThenInclude(t => t.UnitValues)
.ThenInclude(t => t.Field)
.Include(t => t.ParentUnits)
.ThenInclude(t => t.ParentUnit)
.ThenInclude(t => t.UnitValues)
.ThenInclude(t => t.Value)
.Include(t => t.ChildUnits)
.ThenInclude(t => t.ChildUnit)
.ThenInclude(t => t.UnitValues)
.ThenInclude(t => t.Field)
.Include(t => t.ChildUnits)
.ThenInclude(t => t.ChildUnit)
.ThenInclude(t => t.UnitValues)
.ThenInclude(t => t.Value);
logger.LogInformation($"В baseQuery записей {await baseQuery.CountAsync()}");
foreach (var unitFilter in job.UnitFilters)
{
var query = baseQuery.Where(t => EF.Functions.ILike(t.Name, unitFilter.UnitFilter));
foreach (var fieldFilter in unitFilter.FieldFilters)
query = query.Where(t => t.UnitValues.Any(x => x.FieldId == fieldFilter.FieldId && (x.Value!.Value != null && EF.Functions.ILike(x.Value!.Value, fieldFilter.ValueMask))));
if (unitFilter.RelationshipFilters.Any())
{
var parentFilters = unitFilter.RelationshipFilters.Where(t => t.IsParent == true).ToList();
foreach (var parentFilter in parentFilters)
{
if (parentFilter.IsInverse == false)
{
if (parentFilter.IsFullMatch)
query = query.Where(t => !t.ParentUnits.Any() || t.ParentUnits.All(p => p.ParentUnit!.UnitValues.Any(pf => pf.FieldId == parentFilter.FieldId && (pf.Value!.Value != null && EF.Functions.ILike(pf.Value.Value, parentFilter.ValueMask)))));
else
query = query.Where(t => !t.ParentUnits.Any() || t.ParentUnits.Any(p => p.ParentUnit!.UnitValues.Any(pf => pf.FieldId == parentFilter.FieldId && (pf.Value!.Value != null && EF.Functions.ILike(pf.Value.Value, parentFilter.ValueMask)))));
}
else
{
if (parentFilter.IsFullMatch)
query = query.Where(t => !t.ParentUnits.Any() || !t.ParentUnits.All(p => p.ParentUnit!.UnitValues.Any(pf => pf.FieldId == parentFilter.FieldId && (pf.Value!.Value != null && EF.Functions.ILike(pf.Value.Value, parentFilter.ValueMask)))));
else
query = query.Where(t => !t.ParentUnits.Any() || !t.ParentUnits.Any(p => p.ParentUnit!.UnitValues.Any(pf => pf.FieldId == parentFilter.FieldId && (pf.Value!.Value != null && EF.Functions.ILike(pf.Value.Value, parentFilter.ValueMask)))));
}
logger.LogInformation($"!В query записей {await query.CountAsync()}");
}
}
var newUnits = await query.Select(t => t.Id).ToListAsync();
result.AddRange(newUnits);
}
return result;
}
public async Task<IEnumerable<Guid>?> GetUnitsIdForExistTemplatesByJobFilterAsync(Guid jobId)
{
var unitIdsMustBeCreated = await GetUnitsIdByJobFilterAsync(jobId);
if (unitIdsMustBeCreated == null || !unitIdsMustBeCreated.Any())
return null;
var unitWithTemplates = await templateService.Get().AsNoTracking().Where(t => t.JobId == jobId && unitIdsMustBeCreated.Any(x => x == t.UnitId)).Select(t => t.UnitId).ToListAsync();
return unitWithTemplates;
}
public async Task<IEnumerable<Guid>?> GetUnitsIdForNotExistTemplatesByJobFilterAsync(Guid jobId)
{
var unitIdsMustBeCreated = await GetUnitsIdByJobFilterAsync(jobId);
if (unitIdsMustBeCreated == null || !unitIdsMustBeCreated.Any())
return null;
var unitIdsWithTemplate = await templateService.Get().AsNoTracking().Where(t => t.JobId == jobId).Select(t => t.UnitId).ToListAsync();
var unitsIdToCreateTemplate = unitIdsMustBeCreated.Where(t => !unitIdsWithTemplate.Any(x => x == t));
return unitsIdToCreateTemplate;
}
}
}

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PARR.DAL.DomainServices
namespace PARR.DAL.DomainServices.Interfaces
{
public interface IShortcodesService
{

View File

@@ -6,7 +6,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PARR.DAL.DomainServices
namespace PARR.DAL.DomainServices.Interfaces
{
public interface ITemplateNameGeneratorService
{

View File

@@ -0,0 +1,27 @@
namespace PARR.DAL.DomainServices.Interfaces
{
public interface IUnitFilterService
{
/// <summary>
/// Получить все Unit.Id, для которых должны быть созданы Template
/// </summary>
/// <param name="jobId">Id работы</param>
/// <returns></returns>
Task<IEnumerable<Guid>?> GetUnitsIdByJobFilterAsync(Guid jobId);
/// <summary>
/// Получить все Unit.Id, для которых не были созданы Template
/// </summary>
/// <param name="jobId">Id работы</param>
/// <returns></returns>
Task<IEnumerable<Guid>?> GetUnitsIdForNotExistTemplatesByJobFilterAsync(Guid jobId);
/// <summary>
/// Получить все Unit.Id, для которых были созданы Template
/// </summary>
/// <param name="jobId">Id работы</param>
/// <returns></returns>
Task<IEnumerable<Guid>?> GetUnitsIdForExistTemplatesByJobFilterAsync(Guid jobId);
}
}