diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index ba566bca..4f77a485 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -22,6 +22,8 @@ namespace AsbCloudInfrastructure.Repository; public class WellOperationRepository : IWellOperationRepository { private const string KeyCacheSections = "OperationsBySectionSummarties"; + private const int Gap = 90; + private readonly IAsbCloudDbContext db; private readonly IMemoryCache memoryCache; private readonly IWellService wellService; @@ -344,21 +346,31 @@ public class WellOperationRepository : IWellOperationRepository OperationType = firstOperation.IdType, }; - var operationWithMaxDateStart = await BuildQuery(request) - .OrderByDescending(o => o.DateStart) - .FirstOrDefaultAsync(token); + var entities = await BuildQuery(request) + .AsNoTracking() + .ToArrayAsync(token) + .ConfigureAwait(false); - if (operationWithMaxDateStart is null) + if (!entities.Any()) return Validate(wellOperationDtos); - var maxOperationDateStart = operationWithMaxDateStart.DateStart; - foreach (var dto in wellOperationDtos) - { - var currentOperationDateStart = dto.DateStart.ToUniversalTime(); - if (maxOperationDateStart!.AddMonths(3) < currentOperationDateStart) - return false; + var wellOperationsUnion = entities.Union(wellOperationDtos).OrderBy(o => o.DateStart).ToArray(); - maxOperationDateStart = currentOperationDateStart; + for(var i = 1; i < wellOperationsUnion.Count(); i++) + { + var prevOperation = wellOperationsUnion[i - 1]; + var currentOperation = wellOperationsUnion[i]; + + var prevOperationDateStart = prevOperation.DateStart.ToUniversalTime(); + var currentOperationDateStart = currentOperation.DateStart.ToUniversalTime(); + + var prevOperationDateEnd = prevOperation.DateStart.AddHours(prevOperation.DurationHours).ToUniversalTime(); + var currrentOperationDateEnd = currentOperation.DateStart.AddHours(currentOperation.DurationHours).ToUniversalTime(); + + if (currentOperation.Id == 0 && ((prevOperationDateStart.AddDays(Gap) < currentOperationDateStart) || (prevOperationDateEnd >= currrentOperationDateEnd))) + { + return false; + } } return true; } @@ -375,7 +387,7 @@ public class WellOperationRepository : IWellOperationRepository foreach (var dto in wellOperationDtos) { var currentOperationDateStart = dto.DateStart.ToUniversalTime(); - if (maxOperationDateStart.AddMonths(3) < currentOperationDateStart) + if (maxOperationDateStart.AddDays(Gap) < currentOperationDateStart) return false; maxOperationDateStart = currentOperationDateStart;