forked from ddrilling/AsbCloudServer
Рефакторинг WellOperationRepository
This commit is contained in:
parent
2734b674ce
commit
88c928cd5d
@ -1,5 +1,5 @@
|
|||||||
using AsbCloudApp.Data;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
using AsbCloudApp.Data.WellOperation;
|
||||||
|
|
||||||
namespace AsbCloudApp.Repositories
|
namespace AsbCloudApp.Repositories
|
||||||
{
|
{
|
||||||
|
@ -4,6 +4,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using AsbCloudApp.Data.WellOperation;
|
||||||
|
|
||||||
namespace AsbCloudApp.Repositories
|
namespace AsbCloudApp.Repositories
|
||||||
{
|
{
|
||||||
@ -18,22 +19,6 @@ namespace AsbCloudApp.Repositories
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IEnumerable<WellSectionTypeDto> GetSectionTypes();
|
IEnumerable<WellSectionTypeDto> GetSectionTypes();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// список плановых операций для сопоставления
|
|
||||||
/// <param name="idWell"></param>
|
|
||||||
/// <param name="currentDate"></param>
|
|
||||||
/// <param name="token"></param>
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task<WellOperationPlanDto> GetOperationsPlanAsync(int idWell, DateTime? currentDate, CancellationToken token);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// дата/время первой операции по скважине
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="idWell"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
DateTimeOffset? FirstOperationDate(int idWell);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить страницу списка операций
|
/// Получить страницу списка операций
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -42,14 +27,6 @@ namespace AsbCloudApp.Repositories
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<WellOperationDto>> GetAsync(WellOperationRequest request, CancellationToken token);
|
Task<IEnumerable<WellOperationDto>> GetAsync(WellOperationRequest request, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Получить список операций по запросу
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="request"></param>
|
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task<IEnumerable<WellOperationDataDto>> GetAsync(WellsOperationRequest request, CancellationToken token);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить страницу списка операций
|
/// Получить страницу списка операций
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -58,39 +35,30 @@ namespace AsbCloudApp.Repositories
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<PaginationContainer<WellOperationDto>> GetPageAsync(WellOperationRequest request, CancellationToken token);
|
Task<PaginationContainer<WellOperationDto>> GetPageAsync(WellOperationRequest request, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Получить операцию по id
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="id"></param>
|
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task<WellOperationDto?> GetOrDefaultAsync(int id, CancellationToken token);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить статистику операции по скважине с группировкой по категориям
|
/// Получить статистику операции по скважине с группировкой по категориям
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(
|
Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(WellOperationRequest request, CancellationToken token);
|
||||||
WellOperationRequest request,
|
|
||||||
CancellationToken token);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавить несколько операций за один раз
|
/// Добавить несколько операций
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="wellOperationDtos"></param>
|
/// <param name="dtos"></param>
|
||||||
|
/// <param name="deleteBeforeInsert"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> InsertRangeAsync(IEnumerable<WellOperationDto> wellOperationDtos, CancellationToken token);
|
Task<int> InsertRangeAsync(IEnumerable<WellOperationDto> dtos, bool deleteBeforeInsert, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Обновить существующую операцию
|
/// Обновить несколько существующую операций
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> UpdateAsync(WellOperationDto dto, CancellationToken token);
|
Task<int> UpdateRangeAsync(IEnumerable<WellOperationDto> dtos, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удалить операции по id
|
/// Удалить операции по id
|
||||||
@ -98,7 +66,7 @@ namespace AsbCloudApp.Repositories
|
|||||||
/// <param name="ids"></param>
|
/// <param name="ids"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token);
|
Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить секции скважин из операций ГГД. Секцие поделены на плановые и фактические.
|
/// Получить секции скважин из операций ГГД. Секцие поделены на плановые и фактические.
|
||||||
@ -116,23 +84,5 @@ namespace AsbCloudApp.Repositories
|
|||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken);
|
Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Удаление полных дубликатов операций по всем скважинам
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="onProgressCallback"></param>
|
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task<int> RemoveDuplicates(Action<string, double?> onProgressCallback, CancellationToken token);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Усечение пересекающейся последующей операции по дате и глубине забоя
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="geDate">Фильтр по дате. Если хоть одна операция попадет в в фильтр, то будет обработана вся скважина, а не только эта операция</param>
|
|
||||||
/// <param name="leDate">Фильтр по дате. Если хоть одна операция попадет в в фильтр, то будет обработана вся скважина, а не только эта операция</param>
|
|
||||||
/// <param name="onProgressCallback"></param>
|
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task<int> TrimOverlapping(DateTimeOffset? geDate, DateTimeOffset leDate, Action<string, double?> onProgressCallback, CancellationToken token);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,110 +3,50 @@ using System.Collections.Generic;
|
|||||||
|
|
||||||
namespace AsbCloudApp.Requests
|
namespace AsbCloudApp.Requests
|
||||||
{
|
{
|
||||||
/// <summary>
|
public class WellOperationRequest : RequestBase
|
||||||
/// параметры для запроса списка операций
|
|
||||||
/// </summary>
|
|
||||||
public class WellOperationRequestBase : RequestBase
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// фильтр по дате начала операции
|
/// Идентификаторы скважин
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime? GeDate { get; set; }
|
public IEnumerable<int>? IdsWell { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// фильтр по дате окончания операции
|
/// Больше или равно дате начала операции
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime? LtDate { get; set; }
|
public DateTimeOffset? GeDate { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// фильтр. Больше или равно глубины скважины на начало операции.
|
/// Меньше или равно дате окончания операции
|
||||||
|
/// </summary>
|
||||||
|
public DateTimeOffset? LeDate { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Больше или равно глубины скважины на начало операции.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public double? GeDepth { get; set; }
|
public double? GeDepth { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// фильтр. Меньше или равно глубины скважины на конец операции.
|
/// Меньше или равно глубины скважины на конец операции.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public double? LeDepth { get; set; }
|
public double? LeDepth { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// фильтр по списку id категорий операции
|
/// Идентификаторы категорий операции
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<int>? OperationCategoryIds { get; set; }
|
public IEnumerable<int>? OperationCategoryIds { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// фильтр по план = 0, факт = 1
|
/// Тип операций
|
||||||
|
/// <list type="bullet">
|
||||||
|
/// <item>0 - плановая операция</item>
|
||||||
|
/// <item>1 - фактическая операция</item>
|
||||||
|
/// </list>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int? OperationType { get; set; }
|
public int? OperationType { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// фильтр по списку id конструкций секции
|
/// Идентификаторы конструкций секции
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<int>? SectionTypeIds { get; set; }
|
public IEnumerable<int>? SectionTypeIds { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Параметры для запроса списка операций.
|
|
||||||
/// Базовый конструктор
|
|
||||||
/// </summary>
|
|
||||||
public WellOperationRequestBase()
|
|
||||||
{ }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Параметры для запроса списка операций.
|
|
||||||
/// Копирующий конструктор
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="request"></param>
|
|
||||||
public WellOperationRequestBase(WellOperationRequestBase request)
|
|
||||||
{
|
|
||||||
GeDepth = request.GeDepth;
|
|
||||||
LeDepth = request.LeDepth;
|
|
||||||
GeDate = request.GeDate;
|
|
||||||
LtDate = request.LtDate;
|
|
||||||
|
|
||||||
OperationCategoryIds = request.OperationCategoryIds;
|
|
||||||
OperationType = request.OperationType;
|
|
||||||
SectionTypeIds = request.SectionTypeIds;
|
|
||||||
|
|
||||||
Skip = request.Skip;
|
|
||||||
Take = request.Take;
|
|
||||||
SortFields = request.SortFields;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Параметры для запроса списка операций (с id скважины)
|
|
||||||
/// </summary>
|
|
||||||
public class WellOperationRequest : WellOperationRequestBase
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// id скважины
|
|
||||||
/// </summary>
|
|
||||||
public int IdWell { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ctor
|
|
||||||
/// </summary>
|
|
||||||
public WellOperationRequest() { }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// копирующий конструктор
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="request"></param>
|
|
||||||
/// <param name="idWell"></param>
|
|
||||||
public WellOperationRequest(WellOperationRequestBase request, int idWell)
|
|
||||||
:base(request)
|
|
||||||
{
|
|
||||||
IdWell = idWell;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Параметры для запроса списка операций (с массивом id скважин)
|
|
||||||
/// </summary>
|
|
||||||
public class WellsOperationRequest : WellOperationRequestBase
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// ids скважин
|
|
||||||
/// </summary>
|
|
||||||
public IEnumerable<int> IdsWell { get; set; } = null!;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,10 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using AsbCloudApp.Data;
|
|
||||||
using AsbCloudApp.Data.DetectedOperation;
|
using AsbCloudApp.Data.DetectedOperation;
|
||||||
using AsbCloudApp.Requests;
|
using AsbCloudApp.Requests;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using AsbCloudApp.Data.WellOperation;
|
||||||
|
|
||||||
namespace AsbCloudApp.Services
|
namespace AsbCloudApp.Services
|
||||||
{
|
{
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using AsbCloudApp.Data.WellOperation;
|
||||||
|
|
||||||
namespace AsbCloudApp.Services
|
namespace AsbCloudApp.Services
|
||||||
{
|
{
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
using AsbCloudApp.Data;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using AsbCloudApp.Data.WellOperation;
|
||||||
|
|
||||||
namespace AsbCloudApp.Services
|
namespace AsbCloudApp.Services
|
||||||
{
|
{
|
||||||
@ -16,6 +16,6 @@ namespace AsbCloudApp.Services
|
|||||||
/// <param name="idsWells"></param>
|
/// <param name="idsWells"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<List<Dictionary<int, WellOperationDataDto>>> GetAsync(IEnumerable<int> idsWells, CancellationToken token);
|
Task<IEnumerable<Dictionary<int, WellOperationDto>>> GetAsync(IEnumerable<int> idsWells, CancellationToken token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,6 +115,26 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
return entry.Entity.Id;
|
return entry.Entity.Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task<int> UpdateRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (!dtos.Any())
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var ids = dtos.Select(d => d.Id);
|
||||||
|
|
||||||
|
var countExistingEntities = await dbSet
|
||||||
|
.Where(d => ids.Contains(d.Id))
|
||||||
|
.CountAsync(token);
|
||||||
|
|
||||||
|
if (ids.Count() > countExistingEntities)
|
||||||
|
return ICrudRepository<TDto>.ErrorIdNotFound;
|
||||||
|
|
||||||
|
var entities = dtos.Select(Convert);
|
||||||
|
dbContext.Set<TEntity>().UpdateRange(entities);
|
||||||
|
|
||||||
|
return await dbContext.SaveChangesAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public virtual Task<int> DeleteAsync(int id, CancellationToken token)
|
public virtual Task<int> DeleteAsync(int id, CancellationToken token)
|
||||||
{
|
{
|
||||||
@ -129,6 +149,14 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
return affected;
|
return affected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token)
|
||||||
|
{
|
||||||
|
var query = dbContext.Set<TEntity>().Where(e => ids.Contains(e.Id));
|
||||||
|
dbContext.Set<TEntity>().RemoveRange(query);
|
||||||
|
|
||||||
|
return await dbContext.SaveChangesAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
|
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
|
||||||
|
|
||||||
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
|
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Repositories;
|
|
||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using AsbCloudApp.Data.WellOperation;
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Repository;
|
namespace AsbCloudInfrastructure.Repository;
|
||||||
|
|
||||||
|
@ -1,4 +1,11 @@
|
|||||||
using AsbCloudApp.Data;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Data.WellOperation;
|
||||||
|
using AsbCloudApp.Exceptions;
|
||||||
using AsbCloudApp.Repositories;
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Requests;
|
using AsbCloudApp.Requests;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
@ -7,102 +14,246 @@ 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.ComponentModel.DataAnnotations;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Repository;
|
namespace AsbCloudInfrastructure.Repository;
|
||||||
|
|
||||||
/// <summary>
|
public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, WellOperation>,
|
||||||
/// репозиторий операций по скважине
|
IWellOperationRepository
|
||||||
/// </summary>
|
|
||||||
public class WellOperationRepository : IWellOperationRepository
|
|
||||||
{
|
{
|
||||||
private const string KeyCacheSections = "OperationsBySectionSummarties";
|
|
||||||
|
|
||||||
private readonly IAsbCloudDbContext db;
|
|
||||||
private readonly IMemoryCache memoryCache;
|
private readonly IMemoryCache memoryCache;
|
||||||
private readonly IWellService wellService;
|
|
||||||
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
|
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
|
||||||
|
private readonly IWellService wellService;
|
||||||
|
|
||||||
public WellOperationRepository(IAsbCloudDbContext db, IMemoryCache memoryCache, IWellService wellService, IWellOperationCategoryRepository wellOperationCategoryRepository)
|
public WellOperationRepository(IAsbCloudDbContext context,
|
||||||
|
IMemoryCache memoryCache,
|
||||||
|
IWellOperationCategoryRepository wellOperationCategoryRepository,
|
||||||
|
IWellService wellService)
|
||||||
|
: base(context, dbSet => dbSet.Include(e => e.WellSectionType)
|
||||||
|
.Include(e => e.OperationCategory))
|
||||||
{
|
{
|
||||||
this.db = db;
|
|
||||||
this.memoryCache = memoryCache;
|
this.memoryCache = memoryCache;
|
||||||
this.wellService = wellService;
|
|
||||||
this.wellOperationCategoryRepository = wellOperationCategoryRepository;
|
this.wellOperationCategoryRepository = wellOperationCategoryRepository;
|
||||||
|
this.wellService = wellService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<WellSectionTypeDto> GetSectionTypes() =>
|
public IEnumerable<WellSectionTypeDto> GetSectionTypes() =>
|
||||||
memoryCache
|
memoryCache
|
||||||
.GetOrCreateBasic(db.Set<WellSectionType>())
|
.GetOrCreateBasic(dbContext.WellSectionTypes)
|
||||||
.OrderBy(s => s.Order)
|
.OrderBy(s => s.Order)
|
||||||
.Select(s => s.Adapt<WellSectionTypeDto>());
|
.Select(s => s.Adapt<WellSectionTypeDto>());
|
||||||
|
|
||||||
public async Task<WellOperationPlanDto> GetOperationsPlanAsync(int idWell, DateTime? currentDate, CancellationToken token)
|
public async Task<IEnumerable<WellOperationDto>> GetAsync(WellOperationRequest request, CancellationToken token)
|
||||||
{
|
{
|
||||||
var timezone = wellService.GetTimezone(idWell);
|
var entities = await BuildQuery(request)
|
||||||
var request = new WellOperationRequest()
|
|
||||||
{
|
|
||||||
IdWell = idWell,
|
|
||||||
OperationType = WellOperation.IdOperationTypePlan,
|
|
||||||
};
|
|
||||||
|
|
||||||
var dtos = await BuildQuery(request)
|
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
.ToArrayAsync(token);
|
.ToArrayAsync(token);
|
||||||
|
|
||||||
var dateLastAssosiatedPlanOperation = await GetDateLastAssosiatedPlanOperationAsync(idWell, currentDate, timezone.Hours, token);
|
var dtos = entities.Select(Convert);
|
||||||
|
|
||||||
var result = new WellOperationPlanDto()
|
return dtos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PaginationContainer<WellOperationDto>> GetPageAsync(WellOperationRequest request, CancellationToken token)
|
||||||
{
|
{
|
||||||
WellOperationsPlan = dtos.Select(Convert),
|
var skip = request.Skip ?? 0;
|
||||||
DateLastAssosiatedPlanOperation = dateLastAssosiatedPlanOperation
|
var take = request.Take ?? 32;
|
||||||
|
|
||||||
|
var query = BuildQuery(request);
|
||||||
|
|
||||||
|
var entites = await query.Skip(skip)
|
||||||
|
.Take(take)
|
||||||
|
.AsNoTracking()
|
||||||
|
.ToArrayAsync(token);
|
||||||
|
|
||||||
|
var paginationContainer = new PaginationContainer<WellOperationDto>
|
||||||
|
{
|
||||||
|
Skip = skip,
|
||||||
|
Take = take,
|
||||||
|
Count = await query.CountAsync(token),
|
||||||
|
Items = entites.Select(Convert)
|
||||||
};
|
};
|
||||||
|
|
||||||
return result;
|
return paginationContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<DateTime?> GetDateLastAssosiatedPlanOperationAsync(
|
public async Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(WellOperationRequest request, CancellationToken token)
|
||||||
int idWell,
|
{
|
||||||
DateTime? lessThenDate,
|
var query = BuildQuery(request);
|
||||||
double timeZoneHours,
|
var entities = await query
|
||||||
|
.Select(o => new
|
||||||
|
{
|
||||||
|
o.IdCategory,
|
||||||
|
DurationMinutes = o.DurationHours * 60,
|
||||||
|
DurationDepth = o.DepthEnd - o.DepthStart
|
||||||
|
})
|
||||||
|
.ToArrayAsync(token);
|
||||||
|
|
||||||
|
var parentRelationDictionary = wellOperationCategoryRepository.Get(true)
|
||||||
|
.ToDictionary(c => c.Id, c => new
|
||||||
|
{
|
||||||
|
c.Name,
|
||||||
|
c.IdParent
|
||||||
|
});
|
||||||
|
|
||||||
|
var dtos = entities
|
||||||
|
.GroupBy(o => o.IdCategory)
|
||||||
|
.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))
|
||||||
|
{
|
||||||
|
dtos = dtos
|
||||||
|
.GroupBy(o => o.IdParent!)
|
||||||
|
.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> InsertRangeAsync(IEnumerable<WellOperationDto> dtos,
|
||||||
|
bool deleteBeforeInsert,
|
||||||
CancellationToken token)
|
CancellationToken token)
|
||||||
{
|
{
|
||||||
if (lessThenDate is null)
|
EnsureValidWellOperations(dtos);
|
||||||
return null;
|
|
||||||
|
|
||||||
var currentDateOffset = lessThenDate.Value.ToUtcDateTimeOffset(timeZoneHours);
|
if (!deleteBeforeInsert)
|
||||||
var timeZoneOffset = TimeSpan.FromHours(timeZoneHours);
|
return await InsertRangeAsync(dtos, token);
|
||||||
|
|
||||||
var lastFactOperation = await db.WellOperations
|
var idType = dtos.First().IdType;
|
||||||
.Where(o => o.IdWell == idWell)
|
var idWell = dtos.First().IdWell;
|
||||||
.Where(o => o.IdType == WellOperation.IdOperationTypeFact)
|
|
||||||
.Where(o => o.IdPlan != null)
|
|
||||||
.Where(o => o.DateStart < currentDateOffset)
|
|
||||||
.Include(x => x.OperationPlan)
|
|
||||||
.OrderByDescending(x => x.DateStart)
|
|
||||||
.FirstOrDefaultAsync(token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (lastFactOperation is not null)
|
var existingOperationIds = await dbContext.WellOperations
|
||||||
return DateTime.SpecifyKind(lastFactOperation.OperationPlan!.DateStart.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified);
|
.Where(e => e.IdWell == idWell && e.IdType == idType)
|
||||||
|
.Select(e => e.Id)
|
||||||
|
.ToArrayAsync(token);
|
||||||
|
|
||||||
return null;
|
await DeleteRangeAsync(existingOperationIds, token);
|
||||||
|
|
||||||
|
return await InsertRangeAsync(dtos, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Task<int> UpdateRangeAsync(IEnumerable<WellOperationDto> dtos, CancellationToken token)
|
||||||
|
{
|
||||||
|
EnsureValidWellOperations(dtos);
|
||||||
|
|
||||||
|
return base.UpdateRangeAsync(dtos, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void EnsureValidWellOperations(IEnumerable<WellOperationDto> dtos)
|
||||||
|
{
|
||||||
|
if (dtos.GroupBy(d => d.IdType).Count() > 1)
|
||||||
|
throw new ArgumentInvalidException(nameof(dtos), "Все операции должны быть одного типа");
|
||||||
|
|
||||||
|
if (dtos.GroupBy(d => d.IdType).Count() > 1)
|
||||||
|
throw new ArgumentInvalidException(nameof(dtos), "Все операции должны принадлежать одной скважине");
|
||||||
|
}
|
||||||
|
|
||||||
|
private IQueryable<WellOperation> BuildQuery(WellOperationRequest request)
|
||||||
|
{
|
||||||
|
var currentWellOperations = GetQuery()
|
||||||
|
.Where(e => request.IdsWell != null && request.IdsWell.Contains(e.Id));
|
||||||
|
|
||||||
|
var query = GetQuery()
|
||||||
|
.Where(e => request.IdsWell != null && request.IdsWell.Contains(e.Id))
|
||||||
|
.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
|
||||||
|
});
|
||||||
|
|
||||||
|
if (request.OperationType.HasValue)
|
||||||
|
query = query.Where(e => e.IdType == request.OperationType.Value);
|
||||||
|
|
||||||
|
if (request.SectionTypeIds?.Any() is true)
|
||||||
|
query = query.Where(e => request.SectionTypeIds.Contains(e.IdWellSectionType));
|
||||||
|
|
||||||
|
if (request.OperationCategoryIds?.Any() is true)
|
||||||
|
query = query.Where(e => request.OperationCategoryIds.Contains(e.IdCategory));
|
||||||
|
|
||||||
|
if (request.GeDepth.HasValue)
|
||||||
|
query = query.Where(e => e.DepthEnd >= request.GeDepth.Value);
|
||||||
|
|
||||||
|
if (request.LeDepth.HasValue)
|
||||||
|
query = query.Where(e => e.DepthEnd <= request.LeDepth.Value);
|
||||||
|
|
||||||
|
if (request.GeDate.HasValue)
|
||||||
|
{
|
||||||
|
var geDateUtc = request.GeDate.Value.UtcDateTime;
|
||||||
|
query = query.Where(e => e.DateStart >= geDateUtc);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.LeDate.HasValue)
|
||||||
|
{
|
||||||
|
var leDateUtc = request.LeDate.Value.UtcDateTime;
|
||||||
|
query = query.Where(e => e.DateStart <= leDateUtc);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.SortFields?.Any() is true)
|
||||||
|
query = query.SortBy(request.SortFields);
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
|
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
|
||||||
{
|
{
|
||||||
var cache = await memoryCache.GetOrCreateAsync(KeyCacheSections, async (entry) =>
|
const string keyCacheSections = "OperationsBySectionSummarties";
|
||||||
|
|
||||||
|
var cache = await memoryCache.GetOrCreateAsync(keyCacheSections, async (entry) =>
|
||||||
{
|
{
|
||||||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
|
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
|
||||||
|
|
||||||
var query = db.Set<WellOperation>()
|
var query = dbContext.Set<WellOperation>()
|
||||||
.GroupBy(operation => new
|
.GroupBy(operation => new
|
||||||
{
|
{
|
||||||
operation.IdWell,
|
operation.IdWell,
|
||||||
@ -165,9 +316,7 @@ public class WellOperationRepository : IWellOperationRepository
|
|||||||
|
|
||||||
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)
|
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var timezone = wellService.GetTimezone(idWell);
|
var query = dbContext.WellOperations.Where(o => o.IdWell == idWell && o.IdType == idType);
|
||||||
|
|
||||||
var query = db.WellOperations.Where(o => o.IdWell == idWell && o.IdType == idType);
|
|
||||||
|
|
||||||
if (!await query.AnyAsync(cancellationToken))
|
if (!await query.AnyAsync(cancellationToken))
|
||||||
return null;
|
return null;
|
||||||
@ -177,523 +326,28 @@ public class WellOperationRepository : IWellOperationRepository
|
|||||||
|
|
||||||
return new DatesRangeDto
|
return new DatesRangeDto
|
||||||
{
|
{
|
||||||
From = minDate.ToRemoteDateTime(timezone.Hours),
|
From = minDate.ToOffset(minDate.Offset),
|
||||||
To = maxDate.ToRemoteDateTime(timezone.Hours)
|
To = maxDate.ToOffset(minDate.Offset)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
protected override WellOperation Convert(WellOperationDto src)
|
||||||
public DateTimeOffset? FirstOperationDate(int idWell)
|
|
||||||
{
|
{
|
||||||
var sections = GetSectionsAsync(new[] { idWell }, CancellationToken.None).Result;
|
var entity = src.Adapt<WellOperation>();
|
||||||
var first = sections.FirstOrDefault(section => section.IdType == WellOperation.IdOperationTypeFact)
|
entity.LastUpdateDate = src.LastUpdateDate?.UtcDateTime;
|
||||||
?? sections.FirstOrDefault(section => section.IdType == WellOperation.IdOperationTypePlan);
|
entity.DateStart = src.DateStart.UtcDateTime;
|
||||||
|
return entity;
|
||||||
return first?.DateStart;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
protected override WellOperationDto Convert(WellOperation src)
|
||||||
public async Task<IEnumerable<WellOperationDto>> GetAsync(
|
|
||||||
WellOperationRequest request,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
{
|
||||||
var query = BuildQuery(request)
|
//TODO: пока такое получение TimeZone скважины, нужно исправить на Lazy
|
||||||
.AsNoTracking();
|
//Хоть мы и тянем данные из кэша, но от получения TimeZone в этом методе нужно избавиться, пока так
|
||||||
|
var timeZoneOffset = wellService.GetTimezone(src.IdWell).Offset;
|
||||||
|
|
||||||
var dtos = await query.ToArrayAsync(token);
|
var dto = src.Adapt<WellOperationDto>();
|
||||||
|
dto.DateStart = src.DateStart.ToOffset(timeZoneOffset);
|
||||||
return dtos.Select(Convert);
|
dto.LastUpdateDate = src.LastUpdateDate?.ToOffset(timeZoneOffset);
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IEnumerable<WellOperationDataDto>> GetAsync(
|
|
||||||
WellsOperationRequest request,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
var query = BuildQuery(request)
|
|
||||||
.AsNoTracking();
|
|
||||||
|
|
||||||
var dtos = await query.ToArrayAsync(token);
|
|
||||||
return dtos;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<PaginationContainer<WellOperationDto>> GetPageAsync(
|
|
||||||
WellOperationRequest request,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
var query = BuildQuery(request);
|
|
||||||
|
|
||||||
var result = new PaginationContainer<WellOperationDto>
|
|
||||||
{
|
|
||||||
Skip = request.Skip ?? 0,
|
|
||||||
Take = request.Take ?? 32,
|
|
||||||
Count = await query.CountAsync(token),
|
|
||||||
};
|
|
||||||
|
|
||||||
var dtos = await query.ToArrayAsync(token);
|
|
||||||
|
|
||||||
result.Items = dtos.Select(Convert);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<WellOperationDto?> GetOrDefaultAsync(int id,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
var entity = await db.WellOperations
|
|
||||||
.Include(s => s.WellSectionType)
|
|
||||||
.Include(s => s.OperationCategory)
|
|
||||||
.FirstOrDefaultAsync(e => e.Id == id, token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (entity is null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var timezone = wellService.GetTimezone(entity.IdWell);
|
|
||||||
|
|
||||||
var dto = entity.Adapt<WellOperationDto>();
|
|
||||||
dto.WellSectionTypeName = entity.WellSectionType.Caption;
|
|
||||||
dto.DateStart = entity.DateStart.ToRemoteDateTime(timezone.Hours);
|
|
||||||
dto.CategoryName = entity.OperationCategory.Name;
|
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(
|
|
||||||
WellOperationRequest request,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
// TODO: Rename controller method
|
|
||||||
request.OperationType = WellOperation.IdOperationTypeFact;
|
|
||||||
var query = BuildQuery(request);
|
|
||||||
var entities = await query
|
|
||||||
.Select(o => new
|
|
||||||
{
|
|
||||||
o.IdCategory,
|
|
||||||
DurationMinutes = o.DurationHours * 60,
|
|
||||||
DurationDepth = o.DepthEnd - o.DepthStart
|
|
||||||
})
|
|
||||||
.ToListAsync(token);
|
|
||||||
var parentRelationDictionary = wellOperationCategoryRepository.Get(true)
|
|
||||||
.ToDictionary(c => c.Id, c => new
|
|
||||||
{
|
|
||||||
c.Name,
|
|
||||||
c.IdParent
|
|
||||||
});
|
|
||||||
|
|
||||||
var dtos = entities
|
|
||||||
.GroupBy(o => o.IdCategory)
|
|
||||||
.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))
|
|
||||||
{
|
|
||||||
dtos = dtos
|
|
||||||
.GroupBy(o => o.IdParent!)
|
|
||||||
.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<int> InsertRangeAsync(
|
|
||||||
IEnumerable<WellOperationDto> wellOperationDtos,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
var firstOperation = wellOperationDtos
|
|
||||||
.FirstOrDefault();
|
|
||||||
if (firstOperation is null)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
var idWell = firstOperation.IdWell;
|
|
||||||
|
|
||||||
var timezone = wellService.GetTimezone(idWell);
|
|
||||||
foreach (var dto in wellOperationDtos)
|
|
||||||
{
|
|
||||||
var entity = dto.Adapt<WellOperation>();
|
|
||||||
entity.Id = default;
|
|
||||||
entity.DateStart = dto.DateStart.DateTime.ToUtcDateTimeOffset(timezone.Hours);
|
|
||||||
entity.IdWell = idWell;
|
|
||||||
entity.LastUpdateDate = DateTimeOffset.UtcNow;
|
|
||||||
db.WellOperations.Add(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = await db.SaveChangesAsync(token);
|
|
||||||
if (result > 0)
|
|
||||||
memoryCache.Remove(KeyCacheSections);
|
|
||||||
return result;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<int> UpdateAsync(
|
|
||||||
WellOperationDto dto, CancellationToken token)
|
|
||||||
{
|
|
||||||
var timezone = wellService.GetTimezone(dto.IdWell);
|
|
||||||
var entity = dto.Adapt<WellOperation>();
|
|
||||||
entity.DateStart = dto.DateStart.DateTime.ToUtcDateTimeOffset(timezone.Hours);
|
|
||||||
entity.LastUpdateDate = DateTimeOffset.UtcNow;
|
|
||||||
db.WellOperations.Update(entity);
|
|
||||||
|
|
||||||
var result = await db.SaveChangesAsync(token);
|
|
||||||
if (result > 0)
|
|
||||||
memoryCache.Remove(KeyCacheSections);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public async Task<int> DeleteAsync(IEnumerable<int> ids,
|
|
||||||
CancellationToken token)
|
|
||||||
{
|
|
||||||
var query = db.WellOperations.Where(e => ids.Contains(e.Id));
|
|
||||||
db.WellOperations.RemoveRange(query);
|
|
||||||
|
|
||||||
var result = await db.SaveChangesAsync(token);
|
|
||||||
if (result > 0)
|
|
||||||
memoryCache.Remove(KeyCacheSections);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// В результате попрежнему требуется конвертировать дату
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="request"></param>
|
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private IQueryable<WellOperationDto> BuildQuery(WellOperationRequest request)
|
|
||||||
{
|
|
||||||
var timezone = wellService.GetTimezone(request.IdWell);
|
|
||||||
var timeZoneOffset = timezone.Hours;
|
|
||||||
|
|
||||||
var query = db.WellOperations
|
|
||||||
.Include(s => s.WellSectionType)
|
|
||||||
.Include(s => s.OperationCategory)
|
|
||||||
.Where(o => o.IdWell == request.IdWell);
|
|
||||||
|
|
||||||
if (request.OperationType.HasValue)
|
|
||||||
query = query.Where(e => e.IdType == request.OperationType.Value);
|
|
||||||
|
|
||||||
if (request.SectionTypeIds?.Any() == true)
|
|
||||||
query = query.Where(e => request.SectionTypeIds.Contains(e.IdWellSectionType));
|
|
||||||
|
|
||||||
if (request.OperationCategoryIds?.Any() == true)
|
|
||||||
query = query.Where(e => request.OperationCategoryIds.Contains(e.IdCategory));
|
|
||||||
|
|
||||||
if (request.GeDepth.HasValue)
|
|
||||||
query = query.Where(e => e.DepthEnd >= request.GeDepth.Value);
|
|
||||||
|
|
||||||
if (request.LeDepth.HasValue)
|
|
||||||
query = query.Where(e => e.DepthEnd <= request.LeDepth.Value);
|
|
||||||
|
|
||||||
if (request.GeDate.HasValue)
|
|
||||||
{
|
|
||||||
var geDateOffset = request.GeDate.Value.ToUtcDateTimeOffset(timeZoneOffset);
|
|
||||||
query = query.Where(e => e.DateStart >= geDateOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.LtDate.HasValue)
|
|
||||||
{
|
|
||||||
var ltDateOffset = request.LtDate.Value.ToUtcDateTimeOffset(timeZoneOffset);
|
|
||||||
query = query.Where(e => e.DateStart < ltDateOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentWellOperations = db.WellOperations
|
|
||||||
.Where(subOp => subOp.IdWell == request.IdWell);
|
|
||||||
|
|
||||||
var wellOperationsWithCategoryNpt = currentWellOperations
|
|
||||||
.Where(subOp => subOp.IdType == 1)
|
|
||||||
.Where(subOp => WellOperationCategory.NonProductiveTimeSubIds.Contains(subOp.IdCategory));
|
|
||||||
|
|
||||||
// TODO: Вынести query.Select из метода BuildQuery
|
|
||||||
var dtos = query.Select(o => new WellOperationDto
|
|
||||||
{
|
|
||||||
Id = o.Id,
|
|
||||||
IdPlan = o.IdPlan,
|
|
||||||
IdType = o.IdType,
|
|
||||||
IdWell = o.IdWell,
|
|
||||||
IdWellSectionType = o.IdWellSectionType,
|
|
||||||
IdCategory = o.IdCategory,
|
|
||||||
IdParentCategory = o.OperationCategory.IdParent,
|
|
||||||
|
|
||||||
CategoryName = o.OperationCategory.Name,
|
|
||||||
WellSectionTypeName = o.WellSectionType.Caption,
|
|
||||||
DateStart = o.DateStart,
|
|
||||||
DepthStart = o.DepthStart,
|
|
||||||
DepthEnd = o.DepthEnd,
|
|
||||||
DurationHours = o.DurationHours,
|
|
||||||
CategoryInfo = o.CategoryInfo,
|
|
||||||
Comment = o.Comment,
|
|
||||||
|
|
||||||
NptHours = wellOperationsWithCategoryNpt
|
|
||||||
.Where(subOp => subOp.DateStart <= o.DateStart)
|
|
||||||
.Select(subOp => subOp.DurationHours)
|
|
||||||
.Sum(),
|
|
||||||
|
|
||||||
Day = (o.DateStart - currentWellOperations
|
|
||||||
.Where(subOp => subOp.IdType == o.IdType)
|
|
||||||
.Where(subOp => subOp.DateStart <= o.DateStart)
|
|
||||||
.Min(subOp => subOp.DateStart))
|
|
||||||
.TotalDays,
|
|
||||||
IdUser = o.IdUser,
|
|
||||||
LastUpdateDate = o.LastUpdateDate,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (request.SortFields?.Any() == true)
|
|
||||||
{
|
|
||||||
dtos = dtos.SortBy(request.SortFields);
|
|
||||||
}
|
|
||||||
|
|
||||||
dtos = dtos
|
|
||||||
.OrderBy(e => e.DateStart)
|
|
||||||
.ThenBy(e => e.DepthEnd)
|
|
||||||
.ThenBy(e => e.Id);
|
|
||||||
|
|
||||||
if (request.Skip.HasValue)
|
|
||||||
dtos = dtos.Skip(request.Skip.Value);
|
|
||||||
|
|
||||||
if (request.Take.HasValue)
|
|
||||||
dtos = dtos.Take(request.Take.Value);
|
|
||||||
|
|
||||||
return dtos.AsNoTracking();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Получение данных по запросу
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="request"></param>
|
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private IQueryable<WellOperationDataDto> BuildQuery(WellsOperationRequest request)
|
|
||||||
{
|
|
||||||
var query = db.WellOperations
|
|
||||||
.Where(o => request.IdsWell.Contains(o.IdWell))
|
|
||||||
.Where(o => request.OperationType == o.IdType);
|
|
||||||
|
|
||||||
if (request.SectionTypeIds?.Any() == true)
|
|
||||||
query = query.Where(o => request.SectionTypeIds.Contains(o.IdWellSectionType));
|
|
||||||
|
|
||||||
if (request.OperationCategoryIds?.Any() == true)
|
|
||||||
query = query.Where(o => request.OperationCategoryIds.Contains(o.IdCategory));
|
|
||||||
|
|
||||||
// TODO: Вынести query.Select из метода BuildQuery
|
|
||||||
var dtos = query.Select(o => new WellOperationDataDto
|
|
||||||
{
|
|
||||||
DepthStart = o.DepthStart,
|
|
||||||
DurationHours = o.DurationHours,
|
|
||||||
IdCategory = o.IdCategory,
|
|
||||||
IdWell = o.IdWell,
|
|
||||||
IdWellSectionType = o.IdWellSectionType,
|
|
||||||
OperationCategoryName = o.OperationCategory.Name,
|
|
||||||
WellSectionTypeCaption = o.WellSectionType.Caption,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (request.SortFields?.Any() == true)
|
|
||||||
{
|
|
||||||
dtos = dtos.SortBy(request.SortFields);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.Skip.HasValue)
|
|
||||||
dtos = dtos.Skip(request.Skip.Value);
|
|
||||||
|
|
||||||
if (request.Take.HasValue)
|
|
||||||
dtos = dtos.Take(request.Take.Value);
|
|
||||||
|
|
||||||
return dtos.AsNoTracking();
|
|
||||||
}
|
|
||||||
|
|
||||||
private WellOperationDto Convert(WellOperationDto dto)
|
|
||||||
{
|
|
||||||
var timezone = wellService.GetTimezone(dto.IdWell);
|
|
||||||
var timezoneOffset = TimeSpan.FromHours(timezone.Hours);
|
|
||||||
|
|
||||||
var dtoWithRemoteDateTime = dto.Adapt<WellOperationDto>();
|
|
||||||
|
|
||||||
dtoWithRemoteDateTime.DateStart = dto.DateStart.ToOffset(TimeSpan.FromHours(timezoneOffset.Hours));
|
|
||||||
dtoWithRemoteDateTime.LastUpdateDate = dto.LastUpdateDate?.ToOffset(TimeSpan.FromHours(timezoneOffset.Hours));
|
|
||||||
|
|
||||||
return dtoWithRemoteDateTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int> RemoveDuplicates(Action<string, double?> onProgressCallback, CancellationToken token)
|
|
||||||
{
|
|
||||||
IQueryable<WellOperation> dbset = db.Set<WellOperation>();
|
|
||||||
var query = dbset
|
|
||||||
.GroupBy(o => new { o.IdWell, o.IdType })
|
|
||||||
.Select(g => new { g.Key.IdWell, g.Key.IdType });
|
|
||||||
|
|
||||||
var groups = await query
|
|
||||||
.ToArrayAsync(token);
|
|
||||||
|
|
||||||
var count = groups.Count();
|
|
||||||
var i = 0;
|
|
||||||
var totalRemoved = 0;
|
|
||||||
var total = 0;
|
|
||||||
foreach (var group in groups)
|
|
||||||
{
|
|
||||||
var result = await RemoveDuplicatesInGroup(group.IdWell, group.IdType, token);
|
|
||||||
totalRemoved += result.removed;
|
|
||||||
total += result.total;
|
|
||||||
var percent = i++ / count;
|
|
||||||
var message = $"RemoveDuplicates [{i} of {count}] wellId: {group.IdWell}, opType: {group.IdType}, affected: {result.removed} of {result.total}";
|
|
||||||
onProgressCallback?.Invoke(message, percent);
|
|
||||||
Trace.TraceInformation(message);
|
|
||||||
}
|
|
||||||
var messageDone = $"RemoveDuplicates done [{i} of {count}] totalAffected: {totalRemoved} of {total}";
|
|
||||||
Trace.TraceInformation(messageDone);
|
|
||||||
onProgressCallback?.Invoke(messageDone, 1);
|
|
||||||
return totalRemoved;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<(int removed, int total)> RemoveDuplicatesInGroup(int idWell, int idType, CancellationToken token)
|
|
||||||
{
|
|
||||||
var dbset = db.Set<WellOperation>();
|
|
||||||
var entities = await dbset
|
|
||||||
.Where(o => o.IdWell == idWell && o.IdType == idType)
|
|
||||||
.OrderBy(o => o.DateStart)
|
|
||||||
.ToListAsync(token);
|
|
||||||
|
|
||||||
using var entitiesEnumerator = entities.GetEnumerator();
|
|
||||||
|
|
||||||
if (!entitiesEnumerator.MoveNext())
|
|
||||||
return (0, 0);
|
|
||||||
|
|
||||||
var preEntity = entitiesEnumerator.Current;
|
|
||||||
while (entitiesEnumerator.MoveNext())
|
|
||||||
{
|
|
||||||
var entity = entitiesEnumerator.Current;
|
|
||||||
if (preEntity.IsSame(entity))
|
|
||||||
dbset.Remove(entity);
|
|
||||||
else
|
|
||||||
preEntity = entity;
|
|
||||||
}
|
|
||||||
var removed = await db.SaveChangesAsync(token);
|
|
||||||
return (removed, entities.Count);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int> TrimOverlapping(DateTimeOffset? geDate, DateTimeOffset leDate, Action<string, double?> onProgressCallback, CancellationToken token)
|
|
||||||
{
|
|
||||||
var leDateUtc = leDate.ToUniversalTime();
|
|
||||||
IQueryable<WellOperation> query = db.Set<WellOperation>();
|
|
||||||
if (geDate.HasValue)
|
|
||||||
{
|
|
||||||
var geDateUtc = geDate.Value.ToUniversalTime();
|
|
||||||
query = query.Where(e => e.DateStart >= geDateUtc);
|
|
||||||
}
|
|
||||||
|
|
||||||
var groups = await query
|
|
||||||
.GroupBy(o => new { o.IdWell, o.IdType })
|
|
||||||
.Select(g => new{
|
|
||||||
MaxDate = g.Max(o => o.DateStart),
|
|
||||||
g.Key.IdWell,
|
|
||||||
g.Key.IdType,
|
|
||||||
})
|
|
||||||
.Where(g => g.MaxDate <= leDateUtc)
|
|
||||||
.ToArrayAsync(token);
|
|
||||||
|
|
||||||
var count = groups.Count();
|
|
||||||
var i = 0;
|
|
||||||
(int takeover, int trimmed,int total) totalResult = (0, 0, 0);
|
|
||||||
foreach (var group in groups)
|
|
||||||
{
|
|
||||||
var result = await TrimOverlapping(group.IdWell, group.IdType, token);
|
|
||||||
totalResult.takeover += result.takeover;
|
|
||||||
totalResult.trimmed += result.trimmed;
|
|
||||||
totalResult.total += result.total;
|
|
||||||
var percent = i++ / count;
|
|
||||||
var message = $"TrimOverlapping [{i} of {count}] wellId: {group.IdWell}, opType: {group.IdType}, takeover:{result.takeover}, trimmed:{result.trimmed}, of {result.total}";
|
|
||||||
onProgressCallback?.Invoke(message, percent);
|
|
||||||
Trace.TraceInformation(message);
|
|
||||||
}
|
|
||||||
var messageDone = $"TrimOverlapping done [{i} of {count}] total takeover:{totalResult.takeover}, total trimmed:{totalResult.trimmed} of {totalResult.total}";
|
|
||||||
Trace.TraceInformation(messageDone);
|
|
||||||
onProgressCallback?.Invoke(messageDone, 1);
|
|
||||||
return totalResult.takeover + totalResult.trimmed;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<(int takeover, int trimmed, int total)> TrimOverlapping(int idWell, int idType, CancellationToken token)
|
|
||||||
{
|
|
||||||
var dbset = db.Set<WellOperation>();
|
|
||||||
var query = dbset
|
|
||||||
.Where(o => o.IdWell == idWell)
|
|
||||||
.Where(o => o.IdType == idType)
|
|
||||||
.OrderBy(o => o.DateStart)
|
|
||||||
.ThenBy(o => o.DepthStart);
|
|
||||||
|
|
||||||
var entities = await query
|
|
||||||
.ToListAsync(token);
|
|
||||||
|
|
||||||
using var entitiesEnumerator = entities.GetEnumerator();
|
|
||||||
|
|
||||||
if (!entitiesEnumerator.MoveNext())
|
|
||||||
return (0, 0, 0);
|
|
||||||
|
|
||||||
int takeover = 0;
|
|
||||||
int trimmed = 0;
|
|
||||||
var preEntity = entitiesEnumerator.Current;
|
|
||||||
while (entitiesEnumerator.MoveNext())
|
|
||||||
{
|
|
||||||
var entity = entitiesEnumerator.Current;
|
|
||||||
var preDepth = preEntity.DepthEnd;
|
|
||||||
|
|
||||||
if (preEntity.DepthEnd >= entity.DepthEnd)
|
|
||||||
{
|
|
||||||
dbset.Remove(entity);
|
|
||||||
takeover++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (preEntity.DepthEnd > entity.DepthStart)
|
|
||||||
{
|
|
||||||
entity.DepthStart = preEntity.DepthEnd;
|
|
||||||
trimmed++;
|
|
||||||
}
|
|
||||||
|
|
||||||
var preDate = preEntity.DateStart.AddHours(preEntity.DurationHours);
|
|
||||||
|
|
||||||
if (preDate >= entity.DateStart.AddHours(entity.DurationHours))
|
|
||||||
{
|
|
||||||
dbset.Remove(entity);
|
|
||||||
takeover++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (preDate > entity.DateStart)
|
|
||||||
{
|
|
||||||
var entityDateEnd = entity.DateStart.AddHours(entity.DurationHours);
|
|
||||||
entity.DateStart = preDate;
|
|
||||||
entity.DurationHours = (entityDateEnd - entity.DateStart).TotalHours;
|
|
||||||
trimmed++;
|
|
||||||
}
|
|
||||||
|
|
||||||
preEntity = entity;
|
|
||||||
}
|
|
||||||
var affected = await db.SaveChangesAsync(token);
|
|
||||||
return (takeover, trimmed, entities.Count);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user