using Microsoft.Extensions.Configuration; namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; internal static class MdpEdgeRoute { // The existing command already holds the dataset row. This is the same lock used by edge approval. internal static async Task RequireDirectAsync(ISqlSugarClient db, IConfiguration? configuration, long tenant, long factory, long dataset, long source) { if (configuration?.GetValue("MdpEdge:Enabled") != true) return; if (await db.Ado.GetIntAsync(""" SELECT COUNT(*) FROM mdp_edge_device WHERE tenant_id=@tenant AND factory_id=@factory AND dataset_id=@dataset AND source_id=@source AND config_json IS NOT NULL """, new { tenant, factory, dataset, source }) != 0) throw new MdpIngestHttpException(409, "EDGE_ROUTE_OWNED"); } internal static async Task RequireNoDirectRunAsync(ISqlSugarClient db, long tenant, long factory, long dataset, long source, bool allowHistory) { foreach (var table in new[] { "mdp_connector_api_pull_run", "mdp_connector_relational_sync_run" }) { if (await db.Ado.GetIntAsync(""" SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name=@table """, new { table }) == 0) continue; if (await db.Ado.GetIntAsync($""" SELECT COUNT(*) FROM {table} WHERE tenant_id=@tenant AND factory_id=@factory AND dataset_id=@dataset AND source_id=@source AND (active_slot=1 OR @allowHistory=0) """, new { tenant, factory, dataset, source, allowHistory }) != 0) throw new MdpIngestHttpException(409, "EDGE_DIRECT_HANDOFF_REQUIRED"); } } }