From ac363ff7fa57e2a032fa054f1623f49fbc3c90e4 Mon Sep 17 00:00:00 2001 From: Mikhail Kuznetsov Date: Thu, 21 May 2026 10:46:40 +1000 Subject: [PATCH] =?UTF-8?q?fix(templateMatcher):=20=D0=A3=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=BE=20=D0=BB=D0=B8=D1=88=D0=BD?= =?UTF-8?q?=D0=B5=D0=B5=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=B8=D0=B7-=D0=B7=D0=B0=20=D0=BD=D0=B5=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D0=B1=D0=B8=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D1=81=D0=BE=D1=80?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B8=20=D1=8E=D0=BD=D0=B8?= =?UTF-8?q?=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PARR.TemplateMatcher.csproj | 1 - .../Implementations/GroupedTemplateBuilder.cs | 29 +++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/PARR.TemplateMatcher/PARR.TemplateMatcher.csproj b/PARR.TemplateMatcher/PARR.TemplateMatcher.csproj index a17ad3a6..fc46dc69 100644 --- a/PARR.TemplateMatcher/PARR.TemplateMatcher.csproj +++ b/PARR.TemplateMatcher/PARR.TemplateMatcher.csproj @@ -7,7 +7,6 @@ - diff --git a/PARR.TemplateMatcher/Services/Implementations/GroupedTemplateBuilder.cs b/PARR.TemplateMatcher/Services/Implementations/GroupedTemplateBuilder.cs index f4d4b654..32a869fc 100644 --- a/PARR.TemplateMatcher/Services/Implementations/GroupedTemplateBuilder.cs +++ b/PARR.TemplateMatcher/Services/Implementations/GroupedTemplateBuilder.cs @@ -24,10 +24,10 @@ internal class GroupedTemplateBuilder : IGroupedTemplateBuilder } public async Task> BuildAsync( - Dictionary> initialReverseMapping, - JobGroup jobGroup, - Job maxJob, - CancellationToken ct = default) + Dictionary> initialReverseMapping, + JobGroup jobGroup, + Job maxJob, + CancellationToken ct = default) { logger.LogDebug("Начало построения структуры групп для JobGroup {JobGroupId}.", jobGroup.Id); @@ -51,17 +51,21 @@ internal class GroupedTemplateBuilder : IGroupedTemplateBuilder var relevantUnitInValues = await unitInValueRepository.Get() .AsNoTracking() .Where(uiv => allSourceUnitIds.Contains(uiv.UnitId) && uiv.FieldId == groupingFieldId) + .OrderBy(uiv => uiv.UnitId) + .ThenBy(uiv => uiv.ValueId) .Select(uiv => new { uiv.UnitId, uiv.ValueId }) .ToListAsync(ct); var uivLookup = relevantUnitInValues .GroupBy(x => x.UnitId) - .ToDictionary(g => g.Key, g => g.Select(x => x.ValueId).ToList()); + .ToDictionary( + g => g.Key, + g => g.Select(x => x.ValueId).ToList()); // 4. Трансформируем в reverseMapping с парами var reverseMapping = new Dictionary>(); - foreach (var kvp in initialReverseMapping) + foreach (var kvp in initialReverseMapping.OrderBy(k => k.Key)) { var potentialUnitId = kvp.Key; var sourceDtoIds = kvp.Value; @@ -79,6 +83,8 @@ internal class GroupedTemplateBuilder : IGroupedTemplateBuilder var uniqueEntries = entries .GroupBy(e => (e.UnitId, e.UnitFieldValueId)) .Select(g => g.First()) + .OrderBy(e => e.UnitId) // ИСПРАВЛЕНО: стабильная сортировка + .ThenBy(e => e.UnitFieldValueId) .ToList(); if (uniqueEntries.Any()) @@ -94,7 +100,7 @@ internal class GroupedTemplateBuilder : IGroupedTemplateBuilder var innerGroupingValues = await unitInValueRepository.GetByUnitIdsAndFieldIdsAsync( allUnitIdsForInnerGrouping, - (new HashSet { innerGroupingFieldId }), + new HashSet { innerGroupingFieldId }, ct); var unitIdToInnerGroupingValueMap = innerGroupingValues @@ -105,7 +111,7 @@ internal class GroupedTemplateBuilder : IGroupedTemplateBuilder var templateGroups = new List(); int maxValueForSplitting = maxJob.MaxValueRelationships!.Value; - foreach (var kvp in reverseMapping) + foreach (var kvp in reverseMapping.OrderBy(k => k.Key)) { var potentialUnitId = kvp.Key; var unitsInTemplateForThisPotentialUnitId = kvp.Value; @@ -134,8 +140,13 @@ internal class GroupedTemplateBuilder : IGroupedTemplateBuilder foreach (var subGroupEntries in splitSubGroups) { + var sortedEntries = subGroupEntries + .OrderBy(e => e.UnitId) + .ThenBy(e => e.UnitFieldValueId) + .ToList(); + subGroups.Add(new GroupedTemplateSubGroup( - Entries: subGroupEntries, + Entries: sortedEntries, InnerGroupName: innerGroupName, GlobalIndex: globalIndex ));