diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index d1c47800..ba5ffba5 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -109,71 +109,64 @@ public class WellOperationRepository : CrudRepositoryBase> GetSectionsAsync(IEnumerable idsWells, CancellationToken token) { - var keyCacheSections = $"OperationsBySectionSummaries_{string.Join('_', idsWells.OrderBy(x => x))}"; + const string keyCacheTemplate = "OperationsBySectionSummaries_{0}"; - var cache = await memoryCache.GetOrCreateAsync(keyCacheSections, async (entry) => + var result = new List(); + + foreach (var idWell in idsWells) { - entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); + var sections = await memoryCache.GetOrCreateAsync(string.Format(keyCacheTemplate, idWell), async (entry) => + { + entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); - var query = dbContext.Set() - .GroupBy(operation => new - { - operation.IdWell, - operation.IdType, - operation.IdWellSectionType, - operation.WellSectionType.Caption, - }) - .Select(group => new - { - group.Key.IdWell, - group.Key.IdType, - group.Key.IdWellSectionType, - group.Key.Caption, + var query = dbContext.Set() + .Where(x => x.IdWell == idWell) + .GroupBy(operation => new + { + operation.IdType, operation.IdWellSectionType, operation.WellSectionType.Caption, + }) + .Select(group => new + { + group.Key.IdType, + group.Key.IdWellSectionType, + group.Key.Caption, + First = group + .OrderBy(operation => operation.DateStart) + .Select(operation => new { operation.DateStart, operation.DepthStart, }) + .First(), + Last = group + .OrderByDescending(operation => operation.DateStart) + .Select(operation => new + { + operation.DateStart, operation.DurationHours, operation.DepthEnd, + }) + .First(), + }); - First = group - .OrderBy(operation => operation.DateStart) - .Select(operation => new - { - operation.DateStart, - operation.DepthStart, - }) - .First(), + var dbData = await query.ToArrayAsync(token); - Last = group - .OrderByDescending(operation => operation.DateStart) - .Select(operation => new - { - operation.DateStart, - operation.DurationHours, - operation.DepthEnd, - }) - .First(), - }) - .Where(s => idsWells.Contains(s.IdWell)); - var dbData = await query.ToArrayAsync(token); - var sections = dbData.Select( + var sections = dbData.Select( item => new SectionByOperationsDto { - IdWell = item.IdWell, + IdWell = idWell, IdType = item.IdType, IdWellSectionType = item.IdWellSectionType, - Caption = item.Caption, - DateStart = item.First.DateStart, DepthStart = item.First.DepthStart, - DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours), DepthEnd = item.Last.DepthEnd, - }) - .ToArray() - .AsEnumerable(); + }); - entry.Value = sections; - return sections; - }); - return cache!; + entry.Value = sections; + return sections; + }); + + result.AddRange(sections!); + } + + return result; } public async Task GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)