using Admin.NET.Plugin.AiDOP.DataPlatform.Core; using Admin.NET.Plugin.AiDOP.DataPlatform.S0Dim; namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; internal static class MdpGovernanceMasterDirectory { internal static async Task Exists(ISqlSugarClient db, long tenant, long factory, GovernanceMaster target) { var (definition, key, scoped) = target.Kind switch { "item" => (S0DimCatalog.Item, "item_code", false), "customer" => (S0DimCatalog.Customer, "customer_code", false), "supplier" => (S0DimCatalog.Supplier, "supplier_code", false), "organization" => (S0DimCatalog.Department, "department_code", true), "warehouse" => (S0DimCatalog.Location, "location_code", true), _ => throw new ArgumentException("未知主数据类型") }; // Identifiers above come only from the S0 catalog. Department and warehouse // codes are unique within domain, with factory assigned by the standard directory. var suffix = scoped ? " AND factory_id=@factory" : ""; var rows = await db.Ado.GetIntAsync($""" SELECT COUNT(*) FROM {definition.DimTable} WHERE tenant_id=@tenant AND BINARY {key}=BINARY @code AND BINARY COALESCE(domain_code,'')=BINARY @domain {suffix} """, new { tenant, factory, code = target.Code, domain = target.Domain }); return rows == 1; } }