AllScopeBindingHelper.cs 23.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
using FoodLabeling.Application.Services.DbModels;
using SqlSugar;

namespace FoodLabeling.Application.Helpers;

/// <summary>
/// 「适用全部 Region/Location/Company」:保存为 ALL(不写关联快照),查询时动态包含后续新增的 Region/Location。
/// </summary>
public static class AllScopeBindingHelper
{
    public const string ScopeAll = "ALL";
    public const string ScopeSpecified = "SPECIFIED";

    public const string AllRegionsDisplay = FoodLabelingDisplayConsts.AllRegion;
    public const string AllLocationsDisplay = FoodLabelingDisplayConsts.AllLocation;
    public const string AllCompaniesDisplay = "All Companies";

    /// <summary>选中 Id 是否覆盖当前上下文中全部可选项(用于将「全选」规范为 ALL)。</summary>
    public static bool IsFullIdSelection(IReadOnlyList<string> selected, IReadOnlyList<string> universe)
    {
        if (universe.Count == 0)
        {
            return selected.Count == 0;
        }

        var universeSet = new HashSet<string>(
            universe.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()),
            StringComparer.OrdinalIgnoreCase);
        var selectedSet = new HashSet<string>(
            selected.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()),
            StringComparer.OrdinalIgnoreCase);

        if (selectedSet.Count != universeSet.Count)
        {
            return false;
        }

        return selectedSet.SetEquals(universeSet);
    }

    /// <summary>
    /// 解析维度类型:显式 ALL、空数组 ALL、或 Id 列表全选时视为 ALL。
    /// 入参显式 <see cref="ScopeSpecified"/> 时保留 SPECIFIED 并落库全部 Id,不因「当前上下文全选」折叠为 ALL。
    /// </summary>
    public static string ResolveDimensionType(
        string? declaredType,
        IReadOnlyList<string> ids,
        bool hasArrayInPayload,
        bool isFullSelection)
    {
        if (IsDeclaredSpecified(declaredType))
        {
            return ScopeSpecified;
        }

        if (isFullSelection || (ids.Count == 0 && IsDeclaredAll(declaredType)))
        {
            return ScopeAll;
        }

        if (ids.Count > 0)
        {
            return ScopeSpecified;
        }

        if (hasArrayInPayload && IsDeclaredAll(declaredType))
        {
            return ScopeAll;
        }

        var normalized = (declaredType ?? ScopeAll).Trim().ToUpperInvariant();
        return normalized == ScopeSpecified ? ScopeSpecified : ScopeAll;
    }

    public static bool IsDeclaredAll(string? type) =>
        string.Equals((type ?? ScopeAll).Trim(), ScopeAll, StringComparison.OrdinalIgnoreCase);

    public static bool IsDeclaredSpecified(string? type) =>
        string.Equals((type ?? string.Empty).Trim(), ScopeSpecified, StringComparison.OrdinalIgnoreCase);

    /// <summary>全部 Company(<c>fl_partner.Id</c>)。</summary>
    public static async Task<List<string>> ResolveAllPartnerIdsAsync(ISqlSugarClient db)
    {
        var rows = await db.Queryable<FlPartnerDbEntity>()
            .Where(x => !x.IsDeleted)
            .Select(x => x.Id)
            .ToListAsync();
        return LocationScopeBindingHelper.NormalizeIds(rows);
    }

    /// <summary>全部 Region;可按 Company 限定。</summary>
    public static async Task<List<string>> ResolveAllRegionIdsAsync(
        ISqlSugarClient db,
        IReadOnlyList<string>? partnerIds)
    {
        var query = db.Queryable<FlGroupDbEntity>().Where(x => !x.IsDeleted);
        var pids = LocationScopeBindingHelper.NormalizeIds(partnerIds);
        if (pids.Count > 0)
        {
            query = query.Where(x => pids.Contains(x.PartnerId));
        }

        var rows = await query.Select(x => x.Id).ToListAsync();
        return LocationScopeBindingHelper.NormalizeIds(rows);
    }

    /// <summary>
    /// 全部门店;可按 Company / Region 限定。
    /// 同时传 Company 与 Region 时取<strong>交集</strong>(该 Region 下且属于这些 Company 的门店),
    /// 禁止并集——否则回显「区内全选」无法折叠为 <c>locationIds:["ALL"]</c>。
    /// </summary>
    public static async Task<List<string>> ResolveAllLocationIdsAsync(
        ISqlSugarClient db,
        IReadOnlyList<string>? partnerIds,
        IReadOnlyList<string>? regionIds)
    {
        var partners = LocationScopeBindingHelper.NormalizeIds(partnerIds);
        var regions = LocationScopeBindingHelper.FilterConcreteScopeIds(regionIds);

        if (regions.Count > 0)
        {
            var fromRegions = await LocationScopeBindingHelper.ResolveLocationIdsFromGroupIdsAsync(db, regions);
            if (partners.Count == 0)
            {
                return LocationScopeBindingHelper.NormalizeIds(fromRegions);
            }

            var partnerLocSet = new HashSet<string>(
                await LocationScopeBindingHelper.ResolveLocationIdsFromPartnerIdsAsync(db, partners),
                StringComparer.OrdinalIgnoreCase);
            return LocationScopeBindingHelper.NormalizeIds(
                fromRegions.Where(id => partnerLocSet.Contains(id)).ToList());
        }

        if (partners.Count > 0)
        {
            return await LocationScopeBindingHelper.ResolveLocationIdsFromPartnerIdsAsync(db, partners);
        }

        var merged = await LocationScopeBindingHelper.MergeToLocationIdsAsync(
            db,
            (IReadOnlyList<string>?)null,
            null,
            null);
        return LocationScopeBindingHelper.NormalizeIds(merged);
    }

    /// <summary>Id 数组是否含 <c>ALL</c> 哨兵(大小写不敏感;与具体 Guid 同传时以 ALL 为准)。</summary>
    public static bool HasAllScopeSentinelSelection(IReadOnlyList<string>? ids) =>
        LocationScopeBindingHelper.ContainsAllScopeSentinel(ids);

    /// <summary>Company 维度是否应存为 ALL(含「勾选了全部 Company」)。</summary>
    public static async Task<(string Type, List<string> Ids)> NormalizePartnerScopeAsync(
        ISqlSugarClient db,
        string? declaredType,
        IReadOnlyList<string>? partnerIds,
        IReadOnlyList<string>? companyIds,
        bool hasArrayInPayload)
    {
        var ids = LabelEntityPartnerScopeHelper.NormalizePartnerIds(partnerIds, companyIds);
        if (HasAllScopeSentinelSelection(ids))
        {
            return (ScopeAll, new List<string>());
        }
        var allPartners = await ResolveAllPartnerIdsAsync(db);
        // 勾选当前全部 Company(含前端仍传 SPECIFIED + 全量 Id)→ 归档 ALL,后续新增 Company 动态适用
        var isFull = ids.Count > 0 && IsFullIdSelection(ids, allPartners);
        var type = ResolveDimensionType(declaredType, ids, hasArrayInPayload, isFull);
        return (type, type == ScopeSpecified ? ids : new List<string>());
    }

    /// <summary>Region 维度是否应存为 ALL。</summary>
    public static async Task<(string Type, List<string> Ids)> NormalizeRegionScopeAsync(
        ISqlSugarClient db,
        string? declaredType,
        IReadOnlyList<string> regionIds,
        bool hasArrayInPayload,
        IReadOnlyList<string>? partnerIdsForContext)
    {
        if (HasAllScopeSentinelSelection(regionIds))
        {
            return (ScopeAll, new List<string>());
        }

        var ids = LocationScopeBindingHelper.FilterConcreteScopeIds(regionIds);
        var allRegions = await ResolveAllRegionIdsAsync(db, partnerIdsForContext);
        var isFull = ids.Count > 0 && IsFullIdSelection(ids, allRegions);
        var type = ResolveDimensionType(declaredType, ids, hasArrayInPayload, isFull);
        return (type, type == ScopeSpecified ? ids : new List<string>());
    }

    /// <summary>Location 维度是否应存为 ALL。</summary>
    public static async Task<(string Type, List<string> Ids)> NormalizeLocationScopeAsync(
        ISqlSugarClient db,
        string? declaredType,
        IReadOnlyList<string> locationIds,
        bool hasArrayInPayload,
        IReadOnlyList<string>? partnerIdsForContext,
        IReadOnlyList<string>? regionIdsForContext)
    {
        if (HasAllScopeSentinelSelection(locationIds))
        {
            return (ScopeAll, new List<string>());
        }

        var ids = LocationScopeBindingHelper.FilterConcreteScopeIds(locationIds);
        var allLocations = await ResolveAllLocationIdsAsync(db, partnerIdsForContext, regionIdsForContext);
        var isFull = ids.Count > 0 && IsFullIdSelection(ids, allLocations);
        var type = ResolveDimensionType(declaredType, ids, hasArrayInPayload, isFull);
        return (type, type == ScopeSpecified ? ids : new List<string>());
    }

    /// <summary>
    /// 由 PartnerId / Region / Location 反推 Company 上下文,用于判断「Select All」是否覆盖该公司全部门店。
    /// </summary>
    public static async Task<List<string>?> ResolvePartnerContextFromScopeAsync(
        ISqlSugarClient db,
        string? partnerId,
        IReadOnlyList<string>? regionOrGroupIds,
        IReadOnlyList<string>? locationIds)
    {
        if (!string.IsNullOrWhiteSpace(partnerId))
        {
            return new List<string> { partnerId.Trim() };
        }

        var regionIds = LocationScopeBindingHelper.FilterConcreteScopeIds(regionOrGroupIds);
        if (regionIds.Count > 0)
        {
            var rows = await db.Queryable<FlGroupDbEntity>()
                .Where(g => !g.IsDeleted && regionIds.Contains(g.Id))
                .Select(g => g.PartnerId)
                .ToListAsync();
            var normalized = LocationScopeBindingHelper.NormalizeIds(rows);
            return normalized.Count > 0 ? normalized : null;
        }

        var locIds = LocationScopeBindingHelper.FilterConcreteScopeIds(locationIds);
        if (locIds.Count == 0)
        {
            return null;
        }

        var fromLoc = await LocationScopeBindingHelper.ResolvePartnerIdsFromLocationIdsAsync(db, locIds);
        var fromLocNormalized = LocationScopeBindingHelper.NormalizeIds(fromLoc);
        return fromLocNormalized.Count > 0 ? fromLocNormalized : null;
    }

    /// <summary>
    /// 标签类型/分类/多选项/产品分类:Region+Location 合并后是否应视为 ALL。
    /// </summary>
    public static async Task<bool> ShouldTreatMergedLocationScopeAsAllAsync(
        ISqlSugarClient db,
        string? declaredAvailabilityType,
        IReadOnlyList<string>? regionIds,
        IReadOnlyList<string>? locationIds,
        bool hasScopeArrays,
        IReadOnlyList<string>? partnerIdsForContext)
    {
        var concreteLocations = LocationScopeBindingHelper.FilterConcreteScopeIds(locationIds);
        var concreteRegions = LocationScopeBindingHelper.FilterConcreteScopeIds(regionIds);

        // 具体 Region(非全局 ALL)下即使覆盖该区全部门店,也只存 SPECIFIED 快照,不升全局 AvailabilityType=ALL
        if (concreteRegions.Count > 0)
        {
            return false;
        }

        // locationIds 含 ALL 且无具体 Region:由调用方按 Company 展开或归档全局 ALL
        if (HasAllScopeSentinelSelection(locationIds))
        {
            return true;
        }

        if (HasAllScopeSentinelSelection(regionIds) && concreteLocations.Count == 0)
        {
            return true;
        }

        if (IsDeclaredAll(declaredAvailabilityType)
            && concreteRegions.Count == 0
            && concreteLocations.Count == 0)
        {
            return true;
        }

        if (hasScopeArrays
            && concreteRegions.Count == 0
            && concreteLocations.Count == 0
            && IsDeclaredAll(declaredAvailabilityType))
        {
            return true;
        }

        // 前端 Select All 常传 SPECIFIED + 当前全量 Id:覆盖上下文全部门店时归档 ALL。
        // 有具体 locationIds 时:只按这些门店落 SPECIFIED 快照,禁止再升成 AvailabilityType=ALL
        // (否则「Region=ALL + 选 1 店」在公司仅 1 店或误判全选时会清空快照并回显成全局 ALL)。
        if (concreteLocations.Count > 0)
        {
            return false;
        }

        List<string> merged = await LocationScopeBindingHelper.MergeToLocationIdsAsync(
            db,
            (IReadOnlyList<string>?)null,
            concreteRegions,
            concreteLocations);

        if (merged.Count == 0)
        {
            return false;
        }

        var partnerContext = LocationScopeBindingHelper.NormalizeIds(partnerIdsForContext);
        if (partnerContext.Count == 0)
        {
            partnerContext = await ResolvePartnerContextFromScopeAsync(db, null, concreteRegions, concreteLocations)
                             ?? new List<string>();
        }

        var allLocations = await ResolveAllLocationIdsAsync(
            db,
            partnerContext.Count > 0 ? partnerContext : null,
            concreteRegions.Count > 0 ? concreteRegions : null);
        return IsFullIdSelection(merged, allLocations);
    }

    /// <summary>产品门店范围是否应存为 ALL(不写 <c>fl_location_product</c> 快照)。</summary>
    public static async Task<bool> ShouldTreatProductLocationScopeAsAllAsync(
        ISqlSugarClient db,
        string? declaredAvailabilityType,
        string? partnerId,
        IReadOnlyList<string>? groupIds,
        IReadOnlyList<string>? locationIds,
        bool hasScopePayload)
    {
        // partnerId 字符串为 ALL 哨兵:全选 Company,归档 ALL(禁止把 ALL 当 Guid 查库)
        if (LocationScopeBindingHelper.IsAllScopeSentinel(partnerId))
        {
            return true;
        }

        var concreteLocations = LocationScopeBindingHelper.FilterConcreteScopeIds(locationIds);
        var concreteGroups = LocationScopeBindingHelper.FilterConcreteScopeIds(groupIds);

        // 具体 Region 下不升全局 AvailabilityType=ALL(与 ShouldTreatMergedLocationScopeAsAllAsync 一致)
        if (concreteGroups.Count > 0)
        {
            return false;
        }

        if (HasAllScopeSentinelSelection(locationIds))
        {
            return true;
        }

        if (HasAllScopeSentinelSelection(groupIds) && concreteLocations.Count == 0)
        {
            return true;
        }

        if (IsDeclaredAll(declaredAvailabilityType)
            && string.IsNullOrWhiteSpace(partnerId)
            && concreteGroups.Count == 0
            && concreteLocations.Count == 0)
        {
            return true;
        }

        if (!hasScopePayload)
        {
            return false;
        }

        // 有具体门店时不升全局 ALL
        if (concreteLocations.Count > 0)
        {
            return false;
        }

        var merged = await LocationScopeBindingHelper.MergeToLocationIdsAsync(
            db,
            partnerId,
            concreteGroups,
            concreteLocations);

        if (merged.Count == 0)
        {
            return false;
        }

        var partnerContext = await ResolvePartnerContextFromScopeAsync(
            db, partnerId, concreteGroups, concreteLocations);
        var allLocations = await ResolveAllLocationIdsAsync(
            db,
            partnerContext,
            concreteGroups.Count > 0 ? concreteGroups : null);
        return IsFullIdSelection(merged, allLocations);
    }

    /// <summary>
    /// 详情回显:AvailabilityType/Applied*=ALL 时不落快照,展开为当前可见 Company/Region/Location 全集(对齐 Label)。
    /// <paramref name="scopedLocationIds"/> 为 null 表示管理员全量;非 null 为可见门店范围。
    /// </summary>
    public static async Task<(List<string> PartnerIds, List<string> RegionIds, List<string> LocationIds)>
        ResolveDisplayIdsForAllScopeAsync(
            ISqlSugarClient db,
            IReadOnlyList<string>? scopedLocationIds,
            IReadOnlyList<string>? preferredPartnerIds = null)
    {
        var preferred = LocationScopeBindingHelper.NormalizeIds(preferredPartnerIds);

        if (scopedLocationIds is null)
        {
            var partners = preferred.Count > 0 ? preferred : await ResolveAllPartnerIdsAsync(db);
            var partnerContext = partners.Count > 0 ? partners : null;
            var regions = await ResolveAllRegionIdsAsync(db, partnerContext);
            var locations = await ResolveAllLocationIdsAsync(db, partnerContext, null);
            return (partners, regions, locations);
        }

        var locs = LocationScopeBindingHelper.NormalizeIds(scopedLocationIds);
        var regionIds = await LocationScopeBindingHelper.ResolveGroupIdsFromLocationIdsAsync(db, locs);
        var partnerIds = preferred.Count > 0
            ? preferred
            : await LocationScopeBindingHelper.ResolvePartnerIdsFromLocationIdsAsync(db, locs);
        return (partnerIds, regionIds, locs);
    }

    /// <summary>
    /// 标签/产品分类等:解析 Region + Location 落库(对齐 Label:Region=ALL 时可保留具体门店快照)。
    /// </summary>
    public sealed class LabelEntityRegionLocationSaveResult
    {
        public string AvailabilityType { get; init; } = ScopeSpecified;

        public string AppliedRegionType { get; init; } = ScopeAll;

        public List<string> LocationIds { get; init; } = new();
    }

    public static async Task<LabelEntityRegionLocationSaveResult> ResolveLabelEntityRegionLocationForSaveAsync(
        ISqlSugarClient db,
        string? declaredAvailabilityType,
        IReadOnlyList<string>? partnerIdsForContext,
        IReadOnlyList<string>? mergedRegionIdsRaw,
        IReadOnlyList<string>? locationIdsRaw,
        bool hasScopeArrays)
    {
        var normalizedLocationIds = LocationScopeBindingHelper.NormalizeIds(locationIdsRaw);
        var mergedRegionIds = LocationScopeBindingHelper.NormalizeIds(mergedRegionIdsRaw);
        var regionIds = LocationScopeBindingHelper.FilterConcreteScopeIds(mergedRegionIds);
        var explicitLocationIds = LocationScopeBindingHelper.FilterConcreteScopeIds(normalizedLocationIds);
        var locationHasAll = HasAllScopeSentinelSelection(normalizedLocationIds);
        var regionHasAllSentinel = HasAllScopeSentinelSelection(mergedRegionIds);
        var hasConcretePartner = LocationScopeBindingHelper.NormalizeIds(partnerIdsForContext).Count > 0;

        // 前端 Select All Region 常展开为全量 Guid,需识别为 Region=ALL
        var regionHasAll = regionHasAllSentinel;
        if (!regionHasAll && regionIds.Count > 0)
        {
            var allRegions = await ResolveAllRegionIdsAsync(
                db,
                hasConcretePartner ? partnerIdsForContext : null);
            regionHasAll = allRegions.Count > 0 && IsFullIdSelection(regionIds, allRegions);
        }

        // location ALL 哨兵,或具体 Region 下「空选/全选门店」:有 Company/Region 上下文则展开 SPECIFIED
        var locationCoversConcreteRegions = false;
        if (!locationHasAll && regionIds.Count > 0 && !regionHasAll)
        {
            if (explicitLocationIds.Count == 0)
            {
                locationCoversConcreteRegions = true;
            }
            else
            {
                var regionUniverse = await ResolveAllLocationIdsAsync(
                    db,
                    hasConcretePartner ? partnerIdsForContext : null,
                    regionIds);
                locationCoversConcreteRegions = regionUniverse.Count > 0
                                               && IsFullIdSelection(explicitLocationIds, regionUniverse);
            }
        }

        if (locationHasAll || locationCoversConcreteRegions)
        {
            var expandRegions = regionHasAll ? null : (regionIds.Count > 0 ? regionIds : null);
            var expanded = await LocationScopeBindingHelper.ExpandScopedAllLocationsForSaveAsync(
                db,
                hasConcretePartner ? partnerIdsForContext : null,
                expandRegions);
            if (expanded is not null)
            {
                return new LabelEntityRegionLocationSaveResult
                {
                    AvailabilityType = ScopeSpecified,
                    AppliedRegionType = regionHasAll || regionIds.Count == 0 ? ScopeAll : ScopeSpecified,
                    LocationIds = expanded
                };
            }

            return new LabelEntityRegionLocationSaveResult
            {
                AvailabilityType = ScopeAll,
                AppliedRegionType = ScopeAll,
                LocationIds = new List<string>()
            };
        }

        // Region=ALL + 具体门店:Region 存 ALL,门店 SPECIFIED 快照(可回显 regionIds=["ALL"] + 单个 locationId)
        if (regionHasAll && explicitLocationIds.Count > 0)
        {
            await LocationScopeBindingHelper.ValidateLocationIdsExistAsync(db, explicitLocationIds);
            return new LabelEntityRegionLocationSaveResult
            {
                AvailabilityType = ScopeSpecified,
                AppliedRegionType = ScopeAll,
                LocationIds = explicitLocationIds
            };
        }

        // Region=ALL 且无具体门店 → 双 ALL
        if (regionHasAll)
        {
            return new LabelEntityRegionLocationSaveResult
            {
                AvailabilityType = ScopeAll,
                AppliedRegionType = ScopeAll,
                LocationIds = new List<string>()
            };
        }

        // 有具体门店(可同传具体 Region):以门店为准
        if (explicitLocationIds.Count > 0)
        {
            await LocationScopeBindingHelper.ValidateLocationIdsExistAsync(db, explicitLocationIds);
            return new LabelEntityRegionLocationSaveResult
            {
                AvailabilityType = ScopeSpecified,
                AppliedRegionType = ScopeSpecified,
                LocationIds = explicitLocationIds
            };
        }

        if (await ShouldTreatMergedLocationScopeAsAllAsync(
                db,
                declaredAvailabilityType,
                regionIds,
                explicitLocationIds,
                hasScopeArrays,
                partnerIdsForContext))
        {
            return new LabelEntityRegionLocationSaveResult
            {
                AvailabilityType = ScopeAll,
                AppliedRegionType = ScopeAll,
                LocationIds = new List<string>()
            };
        }

        var availabilityType = (declaredAvailabilityType ?? ScopeAll).Trim().ToUpperInvariant();
        if (regionIds.Count > 0)
        {
            availabilityType = ScopeSpecified;
        }
        else if (hasScopeArrays && IsDeclaredAll(availabilityType))
        {
            availabilityType = ScopeAll;
        }

        if (availabilityType != ScopeAll && availabilityType != ScopeSpecified)
        {
            throw new UserFriendlyException("门店可用范围不合法(ALL/SPECIFIED)");
        }

        var locationSpecified = string.Equals(availabilityType, ScopeSpecified, StringComparison.OrdinalIgnoreCase);
        var savedLocationIds = await LocationScopeBindingHelper.ResolveEntityLocationIdsForSaveAsync(
            db,
            locationSpecified,
            regionIds,
            explicitLocationIds);

        return new LabelEntityRegionLocationSaveResult
        {
            AvailabilityType = locationSpecified ? ScopeSpecified : ScopeAll,
            AppliedRegionType = locationSpecified ? ScopeSpecified : ScopeAll,
            LocationIds = savedLocationIds
        };
    }
}