using FoodLabeling.Domain.Shared;
using SqlSugar;
namespace FoodLabeling.Application.Helpers;
///
/// 标签类型/分类/多选项等:列表 Region/Location 列展示(适用全部时分别为 All Region / All Location)。
///
public static class EntityLocationScopeDisplayHelper
{
///
/// 根据可用范围类型与已解析的 Region/Location Id、名称,生成列表展示文案。
///
///
/// 有具体门店 Guid 时始终展示门店名称,不因「覆盖某 Region 下全部门店」误显示 All Location
/// (常见于 AppliedRegionType=ALL + 单个 locationIds)。
///
public static async Task<(string Region, string Location)> BuildListDisplayAsync(
ISqlSugarClient db,
string? availabilityType,
IReadOnlyList regionIds,
IReadOnlyList locationIds,
IEnumerable regionNames,
IEnumerable locationNames,
IReadOnlyList? partnerIdsForContext,
string? appliedRegionType = null)
{
if (AllScopeBindingHelper.IsDeclaredAll(availabilityType))
{
return (AllScopeBindingHelper.AllRegionsDisplay, AllScopeBindingHelper.AllLocationsDisplay);
}
var normRegionIds = LocationScopeBindingHelper.NormalizeIds(regionIds);
var normLocationIds = LocationScopeBindingHelper.NormalizeIds(locationIds);
var partnerContext = partnerIdsForContext is { Count: > 0 } ? partnerIdsForContext : null;
string regionDisplay;
if (AllScopeBindingHelper.IsDeclaredAll(appliedRegionType)
|| AllScopeBindingHelper.HasAllScopeSentinelSelection(normRegionIds))
{
regionDisplay = AllScopeBindingHelper.AllRegionsDisplay;
}
else
{
var allRegionIds = await AllScopeBindingHelper.ResolveAllRegionIdsAsync(db, partnerContext);
var regionIsAll = normRegionIds.Count > 0
&& allRegionIds.Count > 0
&& AllScopeBindingHelper.IsFullIdSelection(normRegionIds, allRegionIds);
regionDisplay = regionIsAll
? AllScopeBindingHelper.AllRegionsDisplay
: JoinDistinctNames(regionNames);
}
// 具体门店 Guid → 显示门店名;仅 AvailabilityType=ALL 或 locationIds 含 ALL 哨兵才显示 All Location
string locationDisplay;
if (AllScopeBindingHelper.HasAllScopeSentinelSelection(normLocationIds))
{
locationDisplay = AllScopeBindingHelper.AllLocationsDisplay;
}
else
{
locationDisplay = JoinDistinctNames(locationNames);
}
return (regionDisplay, locationDisplay);
}
private static string JoinDistinctNames(IEnumerable names)
{
var ordered = names
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(x => x, StringComparer.OrdinalIgnoreCase)
.ToList();
return ordered.Count > 0 ? string.Join(", ", ordered) : FoodLabelingDisplayConsts.NotAvailable;
}
}