using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Report; using GB5Shared.Query.Report; using GB5Shared.QueryExecutor; using Microsoft.Extensions.Caching.Memory; namespace GB5Shared.Export { // Loads the computed-field definitions (MREPORTVSFIELDS.COMPUTEEXPRESSION) configured on a // given ReportViewId. Static with the two services passed in (not DI-registered) so this can be // called from BOTH the grid path (GB5Shared/FastEndPoint/BaseReportEndpoint.cs, which has no // IReportExport dependency) and the export path (ReportExport.cs) without adding a new // registration to every host's Program.cs -- IQueryExecutor/IMemoryCache are already registered // nearly everywhere; IReportExport is registered in only a handful of hosts. public static class ComputedFieldCatalog { public static async Task> GetForViewAsync( IQueryExecutor? queryExecutor, IMemoryCache? cache, int reportViewId, LoginDTO login, CancellationToken ct) { // Outermost no-op guard -- most grid calls have no ReportViewId at all (it's an // export/pivot concept the plain JSON path never needed before this feature). if (reportViewId <= 0 || queryExecutor == null) return new List(); var cacheKey = $"cfd:{reportViewId}:{login.DatabaseName}"; if (cache != null && cache.TryGetValue(cacheKey, out List? cached) && cached != null) return cached; var parameters = new Dictionary { { "@ReportViewId", reportViewId } }; var result = (await queryExecutor .QueryAsync(login, ReportQB.GET_COMPUTED_FIELDS_BY_REPORTVIEW, parameters, cancellationToken: ct) .ConfigureAwait(false)).ToList(); cache?.Set(cacheKey, result, TimeSpan.FromMinutes(5)); return result; } } }