From aa5c67d2e7ee0a46b5dfbcbe11f21a4d12a45099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Wed, 4 Sep 2024 14:06:28 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B5=D0=B9?= =?UTF-8?q?=20=D0=B2=20=D0=BA=D1=8D=D1=88=20=D0=B4=D0=BB=D1=8F=20=D0=BA?= =?UTF-8?q?=D0=B0=D0=B6=D0=B4=D0=BE=D0=B9=20=D1=81=D0=BA=D0=B2=D0=B0=D0=B6?= =?UTF-8?q?=D0=B8=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repository/WellOperationRepository.cs | 91 +++++++++---------- 1 file changed, 42 insertions(+), 49 deletions(-) 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)