PurchaseVersionConfiguration.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Security.Cryptography;
  2. using System.Text;
  3. using System.Text.Json;
  4. using System.Text.RegularExpressions;
  5. namespace Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  6. /// <summary>冻结采购字段到来源字段的映射。结构、转换规则及二级包由版本化的采购模板提供。</summary>
  7. public sealed class PurchaseVersionConfiguration : ISchemaRegistry
  8. {
  9. internal static IReadOnlyList<string> Fields { get; } =
  10. ["PoNo", "PoLine", "ItemCode", "OrderQty", "SupplierCode", "OrderDate", "DueDate", "Status", "sourceUpdatedAt"];
  11. private static readonly IngestContract Baseline = new PurchaseOrderSchemaRegistry().Find(PurchaseOrderSchemaRegistry.DatasetKey, PurchaseOrderSchemaRegistry.SourceVersion)!;
  12. private readonly IngestContract contract;
  13. public string MappingJson { get; }
  14. public IReadOnlyDictionary<string, string> Names { get; }
  15. public PurchaseVersionConfiguration(string dataset, string version, string mappingJson)
  16. {
  17. if (string.IsNullOrWhiteSpace(version) || string.IsNullOrWhiteSpace(mappingJson) || !Regex.IsMatch(version, @"\A[A-Za-z0-9][A-Za-z0-9._-]{0,63}\z") || mappingJson.Length > 8192)
  18. throw new ArgumentException("版本或映射格式无效");
  19. using var parsed = JsonDocument.Parse(mappingJson);
  20. if (parsed.RootElement.ValueKind != JsonValueKind.Object) throw new ArgumentException("映射应为字段对象");
  21. var names = new SortedDictionary<string, string>(StringComparer.Ordinal);
  22. foreach (var pair in parsed.RootElement.EnumerateObject())
  23. {
  24. if (!Fields.Contains(pair.Name) || pair.Value.ValueKind != JsonValueKind.String
  25. || pair.Value.GetString() is not { Length: > 0 and <= 128 } value || value.Any(char.IsControl)
  26. || !names.TryAdd(pair.Name, value))
  27. throw new ArgumentException("采购字段映射无效或重复");
  28. }
  29. foreach (var field in Fields) names.TryAdd(field, field);
  30. if (names.Values.Distinct(StringComparer.Ordinal).Count() != names.Count)
  31. throw new ArgumentException("一个来源字段不能同时映射多个采购字段");
  32. Names = names;
  33. MappingJson = JsonSerializer.Serialize(names);
  34. if (version == PurchaseOrderSchemaRegistry.SourceVersion && dataset == PurchaseOrderSchemaRegistry.DatasetKey)
  35. {
  36. if (names.Any(p => p.Key != p.Value)) throw new ArgumentException("内置采购 v1 已冻结,请另建版本");
  37. contract = Baseline;
  38. return;
  39. }
  40. var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(MappingJson))).ToLowerInvariant();
  41. contract = Baseline with
  42. {
  43. DatasetKey = dataset,
  44. SourceSchema = Baseline.SourceSchema with { Stamp = new SchemaStamp(version, Baseline.SourceSchema.Stamp.Sha256) },
  45. MappingVersion = hash,
  46. ConfigVersion = version,
  47. FieldMapJson = MappingJson
  48. };
  49. }
  50. public IngestContract? Find(string datasetKey, string sourceSchemaVersion) =>
  51. datasetKey == contract.DatasetKey && sourceSchemaVersion == contract.SourceSchema.Stamp.Version ? contract : null;
  52. }