WellboreService refactoring

This commit is contained in:
ngfrolov 2023-08-15 16:08:51 +05:00
parent c39784a25d
commit b43a8691f2
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
4 changed files with 70 additions and 82 deletions

View File

@ -45,20 +45,20 @@ public class WellboreDto
/// <summary>
/// Начальная глубина ствола
/// </summary>
public double DepthFrom { get; set; }
public double DepthStart { get; set; }
/// <summary>
/// Конечная глубина скважины
/// </summary>
public double DepthTo { get; set; }
public double DepthEnd { get; set; }
/// <summary>
/// Дата начала первой операции
/// </summary>
public DateTimeOffset DateFrom { get; set; }
public DateTimeOffset DateStart { get; set; }
/// <summary>
/// Дата завершения последней операции
/// </summary>
public DateTimeOffset DateTo { get; set; }
public DateTimeOffset DateEnd { get; set; }
}

View File

@ -7,7 +7,6 @@ using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
namespace AsbCloudInfrastructure.Services;
@ -24,81 +23,59 @@ public class WellboreService : IWellboreService
public async Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken)
{
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken);
var section = wellOperationRepository.GetSectionTypes()
.FirstOrDefault(w => w.Id == idSection);
if (well is null || section is null)
return null;
var factOperations = await GetFactOperationsAsync(idWell, idSection, cancellationToken);
if (!factOperations.Any())
return null;
var firstOperation = factOperations.First();
var lastOperation = factOperations.Last();
return new WellboreDto
var request = new WellboreRequest
{
Id = section.Id,
Name = section.Caption,
IdWell = well.Id,
WellName = well.Caption,
IdWellState = well.IdState,
IdWellTelemetry = well.IdTelemetry,
WellTimezone = well.Timezone.Adapt<SimpleTimezoneDto>(),
DepthFrom = firstOperation.DepthStart,
DepthTo = lastOperation.DepthEnd,
DateFrom = firstOperation.DateStart,
DateTo = lastOperation.DateStart.AddHours(lastOperation.DurationHours),
Ids = new (int, int?)[] { (idWell, idSection) },
Take = 1,
};
}
var data = await GetWellboresAsync(request, cancellationToken);
return data.FirstOrDefault();
}
public async Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request,
CancellationToken cancellationToken)
{
var wellbores = new List<WellboreDto>();
var wellbores = new List<WellboreDto>(request.Ids.Count());
var skip = request.Skip ?? 0;
var take = request.Take ?? 10;
var sections = wellOperationRepository.GetSectionTypes()
.ToDictionary(w => w.Id, w => w);
foreach (var (idWell, idSection) in request.Ids)
var ids = request.Ids.GroupBy(i => i.idWell);
foreach (var id in ids)
{
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken);
var well = await wellService.GetOrDefaultAsync(id.Key, cancellationToken);
if (well is null)
continue;
var factOperations = (await GetFactOperationsAsync(well.Id, idSection, cancellationToken))
.GroupBy(o => o.IdWellSectionType);
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,
IdWell = well.Id,
IdWellState = well.IdState,
IdWellTelemetry = well.IdTelemetry,
Name = sections[group.Key].Caption,
WellName = well.Caption,
WellTimezone = well.Timezone,
wellbores.AddRange(from factOperation in factOperations
let firstOperation = factOperation.First()
let lastOperation = factOperation.Last()
select new WellboreDto
{
Id = sections[factOperation.Key].Id,
Name = sections[factOperation.Key].Caption,
IdWell = well.Id,
WellName = well.Caption,
IdWellState = well.IdState,
IdWellTelemetry = well.IdTelemetry,
WellTimezone = well.Timezone.Adapt<SimpleTimezoneDto>(),
DepthFrom = firstOperation.DepthStart,
DepthTo = lastOperation.DepthEnd,
DateFrom = firstOperation.DateStart,
DateTo = lastOperation.DateStart.AddHours(lastOperation.DurationHours),
});
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);
}
return wellbores.Skip(skip).Take(take);
return wellbores
.OrderBy(w =>w.IdWell).ThenBy(w=>w.Id)
.Skip(skip).Take(take);
}
private async Task<IOrderedEnumerable<WellOperationDto>> GetFactOperationsAsync(int idWell, int? idSection,
private async Task<IOrderedEnumerable<WellOperationDto>> GetFactOperationsAsync(int idWell, IEnumerable<int?> idsSections,
CancellationToken cancellationToken)
{
var request = new WellOperationRequest
@ -108,8 +85,9 @@ public class WellboreService : IWellboreService
SortFields = new[] { "DateStart asc" },
};
if (idSection.HasValue)
request.SectionTypeIds = new[] { idSection.Value };
request.SectionTypeIds = idsSections.All(i => i.HasValue)
? idsSections.Select(i => i!.Value)
: null;
return (await wellOperationRepository.GetAsync(request, cancellationToken))
.OrderBy(o => o.DateStart);

View File

@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@ -64,34 +66,27 @@ public class WellboreController : ControllerBase
{
var request = new WellboreRequest
{
Ids = ParseIds(ids),
Ids = ids.Select(id => ParseId(id)),
Skip = skip,
Take = take
};
return Ok(await wellboreService.GetWellboresAsync(request, cancellationToken));
}
private static IEnumerable<(int, int?)> ParseIds(IEnumerable<string> ids)
private static (int, int?) ParseId(string id)
{
var result = new List<(int, int?)>();
foreach (var id in ids)
var idPair = id.Split(',');
if (!int.TryParse(idPair[0], out var idWell))
throw new ArgumentInvalidException($"Не удалось получить Id скважины \"{idPair[0]}\"", nameof(id));
if (idPair.Length > 1)
{
var idPair = id.Split(',');
if (!int.TryParse(idPair[0], out var idWell))
throw new ArgumentInvalidException("Не удалось получить Id скважины", nameof(ids));
if (idPair.Length < 2 || !int.TryParse(idPair[1], out var idWellSectionType))
{
result.Add((idWell, null));
continue;
}
result.Add((idWell, idWellSectionType));
}
return result;
}
if (int.TryParse(idPair[1], out int idWellSectionType))
return (idWell, idWellSectionType);
else
throw new ArgumentInvalidException($"Не удалось получить Id ствола \"{idPair[1]}\"", nameof(id));
}
return (idWell, null);
}
}

View File

@ -0,0 +1,15 @@
@baseUrl = http://127.0.0.1:5000
@contentType = application/json
@auth = Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6IkpXVCJ9.eyJpZCI6IjEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiZGV2IiwiaWRDb21wYW55IjoiMSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InJvb3QiLCJuYmYiOjE2NjI1NDgxNjIsImV4cCI6MTY5NDEwNTc2MiwiaXNzIjoiYSIsImF1ZCI6ImEifQ.OEAlNzxi7Jat6pzDBTAjTbChskc-tdJthJexyWwwUKE
@uid = 20210101_000000000
@idCluster = 1
@idWell = 1
# https://marketplace.visualstudio.com/items?itemName=humao.rest-client
###
GET {{baseUrl}}/api/well/wellbore?ids=1,2
Content-Type: {{contentType}}
accept: */*
Authorization: {{auth}}