MdpTenantStdSourceResolver.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. namespace Admin.NET.Plugin.AiDOP.DataPlatform;
  2. public sealed class MdpTenantStdSource
  3. {
  4. public string SourceSystem { get; init; } = "";
  5. public string SourceScope { get; init; } = "";
  6. }
  7. /// <summary>未登记或来源不一致时返回 null。不回退任何默认来源。</summary>
  8. public sealed class MdpTenantStdSourceResolver : ITransient
  9. {
  10. private readonly ISqlSugarClient _db;
  11. public MdpTenantStdSourceResolver(ISqlSugarClient db) => _db = db;
  12. public Task<MdpTenantStdSource?> GetSourceAsync(long tenantId, string stdObject, CancellationToken cancellationToken = default)
  13. => ReadAsync(tenantId, stdObject, false, cancellationToken);
  14. internal Task<MdpTenantStdSource?> GetSourceForShareAsync(long tenantId, string stdObject)
  15. => ReadAsync(tenantId, stdObject, true, default);
  16. private async Task<MdpTenantStdSource?> ReadAsync(long tenantId, string stdObject, bool forShare, CancellationToken cancellationToken)
  17. {
  18. if (tenantId <= 0 || string.IsNullOrWhiteSpace(stdObject))
  19. return null;
  20. var rows = await _db.Ado.SqlQueryAsync<MdpTenantStdSource>(
  21. """
  22. SELECT source_system AS SourceSystem, IFNULL(source_scope,'') AS SourceScope
  23. FROM mdp_tenant_std_source
  24. WHERE tenant_id=@tenant AND std_object=@obj
  25. LIMIT 1
  26. """ + (forShare ? " FOR SHARE" : string.Empty),
  27. new { tenant = tenantId, obj = stdObject.Trim() });
  28. cancellationToken.ThrowIfCancellationRequested();
  29. return rows.FirstOrDefault();
  30. }
  31. }