2024-07-04 11:02:45 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2024-03-20 12:52:28 +05:00
|
|
|
|
using AsbCloudApp.Data.WellOperation;
|
|
|
|
|
using AsbCloudApp.Exceptions;
|
2023-02-15 17:02:43 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2022-12-21 18:02:22 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2022-12-21 18:02:22 +05:00
|
|
|
|
using AsbCloudDb;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Mapster;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-11-18 15:17:04 +05:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2024-04-22 09:17:42 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
|
2023-10-06 15:19:02 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
|
|
2024-03-20 12:52:28 +05:00
|
|
|
|
public class WellOperationRepository : CrudRepositoryBase<WellOperationDto, WellOperation>,
|
2024-04-22 09:17:42 +05:00
|
|
|
|
IWellOperationRepository
|
2021-08-13 17:25:06 +05:00
|
|
|
|
{
|
2024-05-08 10:47:31 +05:00
|
|
|
|
private const string cacheKeyWellOperations = "FirstAndLastFactWellsOperations";
|
2024-04-22 09:17:42 +05:00
|
|
|
|
private readonly IMemoryCache memoryCache;
|
|
|
|
|
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
|
|
|
|
|
private readonly IWellService wellService;
|
2024-04-22 15:31:04 +05:00
|
|
|
|
private Lazy<IDictionary<int, WellOperationCategoryDto>> LazyWellCategories { get; }
|
|
|
|
|
private Lazy<IDictionary<int, WellSectionTypeDto>> LazyWellSectionTypes { get; }
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
|
|
|
|
public WellOperationRepository(IAsbCloudDbContext context,
|
|
|
|
|
IMemoryCache memoryCache,
|
|
|
|
|
IWellOperationCategoryRepository wellOperationCategoryRepository,
|
|
|
|
|
IWellService wellService)
|
2024-04-22 11:16:23 +05:00
|
|
|
|
: base(context, dbSet => dbSet)
|
2024-04-22 09:17:42 +05:00
|
|
|
|
{
|
|
|
|
|
this.memoryCache = memoryCache;
|
|
|
|
|
this.wellOperationCategoryRepository = wellOperationCategoryRepository;
|
|
|
|
|
this.wellService = wellService;
|
2024-04-22 15:31:04 +05:00
|
|
|
|
|
|
|
|
|
LazyWellCategories = new(() => wellOperationCategoryRepository.Get(true, false).ToDictionary(c => c.Id));
|
|
|
|
|
LazyWellSectionTypes = new(() => GetSectionTypes().ToDictionary(c => c.Id));
|
2024-04-22 09:17:42 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 09:38:53 +05:00
|
|
|
|
public async Task<WellOperationDto?> GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
var entity = await dbContext.WellOperations.FirstOrDefaultAsync(e => e.Id == id, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (entity == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var firstWellOperation = await dbContext.WellOperations.Where(o => o.IdWell == entity.IdWell &&
|
|
|
|
|
o.IdType == entity.IdType)
|
|
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
|
.FirstAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
var operationsWithNpt = await dbContext.WellOperations
|
|
|
|
|
.Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory) &&
|
|
|
|
|
o.IdWell == entity.IdWell &&
|
|
|
|
|
o.IdType == entity.IdType)
|
|
|
|
|
.ToArrayAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
var timezoneOffset = wellService.GetTimezone(entity.IdWell).Offset;
|
|
|
|
|
|
|
|
|
|
var dto = Convert(entity, timezoneOffset);
|
|
|
|
|
dto.Day = (entity.DateStart - firstWellOperation.DateStart).TotalDays;
|
|
|
|
|
dto.NptHours = operationsWithNpt
|
|
|
|
|
.Where(o => o.DateStart <= entity.DateStart)
|
|
|
|
|
.Sum(o => o.DurationHours);
|
|
|
|
|
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 09:17:42 +05:00
|
|
|
|
public IEnumerable<WellSectionTypeDto> GetSectionTypes() =>
|
|
|
|
|
memoryCache
|
|
|
|
|
.GetOrCreateBasic(dbContext.WellSectionTypes)
|
|
|
|
|
.OrderBy(s => s.Order)
|
|
|
|
|
.Select(s => s.Adapt<WellSectionTypeDto>());
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<WellOperationDto>> GetAsync(WellOperationRequest request, CancellationToken token)
|
|
|
|
|
{
|
2024-04-22 15:31:04 +05:00
|
|
|
|
var (items, _) = await GetWithDaysAndNpvAsync(request, token);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<PaginationContainer<WellOperationDto>> GetPageAsync(WellOperationRequest request, CancellationToken token)
|
|
|
|
|
{
|
2024-05-06 10:49:47 +05:00
|
|
|
|
request.Skip = request.Skip ?? 0;
|
|
|
|
|
request.Take = request.Take ?? 32;
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
2024-04-22 15:31:04 +05:00
|
|
|
|
var (items, count) = await GetWithDaysAndNpvAsync(request, token);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
|
|
|
|
var paginationContainer = new PaginationContainer<WellOperationDto>
|
|
|
|
|
{
|
2024-05-06 10:49:47 +05:00
|
|
|
|
Skip = request.Skip!.Value,
|
|
|
|
|
Take = request.Take!.Value,
|
2024-04-22 09:17:42 +05:00
|
|
|
|
Count = count,
|
|
|
|
|
Items = items
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return paginationContainer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<WellGroupOpertionDto>> GetGroupOperationsStatAsync(WellOperationRequest request, CancellationToken token)
|
|
|
|
|
{
|
2024-04-24 15:21:16 +05:00
|
|
|
|
var query = BuildQuery(request);
|
|
|
|
|
var entities = await query
|
2024-04-22 09:17:42 +05:00
|
|
|
|
.Select(o => new
|
|
|
|
|
{
|
|
|
|
|
o.IdCategory,
|
|
|
|
|
DurationMinutes = o.DurationHours * 60,
|
|
|
|
|
DurationDepth = o.DepthEnd - o.DepthStart
|
|
|
|
|
})
|
2024-04-24 15:21:16 +05:00
|
|
|
|
.ToArrayAsync(token);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
EnsureValidWellOperations(dtos);
|
|
|
|
|
|
2024-05-13 11:06:41 +05:00
|
|
|
|
var result = 0;
|
|
|
|
|
|
2024-04-22 09:17:42 +05:00
|
|
|
|
if (!deleteBeforeInsert)
|
2024-05-13 11:06:41 +05:00
|
|
|
|
{
|
|
|
|
|
result = await InsertRangeAsync(dtos, token);
|
|
|
|
|
|
|
|
|
|
if (result > 0)
|
|
|
|
|
memoryCache.Remove(cacheKeyWellOperations);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
|
|
|
|
var idType = dtos.First().IdType;
|
|
|
|
|
var idWell = dtos.First().IdWell;
|
|
|
|
|
|
|
|
|
|
var existingOperationIds = await dbContext.WellOperations
|
|
|
|
|
.Where(e => e.IdWell == idWell && e.IdType == idType)
|
|
|
|
|
.Select(e => e.Id)
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
await DeleteRangeAsync(existingOperationIds, token);
|
|
|
|
|
|
2024-05-13 11:06:41 +05:00
|
|
|
|
result = await InsertRangeAsync(dtos, token);
|
|
|
|
|
|
|
|
|
|
if (result > 0)
|
|
|
|
|
memoryCache.Remove(cacheKeyWellOperations);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
2024-04-22 09:17:42 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 10:47:31 +05:00
|
|
|
|
public override async Task<int> UpdateRangeAsync(IEnumerable<WellOperationDto> dtos, CancellationToken token)
|
2024-04-22 09:17:42 +05:00
|
|
|
|
{
|
|
|
|
|
EnsureValidWellOperations(dtos);
|
|
|
|
|
|
2024-05-08 10:47:31 +05:00
|
|
|
|
var result = await base.UpdateRangeAsync(dtos, token);
|
|
|
|
|
|
|
|
|
|
if (result > 0)
|
|
|
|
|
memoryCache.Remove(cacheKeyWellOperations);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
2024-04-22 09:17:42 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 15:31:04 +05:00
|
|
|
|
private async Task<(IEnumerable<WellOperationDto> items, int count)> GetWithDaysAndNpvAsync(WellOperationRequest request, CancellationToken token)
|
2024-04-22 09:17:42 +05:00
|
|
|
|
{
|
|
|
|
|
var entities = await GetByIdsWells(request.IdsWell, token);
|
2024-04-22 15:31:04 +05:00
|
|
|
|
var groupedByWellAndType = entities
|
|
|
|
|
.GroupBy(e => new { e.IdWell, e.IdType });
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
|
|
|
|
var result = new List<WellOperationDto>();
|
2024-04-22 11:16:23 +05:00
|
|
|
|
var count = 0;
|
2024-04-22 15:31:04 +05:00
|
|
|
|
foreach (var wellOperationsWithType in groupedByWellAndType)
|
2024-04-22 09:17:42 +05:00
|
|
|
|
{
|
2024-07-10 09:38:53 +05:00
|
|
|
|
var firstWellOperation = wellOperationsWithType.MinBy(e => e.DateStart);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
2024-04-22 11:16:23 +05:00
|
|
|
|
var operationsWithNpt = wellOperationsWithType
|
2024-04-22 09:17:42 +05:00
|
|
|
|
.Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory));
|
|
|
|
|
|
2024-04-24 12:28:37 +05:00
|
|
|
|
IEnumerable<WellOperation> filteredWellOperations = FilterByRequest(wellOperationsWithType.AsQueryable(), request);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
2024-05-06 10:49:47 +05:00
|
|
|
|
count += filteredWellOperations.Count();
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
2024-05-06 10:49:47 +05:00
|
|
|
|
if (request.Skip != null)
|
|
|
|
|
filteredWellOperations = filteredWellOperations.Skip((int)request.Skip);
|
|
|
|
|
if (request.Take != null)
|
|
|
|
|
filteredWellOperations = filteredWellOperations.Take((int)request.Take);
|
2024-07-10 09:38:53 +05:00
|
|
|
|
|
|
|
|
|
var timezoneOffset = wellService.GetTimezone(wellOperationsWithType.Key.IdWell).Offset;
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
2024-05-06 10:49:47 +05:00
|
|
|
|
var dtos = filteredWellOperations
|
2024-04-22 15:31:04 +05:00
|
|
|
|
.Select(entity =>
|
|
|
|
|
{
|
2024-07-10 09:38:53 +05:00
|
|
|
|
var dto = Convert(entity, timezoneOffset);
|
2024-04-22 15:31:04 +05:00
|
|
|
|
dto.Day = (entity.DateStart - firstWellOperation.DateStart).TotalDays;
|
|
|
|
|
dto.NptHours = operationsWithNpt
|
|
|
|
|
.Where(o => o.DateStart <= entity.DateStart)
|
|
|
|
|
.Sum(e => e.DurationHours);
|
|
|
|
|
return dto;
|
|
|
|
|
});
|
|
|
|
|
|
2024-04-22 09:17:42 +05:00
|
|
|
|
result.AddRange(dtos);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 11:16:23 +05:00
|
|
|
|
return (result, count);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 15:21:16 +05:00
|
|
|
|
private static IQueryable<WellOperation> FilterByRequest(IQueryable<WellOperation> entities, WellOperationRequest request)
|
2024-04-22 09:17:42 +05:00
|
|
|
|
{
|
|
|
|
|
if (request.OperationType.HasValue)
|
2024-04-24 15:21:16 +05:00
|
|
|
|
entities = entities.Where(e => e.IdType == request.OperationType.Value);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
if (request.SectionTypeIds?.Any() is true)
|
2024-04-24 15:21:16 +05:00
|
|
|
|
entities = entities.Where(e => request.SectionTypeIds.Contains(e.IdWellSectionType));
|
2024-04-22 09:17:42 +05:00
|
|
|
|
if (request.OperationCategoryIds?.Any() is true)
|
2024-04-24 15:21:16 +05:00
|
|
|
|
entities = entities.Where(e => request.OperationCategoryIds.Contains(e.IdCategory));
|
2024-04-22 09:17:42 +05:00
|
|
|
|
if (request.GeDepth.HasValue)
|
2024-04-24 15:21:16 +05:00
|
|
|
|
entities = entities.Where(e => e.DepthEnd >= request.GeDepth.Value);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
if (request.LeDepth.HasValue)
|
2024-04-24 15:21:16 +05:00
|
|
|
|
entities = entities.Where(e => e.DepthEnd <= request.LeDepth.Value);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
|
|
|
|
if (request.GeDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var geDateUtc = request.GeDate.Value.UtcDateTime;
|
2024-04-24 15:21:16 +05:00
|
|
|
|
entities = entities.Where(e => e.DateStart >= geDateUtc);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.LeDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var leDateUtc = request.LeDate.Value.UtcDateTime;
|
2024-04-24 15:21:16 +05:00
|
|
|
|
entities = entities.Where(e => e.DateStart <= leDateUtc);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
}
|
|
|
|
|
if (request.SortFields?.Any() is true)
|
2024-04-24 15:21:16 +05:00
|
|
|
|
entities = entities.AsQueryable().SortBy(request.SortFields);
|
|
|
|
|
else
|
|
|
|
|
entities = entities.AsQueryable().OrderBy(e => e.DateStart);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
|
|
|
|
return entities;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 15:21:16 +05:00
|
|
|
|
private IQueryable<WellOperation> BuildQuery(WellOperationRequest request)
|
2024-04-22 09:17:42 +05:00
|
|
|
|
{
|
2024-04-24 12:28:37 +05:00
|
|
|
|
var query = GetQuery()
|
|
|
|
|
.Where(e => request.IdsWell.Contains(e.IdWell))
|
|
|
|
|
.OrderBy(e => e.DateStart)
|
|
|
|
|
.AsQueryable();
|
|
|
|
|
query = FilterByRequest(query, request);
|
2024-04-22 09:17:42 +05:00
|
|
|
|
|
2024-04-24 12:28:37 +05:00
|
|
|
|
return query;
|
2024-04-22 09:17:42 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
const string keyCacheSections = "OperationsBySectionSummarties";
|
|
|
|
|
|
|
|
|
|
var cache = await memoryCache.GetOrCreateAsync(keyCacheSections, async (entry) =>
|
|
|
|
|
{
|
|
|
|
|
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
|
|
|
|
|
|
|
|
|
|
var query = dbContext.Set<WellOperation>()
|
|
|
|
|
.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,
|
|
|
|
|
|
|
|
|
|
First = group
|
|
|
|
|
.OrderBy(operation => operation.DateStart)
|
|
|
|
|
.Select(operation => new
|
|
|
|
|
{
|
|
|
|
|
operation.DateStart,
|
|
|
|
|
operation.DepthStart,
|
|
|
|
|
})
|
|
|
|
|
.First(),
|
|
|
|
|
|
|
|
|
|
Last = group
|
|
|
|
|
.OrderByDescending(operation => operation.DateStart)
|
|
|
|
|
.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,
|
|
|
|
|
|
|
|
|
|
DateStart = item.First.DateStart,
|
|
|
|
|
DepthStart = item.First.DepthStart,
|
|
|
|
|
|
|
|
|
|
DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours),
|
|
|
|
|
DepthEnd = item.Last.DepthEnd,
|
|
|
|
|
})
|
|
|
|
|
.ToArray()
|
|
|
|
|
.AsEnumerable();
|
|
|
|
|
|
|
|
|
|
entry.Value = sections;
|
|
|
|
|
return sections;
|
|
|
|
|
});
|
|
|
|
|
|
2024-04-24 15:21:16 +05:00
|
|
|
|
return cache!;
|
2024-04-22 09:17:42 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var query = dbContext.WellOperations.Where(o => o.IdWell == idWell && o.IdType == idType);
|
|
|
|
|
|
|
|
|
|
if (!await query.AnyAsync(cancellationToken))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var timeZoneOffset = wellService.GetTimezone(idWell).Offset;
|
|
|
|
|
|
|
|
|
|
var minDate = await query.MinAsync(o => o.DateStart, cancellationToken);
|
|
|
|
|
var maxDate = await query.MaxAsync(o => o.DateStart, cancellationToken);
|
|
|
|
|
|
|
|
|
|
return new DatesRangeDto
|
|
|
|
|
{
|
|
|
|
|
From = minDate.ToOffset(timeZoneOffset),
|
|
|
|
|
To = maxDate.ToOffset(timeZoneOffset)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-08 10:47:31 +05:00
|
|
|
|
public (WellOperationDto First, WellOperationDto Last)? GetFirstAndLastFact(int idWell)
|
|
|
|
|
{
|
|
|
|
|
var cachedDictionary = memoryCache.GetOrCreate(cacheKeyWellOperations, (entry) =>
|
|
|
|
|
{
|
|
|
|
|
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5);
|
|
|
|
|
var query = dbContext.Set<WellOperation>()
|
|
|
|
|
.Where(o => o.IdType == WellOperation.IdOperationTypeFact)
|
|
|
|
|
.GroupBy(o => o.IdWell)
|
|
|
|
|
.Select(group => new
|
|
|
|
|
{
|
|
|
|
|
IdWell = group.Key,
|
|
|
|
|
FirstFact = group.OrderBy(o => o.DateStart).First(),
|
|
|
|
|
LastFact = group.OrderBy(o => o.DateStart).Last(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var entities = query.ToArray();
|
|
|
|
|
|
|
|
|
|
var dictionary = entities.ToDictionary(s => s.IdWell, s => (Convert(s.FirstFact), Convert(s.LastFact)));
|
|
|
|
|
entry.Value = dictionary;
|
|
|
|
|
|
|
|
|
|
return dictionary;
|
|
|
|
|
|
|
|
|
|
})!;
|
|
|
|
|
|
|
|
|
|
var firstAndLast = cachedDictionary.GetValueOrDefault(idWell);
|
|
|
|
|
return firstAndLast;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<int> DeleteAsync(int id, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = await base.DeleteAsync(id, token);
|
|
|
|
|
if (result > 0)
|
|
|
|
|
memoryCache.Remove(cacheKeyWellOperations);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = await base.DeleteRangeAsync(ids, token);
|
|
|
|
|
if (result > 0)
|
|
|
|
|
memoryCache.Remove(cacheKeyWellOperations);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 09:17:42 +05:00
|
|
|
|
protected override WellOperation Convert(WellOperationDto src)
|
|
|
|
|
{
|
|
|
|
|
var entity = src.Adapt<WellOperation>();
|
|
|
|
|
entity.DateStart = src.DateStart.UtcDateTime;
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 09:38:53 +05:00
|
|
|
|
private WellOperationDto Convert(WellOperation src, TimeSpan timezoneOffset)
|
2024-04-22 09:17:42 +05:00
|
|
|
|
{
|
|
|
|
|
var dto = src.Adapt<WellOperationDto>();
|
2024-07-10 09:38:53 +05:00
|
|
|
|
dto.DateStart = src.DateStart.ToOffset(timezoneOffset);
|
|
|
|
|
dto.LastUpdateDate = src.LastUpdateDate.ToOffset(timezoneOffset);
|
2024-04-22 15:31:04 +05:00
|
|
|
|
|
|
|
|
|
dto.OperationCategoryName = LazyWellCategories.Value.TryGetValue(src.IdCategory, out WellOperationCategoryDto? category) ? category.Name : string.Empty;
|
|
|
|
|
dto.WellSectionTypeCaption = LazyWellSectionTypes.Value.TryGetValue(src.IdWellSectionType, out WellSectionTypeDto? sectionType) ? sectionType.Caption : string.Empty;
|
2024-04-22 09:17:42 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
2024-03-20 12:52:28 +05:00
|
|
|
|
}
|