forked from ddrilling/AsbCloudServer
Well operation refactor
This commit is contained in:
parent
01e953c10f
commit
4a878f5124
@ -80,5 +80,5 @@ public class WellOperationRequest : WellOperationRequestBase
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Идентификаторы скважин
|
/// Идентификаторы скважин
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<int>? IdsWell { get; }
|
public IEnumerable<int> IdsWell { get; }
|
||||||
}
|
}
|
@ -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,350 +9,402 @@ 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;
|
||||||
|
|
||||||
public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, WellOperation>,
|
public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, WellOperation>,
|
||||||
IWellOperationRepository
|
IWellOperationRepository
|
||||||
{
|
{
|
||||||
private readonly IMemoryCache memoryCache;
|
private readonly IMemoryCache memoryCache;
|
||||||
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
|
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
|
||||||
private readonly IWellService wellService;
|
private readonly IWellService wellService;
|
||||||
|
|
||||||
public WellOperationRepository(IAsbCloudDbContext context,
|
public WellOperationRepository(IAsbCloudDbContext context,
|
||||||
IMemoryCache memoryCache,
|
IMemoryCache memoryCache,
|
||||||
IWellOperationCategoryRepository wellOperationCategoryRepository,
|
IWellOperationCategoryRepository wellOperationCategoryRepository,
|
||||||
IWellService wellService)
|
IWellService wellService)
|
||||||
: base(context, dbSet => dbSet.Include(e => e.WellSectionType)
|
: base(context, dbSet => dbSet.Include(e => e.WellSectionType)
|
||||||
.Include(e => e.OperationCategory))
|
.Include(e => e.OperationCategory))
|
||||||
{
|
{
|
||||||
this.memoryCache = memoryCache;
|
this.memoryCache = memoryCache;
|
||||||
this.wellOperationCategoryRepository = wellOperationCategoryRepository;
|
this.wellOperationCategoryRepository = wellOperationCategoryRepository;
|
||||||
this.wellService = wellService;
|
this.wellService = wellService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<WellSectionTypeDto> GetSectionTypes() =>
|
public IEnumerable<WellSectionTypeDto> GetSectionTypes() =>
|
||||||
memoryCache
|
memoryCache
|
||||||
.GetOrCreateBasic(dbContext.WellSectionTypes)
|
.GetOrCreateBasic(dbContext.WellSectionTypes)
|
||||||
.OrderBy(s => s.Order)
|
.OrderBy(s => s.Order)
|
||||||
.Select(s => s.Adapt<WellSectionTypeDto>());
|
.Select(s => s.Adapt<WellSectionTypeDto>());
|
||||||
|
|
||||||
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)
|
public async Task<PaginationContainer<WellOperationDto>> GetPageAsync(WellOperationRequest request, CancellationToken token)
|
||||||
query = query.Skip(request.Skip.Value);
|
{
|
||||||
|
var skip = request.Skip ?? 0;
|
||||||
|
var take = request.Take ?? 32;
|
||||||
|
|
||||||
if (request.Take.HasValue)
|
var (items, count) = await GetPrivateAsync(request, token);
|
||||||
query = query.Take(request.Take.Value);
|
|
||||||
|
|
||||||
var entities = await query.AsNoTracking()
|
var paginationContainer = new PaginationContainer<WellOperationDto>
|
||||||
.ToArrayAsync(token);
|
{
|
||||||
|
Skip = skip,
|
||||||
|
Take = take,
|
||||||
|
Count = count,
|
||||||
|
Items = items
|
||||||
|
};
|
||||||
|
|
||||||
return await ConvertWithDrillingDaysAndNpvHoursAsync(entities, token);
|
return paginationContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<PaginationContainer<WellOperationDto>> GetPageAsync(WellOperationRequest request, CancellationToken token)
|
public async Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(WellOperationRequest request, CancellationToken token)
|
||||||
{
|
{
|
||||||
var skip = request.Skip ?? 0;
|
var query = BuildQuery(request, token);
|
||||||
var take = request.Take ?? 32;
|
var entities = (await query)
|
||||||
|
.Select(o => new
|
||||||
|
{
|
||||||
|
o.IdCategory,
|
||||||
|
DurationMinutes = o.DurationHours * 60,
|
||||||
|
DurationDepth = o.DepthEnd - o.DepthStart
|
||||||
|
})
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
var query = BuildQuery(request);
|
var parentRelationDictionary = wellOperationCategoryRepository.Get(true)
|
||||||
|
.ToDictionary(c => c.Id, c => new
|
||||||
|
{
|
||||||
|
c.Name,
|
||||||
|
c.IdParent
|
||||||
|
});
|
||||||
|
|
||||||
var entities = await query.Skip(skip)
|
var dtos = entities
|
||||||
.Take(take)
|
.GroupBy(o => o.IdCategory)
|
||||||
.AsNoTracking()
|
.Select(g => new WellGroupOpertionDto
|
||||||
.ToArrayAsync(token);
|
{
|
||||||
|
IdCategory = g.Key,
|
||||||
|
Category = parentRelationDictionary[g.Key].Name,
|
||||||
|
Count = g.Count(),
|
||||||
|
MinutesAverage = g.Average(o => o.DurationMinutes),
|
||||||
|
MinutesMin = g.Min(o => o.DurationMinutes),
|
||||||
|
MinutesMax = g.Max(o => o.DurationMinutes),
|
||||||
|
TotalMinutes = g.Sum(o => o.DurationMinutes),
|
||||||
|
DeltaDepth = g.Sum(o => o.DurationDepth),
|
||||||
|
IdParent = parentRelationDictionary[g.Key].IdParent
|
||||||
|
});
|
||||||
|
|
||||||
var paginationContainer = new PaginationContainer<WellOperationDto>
|
while (dtos.All(x => x.IdParent != null))
|
||||||
{
|
{
|
||||||
Skip = skip,
|
dtos = dtos
|
||||||
Take = take,
|
.GroupBy(o => o.IdParent!)
|
||||||
Count = await query.CountAsync(token),
|
.Select(g =>
|
||||||
Items = await ConvertWithDrillingDaysAndNpvHoursAsync(entities, token)
|
{
|
||||||
};
|
var idCategory = g.Key ?? int.MinValue;
|
||||||
|
var category = parentRelationDictionary.GetValueOrDefault(idCategory);
|
||||||
|
var newDto = new WellGroupOpertionDto
|
||||||
|
{
|
||||||
|
IdCategory = idCategory,
|
||||||
|
Category = category?.Name ?? "unknown",
|
||||||
|
Count = g.Sum(o => o.Count),
|
||||||
|
DeltaDepth = g.Sum(o => o.DeltaDepth),
|
||||||
|
TotalMinutes = g.Sum(o => o.TotalMinutes),
|
||||||
|
Items = g.ToList(),
|
||||||
|
IdParent = category?.IdParent,
|
||||||
|
};
|
||||||
|
return newDto;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return paginationContainer;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(WellOperationRequest request, CancellationToken token)
|
public async Task<int> InsertRangeAsync(IEnumerable<WellOperationDto> dtos,
|
||||||
{
|
bool deleteBeforeInsert,
|
||||||
var query = BuildQuery(request);
|
CancellationToken token)
|
||||||
var entities = await query
|
{
|
||||||
.Select(o => new
|
EnsureValidWellOperations(dtos);
|
||||||
{
|
|
||||||
o.IdCategory,
|
|
||||||
DurationMinutes = o.DurationHours * 60,
|
|
||||||
DurationDepth = o.DepthEnd - o.DepthStart
|
|
||||||
})
|
|
||||||
.ToArrayAsync(token);
|
|
||||||
|
|
||||||
var parentRelationDictionary = wellOperationCategoryRepository.Get(true)
|
if (!deleteBeforeInsert)
|
||||||
.ToDictionary(c => c.Id, c => new
|
return await InsertRangeAsync(dtos, token);
|
||||||
{
|
|
||||||
c.Name,
|
|
||||||
c.IdParent
|
|
||||||
});
|
|
||||||
|
|
||||||
var dtos = entities
|
var idType = dtos.First().IdType;
|
||||||
.GroupBy(o => o.IdCategory)
|
var idWell = dtos.First().IdWell;
|
||||||
.Select(g => new WellGroupOpertionDto
|
|
||||||
{
|
|
||||||
IdCategory = g.Key,
|
|
||||||
Category = parentRelationDictionary[g.Key].Name,
|
|
||||||
Count = g.Count(),
|
|
||||||
MinutesAverage = g.Average(o => o.DurationMinutes),
|
|
||||||
MinutesMin = g.Min(o => o.DurationMinutes),
|
|
||||||
MinutesMax = g.Max(o => o.DurationMinutes),
|
|
||||||
TotalMinutes = g.Sum(o => o.DurationMinutes),
|
|
||||||
DeltaDepth = g.Sum(o => o.DurationDepth),
|
|
||||||
IdParent = parentRelationDictionary[g.Key].IdParent
|
|
||||||
});
|
|
||||||
|
|
||||||
while (dtos.All(x => x.IdParent != null))
|
var existingOperationIds = await dbContext.WellOperations
|
||||||
{
|
.Where(e => e.IdWell == idWell && e.IdType == idType)
|
||||||
dtos = dtos
|
.Select(e => e.Id)
|
||||||
.GroupBy(o => o.IdParent!)
|
.ToArrayAsync(token);
|
||||||
.Select(g =>
|
|
||||||
{
|
|
||||||
var idCategory = g.Key ?? int.MinValue;
|
|
||||||
var category = parentRelationDictionary.GetValueOrDefault(idCategory);
|
|
||||||
var newDto = new WellGroupOpertionDto
|
|
||||||
{
|
|
||||||
IdCategory = idCategory,
|
|
||||||
Category = category?.Name ?? "unknown",
|
|
||||||
Count = g.Sum(o => o.Count),
|
|
||||||
DeltaDepth = g.Sum(o => o.DeltaDepth),
|
|
||||||
TotalMinutes = g.Sum(o => o.TotalMinutes),
|
|
||||||
Items = g.ToList(),
|
|
||||||
IdParent = category?.IdParent,
|
|
||||||
};
|
|
||||||
return newDto;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return dtos;
|
await DeleteRangeAsync(existingOperationIds, token);
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int> InsertRangeAsync(IEnumerable<WellOperationDto> dtos,
|
return await InsertRangeAsync(dtos, token);
|
||||||
bool deleteBeforeInsert,
|
}
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
EnsureValidWellOperations(dtos);
|
|
||||||
|
|
||||||
if (!deleteBeforeInsert)
|
public override Task<int> UpdateRangeAsync(IEnumerable<WellOperationDto> dtos, CancellationToken token)
|
||||||
return await InsertRangeAsync(dtos, token);
|
{
|
||||||
|
EnsureValidWellOperations(dtos);
|
||||||
|
|
||||||
var idType = dtos.First().IdType;
|
return base.UpdateRangeAsync(dtos, token);
|
||||||
var idWell = dtos.First().IdWell;
|
}
|
||||||
|
|
||||||
var existingOperationIds = await dbContext.WellOperations
|
private static void EnsureValidWellOperations(IEnumerable<WellOperationDto> dtos)
|
||||||
.Where(e => e.IdWell == idWell && e.IdType == idType)
|
{
|
||||||
.Select(e => e.Id)
|
if (dtos.GroupBy(d => d.IdType).Count() > 1)
|
||||||
.ToArrayAsync(token);
|
throw new ArgumentInvalidException(nameof(dtos), "Все операции должны быть одного типа");
|
||||||
|
|
||||||
await DeleteRangeAsync(existingOperationIds, token);
|
if (dtos.GroupBy(d => d.IdType).Count() > 1)
|
||||||
|
throw new ArgumentInvalidException(nameof(dtos), "Все операции должны принадлежать одной скважине");
|
||||||
|
}
|
||||||
|
|
||||||
return await InsertRangeAsync(dtos, token);
|
private async Task<IEnumerable<WellOperation>> GetByIdsWells(IEnumerable<int> idsWells, CancellationToken token)
|
||||||
}
|
{
|
||||||
|
var query = GetQuery()
|
||||||
|
.Where(e => idsWells.Contains(e.IdWell))
|
||||||
|
.OrderBy(e => e.DateStart);
|
||||||
|
var entities = await query.ToArrayAsync(token);
|
||||||
|
return entities;
|
||||||
|
}
|
||||||
|
|
||||||
public override Task<int> UpdateRangeAsync(IEnumerable<WellOperationDto> dtos, CancellationToken token)
|
private async Task<(IEnumerable<WellOperationDto> items, int count)> GetPrivateAsync(WellOperationRequest request, CancellationToken token)
|
||||||
{
|
{
|
||||||
EnsureValidWellOperations(dtos);
|
var skip = request.Skip ?? 0;
|
||||||
|
var take = request.Take ?? 32;
|
||||||
|
|
||||||
return base.UpdateRangeAsync(dtos, token);
|
/*
|
||||||
}
|
каунт = сумма всех фильтеред1
|
||||||
|
for{
|
||||||
|
все записи по скважине и типу план/факт = wellOperationswithType
|
||||||
|
из wellOperationswithType выбираем первую операцию
|
||||||
|
из wellOperationswithType все НПВ
|
||||||
|
к wellOperationswithType применяем оставшиеся фильтры из buildquery = фильтеред1
|
||||||
|
к фильтеред1 применить скип тэйк = фильтеред2
|
||||||
|
фильтеред2 конвертировать в дто и рассчитать дэй и время нпв
|
||||||
|
...
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
private static void EnsureValidWellOperations(IEnumerable<WellOperationDto> dtos)
|
var entities = await GetByIdsWells(request.IdsWell, token);
|
||||||
{
|
var entitiesByWellAndType = entities
|
||||||
if (dtos.GroupBy(d => d.IdType).Count() > 1)
|
.GroupBy(e => new { e.IdWell, e.IdType })
|
||||||
throw new ArgumentInvalidException(nameof(dtos), "Все операции должны быть одного типа");
|
.Select(grp => grp.ToArray());
|
||||||
|
|
||||||
if (dtos.GroupBy(d => d.IdType).Count() > 1)
|
var result = new List<WellOperationDto>();
|
||||||
throw new ArgumentInvalidException(nameof(dtos), "Все операции должны принадлежать одной скважине");
|
foreach (var wellOperationswithType in entitiesByWellAndType)
|
||||||
}
|
{
|
||||||
|
var firstWellOperation = wellOperationswithType
|
||||||
|
.OrderBy(e => e.DateStart)
|
||||||
|
.FirstOrDefault();
|
||||||
|
|
||||||
private IQueryable<WellOperation> BuildQuery(WellOperationRequest request)
|
//НПВ
|
||||||
{
|
var operationsWithNpt = wellOperationswithType
|
||||||
var query = GetQuery()
|
.Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory));
|
||||||
.Where(e => request.IdsWell != null && request.IdsWell.Contains(e.IdWell))
|
|
||||||
.OrderBy(e => e.DateStart)
|
|
||||||
.AsQueryable();
|
|
||||||
|
|
||||||
if (request.OperationType.HasValue)
|
var filteredWellOperations = FilterOperations(wellOperationswithType, request);
|
||||||
query = query.Where(e => e.IdType == request.OperationType.Value);
|
|
||||||
|
|
||||||
if (request.SectionTypeIds?.Any() is true)
|
var filteredWellOperationsPart = filteredWellOperations
|
||||||
query = query.Where(e => request.SectionTypeIds.Contains(e.IdWellSectionType));
|
.Skip(skip)
|
||||||
|
.Take(take);
|
||||||
|
|
||||||
if (request.OperationCategoryIds?.Any() is true)
|
var dtos = filteredWellOperationsPart
|
||||||
query = query.Where(e => request.OperationCategoryIds.Contains(e.IdCategory));
|
.Select(o => ConvertWithDrillingDaysAndNpvHours(o, firstWellOperation, operationsWithNpt, token));
|
||||||
|
result.AddRange(dtos);
|
||||||
|
}
|
||||||
|
|
||||||
if (request.GeDepth.HasValue)
|
return (result, entities.Count());
|
||||||
query = query.Where(e => e.DepthEnd >= request.GeDepth.Value);
|
}
|
||||||
|
|
||||||
if (request.LeDepth.HasValue)
|
private IEnumerable<WellOperation> FilterOperations(IEnumerable<WellOperation> entities, WellOperationRequest request)
|
||||||
query = query.Where(e => e.DepthEnd <= request.LeDepth.Value);
|
{
|
||||||
|
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)
|
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);
|
||||||
|
|
||||||
if (request.SortFields?.Any() is true)
|
return entities;
|
||||||
query = query.SortBy(request.SortFields);
|
}
|
||||||
|
|
||||||
return query;
|
private async Task<IEnumerable<WellOperation>> BuildQuery(WellOperationRequest request, CancellationToken token)
|
||||||
}
|
{
|
||||||
|
var entities = await GetByIdsWells(request.IdsWell, token);
|
||||||
|
|
||||||
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
|
if (request.OperationType.HasValue)
|
||||||
{
|
entities = entities.Where(e => e.IdType == request.OperationType.Value);
|
||||||
const string keyCacheSections = "OperationsBySectionSummarties";
|
|
||||||
|
|
||||||
var cache = await memoryCache.GetOrCreateAsync(keyCacheSections, async (entry) =>
|
if (request.SectionTypeIds?.Any() is true)
|
||||||
{
|
entities = entities.Where(e => request.SectionTypeIds.Contains(e.IdWellSectionType));
|
||||||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
|
|
||||||
|
|
||||||
var query = dbContext.Set<WellOperation>()
|
if (request.OperationCategoryIds?.Any() is true)
|
||||||
.GroupBy(operation => new
|
entities = entities.Where(e => request.OperationCategoryIds.Contains(e.IdCategory));
|
||||||
{
|
|
||||||
operation.IdWell,
|
|
||||||
operation.IdType,
|
|
||||||
operation.IdWellSectionType,
|
|
||||||
operation.WellSectionType.Caption,
|
|
||||||
})
|
|
||||||
.Select(group => new
|
|
||||||
{
|
|
||||||
group.Key.IdWell,
|
|
||||||
group.Key.IdType,
|
|
||||||
group.Key.IdWellSectionType,
|
|
||||||
group.Key.Caption,
|
|
||||||
|
|
||||||
First = group
|
if (request.GeDepth.HasValue)
|
||||||
.OrderBy(operation => operation.DateStart)
|
entities = entities.Where(e => e.DepthEnd >= request.GeDepth.Value);
|
||||||
.Select(operation => new
|
|
||||||
{
|
|
||||||
operation.DateStart,
|
|
||||||
operation.DepthStart,
|
|
||||||
})
|
|
||||||
.First(),
|
|
||||||
|
|
||||||
Last = group
|
if (request.LeDepth.HasValue)
|
||||||
.OrderByDescending(operation => operation.DateStart)
|
entities = entities.Where(e => e.DepthEnd <= request.LeDepth.Value);
|
||||||
.Select(operation => new
|
|
||||||
{
|
|
||||||
operation.DateStart,
|
|
||||||
operation.DurationHours,
|
|
||||||
operation.DepthEnd,
|
|
||||||
})
|
|
||||||
.First(),
|
|
||||||
})
|
|
||||||
.Where(s => idsWells.Contains(s.IdWell));
|
|
||||||
var dbData = await query.ToArrayAsync(token);
|
|
||||||
var sections = dbData.Select(
|
|
||||||
item => new SectionByOperationsDto
|
|
||||||
{
|
|
||||||
IdWell = item.IdWell,
|
|
||||||
IdType = item.IdType,
|
|
||||||
IdWellSectionType = item.IdWellSectionType,
|
|
||||||
|
|
||||||
Caption = item.Caption,
|
if (request.GeDate.HasValue)
|
||||||
|
{
|
||||||
|
var geDateUtc = request.GeDate.Value.UtcDateTime;
|
||||||
|
entities = entities.Where(e => e.DateStart >= geDateUtc);
|
||||||
|
}
|
||||||
|
|
||||||
DateStart = item.First.DateStart,
|
if (request.LeDate.HasValue)
|
||||||
DepthStart = item.First.DepthStart,
|
{
|
||||||
|
var leDateUtc = request.LeDate.Value.UtcDateTime;
|
||||||
|
entities = entities.Where(e => e.DateStart <= leDateUtc);
|
||||||
|
}
|
||||||
|
|
||||||
DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours),
|
if (request.SortFields?.Any() is true)
|
||||||
DepthEnd = item.Last.DepthEnd,
|
entities = entities.AsQueryable().SortBy(request.SortFields);
|
||||||
})
|
|
||||||
.ToArray()
|
|
||||||
.AsEnumerable();
|
|
||||||
|
|
||||||
entry.Value = sections;
|
return entities;
|
||||||
return sections;
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return cache;
|
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
|
||||||
}
|
{
|
||||||
|
const string keyCacheSections = "OperationsBySectionSummarties";
|
||||||
|
|
||||||
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)
|
var cache = await memoryCache.GetOrCreateAsync(keyCacheSections, async (entry) =>
|
||||||
{
|
{
|
||||||
var query = dbContext.WellOperations.Where(o => o.IdWell == idWell && o.IdType == idType);
|
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
|
||||||
|
|
||||||
if (!await query.AnyAsync(cancellationToken))
|
var query = dbContext.Set<WellOperation>()
|
||||||
return null;
|
.GroupBy(operation => new
|
||||||
|
{
|
||||||
|
operation.IdWell,
|
||||||
|
operation.IdType,
|
||||||
|
operation.IdWellSectionType,
|
||||||
|
operation.WellSectionType.Caption,
|
||||||
|
})
|
||||||
|
.Select(group => new
|
||||||
|
{
|
||||||
|
group.Key.IdWell,
|
||||||
|
group.Key.IdType,
|
||||||
|
group.Key.IdWellSectionType,
|
||||||
|
group.Key.Caption,
|
||||||
|
|
||||||
var timeZoneOffset = wellService.GetTimezone(idWell).Offset;
|
First = group
|
||||||
|
.OrderBy(operation => operation.DateStart)
|
||||||
var minDate = await query.MinAsync(o => o.DateStart, cancellationToken);
|
.Select(operation => new
|
||||||
var maxDate = await query.MaxAsync(o => o.DateStart, cancellationToken);
|
{
|
||||||
|
operation.DateStart,
|
||||||
|
operation.DepthStart,
|
||||||
|
})
|
||||||
|
.First(),
|
||||||
|
|
||||||
return new DatesRangeDto
|
Last = group
|
||||||
{
|
.OrderByDescending(operation => operation.DateStart)
|
||||||
From = minDate.ToOffset(timeZoneOffset),
|
.Select(operation => new
|
||||||
To = maxDate.ToOffset(timeZoneOffset)
|
{
|
||||||
};
|
operation.DateStart,
|
||||||
}
|
operation.DurationHours,
|
||||||
|
operation.DepthEnd,
|
||||||
|
})
|
||||||
|
.First(),
|
||||||
|
})
|
||||||
|
.Where(s => idsWells.Contains(s.IdWell));
|
||||||
|
var dbData = await query.ToArrayAsync(token);
|
||||||
|
var sections = dbData.Select(
|
||||||
|
item => new SectionByOperationsDto
|
||||||
|
{
|
||||||
|
IdWell = item.IdWell,
|
||||||
|
IdType = item.IdType,
|
||||||
|
IdWellSectionType = item.IdWellSectionType,
|
||||||
|
|
||||||
private async Task<IEnumerable<WellOperationDto>> ConvertWithDrillingDaysAndNpvHoursAsync(IEnumerable<WellOperation> entities,
|
Caption = item.Caption,
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
var idsWell = entities.Select(e => e.IdWell).Distinct();
|
|
||||||
|
|
||||||
var currentWellOperations = GetQuery()
|
DateStart = item.First.DateStart,
|
||||||
.Where(entity => idsWell.Contains(entity.IdWell));
|
DepthStart = item.First.DepthStart,
|
||||||
|
|
||||||
var dateFirstDrillingOperationByIdWell = await currentWellOperations
|
DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours),
|
||||||
.Where(entity => entity.IdType == WellOperation.IdOperationTypeFact)
|
DepthEnd = item.Last.DepthEnd,
|
||||||
.GroupBy(entity => entity.IdWell)
|
})
|
||||||
.ToDictionaryAsync(g => g.Key, g => g.Min(o => o.DateStart), token);
|
.ToArray()
|
||||||
|
.AsEnumerable();
|
||||||
|
|
||||||
var operationsWithNptByIdWell = await currentWellOperations.Where(entity =>
|
entry.Value = sections;
|
||||||
entity.IdType == WellOperation.IdOperationTypeFact &&
|
return sections;
|
||||||
WellOperationCategory.NonProductiveTimeSubIds.Contains(entity.IdCategory))
|
});
|
||||||
.GroupBy(entity => entity.IdWell)
|
|
||||||
.ToDictionaryAsync(g => g.Key, g => g.Select(o => o), token);
|
|
||||||
|
|
||||||
var dtos = entities.Select(entity =>
|
return cache;
|
||||||
{
|
}
|
||||||
var dto = Convert(entity);
|
|
||||||
|
|
||||||
if (dateFirstDrillingOperationByIdWell.TryGetValue(entity.IdWell, out var dateFirstDrillingOperation))
|
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)
|
||||||
dto.Day = (entity.DateStart - dateFirstDrillingOperation).TotalDays;
|
{
|
||||||
|
var query = dbContext.WellOperations.Where(o => o.IdWell == idWell && o.IdType == idType);
|
||||||
|
|
||||||
if (operationsWithNptByIdWell.TryGetValue(entity.IdWell, out var wellOperationsWithNtp))
|
if (!await query.AnyAsync(cancellationToken))
|
||||||
dto.NptHours = wellOperationsWithNtp
|
return null;
|
||||||
.Where(o => o.DateStart <= entity.DateStart)
|
|
||||||
.Sum(e => e.DurationHours);
|
|
||||||
|
|
||||||
return dto;
|
var timeZoneOffset = wellService.GetTimezone(idWell).Offset;
|
||||||
});
|
|
||||||
|
|
||||||
return dtos;
|
var minDate = await query.MinAsync(o => o.DateStart, cancellationToken);
|
||||||
}
|
var maxDate = await query.MaxAsync(o => o.DateStart, cancellationToken);
|
||||||
|
|
||||||
protected override WellOperation Convert(WellOperationDto src)
|
return new DatesRangeDto
|
||||||
{
|
{
|
||||||
var entity = src.Adapt<WellOperation>();
|
From = minDate.ToOffset(timeZoneOffset),
|
||||||
entity.DateStart = src.DateStart.UtcDateTime;
|
To = maxDate.ToOffset(timeZoneOffset)
|
||||||
return entity;
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override WellOperationDto Convert(WellOperation src)
|
private WellOperationDto ConvertWithDrillingDaysAndNpvHours(
|
||||||
{
|
WellOperation entity,
|
||||||
//TODO: пока такое получение TimeZone скважины, нужно исправить на Lazy
|
WellOperation firstOperation,
|
||||||
//Хоть мы и тянем данные из кэша, но от получения TimeZone в этом методе нужно избавиться, пока так
|
IEnumerable<WellOperation> wellOperationsWithNtp,
|
||||||
var timeZoneOffset = wellService.GetTimezone(src.IdWell).Offset;
|
CancellationToken token)
|
||||||
var dto = src.Adapt<WellOperationDto>();
|
{
|
||||||
dto.DateStart = src.DateStart.ToOffset(timeZoneOffset);
|
var dto = Convert(entity);
|
||||||
dto.LastUpdateDate = src.LastUpdateDate.ToOffset(timeZoneOffset);
|
dto.Day = (entity.DateStart - firstOperation.DateStart).TotalDays;
|
||||||
return dto;
|
dto.NptHours = wellOperationsWithNtp
|
||||||
}
|
.Where(o => o.DateStart <= entity.DateStart)
|
||||||
|
.Sum(e => e.DurationHours);
|
||||||
|
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override WellOperation Convert(WellOperationDto src)
|
||||||
|
{
|
||||||
|
var entity = src.Adapt<WellOperation>();
|
||||||
|
entity.DateStart = src.DateStart.UtcDateTime;
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override WellOperationDto Convert(WellOperation src)
|
||||||
|
{
|
||||||
|
//TODO: пока такое получение TimeZone скважины, нужно исправить на Lazy
|
||||||
|
//Хоть мы и тянем данные из кэша, но от получения TimeZone в этом методе нужно избавиться, пока так
|
||||||
|
var timeZoneOffset = wellService.GetTimezone(src.IdWell).Offset;
|
||||||
|
var dto = src.Adapt<WellOperationDto>();
|
||||||
|
dto.DateStart = src.DateStart.ToOffset(timeZoneOffset);
|
||||||
|
dto.LastUpdateDate = src.LastUpdateDate.ToOffset(timeZoneOffset);
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user