Рефакторинг wellOperationRepository (продолжение)

This commit is contained in:
Olga Nemt 2024-04-22 11:16:23 +05:00
parent 4a878f5124
commit 0ba1b5c5fc
2 changed files with 81 additions and 115 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace AsbCloudApp.Requests;
@ -8,44 +9,44 @@ namespace AsbCloudApp.Requests;
/// </summary>
public class WellOperationRequestBase : RequestBase
{
/// <summary>
/// Больше или равно дате начала операции
/// </summary>
public DateTimeOffset? GeDate { get; set; }
/// <summary>
/// Больше или равно дате начала операции
/// </summary>
public DateTimeOffset? GeDate { get; set; }
/// <summary>
/// Меньше или равно дате окончания операции
/// </summary>
public DateTimeOffset? LeDate { get; set; }
/// <summary>
/// Меньше или равно дате окончания операции
/// </summary>
public DateTimeOffset? LeDate { get; set; }
/// <summary>
/// Больше или равно глубины скважины на начало операции.
/// </summary>
public double? GeDepth { get; set; }
/// <summary>
/// Больше или равно глубины скважины на начало операции.
/// </summary>
public double? GeDepth { get; set; }
/// <summary>
/// Меньше или равно глубины скважины на конец операции.
/// </summary>
public double? LeDepth { get; set; }
/// <summary>
/// Меньше или равно глубины скважины на конец операции.
/// </summary>
public double? LeDepth { get; set; }
/// <summary>
/// Идентификаторы категорий операции
/// </summary>
public IEnumerable<int>? OperationCategoryIds { get; set; }
/// <summary>
/// Идентификаторы категорий операции
/// </summary>
public IEnumerable<int>? OperationCategoryIds { get; set; }
/// <summary>
/// Тип операций
/// <list type="bullet">
/// <item>0 - плановая операция</item>
/// <item>1 - фактическая операция</item>
/// </list>
/// </summary>
public int? OperationType { get; set; }
/// <summary>
/// Тип операций
/// <list type="bullet">
/// <item>0 - плановая операция</item>
/// <item>1 - фактическая операция</item>
/// </list>
/// </summary>
public int? OperationType { get; set; }
/// <summary>
/// Идентификаторы конструкций секции
/// </summary>
public IEnumerable<int>? SectionTypeIds { get; set; }
/// <summary>
/// Идентификаторы конструкций секции
/// </summary>
public IEnumerable<int>? SectionTypeIds { get; set; }
}
/// <summary>
@ -53,32 +54,34 @@ public class WellOperationRequestBase : RequestBase
/// </summary>
public class WellOperationRequest : WellOperationRequestBase
{
/// <inheritdoc />
public WellOperationRequest(IEnumerable<int> idsWell)
{
IdsWell = idsWell;
}
/// <inheritdoc />
public WellOperationRequest(IEnumerable<int> idsWell)
{
IdsWell = idsWell;
}
/// <inheritdoc />
public WellOperationRequest(WellOperationRequestBase request, IEnumerable<int> idsWell)
: this(idsWell)
{
GeDepth = request.GeDepth;
LeDepth = request.LeDepth;
GeDate = request.GeDate;
LeDate = request.LeDate;
/// <inheritdoc />
public WellOperationRequest(WellOperationRequestBase request, IEnumerable<int> idsWell)
: this(idsWell)
{
GeDepth = request.GeDepth;
LeDepth = request.LeDepth;
GeDate = request.GeDate;
LeDate = request.LeDate;
OperationCategoryIds = request.OperationCategoryIds;
OperationType = request.OperationType;
SectionTypeIds = request.SectionTypeIds;
OperationCategoryIds = request.OperationCategoryIds;
OperationType = request.OperationType;
SectionTypeIds = request.SectionTypeIds;
Skip = request.Skip;
Take = request.Take;
SortFields = request.SortFields;
}
Skip = request.Skip;
Take = request.Take;
SortFields = request.SortFields;
}
/// <summary>
/// Идентификаторы скважин
/// </summary>
public IEnumerable<int> IdsWell { get; }
/// <summary>
/// Идентификаторы скважин
/// </summary>
[Required]
[Length(1, 1)]
public IEnumerable<int> IdsWell { get; }
}

View File

@ -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);