GovernanceTemplates.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Text.Json;
  2. namespace Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  3. public sealed record GovernanceTemplate(string Key, string System, string Object, string Name,
  4. IReadOnlyList<GovernanceField> Fields, string Config, JsonElement Sample, JsonElement Expected,
  5. SchemaDefinition SourceSchema, SchemaDefinition RawSchema, SchemaDefinition StandardObject);
  6. public static class GovernanceTemplates
  7. {
  8. private static readonly IngestContract Purchase = new PurchaseOrderSchemaRegistry()
  9. .Find(PurchaseOrderSchemaRegistry.DatasetKey, PurchaseOrderSchemaRegistry.SourceVersion)!;
  10. public static IReadOnlyList<GovernanceTemplate> All { get; } =
  11. [
  12. Create("erp-order", "ERP", "SALES_ORDER", "销售订单", [
  13. new("OrderNo", "string", true, true), new("OrderLine", "string", true, true),
  14. new("ItemCode", "string", true), new("CustomerCode", "string", true),
  15. new("OrderQty", "number", true), new("BusinessDate", "string", true), new("CurrencyCode", "string"), new("Amount", "number")
  16. ], [new("ItemCode", "sku"), new("CustomerCode", "customer"),
  17. new("OrderQty", "quantity", "decimal"), new("BusinessDate", "date", "date", DateFormat: "yyyyMMdd"),
  18. new("Amount", "total", "currency", Factor: 1, Scale: 2),
  19. new("CurrencyCode", "currency", "enum", Values: new() { ["RMB"] = "CNY" })],
  20. """{"OrderNo":"SO-1","OrderLine":"1","sku":"SKU-1","customer":"CUST-1","quantity":"15.5","date":"20261201","currency":"RMB","total":"31.255"}""",
  21. """{"OrderNo":"SO-1","OrderLine":"1","ItemCode":"SKU-1","CustomerCode":"CUST-1","OrderQty":15.5,"BusinessDate":"2026-12-01T00:00:00","CurrencyCode":"CNY","Amount":31.26}"""),
  22. Create("mes-work-order", "MES", "WO_LINE_PROD", "工单", [
  23. new("WorkOrderNo", "string", true, true), new("LineNo", "string", true, true),
  24. new("ItemCode", "string", true), new("OrganizationCode", "string", true),
  25. new("PlanQty", "number", true), new("BusinessDate", "string", true)
  26. ], [new("ItemCode", "sku"), new("OrganizationCode", "dept"),
  27. new("PlanQty", "quantity", "decimal"), new("BusinessDate", "date", "date", DateFormat: "yyyyMMdd")],
  28. """{"WorkOrderNo":"WO-1","LineNo":"1","sku":"SKU-1","dept":"DEPT-1","quantity":"20","date":"20261201"}""",
  29. """{"WorkOrderNo":"WO-1","LineNo":"1","ItemCode":"SKU-1","OrganizationCode":"DEPT-1","PlanQty":20,"BusinessDate":"2026-12-01T00:00:00"}"""),
  30. Create("mes-report", "MES", "S6_REPORT", "生产报工", [
  31. new("ReportNo", "string", true, true), new("TaskNo", "string", true),
  32. new("GoodQty", "number", true), new("BusinessDate", "string", true)
  33. ], [new("GoodQty", "qty", "decimal"), new("BusinessDate", "time", "timestamp", DateFormat: "yyyy-MM-dd HH:mm:ss", TimeZone: "Asia/Shanghai")],
  34. """{"ReportNo":"RP-1","TaskNo":"TASK-1","qty":"16","time":"2026-12-01 09:00:00"}""",
  35. """{"ReportNo":"RP-1","TaskNo":"TASK-1","GoodQty":16,"BusinessDate":"2026-12-01T01:00:00.0000000Z"}"""),
  36. Create("erp-purchase", "ERP", "PURCHASE_ORDER", "采购订单", GovernancePackage.PurchaseFields,
  37. [new("ItemCode", "sku"), new("SupplierCode", "supplier"),
  38. new("OrderQty", "qty", "unit", Factor: 10, Scale: 6), new("OrderDate", "orderDate", "date", DateFormat: "yyyyMMdd")],
  39. """{"PoNo":"PO-1","PoLine":"1","sku":"SKU-1","supplier":"SUP-1","qty":"1.55","orderDate":"20261201","sourceUpdatedAt":"2026-12-01T09:00:00+08:00"}""",
  40. """{"PoNo":"PO-1","PoLine":"1","ItemCode":"SKU-1","SupplierCode":"SUP-1","OrderQty":15.5,"OrderDate":"2026-12-01T00:00:00","sourceUpdatedAt":"2026-12-01T09:00:00+08:00"}"""),
  41. Create("wms-inventory", "WMS", "INV_TRANS", "库存流水", [
  42. new("TransNo", "string", true, true), new("ItemCode", "string", true), new("WarehouseCode", "string", true),
  43. new("TransType", "string", true), new("Quantity", "number", true), new("BusinessDate", "string", true)
  44. ], [new("ItemCode", "sku"), new("WarehouseCode", "location"),
  45. new("TransType", "direction", "enum", Values: new() { ["I"] = "IN", ["O"] = "OUT" }),
  46. new("Quantity", "qty", "decimal"), new("BusinessDate", "date", "date", DateFormat: "yyyyMMdd")],
  47. """{"TransNo":"INV-1","sku":"SKU-1","location":"LOC-1","direction":"I","qty":"25","date":"20261201"}""",
  48. """{"TransNo":"INV-1","ItemCode":"SKU-1","WarehouseCode":"LOC-1","TransType":"IN","Quantity":25,"BusinessDate":"2026-12-01T00:00:00"}""")
  49. ];
  50. private static GovernanceTemplate Create(string key, string system, string objectCode, string name, IReadOnlyList<GovernanceField> fields,
  51. GovernanceRule[] rules, string sample, string expected)
  52. {
  53. var config = GovernancePackage.Parse(JsonSerializer.Serialize(new
  54. {
  55. format = GovernancePackage.Format, template = key + "@1.0.0", rules
  56. }, GovernancePackage.JsonOptions), fields);
  57. return new(key, system, objectCode, name, fields, config.Json,
  58. JsonSerializer.Deserialize<JsonElement>(sample), JsonSerializer.Deserialize<JsonElement>(expected),
  59. config.SourceSchema(key + "@1.0.0"), GovernancePackage.RawSchema(Purchase.RawSchema), config.StandardSchema(objectCode + "@1.0.0"));
  60. }
  61. }