forked from ddrilling/AsbCloudServer
Рефакторинг wellOperationRepository (продолжение)
This commit is contained in:
parent
4a878f5124
commit
0ba1b5c5fc
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace AsbCloudApp.Requests;
|
||||
|
||||
@ -80,5 +81,7 @@ public class WellOperationRequest : WellOperationRequestBase
|
||||
/// <summary>
|
||||
/// Идентификаторы скважин
|
||||
/// </summary>
|
||||
[Required]
|
||||
[Length(1, 1)]
|
||||
public IEnumerable<int> IdsWell { get; }
|
||||
}
|
@ -28,8 +28,7 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
|
||||
IMemoryCache memoryCache,
|
||||
IWellOperationCategoryRepository wellOperationCategoryRepository,
|
||||
IWellService wellService)
|
||||
: base(context, dbSet => dbSet.Include(e => e.WellSectionType)
|
||||
.Include(e => e.OperationCategory))
|
||||
: base(context, dbSet => dbSet)
|
||||
{
|
||||
this.memoryCache = memoryCache;
|
||||
this.wellOperationCategoryRepository = wellOperationCategoryRepository;
|
||||
@ -44,7 +43,7 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
|
||||
|
||||
public async Task<IEnumerable<WellOperationDto>> GetAsync(WellOperationRequest request, CancellationToken token)
|
||||
{
|
||||
var (items, _) = await GetPrivateAsync(request, token);
|
||||
var (items, _) = await GetAsyncWithDaysAndNpv(request, token);
|
||||
return items;
|
||||
}
|
||||
|
||||
@ -53,7 +52,7 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
|
||||
var skip = request.Skip ?? 0;
|
||||
var take = request.Take ?? 32;
|
||||
|
||||
var (items, count) = await GetPrivateAsync(request, token);
|
||||
var (items, count) = await GetAsyncWithDaysAndNpv(request, token);
|
||||
|
||||
var paginationContainer = new PaginationContainer<WellOperationDto>
|
||||
{
|
||||
@ -166,61 +165,52 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
|
||||
private async Task<IEnumerable<WellOperation>> GetByIdsWells(IEnumerable<int> idsWells, CancellationToken token)
|
||||
{
|
||||
var query = GetQuery()
|
||||
.Include(e => e.WellSectionType)
|
||||
.Include(e => e.OperationCategory)
|
||||
.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)
|
||||
private async Task<(IEnumerable<WellOperationDto> items, int count)> GetAsyncWithDaysAndNpv(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 count = 0;
|
||||
foreach (var wellOperationsWithType in entitiesByWellAndType)
|
||||
{
|
||||
var firstWellOperation = wellOperationswithType
|
||||
var firstWellOperation = wellOperationsWithType
|
||||
.OrderBy(e => e.DateStart)
|
||||
.FirstOrDefault();
|
||||
.FirstOrDefault()!;
|
||||
|
||||
//НПВ
|
||||
var operationsWithNpt = wellOperationswithType
|
||||
var operationsWithNpt = wellOperationsWithType
|
||||
.Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory));
|
||||
|
||||
var filteredWellOperations = FilterOperations(wellOperationswithType, request);
|
||||
var filteredWellOperations = GetByRequest(wellOperationsWithType, request);
|
||||
|
||||
var filteredWellOperationsPart = filteredWellOperations
|
||||
.Skip(skip)
|
||||
.Take(take);
|
||||
|
||||
var dtos = filteredWellOperationsPart
|
||||
.Select(o => ConvertWithDrillingDaysAndNpvHours(o, firstWellOperation, operationsWithNpt, token));
|
||||
.Select(o => ConvertWithDrillingDaysAndNpvHours(o, firstWellOperation, operationsWithNpt));
|
||||
|
||||
result.AddRange(dtos);
|
||||
count += filteredWellOperations.Count();
|
||||
}
|
||||
|
||||
return (result, entities.Count());
|
||||
return (result, count);
|
||||
}
|
||||
|
||||
private IEnumerable<WellOperation> FilterOperations(IEnumerable<WellOperation> entities, WellOperationRequest request)
|
||||
private IEnumerable<WellOperation> GetByRequest(IEnumerable<WellOperation> entities, WellOperationRequest request)
|
||||
{
|
||||
if (request.OperationType.HasValue)
|
||||
entities = entities.Where(e => e.IdType == request.OperationType.Value);
|
||||
@ -253,36 +243,7 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
|
||||
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)
|
||||
entities = entities.AsQueryable().SortBy(request.SortFields);
|
||||
entities = GetByRequest(entities, request);
|
||||
|
||||
return entities;
|
||||
}
|
||||
@ -378,8 +339,7 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
|
||||
private WellOperationDto ConvertWithDrillingDaysAndNpvHours(
|
||||
WellOperation entity,
|
||||
WellOperation firstOperation,
|
||||
IEnumerable<WellOperation> wellOperationsWithNtp,
|
||||
CancellationToken token)
|
||||
IEnumerable<WellOperation> wellOperationsWithNtp)
|
||||
{
|
||||
var dto = Convert(entity);
|
||||
dto.Day = (entity.DateStart - firstOperation.DateStart).TotalDays;
|
||||
@ -401,6 +361,9 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, Well
|
||||
{
|
||||
//TODO: пока такое получение TimeZone скважины, нужно исправить на Lazy
|
||||
//Хоть мы и тянем данные из кэша, но от получения TimeZone в этом методе нужно избавиться, пока так
|
||||
|
||||
//.Include(e => e.WellSectionType)
|
||||
// .Include(e => e.OperationCategory)
|
||||
var timeZoneOffset = wellService.GetTimezone(src.IdWell).Offset;
|
||||
var dto = src.Adapt<WellOperationDto>();
|
||||
dto.DateStart = src.DateStart.ToOffset(timeZoneOffset);
|
||||
|
Loading…
Reference in New Issue
Block a user