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)