2023-08-15 14:20:28 +05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
using AsbCloudDb.Model;
|
2023-08-15 18:00:35 +05:00
|
|
|
using Mapster;
|
2023-08-15 14:20:28 +05:00
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services;
|
|
|
|
|
|
|
|
public class WellboreService : IWellboreService
|
|
|
|
{
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
private readonly IWellOperationRepository wellOperationRepository;
|
|
|
|
|
|
|
|
public WellboreService(IWellService wellService, IWellOperationRepository wellOperationRepository)
|
|
|
|
{
|
|
|
|
this.wellService = wellService;
|
|
|
|
this.wellOperationRepository = wellOperationRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken)
|
|
|
|
{
|
2023-08-15 16:08:51 +05:00
|
|
|
var request = new WellboreRequest
|
2023-08-15 14:20:28 +05:00
|
|
|
{
|
2023-08-15 16:08:51 +05:00
|
|
|
Ids = new (int, int?)[] { (idWell, idSection) },
|
|
|
|
Take = 1,
|
2023-08-15 14:20:28 +05:00
|
|
|
};
|
2023-08-15 16:08:51 +05:00
|
|
|
var data = await GetWellboresAsync(request, cancellationToken);
|
|
|
|
return data.FirstOrDefault();
|
|
|
|
}
|
2023-08-15 14:20:28 +05:00
|
|
|
|
|
|
|
public async Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request,
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2023-08-15 16:08:51 +05:00
|
|
|
var wellbores = new List<WellboreDto>(request.Ids.Count());
|
2023-08-15 14:20:28 +05:00
|
|
|
var skip = request.Skip ?? 0;
|
|
|
|
var take = request.Take ?? 10;
|
|
|
|
|
|
|
|
var sections = wellOperationRepository.GetSectionTypes()
|
|
|
|
.ToDictionary(w => w.Id, w => w);
|
|
|
|
|
2023-08-15 16:08:51 +05:00
|
|
|
var ids = request.Ids.GroupBy(i => i.idWell);
|
|
|
|
|
|
|
|
foreach (var id in ids)
|
2023-08-15 14:20:28 +05:00
|
|
|
{
|
2023-08-15 16:08:51 +05:00
|
|
|
var well = await wellService.GetOrDefaultAsync(id.Key, cancellationToken);
|
2023-08-15 14:20:28 +05:00
|
|
|
|
|
|
|
if (well is null)
|
|
|
|
continue;
|
|
|
|
|
2023-08-15 16:08:51 +05:00
|
|
|
var wellOperations = await GetFactOperationsAsync(well.Id, id.Select(i => i.idSection), cancellationToken);
|
|
|
|
var groupedOperations = wellOperations.GroupBy(o => o.IdWellSectionType);
|
|
|
|
var wellWellbores = groupedOperations.Select(group => new WellboreDto {
|
|
|
|
Id = group.Key,
|
|
|
|
Name = sections[group.Key].Caption,
|
2023-08-15 18:00:35 +05:00
|
|
|
Well = well.Adapt<WellWithTimezoneDto>(),
|
2023-08-15 16:08:51 +05:00
|
|
|
DateStart = group.Min(operation => operation.DateStart),
|
|
|
|
DateEnd = group.Max(operation => operation.DateStart.AddHours(operation.DurationHours)),
|
|
|
|
DepthStart = group.Min(operation => operation.DepthStart),
|
|
|
|
DepthEnd = group.Max(operation => operation.DepthEnd),
|
|
|
|
});
|
|
|
|
wellbores.AddRange(wellWellbores);
|
2023-08-15 14:20:28 +05:00
|
|
|
}
|
|
|
|
|
2023-08-15 16:08:51 +05:00
|
|
|
return wellbores
|
2023-08-15 18:00:35 +05:00
|
|
|
.OrderBy(w => w.Well.Id).ThenBy(w => w.Id)
|
2023-08-15 16:08:51 +05:00
|
|
|
.Skip(skip).Take(take);
|
2023-08-15 14:20:28 +05:00
|
|
|
}
|
|
|
|
|
2023-08-15 16:08:51 +05:00
|
|
|
private async Task<IOrderedEnumerable<WellOperationDto>> GetFactOperationsAsync(int idWell, IEnumerable<int?> idsSections,
|
2023-08-15 14:20:28 +05:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var request = new WellOperationRequest
|
|
|
|
{
|
|
|
|
IdWell = idWell,
|
|
|
|
OperationType = WellOperation.IdOperationTypeFact,
|
|
|
|
SortFields = new[] { "DateStart asc" },
|
|
|
|
};
|
|
|
|
|
2023-08-15 16:08:51 +05:00
|
|
|
request.SectionTypeIds = idsSections.All(i => i.HasValue)
|
|
|
|
? idsSections.Select(i => i!.Value)
|
|
|
|
: null;
|
2023-08-15 14:20:28 +05:00
|
|
|
|
|
|
|
return (await wellOperationRepository.GetAsync(request, cancellationToken))
|
|
|
|
.OrderBy(o => o.DateStart);
|
|
|
|
}
|
|
|
|
}
|