namespace SimpleAdmin.Application;
///
/// 解析入职审核人系统配置,统一支持全局审核人和公司专属审核人。
///
internal static class OnboardingReviewerConfigParser
{
private const string ConfigKey = "onboard.reviewer.userIds";
///
/// 获取指定公司的全部有效审核人用户 Id。
///
public static HashSet 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();
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");
}
}
///
/// 将 JSON 数组中的审核人 Id 加入集合,并拒绝非整数配置。
///
private static void AddReviewers(
global::System.Text.Json.JsonElement element,
HashSet 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);
}
}
}