PurchaseVersionConfiguration.cs 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 GovernancePackage? Package { get; }
  16. public PurchaseVersionConfiguration(string dataset, string version, string mappingJson)
  17. {
  18. if (string.IsNullOrWhiteSpace(version) || string.IsNullOrWhiteSpace(mappingJson) || !Regex.IsMatch(version, @"\A[A-Za-z0-9][A-Za-z0-9._-]{0,63}\z") || mappingJson.Length > 60000)
  19. throw new ArgumentException("版本或映射格式无效");
  20. using var parsed = JsonDocument.Parse(mappingJson);
  21. if (parsed.RootElement.ValueKind != JsonValueKind.Object) throw new ArgumentException("映射应为字段对象");
  22. if (parsed.RootElement.TryGetProperty("format", out _))
  23. {
  24. if (version == PurchaseOrderSchemaRegistry.SourceVersion)
  25. throw new ArgumentException("内置采购 v1 已冻结,请另建版本");
  26. Package = GovernancePackage.Parse(mappingJson, GovernancePackage.PurchaseFields);
  27. Names = Package.Names;
  28. MappingJson = Package.Json;
  29. contract = Baseline with
  30. {
  31. DatasetKey = dataset,
  32. SourceSchema = Package.SourceSchema(version, Baseline.SourceSchema.Json),
  33. RawSchema = GovernancePackage.RawSchema(Baseline.RawSchema),
  34. MappingVersion = Package.Digest,
  35. RuleVersion = GovernancePackage.Format,
  36. ConfigVersion = version,
  37. FieldMapJson = JsonSerializer.Serialize(Names),
  38. ConfigPackageJson = MappingJson,
  39. StandardSchemaStamp = Package.StandardSchema("PURCHASE_ORDER@1.0.0").Stamp,
  40. KeyFields = [new(Names["PoNo"], 100), new(Names["PoLine"], 50)],
  41. SourceTimeField = Names["sourceUpdatedAt"]
  42. };
  43. return;
  44. }
  45. if (mappingJson.Length > 8192) throw new ArgumentException("采购字段映射过大");
  46. var names = new SortedDictionary<string, string>(StringComparer.Ordinal);
  47. foreach (var pair in parsed.RootElement.EnumerateObject())
  48. {
  49. if (!Fields.Contains(pair.Name) || pair.Value.ValueKind != JsonValueKind.String
  50. || pair.Value.GetString() is not { Length: > 0 and <= 128 } value || value.Any(char.IsControl)
  51. || !names.TryAdd(pair.Name, value))
  52. throw new ArgumentException("采购字段映射无效或重复");
  53. }
  54. foreach (var field in Fields) names.TryAdd(field, field);
  55. if (names.Values.Distinct(StringComparer.Ordinal).Count() != names.Count)
  56. throw new ArgumentException("一个来源字段不能同时映射多个采购字段");
  57. Names = names;
  58. MappingJson = JsonSerializer.Serialize(names);
  59. if (version == PurchaseOrderSchemaRegistry.SourceVersion && dataset == PurchaseOrderSchemaRegistry.DatasetKey)
  60. {
  61. if (names.Any(p => p.Key != p.Value)) throw new ArgumentException("内置采购 v1 已冻结,请另建版本");
  62. contract = Baseline;
  63. return;
  64. }
  65. var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(MappingJson))).ToLowerInvariant();
  66. contract = Baseline with
  67. {
  68. DatasetKey = dataset,
  69. SourceSchema = Baseline.SourceSchema with { Stamp = new SchemaStamp(version, Baseline.SourceSchema.Stamp.Sha256) },
  70. MappingVersion = hash,
  71. ConfigVersion = version,
  72. FieldMapJson = MappingJson,
  73. KeyFields = [new(names["PoNo"], 100), new(names["PoLine"], 50)],
  74. SourceTimeField = names["sourceUpdatedAt"]
  75. };
  76. }
  77. public IngestContract? Find(string datasetKey, string sourceSchemaVersion) =>
  78. datasetKey == contract.DatasetKey && sourceSchemaVersion == contract.SourceSchema.Stamp.Version ? contract : null;
  79. }