From 04c2266591f2fea9b2692c9f99299dc45a84475e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Mon, 14 Aug 2023 18:30:31 +0500 Subject: [PATCH 1/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0=D0=BB?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=81=D1=82=D0=B2=D0=BE=D0=BB=D0=B0=20=D1=81=D0=BA=D0=B2=D0=B0?= =?UTF-8?q?=D0=B6=D0=B8=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/WellboreDto.cs | 64 +++++++++++++ .../Repositories/IWellOperationRepository.cs | 18 ++++ AsbCloudApp/Requests/WellboreRequest.cs | 14 +++ .../Repository/WellOperationRepository.cs | 73 +++++++++++++++ .../Controllers/WellboreController.cs | 90 +++++++++++++++++++ 5 files changed, 259 insertions(+) create mode 100644 AsbCloudApp/Data/WellboreDto.cs create mode 100644 AsbCloudApp/Requests/WellboreRequest.cs create mode 100644 AsbCloudWebApi/Controllers/WellboreController.cs diff --git a/AsbCloudApp/Data/WellboreDto.cs b/AsbCloudApp/Data/WellboreDto.cs new file mode 100644 index 00000000..ea542bf8 --- /dev/null +++ b/AsbCloudApp/Data/WellboreDto.cs @@ -0,0 +1,64 @@ +using System; + +namespace AsbCloudApp.Data; + +/// +/// Ствол скважины +/// +public class WellboreDto +{ + /// + /// Идентификатор + /// + public int? Id { get; set; } + + /// + /// Название + /// + public string? Name { get; set; } + + /// + /// Идентификатор скважины + /// + public int IdWell { get; set; } + + /// + /// Состояние скважины + /// + public int IdWellState { get; set; } + + /// + /// Идентификатор телеметрии + /// + public int? IdWellTelemetry { get; set; } + + /// + /// Временная зона скважины + /// + public SimpleTimezoneDto? WellTimezone { get; set; } + + /// + /// Название скважины + /// + public string WellName { get; set; } = null!; + + /// + /// Начальная глубина ствола + /// + public double? DepthFrom { get; set; } + + /// + /// Конечная глубина скважины + /// + public double? DepthTo { get; set; } + + /// + /// Дата начала первой операции + /// + public DateTimeOffset? DateFrom { get; set; } + + /// + /// Дата завершения последней операции + /// + public DateTimeOffset? DateTo { get; set; } +} \ No newline at end of file diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index b02d3e42..60ddfd80 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -73,6 +73,24 @@ namespace AsbCloudApp.Repositories Task> GetGroupOperationsStatAsync( WellOperationRequest request, CancellationToken token); + + /// + /// Получение ствола скважины + /// + /// + /// + /// + /// + Task GetWellboreAsync(int idWell, int? idWellSectionType, CancellationToken cancellationToken); + + /// + /// Получение стволов скважин + /// + /// + /// + /// + Task> GetWellboresAsync(WellboreRequest request, + CancellationToken cancellationToken); /// /// Добавить несколько операций за один раз diff --git a/AsbCloudApp/Requests/WellboreRequest.cs b/AsbCloudApp/Requests/WellboreRequest.cs new file mode 100644 index 00000000..dadcc7cc --- /dev/null +++ b/AsbCloudApp/Requests/WellboreRequest.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace AsbCloudApp.Requests; + +/// +/// Параметры запроса для ствола скважины +/// +public class WellboreRequest : RequestBase +{ + /// + /// Пары идентификаторов скважины и секции + /// + public IEnumerable<(int idWell, int? idWellSectionType)> Ids { get; set; } = null!; +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index f0eb5f6d..458b93ab 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -255,6 +255,79 @@ namespace AsbCloudInfrastructure.Repository return dtos; } + public async Task GetWellboreAsync(int idWell, int? idWellSectionType, + CancellationToken cancellationToken) + { + var well = await db.Wells.Include(w => w.WellOperations) + .FirstOrDefaultAsync(w => w.Id == idWell, cancellationToken); + + if (well is null) + return null; + + var factOperations = well.WellOperations.AsQueryable(); + + var section = await db.WellSectionTypes + .FirstOrDefaultAsync(w => w.Id == idWellSectionType, cancellationToken); + + if (section is not null) + factOperations = factOperations.Where(w => w.IdWellSectionType == section.Id); + + factOperations = factOperations.OrderBy(f => f.DateStart); + + var firstOperation = factOperations.FirstOrDefault(); + var lastOperation = factOperations.LastOrDefault(); + + return new WellboreDto + { + 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), + }; + } + + public async Task> GetWellboresAsync(WellboreRequest request, + CancellationToken cancellationToken) + { + var wellbores = new List(); + + var skip = request.Skip ?? 0; + var take = request.Take ?? 10; + + foreach (var id in request.Ids) + { + var wellbore = await GetWellboreAsync(id.idWell, + id.idWellSectionType, + cancellationToken); + + if (wellbore is null) + continue; + + wellbores.Add(wellbore); + } + + var result = new PaginationContainer + { + Skip = skip, + Take = take, + Count = wellbores.Count + }; + + if (result.Count < skip) + return result; + + result.Items = wellbores.Skip(skip).Take(take).ToList(); + + return result; + } + /// public async Task InsertRangeAsync( IEnumerable wellOperationDtos, diff --git a/AsbCloudWebApi/Controllers/WellboreController.cs b/AsbCloudWebApi/Controllers/WellboreController.cs new file mode 100644 index 00000000..be562213 --- /dev/null +++ b/AsbCloudWebApi/Controllers/WellboreController.cs @@ -0,0 +1,90 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.Data; +using AsbCloudApp.Exceptions; +using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers; + +/// +/// Ствол скважины +/// +[Authorize] +[ApiController] +[Route("api/well/[controller]")] +public class WellboreController : ControllerBase +{ + private readonly IWellOperationRepository wellOperationRepository; + + public WellboreController(IWellOperationRepository wellOperationRepository) + { + this.wellOperationRepository = wellOperationRepository; + } + + /// + /// Получение ствола скважины + /// + /// Id скважины + /// Опциональный параметр. Id типа секции + /// + /// + + [HttpGet("{idWell:int}")] + [ProducesResponseType(typeof(WellboreDto), (int)System.Net.HttpStatusCode.OK)] + public async Task GetAsync(int idWell, int? idSectionType, CancellationToken cancellationToken) + { + return Ok(await wellOperationRepository.GetWellboreAsync(idWell, idSectionType, cancellationToken)); + } + + /// + /// Получение списка стволов скважин + /// + /// Пары идентификаторов скважины и секции + /// Опциональный параметр. Количество пропускаемых записей + /// Опциональный параметр. Количество получаемых записей + /// + /// + [HttpGet] + [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] + public async Task GetAllAsync([FromQuery] IEnumerable ids, + int? skip, + int? take, + CancellationToken cancellationToken) + { + var request = new WellboreRequest + { + Ids = ParseIds(ids), + Skip = skip, + Take = take + }; + + return Ok(await wellOperationRepository.GetWellboresAsync(request, cancellationToken)); + } + + private static IEnumerable<(int, int?)> ParseIds(IEnumerable ids) + { + 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 скважины", nameof(ids)); + + if (idPair.Length < 2 || !int.TryParse(idPair[1], out var idWellSectionType)) + { + result.Add((idWell, null)); + continue; + } + + result.Add((idWell, idWellSectionType)); + } + + return result; + } +} \ No newline at end of file From 3702cf2e8c71a5b403ac13998eec2a72bf0187a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Tue, 15 Aug 2023 12:28:39 +0500 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20+=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Переделал логику получения стволов скважин. 2. Поправил контроллер. 3. Рефакторинг DTO ствола. --- AsbCloudApp/Data/WellboreDto.cs | 12 +-- .../Repositories/IWellOperationRepository.cs | 7 +- AsbCloudApp/Requests/WellboreRequest.cs | 2 +- .../Repository/WellOperationRepository.cs | 96 +++++++++++-------- .../Controllers/WellboreController.cs | 19 ++-- 5 files changed, 77 insertions(+), 59 deletions(-) diff --git a/AsbCloudApp/Data/WellboreDto.cs b/AsbCloudApp/Data/WellboreDto.cs index ea542bf8..091f5b07 100644 --- a/AsbCloudApp/Data/WellboreDto.cs +++ b/AsbCloudApp/Data/WellboreDto.cs @@ -10,12 +10,12 @@ public class WellboreDto /// /// Идентификатор /// - public int? Id { get; set; } + public int Id { get; set; } /// /// Название /// - public string? Name { get; set; } + public string Name { get; set; } = null!; /// /// Идентификатор скважины @@ -45,20 +45,20 @@ public class WellboreDto /// /// Начальная глубина ствола /// - public double? DepthFrom { get; set; } + public double DepthFrom { get; set; } /// /// Конечная глубина скважины /// - public double? DepthTo { get; set; } + public double DepthTo { get; set; } /// /// Дата начала первой операции /// - public DateTimeOffset? DateFrom { get; set; } + public DateTimeOffset DateFrom { get; set; } /// /// Дата завершения последней операции /// - public DateTimeOffset? DateTo { get; set; } + public DateTimeOffset DateTo { get; set; } } \ No newline at end of file diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index 60ddfd80..634bc56f 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -78,10 +78,10 @@ namespace AsbCloudApp.Repositories /// Получение ствола скважины /// /// - /// + /// /// /// - Task GetWellboreAsync(int idWell, int? idWellSectionType, CancellationToken cancellationToken); + Task GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken); /// /// Получение стволов скважин @@ -89,8 +89,7 @@ namespace AsbCloudApp.Repositories /// /// /// - Task> GetWellboresAsync(WellboreRequest request, - CancellationToken cancellationToken); + Task> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken); /// /// Добавить несколько операций за один раз diff --git a/AsbCloudApp/Requests/WellboreRequest.cs b/AsbCloudApp/Requests/WellboreRequest.cs index dadcc7cc..32b4cbe2 100644 --- a/AsbCloudApp/Requests/WellboreRequest.cs +++ b/AsbCloudApp/Requests/WellboreRequest.cs @@ -10,5 +10,5 @@ public class WellboreRequest : RequestBase /// /// Пары идентификаторов скважины и секции /// - public IEnumerable<(int idWell, int? idWellSectionType)> Ids { get; set; } = null!; + public IEnumerable<(int idWell, int? idSection)> Ids { get; set; } = null!; } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 458b93ab..ce0b68af 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -255,77 +255,89 @@ namespace AsbCloudInfrastructure.Repository return dtos; } - public async Task GetWellboreAsync(int idWell, int? idWellSectionType, - CancellationToken cancellationToken) + public async Task GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken) { var well = await db.Wells.Include(w => w.WellOperations) .FirstOrDefaultAsync(w => w.Id == idWell, cancellationToken); - if (well is null) + var section = await db.WellSectionTypes + .FirstOrDefaultAsync(w => w.Id == idSection, cancellationToken); + + if (well is null || section is null) return null; - var factOperations = well.WellOperations.AsQueryable(); + var factOperations = well.WellOperations + .Where(o => o.IdType == WellOperation.IdOperationTypeFact && + o.IdWellSectionType == section.Id) + .OrderBy(o => o.DateStart); - var section = await db.WellSectionTypes - .FirstOrDefaultAsync(w => w.Id == idWellSectionType, cancellationToken); - - if (section is not null) - factOperations = factOperations.Where(w => w.IdWellSectionType == section.Id); - - factOperations = factOperations.OrderBy(f => f.DateStart); - - var firstOperation = factOperations.FirstOrDefault(); - var lastOperation = factOperations.LastOrDefault(); + if (!factOperations.Any()) + return null; + + var firstOperation = factOperations.First(); + var lastOperation = factOperations.Last(); return new WellboreDto { - Id = section?.Id, - Name = section?.Caption, + 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), + DepthFrom = firstOperation.DepthStart, + DepthTo = lastOperation.DepthEnd, + DateFrom = firstOperation.DateStart, + DateTo = lastOperation.DateStart.AddHours(lastOperation.DurationHours), }; } - public async Task> GetWellboresAsync(WellboreRequest request, - CancellationToken cancellationToken) + public async Task> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken) { var wellbores = new List(); - var skip = request.Skip ?? 0; var take = request.Take ?? 10; + + var sections = GetSectionTypes() + .ToDictionary(w => w.Id, w => w); - foreach (var id in request.Ids) + foreach (var (idWell, idSection) in request.Ids) { - var wellbore = await GetWellboreAsync(id.idWell, - id.idWellSectionType, - cancellationToken); + var well = await db.Wells.Include(w => w.WellOperations) + .FirstOrDefaultAsync(w => w.Id == idWell, cancellationToken); - if (wellbore is null) + if (well is null) continue; + + var factOperations = well.WellOperations + .Where(o => o.IdType == WellOperation.IdOperationTypeFact) + .OrderBy(o => o.DateStart) + .GroupBy(o => o.IdWellSectionType); - wellbores.Add(wellbore); + if(idSection.HasValue) + factOperations = factOperations.Where(w => w.Key == idSection); + + 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), + }); } - var result = new PaginationContainer - { - Skip = skip, - Take = take, - Count = wellbores.Count - }; - - if (result.Count < skip) - return result; - - result.Items = wellbores.Skip(skip).Take(take).ToList(); - - return result; + return wellbores.Skip(skip).Take(take); } /// diff --git a/AsbCloudWebApi/Controllers/WellboreController.cs b/AsbCloudWebApi/Controllers/WellboreController.cs index be562213..aeca9e8d 100644 --- a/AsbCloudWebApi/Controllers/WellboreController.cs +++ b/AsbCloudWebApi/Controllers/WellboreController.cs @@ -6,6 +6,7 @@ using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace AsbCloudWebApi.Controllers; @@ -29,15 +30,21 @@ public class WellboreController : ControllerBase /// Получение ствола скважины /// /// Id скважины - /// Опциональный параметр. Id типа секции + /// Id типа секции скважины /// /// - [HttpGet("{idWell:int}")] - [ProducesResponseType(typeof(WellboreDto), (int)System.Net.HttpStatusCode.OK)] - public async Task GetAsync(int idWell, int? idSectionType, CancellationToken cancellationToken) + [HttpGet("{idWell:int}/{idSection:int}")] + [ProducesResponseType(typeof(WellboreDto), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + public async Task GetAsync(int idWell, int idSection, CancellationToken cancellationToken) { - return Ok(await wellOperationRepository.GetWellboreAsync(idWell, idSectionType, cancellationToken)); + var wellbore = await wellOperationRepository.GetWellboreAsync(idWell, idSection, cancellationToken); + + if (wellbore is null) + return NoContent(); + + return Ok(wellbore); } /// @@ -49,7 +56,7 @@ public class WellboreController : ControllerBase /// /// [HttpGet] - [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task GetAllAsync([FromQuery] IEnumerable ids, int? skip, int? take, From 24637f52540618aa6cb57d8370fb2c6839c856a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Tue, 15 Aug 2023 14:20:28 +0500 Subject: [PATCH 3/4] =?UTF-8?q?=D0=92=D1=8B=D0=BD=D0=B5=D1=81=20=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D0=BA=D1=83=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D1=82=D0=B2=D0=BE=D0=BB=D0=B0?= =?UTF-8?q?=20=D1=81=D0=BA=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD=D1=8B=20=D0=B2=20?= =?UTF-8?q?=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D1=81?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=B8=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IWellOperationRepository.cs | 17 --- AsbCloudApp/Services/IWellboreService.cs | 30 +++++ AsbCloudInfrastructure/DependencyInjection.cs | 2 + .../Repository/WellOperationRepository.cs | 85 ------------- .../Services/WellboreService.cs | 117 ++++++++++++++++++ .../Controllers/WellboreController.cs | 12 +- 6 files changed, 155 insertions(+), 108 deletions(-) create mode 100644 AsbCloudApp/Services/IWellboreService.cs create mode 100644 AsbCloudInfrastructure/Services/WellboreService.cs diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index 634bc56f..b02d3e42 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -73,23 +73,6 @@ namespace AsbCloudApp.Repositories Task> GetGroupOperationsStatAsync( WellOperationRequest request, CancellationToken token); - - /// - /// Получение ствола скважины - /// - /// - /// - /// - /// - Task GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken); - - /// - /// Получение стволов скважин - /// - /// - /// - /// - Task> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken); /// /// Добавить несколько операций за один раз diff --git a/AsbCloudApp/Services/IWellboreService.cs b/AsbCloudApp/Services/IWellboreService.cs new file mode 100644 index 00000000..f42485ae --- /dev/null +++ b/AsbCloudApp/Services/IWellboreService.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.Data; +using AsbCloudApp.Requests; + +namespace AsbCloudApp.Services; + +/// +/// Сервис для ствола скважины +/// +public interface IWellboreService +{ + /// + /// Получение ствола скважины + /// + /// + /// + /// + /// + Task GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken); + + /// + /// Получение стволов скважин + /// + /// + /// + /// + Task> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken); +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 601d05e7..bb894d63 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -222,6 +222,8 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); + services.AddTransient(); + return services; } diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index ce0b68af..f0eb5f6d 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -255,91 +255,6 @@ namespace AsbCloudInfrastructure.Repository return dtos; } - public async Task GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken) - { - var well = await db.Wells.Include(w => w.WellOperations) - .FirstOrDefaultAsync(w => w.Id == idWell, cancellationToken); - - var section = await db.WellSectionTypes - .FirstOrDefaultAsync(w => w.Id == idSection, cancellationToken); - - if (well is null || section is null) - return null; - - var factOperations = well.WellOperations - .Where(o => o.IdType == WellOperation.IdOperationTypeFact && - o.IdWellSectionType == section.Id) - .OrderBy(o => o.DateStart); - - if (!factOperations.Any()) - return null; - - var firstOperation = factOperations.First(); - var lastOperation = factOperations.Last(); - - return new WellboreDto - { - 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), - }; - } - - public async Task> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken) - { - var wellbores = new List(); - var skip = request.Skip ?? 0; - var take = request.Take ?? 10; - - var sections = GetSectionTypes() - .ToDictionary(w => w.Id, w => w); - - foreach (var (idWell, idSection) in request.Ids) - { - var well = await db.Wells.Include(w => w.WellOperations) - .FirstOrDefaultAsync(w => w.Id == idWell, cancellationToken); - - if (well is null) - continue; - - var factOperations = well.WellOperations - .Where(o => o.IdType == WellOperation.IdOperationTypeFact) - .OrderBy(o => o.DateStart) - .GroupBy(o => o.IdWellSectionType); - - if(idSection.HasValue) - factOperations = factOperations.Where(w => w.Key == idSection); - - 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), - }); - } - - return wellbores.Skip(skip).Take(take); - } - /// public async Task InsertRangeAsync( IEnumerable wellOperationDtos, diff --git a/AsbCloudInfrastructure/Services/WellboreService.cs b/AsbCloudInfrastructure/Services/WellboreService.cs new file mode 100644 index 00000000..eb4fa767 --- /dev/null +++ b/AsbCloudInfrastructure/Services/WellboreService.cs @@ -0,0 +1,117 @@ +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; +using Mapster; + +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 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 + { + 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), + }; + } + + public async Task> GetWellboresAsync(WellboreRequest request, + CancellationToken cancellationToken) + { + var wellbores = new List(); + 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 well = await wellService.GetOrDefaultAsync(idWell, cancellationToken); + + if (well is null) + continue; + + var factOperations = (await GetFactOperationsAsync(well.Id, idSection, cancellationToken)) + .GroupBy(o => o.IdWellSectionType); + + 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), + }); + } + + return wellbores.Skip(skip).Take(take); + } + + private async Task> GetFactOperationsAsync(int idWell, int? idSection, + CancellationToken cancellationToken) + { + var request = new WellOperationRequest + { + IdWell = idWell, + OperationType = WellOperation.IdOperationTypeFact, + SortFields = new[] { "DateStart asc" }, + }; + + if (idSection.HasValue) + request.SectionTypeIds = new[] { idSection.Value }; + + return (await wellOperationRepository.GetAsync(request, cancellationToken)) + .OrderBy(o => o.DateStart); + } +} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/WellboreController.cs b/AsbCloudWebApi/Controllers/WellboreController.cs index aeca9e8d..2780ed5b 100644 --- a/AsbCloudWebApi/Controllers/WellboreController.cs +++ b/AsbCloudWebApi/Controllers/WellboreController.cs @@ -3,8 +3,8 @@ using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Exceptions; -using AsbCloudApp.Repositories; using AsbCloudApp.Requests; +using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -19,11 +19,11 @@ namespace AsbCloudWebApi.Controllers; [Route("api/well/[controller]")] public class WellboreController : ControllerBase { - private readonly IWellOperationRepository wellOperationRepository; + private readonly IWellboreService wellboreService; - public WellboreController(IWellOperationRepository wellOperationRepository) + public WellboreController(IWellboreService wellboreService) { - this.wellOperationRepository = wellOperationRepository; + this.wellboreService = wellboreService; } /// @@ -39,7 +39,7 @@ public class WellboreController : ControllerBase [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task GetAsync(int idWell, int idSection, CancellationToken cancellationToken) { - var wellbore = await wellOperationRepository.GetWellboreAsync(idWell, idSection, cancellationToken); + var wellbore = await wellboreService.GetWellboreAsync(idWell, idSection, cancellationToken); if (wellbore is null) return NoContent(); @@ -69,7 +69,7 @@ public class WellboreController : ControllerBase Take = take }; - return Ok(await wellOperationRepository.GetWellboresAsync(request, cancellationToken)); + return Ok(await wellboreService.GetWellboresAsync(request, cancellationToken)); } private static IEnumerable<(int, int?)> ParseIds(IEnumerable ids) From b43a8691f2b4dee6f81dad9653bf785234cd61e0 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Tue, 15 Aug 2023 16:08:51 +0500 Subject: [PATCH 4/4] 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}}