Well operation refactor

This commit is contained in:
Olga Nemt 2024-04-22 09:17:42 +05:00
parent 01e953c10f
commit 4a878f5124
2 changed files with 333 additions and 286 deletions

View File

@ -80,5 +80,5 @@ public class WellOperationRequest : WellOperationRequestBase
/// <summary> /// <summary>
/// Идентификаторы скважин /// Идентификаторы скважин
/// </summary> /// </summary>
public IEnumerable<int>? IdsWell { get; } public IEnumerable<int> IdsWell { get; }
} }

View File

@ -1,9 +1,4 @@
using System; using AsbCloudApp.Data;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Data.WellOperation; using AsbCloudApp.Data.WellOperation;
using AsbCloudApp.Exceptions; using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories; using AsbCloudApp.Repositories;
@ -14,6 +9,11 @@ using AsbCloudDb.Model;
using Mapster; using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository; namespace AsbCloudInfrastructure.Repository;
@ -44,18 +44,8 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
public async Task<IEnumerable<WellOperationDto>> GetAsync(WellOperationRequest request, CancellationToken token) public async Task<IEnumerable<WellOperationDto>> GetAsync(WellOperationRequest request, CancellationToken token)
{ {
var query = BuildQuery(request); var (items, _) = await GetPrivateAsync(request, token);
return items;
if (request.Skip.HasValue)
query = query.Skip(request.Skip.Value);
if (request.Take.HasValue)
query = query.Take(request.Take.Value);
var entities = await query.AsNoTracking()
.ToArrayAsync(token);
return await ConvertWithDrillingDaysAndNpvHoursAsync(entities, token);
} }
public async Task<PaginationContainer<WellOperationDto>> GetPageAsync(WellOperationRequest request, CancellationToken token) public async Task<PaginationContainer<WellOperationDto>> GetPageAsync(WellOperationRequest request, CancellationToken token)
@ -63,19 +53,14 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
var skip = request.Skip ?? 0; var skip = request.Skip ?? 0;
var take = request.Take ?? 32; var take = request.Take ?? 32;
var query = BuildQuery(request); var (items, count) = await GetPrivateAsync(request, token);
var entities = await query.Skip(skip)
.Take(take)
.AsNoTracking()
.ToArrayAsync(token);
var paginationContainer = new PaginationContainer<WellOperationDto> var paginationContainer = new PaginationContainer<WellOperationDto>
{ {
Skip = skip, Skip = skip,
Take = take, Take = take,
Count = await query.CountAsync(token), Count = count,
Items = await ConvertWithDrillingDaysAndNpvHoursAsync(entities, token) Items = items
}; };
return paginationContainer; return paginationContainer;
@ -83,15 +68,15 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
public async Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(WellOperationRequest request, CancellationToken token) public async Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(WellOperationRequest request, CancellationToken token)
{ {
var query = BuildQuery(request); var query = BuildQuery(request, token);
var entities = await query var entities = (await query)
.Select(o => new .Select(o => new
{ {
o.IdCategory, o.IdCategory,
DurationMinutes = o.DurationHours * 60, DurationMinutes = o.DurationHours * 60,
DurationDepth = o.DepthEnd - o.DepthStart DurationDepth = o.DepthEnd - o.DepthStart
}) })
.ToArrayAsync(token); .ToArray();
var parentRelationDictionary = wellOperationCategoryRepository.Get(true) var parentRelationDictionary = wellOperationCategoryRepository.Get(true)
.ToDictionary(c => c.Id, c => new .ToDictionary(c => c.Id, c => new
@ -178,44 +163,128 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
throw new ArgumentInvalidException(nameof(dtos), "Все операции должны принадлежать одной скважине"); throw new ArgumentInvalidException(nameof(dtos), "Все операции должны принадлежать одной скважине");
} }
private IQueryable<WellOperation> BuildQuery(WellOperationRequest request) private async Task<IEnumerable<WellOperation>> GetByIdsWells(IEnumerable<int> idsWells, CancellationToken token)
{ {
var query = GetQuery() var query = GetQuery()
.Where(e => request.IdsWell != null && request.IdsWell.Contains(e.IdWell)) .Where(e => idsWells.Contains(e.IdWell))
.OrderBy(e => e.DateStart);
var entities = await query.ToArrayAsync(token);
return entities;
}
private async Task<(IEnumerable<WellOperationDto> items, int count)> GetPrivateAsync(WellOperationRequest request, CancellationToken token)
{
var skip = request.Skip ?? 0;
var take = request.Take ?? 32;
/*
каунт = сумма всех фильтеред1
for{
все записи по скважине и типу план/факт = wellOperationswithType
из wellOperationswithType выбираем первую операцию
из wellOperationswithType все НПВ
к wellOperationswithType применяем оставшиеся фильтры из buildquery = фильтеред1
к фильтеред1 применить скип тэйк = фильтеред2
фильтеред2 конвертировать в дто и рассчитать дэй и время нпв
...
}
*/
var entities = await GetByIdsWells(request.IdsWell, token);
var entitiesByWellAndType = entities
.GroupBy(e => new { e.IdWell, e.IdType })
.Select(grp => grp.ToArray());
var result = new List<WellOperationDto>();
foreach (var wellOperationswithType in entitiesByWellAndType)
{
var firstWellOperation = wellOperationswithType
.OrderBy(e => e.DateStart) .OrderBy(e => e.DateStart)
.AsQueryable(); .FirstOrDefault();
//НПВ
var operationsWithNpt = wellOperationswithType
.Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory));
var filteredWellOperations = FilterOperations(wellOperationswithType, request);
var filteredWellOperationsPart = filteredWellOperations
.Skip(skip)
.Take(take);
var dtos = filteredWellOperationsPart
.Select(o => ConvertWithDrillingDaysAndNpvHours(o, firstWellOperation, operationsWithNpt, token));
result.AddRange(dtos);
}
return (result, entities.Count());
}
private IEnumerable<WellOperation> FilterOperations(IEnumerable<WellOperation> entities, WellOperationRequest request)
{
if (request.OperationType.HasValue) if (request.OperationType.HasValue)
query = query.Where(e => e.IdType == request.OperationType.Value); entities = entities.Where(e => e.IdType == request.OperationType.Value);
if (request.SectionTypeIds?.Any() is true) if (request.SectionTypeIds?.Any() is true)
query = query.Where(e => request.SectionTypeIds.Contains(e.IdWellSectionType)); entities = entities.Where(e => request.SectionTypeIds.Contains(e.IdWellSectionType));
if (request.OperationCategoryIds?.Any() is true) if (request.OperationCategoryIds?.Any() is true)
query = query.Where(e => request.OperationCategoryIds.Contains(e.IdCategory)); entities = entities.Where(e => request.OperationCategoryIds.Contains(e.IdCategory));
if (request.GeDepth.HasValue) if (request.GeDepth.HasValue)
query = query.Where(e => e.DepthEnd >= request.GeDepth.Value); entities = entities.Where(e => e.DepthEnd >= request.GeDepth.Value);
if (request.LeDepth.HasValue) if (request.LeDepth.HasValue)
query = query.Where(e => e.DepthEnd <= request.LeDepth.Value); entities = entities.Where(e => e.DepthEnd <= request.LeDepth.Value);
if (request.GeDate.HasValue) if (request.GeDate.HasValue)
{ {
var geDateUtc = request.GeDate.Value.UtcDateTime; var geDateUtc = request.GeDate.Value.UtcDateTime;
query = query.Where(e => e.DateStart >= geDateUtc); entities = entities.Where(e => e.DateStart >= geDateUtc);
} }
if (request.LeDate.HasValue) if (request.LeDate.HasValue)
{ {
var leDateUtc = request.LeDate.Value.UtcDateTime; var leDateUtc = request.LeDate.Value.UtcDateTime;
query = query.Where(e => e.DateStart <= leDateUtc); entities = entities.Where(e => e.DateStart <= leDateUtc);
}
if (request.SortFields?.Any() is true)
entities = entities.AsQueryable().SortBy(request.SortFields);
return entities;
}
private async Task<IEnumerable<WellOperation>> BuildQuery(WellOperationRequest request, CancellationToken token)
{
var entities = await GetByIdsWells(request.IdsWell, token);
if (request.OperationType.HasValue)
entities = entities.Where(e => e.IdType == request.OperationType.Value);
if (request.SectionTypeIds?.Any() is true)
entities = entities.Where(e => request.SectionTypeIds.Contains(e.IdWellSectionType));
if (request.OperationCategoryIds?.Any() is true)
entities = entities.Where(e => request.OperationCategoryIds.Contains(e.IdCategory));
if (request.GeDepth.HasValue)
entities = entities.Where(e => e.DepthEnd >= request.GeDepth.Value);
if (request.LeDepth.HasValue)
entities = entities.Where(e => e.DepthEnd <= request.LeDepth.Value);
if (request.GeDate.HasValue)
{
var geDateUtc = request.GeDate.Value.UtcDateTime;
entities = entities.Where(e => e.DateStart >= geDateUtc);
}
if (request.LeDate.HasValue)
{
var leDateUtc = request.LeDate.Value.UtcDateTime;
entities = entities.Where(e => e.DateStart <= leDateUtc);
} }
if (request.SortFields?.Any() is true) if (request.SortFields?.Any() is true)
query = query.SortBy(request.SortFields); entities = entities.AsQueryable().SortBy(request.SortFields);
return query; return entities;
} }
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token) public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
@ -306,41 +375,19 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
}; };
} }
private async Task<IEnumerable<WellOperationDto>> ConvertWithDrillingDaysAndNpvHoursAsync(IEnumerable<WellOperation> entities, private WellOperationDto ConvertWithDrillingDaysAndNpvHours(
WellOperation entity,
WellOperation firstOperation,
IEnumerable<WellOperation> wellOperationsWithNtp,
CancellationToken token) 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); var dto = Convert(entity);
dto.Day = (entity.DateStart - firstOperation.DateStart).TotalDays;
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 dto.NptHours = wellOperationsWithNtp
.Where(o => o.DateStart <= entity.DateStart) .Where(o => o.DateStart <= entity.DateStart)
.Sum(e => e.DurationHours); .Sum(e => e.DurationHours);
return dto; return dto;
});
return dtos;
} }
protected override WellOperation Convert(WellOperationDto src) protected override WellOperation Convert(WellOperationDto src)