From f5e9c036f64b939cfab8cbe96d145ad1911663aa Mon Sep 17 00:00:00 2001 From: Mikhail Kuznetsov Date: Tue, 26 May 2026 10:35:11 +1000 Subject: [PATCH] =?UTF-8?q?refactor(shortcodes):=20%=D0=A1=D0=92=D0=AF?= =?UTF-8?q?=D0=97=D0=AC%=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B2?= =?UTF-8?q?=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89=D0=B0=D0=B5=D1=82=20=D1=81?= =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B5=20=D0=BF=D0=BE=D0=BF=D1=83=D0=BB=D1=8F?= =?UTF-8?q?=D1=80=D0=BD=D0=BE=D0=B5=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=BE=D0=B3=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RelationshipUnitShortcodeHandler.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/PARR.Core/Services/Shortcodes/Handlers/RelationshipUnitShortcodeHandler.cs b/PARR.Core/Services/Shortcodes/Handlers/RelationshipUnitShortcodeHandler.cs index df4cd1a9..a98b5ab8 100644 --- a/PARR.Core/Services/Shortcodes/Handlers/RelationshipUnitShortcodeHandler.cs +++ b/PARR.Core/Services/Shortcodes/Handlers/RelationshipUnitShortcodeHandler.cs @@ -54,7 +54,6 @@ internal class RelationshipUnitShortcodeHandler : IShortcodeHandler private async Task GetRelValueAsync(TemplateForShortcode template, string fieldName, string caller, CancellationToken ct) { - // Оркестратор гарантирует наличие данных согласно Requirements if (template.UnitsInTemplate.Count == 0) { logger.LogDebug("[{Caller}] %СВЯЗЬ:{Field}% пропущен: UnitsInTemplate пуст.", caller, fieldName); @@ -63,7 +62,6 @@ internal class RelationshipUnitShortcodeHandler : IShortcodeHandler var unitIds = template.UnitsInTemplate.Select(u => u.UnitId).Distinct().ToList(); - // Пакетный запрос вместо N+1 var allValues = await unitInValueRepo.Get() .AsNoTracking() .Include(uv => uv.Field) @@ -75,15 +73,15 @@ internal class RelationshipUnitShortcodeHandler : IShortcodeHandler .Select(uv => uv.Value!.Value) .ToListAsync(ct); - var distinctSorted = allValues.Distinct().OrderBy(v => v, StringComparer.OrdinalIgnoreCase).ToList(); - if (distinctSorted.Count == 0) return string.Empty; + if (allValues.Count == 0) return string.Empty; - if (distinctSorted.Count > 1) - { - logger.LogWarning("[{Caller}] %СВЯЗЬ:{Field}% нашёл {Count} значений. Используется первое: '{First}'", - caller, fieldName, distinctSorted.Count, distinctSorted[0]); - } + var mostPopular = allValues + .GroupBy(v => v, StringComparer.OrdinalIgnoreCase) + .Select(g => new { Value = g.Key, Count = g.Count() }) + .OrderByDescending(x => x.Count) + .ThenBy(x => x.Value, StringComparer.OrdinalIgnoreCase) + .FirstOrDefault(); - return distinctSorted[0]!; + return mostPopular?.Value ?? string.Empty; } } \ No newline at end of file