Files
OnboardXAdmin/SimpleAdmin.Application/Services/Onboarding/Common/OnboardingReviewerConfigParser.cs
T
2026-08-27 11:09:20 +08:00

65 lines
2.4 KiB
C#

namespace SimpleAdmin.Application;
/// <summary>
/// 解析入职审核人系统配置,统一支持全局审核人和公司专属审核人。
/// </summary>
internal static class OnboardingReviewerConfigParser
{
private const string ConfigKey = "onboard.reviewer.userIds";
/// <summary>
/// 获取指定公司的全部有效审核人用户 Id。
/// </summary>
public static HashSet<long> Parse(string configValue, long companyId)
{
if (string.IsNullOrWhiteSpace(configValue))
throw Oops.Bah($"系统配置 {ConfigKey} 不能为空");
try
{
using var document = global::System.Text.Json.JsonDocument.Parse(configValue);
if (document.RootElement.ValueKind != global::System.Text.Json.JsonValueKind.Object)
throw new global::System.Text.Json.JsonException();
var reviewerIds = new HashSet<long>();
if (document.RootElement.TryGetProperty("all", out var allReviewers))
AddReviewers(allReviewers, reviewerIds);
if (document.RootElement.TryGetProperty("company", out var companyReviewers))
{
if (companyReviewers.ValueKind != global::System.Text.Json.JsonValueKind.Object)
throw new global::System.Text.Json.JsonException();
if (companyReviewers.TryGetProperty(companyId.ToString(), out var reviewers))
AddReviewers(reviewers, reviewerIds);
}
return reviewerIds;
}
catch (global::System.Text.Json.JsonException)
{
throw Oops.Bah(
$"系统配置 {ConfigKey} 格式不正确,应为包含 all 数组和 company 对象的 JSON");
}
}
/// <summary>
/// 将 JSON 数组中的审核人 Id 加入集合,并拒绝非整数配置。
/// </summary>
private static void AddReviewers(
global::System.Text.Json.JsonElement element,
HashSet<long> reviewerIds)
{
if (element.ValueKind != global::System.Text.Json.JsonValueKind.Array)
throw new global::System.Text.Json.JsonException();
foreach (var reviewer in element.EnumerateArray())
{
if (!reviewer.TryGetInt64(out var reviewerId) || reviewerId <= 0)
throw new global::System.Text.Json.JsonException();
reviewerIds.Add(reviewerId);
}
}
}