2026-05-14 16:47:27 +10:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using PARR.Core.Repositories.Interfaces.Unit;
|
2026-05-28 16:54:51 +10:00
|
|
|
|
using PARR.Core.Services.Shortcodes;
|
|
|
|
|
|
using PARR.Domain.Constants;
|
|
|
|
|
|
using PARR.Domain.Entities;
|
2026-07-03 11:17:48 +10:00
|
|
|
|
using PARR.Domain.Entities.JobEntities;
|
2026-06-15 16:56:23 +10:00
|
|
|
|
using PARR.Domain.Entities.JobGroupEntities;
|
2026-05-14 16:47:27 +10:00
|
|
|
|
using PARR.TemplateMatcher.Models;
|
|
|
|
|
|
|
2026-06-11 15:09:46 +10:00
|
|
|
|
namespace PARR.TemplateMatcher.Services.GroupedSync;
|
2026-05-14 16:47:27 +10:00
|
|
|
|
|
|
|
|
|
|
internal class GroupedTemplateBuilder : IGroupedTemplateBuilder
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<GroupedTemplateBuilder> logger;
|
|
|
|
|
|
private readonly IUnitInValueRepository unitInValueRepository;
|
|
|
|
|
|
private readonly IUnitFieldRepository unitFieldRepository;
|
2026-05-28 16:54:51 +10:00
|
|
|
|
private readonly IShortcodesService shortcodesService;
|
2026-05-14 16:47:27 +10:00
|
|
|
|
|
|
|
|
|
|
public GroupedTemplateBuilder(
|
|
|
|
|
|
ILogger<GroupedTemplateBuilder> logger,
|
|
|
|
|
|
IUnitInValueRepository unitInValueRepository,
|
2026-05-28 16:54:51 +10:00
|
|
|
|
IUnitFieldRepository unitFieldRepository,
|
|
|
|
|
|
IShortcodesService shortcodesService
|
|
|
|
|
|
)
|
2026-05-14 16:47:27 +10:00
|
|
|
|
{
|
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
this.unitInValueRepository = unitInValueRepository;
|
|
|
|
|
|
this.unitFieldRepository = unitFieldRepository;
|
2026-05-28 16:54:51 +10:00
|
|
|
|
this.shortcodesService = shortcodesService;
|
2026-05-14 16:47:27 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<List<GroupedTemplateGroup>> BuildAsync(
|
2026-05-28 16:54:51 +10:00
|
|
|
|
Dictionary<Guid, List<Guid>> initialReverseMapping,
|
|
|
|
|
|
JobGroup jobGroup,
|
|
|
|
|
|
Job maxJob,
|
|
|
|
|
|
CancellationToken ct = default)
|
2026-05-14 16:47:27 +10:00
|
|
|
|
{
|
|
|
|
|
|
logger.LogDebug("Начало построения структуры групп для JobGroup {JobGroupId}.", jobGroup.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if (!initialReverseMapping.Any())
|
|
|
|
|
|
return new List<GroupedTemplateGroup>();
|
|
|
|
|
|
|
2026-05-28 16:54:51 +10:00
|
|
|
|
// 1. Определяем стратегию внутренней группировки
|
|
|
|
|
|
// Если IsGroupByResponsible != true, используем WorkGroupMask через ShortcodesService
|
|
|
|
|
|
bool useWorkGroupMask = jobGroup.IsGroupByResponsible != true;
|
2026-05-14 16:47:27 +10:00
|
|
|
|
|
2026-05-28 16:54:51 +10:00
|
|
|
|
logger.LogDebug("Стратегия внутренней группировки: {Strategy}",
|
|
|
|
|
|
useWorkGroupMask ? "WorkGroupMask" : "Поле 'Ответственный за ЭК'");
|
2026-05-14 16:47:27 +10:00
|
|
|
|
|
|
|
|
|
|
// 2. Собираем все исходные UnitId
|
|
|
|
|
|
var allSourceUnitIds = initialReverseMapping.Values.SelectMany(ids => ids).Distinct().ToList();
|
|
|
|
|
|
|
2026-05-28 16:54:51 +10:00
|
|
|
|
// 3. Загружаем UnitInValue для трансформации по полю группировки из настроек JobGroup
|
|
|
|
|
|
var groupingFieldId = jobGroup.GroupingUnitFieldId!.Value;
|
2026-05-14 16:47:27 +10:00
|
|
|
|
var relevantUnitInValues = await unitInValueRepository.Get()
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.Where(uiv => allSourceUnitIds.Contains(uiv.UnitId) && uiv.FieldId == groupingFieldId)
|
2026-05-21 10:46:40 +10:00
|
|
|
|
.OrderBy(uiv => uiv.UnitId)
|
|
|
|
|
|
.ThenBy(uiv => uiv.ValueId)
|
2026-05-14 16:47:27 +10:00
|
|
|
|
.Select(uiv => new { uiv.UnitId, uiv.ValueId })
|
|
|
|
|
|
.ToListAsync(ct);
|
|
|
|
|
|
|
|
|
|
|
|
var uivLookup = relevantUnitInValues
|
|
|
|
|
|
.GroupBy(x => x.UnitId)
|
2026-05-28 16:54:51 +10:00
|
|
|
|
.ToDictionary(g => g.Key, g => g.Select(x => x.ValueId).ToList());
|
2026-05-14 16:47:27 +10:00
|
|
|
|
|
|
|
|
|
|
// 4. Трансформируем в reverseMapping с парами
|
|
|
|
|
|
var reverseMapping = new Dictionary<Guid, List<(Guid UnitId, Guid UnitFieldValueId)>>();
|
|
|
|
|
|
|
2026-05-21 10:46:40 +10:00
|
|
|
|
foreach (var kvp in initialReverseMapping.OrderBy(k => k.Key))
|
2026-05-14 16:47:27 +10:00
|
|
|
|
{
|
|
|
|
|
|
var potentialUnitId = kvp.Key;
|
|
|
|
|
|
var sourceDtoIds = kvp.Value;
|
|
|
|
|
|
var entries = new List<(Guid UnitId, Guid UnitFieldValueId)>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var dtoId in sourceDtoIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (uivLookup.TryGetValue(dtoId, out var valueIds))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (Guid valId in valueIds)
|
|
|
|
|
|
entries.Add((UnitId: dtoId, UnitFieldValueId: valId));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var uniqueEntries = entries
|
|
|
|
|
|
.GroupBy(e => (e.UnitId, e.UnitFieldValueId))
|
|
|
|
|
|
.Select(g => g.First())
|
2026-05-28 16:54:51 +10:00
|
|
|
|
.OrderBy(e => e.UnitId)
|
2026-05-21 10:46:40 +10:00
|
|
|
|
.ThenBy(e => e.UnitFieldValueId)
|
2026-05-14 16:47:27 +10:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
if (uniqueEntries.Any())
|
|
|
|
|
|
reverseMapping[potentialUnitId] = uniqueEntries;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!reverseMapping.Any())
|
|
|
|
|
|
return new List<GroupedTemplateGroup>();
|
|
|
|
|
|
|
2026-05-28 16:54:51 +10:00
|
|
|
|
// 5. Определяем значения для внутренней группировки
|
|
|
|
|
|
Dictionary<Guid, string> unitIdToGroupingValueMap;
|
2026-05-14 16:47:27 +10:00
|
|
|
|
|
2026-05-28 16:54:51 +10:00
|
|
|
|
if (useWorkGroupMask)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Используем ShortcodesService для получения финальных значений WorkGroupMask
|
|
|
|
|
|
unitIdToGroupingValueMap = new Dictionary<Guid, string>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var potentialUnitId in reverseMapping.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
var relatedUnitIds = reverseMapping[potentialUnitId].Select(e => e.UnitId).Distinct().ToList();
|
2026-05-14 16:47:27 +10:00
|
|
|
|
|
2026-05-28 16:54:51 +10:00
|
|
|
|
foreach (var relatedUnitId in relatedUnitIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Создаем временный Template для применения шорткодов
|
|
|
|
|
|
var tempTemplate = new Template
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
|
Name = "temp",
|
|
|
|
|
|
JobId = maxJob.Id,
|
|
|
|
|
|
UnitId = potentialUnitId, // Родительский юнит шаблона
|
|
|
|
|
|
Job = maxJob,
|
|
|
|
|
|
UnitsInTemplate = new List<UnitsInTemplate>
|
|
|
|
|
|
{
|
|
|
|
|
|
new UnitsInTemplate { UnitId = relatedUnitId, UnitFieldValueId = Guid.Empty }
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var workGroupValue = await shortcodesService.ApplyShortcodesAsync(
|
|
|
|
|
|
maxJob.WorkGroupMask,
|
|
|
|
|
|
tempTemplate,
|
|
|
|
|
|
nameof(GroupedTemplateBuilder));
|
|
|
|
|
|
|
|
|
|
|
|
unitIdToGroupingValueMap[relatedUnitId] = workGroupValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Логика разделения по ответственному за ЭК через служебный код поля
|
|
|
|
|
|
var innerGroupingField = await unitFieldRepository.GetByCodeAsync(UnitFieldCodes.Responsible, ct)
|
|
|
|
|
|
?? throw new InvalidOperationException($"Поле с кодом '{UnitFieldCodes.Responsible}' не найдено в справочнике UnitField.");
|
|
|
|
|
|
|
|
|
|
|
|
var innerGroupingFieldId = innerGroupingField.Id;
|
|
|
|
|
|
var allUnitsInTemplatePairs = reverseMapping.Values.SelectMany(list => list).ToList();
|
|
|
|
|
|
var allUnitIdsForInnerGrouping = allUnitsInTemplatePairs.Select(e => e.UnitId).Distinct().ToList();
|
|
|
|
|
|
|
|
|
|
|
|
var innerGroupingValues = await unitInValueRepository.GetByUnitIdsAndFieldIdsAsync(
|
|
|
|
|
|
allUnitIdsForInnerGrouping,
|
|
|
|
|
|
new HashSet<Guid> { innerGroupingFieldId },
|
|
|
|
|
|
ct);
|
|
|
|
|
|
|
|
|
|
|
|
unitIdToGroupingValueMap = innerGroupingValues
|
|
|
|
|
|
.Where(uv => uv.Value != null && uv.Value.Value != null && !string.IsNullOrEmpty(uv.Value.Value))
|
|
|
|
|
|
.ToDictionary(uv => uv.UnitId, uv => uv.Value!.Value!);
|
|
|
|
|
|
}
|
2026-05-14 16:47:27 +10:00
|
|
|
|
|
|
|
|
|
|
// 6. Формируем итоговую структуру
|
|
|
|
|
|
var templateGroups = new List<GroupedTemplateGroup>();
|
|
|
|
|
|
int maxValueForSplitting = maxJob.MaxValueRelationships!.Value;
|
|
|
|
|
|
|
2026-05-21 10:46:40 +10:00
|
|
|
|
foreach (var kvp in reverseMapping.OrderBy(k => k.Key))
|
2026-05-14 16:47:27 +10:00
|
|
|
|
{
|
|
|
|
|
|
var potentialUnitId = kvp.Key;
|
|
|
|
|
|
var unitsInTemplateForThisPotentialUnitId = kvp.Value;
|
|
|
|
|
|
|
|
|
|
|
|
var innerGroupedUnits = unitsInTemplateForThisPotentialUnitId
|
2026-05-28 16:54:51 +10:00
|
|
|
|
.GroupBy(entry => unitIdToGroupingValueMap.GetValueOrDefault(entry.UnitId, "Нет данных"))
|
2026-05-14 16:47:27 +10:00
|
|
|
|
.OrderBy(g => g.Key, StringComparer.Ordinal)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
var subGroups = new List<GroupedTemplateSubGroup>();
|
|
|
|
|
|
int globalIndex = 1;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var innerGroup in innerGroupedUnits)
|
|
|
|
|
|
{
|
|
|
|
|
|
var innerGroupName = innerGroup.Key;
|
|
|
|
|
|
if (innerGroupName == null)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
var unitsInInnerGroup = innerGroup.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
var splitSubGroups = unitsInInnerGroup
|
|
|
|
|
|
.Select((entry, index) => new { entry, groupIndex = index / maxValueForSplitting })
|
|
|
|
|
|
.GroupBy(x => x.groupIndex)
|
|
|
|
|
|
.Select(g => g.Select(x => x.entry).ToList())
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var subGroupEntries in splitSubGroups)
|
|
|
|
|
|
{
|
2026-05-21 10:46:40 +10:00
|
|
|
|
var sortedEntries = subGroupEntries
|
|
|
|
|
|
.OrderBy(e => e.UnitId)
|
|
|
|
|
|
.ThenBy(e => e.UnitFieldValueId)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
2026-05-14 16:47:27 +10:00
|
|
|
|
subGroups.Add(new GroupedTemplateSubGroup(
|
2026-05-21 10:46:40 +10:00
|
|
|
|
Entries: sortedEntries,
|
2026-05-14 16:47:27 +10:00
|
|
|
|
InnerGroupName: innerGroupName,
|
|
|
|
|
|
GlobalIndex: globalIndex
|
|
|
|
|
|
));
|
|
|
|
|
|
globalIndex++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (subGroups.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
templateGroups.Add(new GroupedTemplateGroup(
|
|
|
|
|
|
PotentialUnitId: potentialUnitId,
|
|
|
|
|
|
SubGroups: subGroups
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.LogDebug("Построено {Count} групп шаблонов.", templateGroups.Count);
|
|
|
|
|
|
return templateGroups;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|