diff --git a/AsbCloudApp/Data/WellOperationPlanDto.cs b/AsbCloudApp/Data/WellOperationPlanDto.cs deleted file mode 100644 index 5f282702..00000000 --- a/AsbCloudApp/Data/WellOperationPlanDto.cs +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/AsbCloudDb/Model/WellOperation.cs b/AsbCloudDb/Model/WellOperation.cs index 7a63e586..9926f0b1 100644 --- a/AsbCloudDb/Model/WellOperation.cs +++ b/AsbCloudDb/Model/WellOperation.cs @@ -51,12 +51,6 @@ namespace AsbCloudDb.Model [Column("comment"), Comment("Комментарий")] public string? Comment { get; set; } - - [NotMapped] - public double NptHours { get; set; } - - [NotMapped] - public double Day { get; set; } [JsonIgnore] [ForeignKey(nameof(IdWell))] diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index c9f91c82..a50ea85d 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -55,9 +55,7 @@ public class WellOperationRepository : CrudRepositoryBase> GetPageAsync(WellOperationRequest request, CancellationToken token) @@ -67,7 +65,7 @@ public class WellOperationRepository : CrudRepositoryBase BuildQuery(WellOperationRequest request) { - var currentWellOperations = GetQuery() - .Where(e => request.IdsWell != null && request.IdsWell.Contains(e.IdWell)); - var query = GetQuery() .Where(e => request.IdsWell != null && request.IdsWell.Contains(e.IdWell)) .OrderBy(e => e.DateStart) - .Select(o => new WellOperation - { - Id = o.Id, - IdPlan = o.IdPlan, - IdType = o.IdType, - IdWell = o.IdWell, - LastUpdateDate = o.LastUpdateDate, - IdWellSectionType = o.IdWellSectionType, - IdCategory = o.IdCategory, - OperationCategory = o.OperationCategory, - WellSectionType = o.WellSectionType, - DateStart = o.DateStart, - DepthStart = o.DepthStart, - DepthEnd = o.DepthEnd, - DurationHours = o.DurationHours, - CategoryInfo = o.CategoryInfo, - Comment = o.Comment, - IdUser = o.IdUser, - - NptHours = currentWellOperations - .Where(e => e.IdType == 1 && e.IdWell == o.IdWell) - .Where(e => WellOperationCategory.NonProductiveTimeSubIds.Contains(e.IdCategory)) - .Select(e => e.DurationHours) - .Sum(), - - Day = (o.DateStart - currentWellOperations - .Where(subOp => subOp.IdType == o.IdType && subOp.IdWell == o.IdWell) - .Where(subOp => subOp.DateStart <= o.DateStart) - .Min(subOp => subOp.DateStart)) - .TotalDays - }); + .AsQueryable(); if (request.OperationType.HasValue) query = query.Where(e => e.IdType == request.OperationType.Value); @@ -341,6 +306,43 @@ public class WellOperationRepository : CrudRepositoryBase> ConvertWithDrillingDaysAndNpvHoursAsync(IEnumerable entities, + CancellationToken token) + { + var idsWell = entities.Select(e => e.IdWell).Distinct(); + + var currentWellOperations = GetQuery() + .Where(entity => idsWell.Contains(entity.IdWell)); + + var dateFirstDrillingOperationByIdWell = await currentWellOperations + .Where(entity => entity.IdType == WellOperation.IdOperationTypeFact) + .GroupBy(entity => entity.IdWell) + .ToDictionaryAsync(g => g.Key, g => g.Min(o => o.DateStart), token); + + var operationsWithNptByIdWell = await currentWellOperations.Where(entity => + entity.IdType == WellOperation.IdOperationTypeFact && + WellOperationCategory.NonProductiveTimeSubIds.Contains(entity.IdCategory)) + .GroupBy(entity => entity.IdWell) + .ToDictionaryAsync(g => g.Key, g => g.Select(o => o), token); + + var dtos = entities.Select(entity => + { + var dto = Convert(entity); + + if (dateFirstDrillingOperationByIdWell.TryGetValue(entity.IdWell, out var dateFirstDrillingOperation)) + dto.Day = (entity.DateStart - dateFirstDrillingOperation).TotalDays; + + if (operationsWithNptByIdWell.TryGetValue(entity.IdWell, out var wellOperationsWithNtp)) + dto.NptHours = wellOperationsWithNtp + .Where(o => o.DateStart <= entity.DateStart) + .Sum(e => e.DurationHours); + + return dto; + }); + + return dtos; + } + protected override WellOperation Convert(WellOperationDto src) { var entity = src.Adapt();