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>
/// Начальная глубина ствола /// Начальная глубина ствола
/// </summary> /// </summary>
public double DepthFrom { get; set; } public double DepthStart { get; set; }
/// <summary> /// <summary>
/// Конечная глубина скважины /// Конечная глубина скважины
/// </summary> /// </summary>
public double DepthTo { get; set; } public double DepthEnd { get; set; }
/// <summary> /// <summary>
/// Дата начала первой операции /// Дата начала первой операции
/// </summary> /// </summary>
public DateTimeOffset DateFrom { get; set; } public DateTimeOffset DateStart { get; set; }
/// <summary> /// <summary>
/// Дата завершения последней операции /// Дата завершения последней операции
/// </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.Requests;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using Mapster;
namespace AsbCloudInfrastructure.Services; namespace AsbCloudInfrastructure.Services;
@ -24,81 +23,59 @@ public class WellboreService : IWellboreService
public async Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken) public async Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken)
{ {
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken); var request = new WellboreRequest
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
{ {
Id = section.Id, Ids = new (int, int?)[] { (idWell, idSection) },
Name = section.Caption, Take = 1,
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),
}; };
} var data = await GetWellboresAsync(request, cancellationToken);
return data.FirstOrDefault();
}
public async Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request, public async Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var wellbores = new List<WellboreDto>(); var wellbores = new List<WellboreDto>(request.Ids.Count());
var skip = request.Skip ?? 0; var skip = request.Skip ?? 0;
var take = request.Take ?? 10; var take = request.Take ?? 10;
var sections = wellOperationRepository.GetSectionTypes() var sections = wellOperationRepository.GetSectionTypes()
.ToDictionary(w => w.Id, w => w); .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) if (well is null)
continue; continue;
var factOperations = (await GetFactOperationsAsync(well.Id, idSection, cancellationToken)) var wellOperations = await GetFactOperationsAsync(well.Id, id.Select(i => i.idSection), cancellationToken);
.GroupBy(o => o.IdWellSectionType); 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 DateStart = group.Min(operation => operation.DateStart),
let firstOperation = factOperation.First() DateEnd = group.Max(operation => operation.DateStart.AddHours(operation.DurationHours)),
let lastOperation = factOperation.Last() DepthStart = group.Min(operation => operation.DepthStart),
select new WellboreDto DepthEnd = group.Max(operation => operation.DepthEnd),
{ });
Id = sections[factOperation.Key].Id, wellbores.AddRange(wellWellbores);
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),
});
} }
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) CancellationToken cancellationToken)
{ {
var request = new WellOperationRequest var request = new WellOperationRequest
@ -108,8 +85,9 @@ public class WellboreService : IWellboreService
SortFields = new[] { "DateStart asc" }, SortFields = new[] { "DateStart asc" },
}; };
if (idSection.HasValue) request.SectionTypeIds = idsSections.All(i => i.HasValue)
request.SectionTypeIds = new[] { idSection.Value }; ? idsSections.Select(i => i!.Value)
: null;
return (await wellOperationRepository.GetAsync(request, cancellationToken)) return (await wellOperationRepository.GetAsync(request, cancellationToken))
.OrderBy(o => o.DateStart); .OrderBy(o => o.DateStart);

View File

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

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}}