MdpGovernanceMasterDirectory.cs 1.4 KB

123456789101112131415161718192021222324252627282930
  1. using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  2. using Admin.NET.Plugin.AiDOP.DataPlatform.S0Dim;
  3. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  4. internal static class MdpGovernanceMasterDirectory
  5. {
  6. internal static async Task<bool> Exists(ISqlSugarClient db, long tenant, long factory, GovernanceMaster target)
  7. {
  8. var (definition, key, scoped) = target.Kind switch
  9. {
  10. "item" => (S0DimCatalog.Item, "item_code", false),
  11. "customer" => (S0DimCatalog.Customer, "customer_code", false),
  12. "supplier" => (S0DimCatalog.Supplier, "supplier_code", false),
  13. "organization" => (S0DimCatalog.Department, "department_code", true),
  14. "warehouse" => (S0DimCatalog.Location, "location_code", true),
  15. _ => throw new ArgumentException("未知主数据类型")
  16. };
  17. // Identifiers above come only from the S0 catalog. Department and warehouse
  18. // codes are unique within domain, with factory assigned by the standard directory.
  19. var suffix = scoped ? " AND factory_id=@factory"
  20. : "";
  21. var rows = await db.Ado.GetIntAsync($"""
  22. SELECT COUNT(*) FROM {definition.DimTable} WHERE tenant_id=@tenant
  23. AND BINARY {key}=BINARY @code AND BINARY COALESCE(domain_code,'')=BINARY @domain
  24. {suffix}
  25. """, new { tenant, factory, code = target.Code, domain = target.Domain });
  26. return rows == 1;
  27. }
  28. }