From b43a8691f2b4dee6f81dad9653bf785234cd61e0 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Tue, 15 Aug 2023 16:08:51 +0500 Subject: [PATCH] WellboreService refactoring --- AsbCloudApp/Data/WellboreDto.cs | 8 +- .../Services/WellboreService.cs | 90 +++++++------------ .../Controllers/WellboreController.cs | 39 ++++---- AsbCloudWebApi/Rest/wellbore.http | 15 ++++ 4 files changed, 70 insertions(+), 82 deletions(-) create mode 100644 AsbCloudWebApi/Rest/wellbore.http diff --git a/AsbCloudApp/Data/WellboreDto.cs b/AsbCloudApp/Data/WellboreDto.cs index 091f5b07..e64a188f 100644 --- a/AsbCloudApp/Data/WellboreDto.cs +++ b/AsbCloudApp/Data/WellboreDto.cs @@ -45,20 +45,20 @@ public class WellboreDto /// /// Начальная глубина ствола /// - public double DepthFrom { get; set; } + public double DepthStart { get; set; } /// /// Конечная глубина скважины /// - public double DepthTo { get; set; } + public double DepthEnd { get; set; } /// /// Дата начала первой операции /// - public DateTimeOffset DateFrom { get; set; } + public DateTimeOffset DateStart { get; set; } /// /// Дата завершения последней операции /// - public DateTimeOffset DateTo { get; set; } + public DateTimeOffset DateEnd { get; set; } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/WellboreService.cs b/AsbCloudInfrastructure/Services/WellboreService.cs index eb4fa767..727878dc 100644 --- a/AsbCloudInfrastructure/Services/WellboreService.cs +++ b/AsbCloudInfrastructure/Services/WellboreService.cs @@ -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 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(), - 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> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken) { - var wellbores = new List(); + var wellbores = new List(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(), - 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> GetFactOperationsAsync(int idWell, int? idSection, + private async Task> GetFactOperationsAsync(int idWell, IEnumerable 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); diff --git a/AsbCloudWebApi/Controllers/WellboreController.cs b/AsbCloudWebApi/Controllers/WellboreController.cs index 2780ed5b..7c8bdaf3 100644 --- a/AsbCloudWebApi/Controllers/WellboreController.cs +++ b/AsbCloudWebApi/Controllers/WellboreController.cs @@ -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 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); + } } \ No newline at end of file diff --git a/AsbCloudWebApi/Rest/wellbore.http b/AsbCloudWebApi/Rest/wellbore.http new file mode 100644 index 00000000..9a7918ac --- /dev/null +++ b/AsbCloudWebApi/Rest/wellbore.http @@ -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}}