From b800759a09db905735804c046606dbc83f3092e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Tue, 12 Oct 2021 16:07:08 +0500 Subject: [PATCH] =?UTF-8?q?CS2-95=20=D0=92=20WellOperationStatController?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D0=B8=D0=BA=D0=B8=20=D0=BF=D0=BE=20=D1=81=D0=BA=D0=B2=D0=B0?= =?UTF-8?q?=D0=B6=D0=B8=D0=BD=D0=B0=D0=BC=20=D0=B8=20=D1=81=D0=B5=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=D0=BC=20=D0=BF=D0=BE=20=D0=BC=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B8=D0=B2=D1=83=20id=20=D1=81=D0=BA=D0=B2=D0=B0=D0=B6?= =?UTF-8?q?=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/IWellOperationsStatService.cs | 1 + AsbCloudApp/Services/IWellService.cs | 1 + .../Services/WellOperationService/Race.cs | 21 ++++ .../WellOperationsStatService.cs | 99 ++++++++----------- .../Services/WellService.cs | 3 + .../WellOperationStatController.cs | 43 +++++++- 6 files changed, 111 insertions(+), 57 deletions(-) create mode 100644 AsbCloudInfrastructure/Services/WellOperationService/Race.cs diff --git a/AsbCloudApp/Services/IWellOperationsStatService.cs b/AsbCloudApp/Services/IWellOperationsStatService.cs index fce85862..a047f68d 100644 --- a/AsbCloudApp/Services/IWellOperationsStatService.cs +++ b/AsbCloudApp/Services/IWellOperationsStatService.cs @@ -10,5 +10,6 @@ namespace AsbCloudApp.Services Task GetStatClusterAsync(int idCluster, CancellationToken token = default); Task GetStatWellAsync(int idWell, CancellationToken token = default); Task>> GetTvdAsync(int idWell, CancellationToken token); + Task> GetWellsStatAsync(IEnumerable idWells, CancellationToken token); } } diff --git a/AsbCloudApp/Services/IWellService.cs b/AsbCloudApp/Services/IWellService.cs index cfa9ec19..ce6f1570 100644 --- a/AsbCloudApp/Services/IWellService.cs +++ b/AsbCloudApp/Services/IWellService.cs @@ -13,5 +13,6 @@ namespace AsbCloudApp.Services Task GetWellCaptionByIdAsync(int idWell, CancellationToken token); Task> GetCompaniesAsync(int idWell, CancellationToken token); Task GetAsync(int idWell, CancellationToken token); + bool IsCompanyInvolvedInWell(int idCompany, int idWell); } } diff --git a/AsbCloudInfrastructure/Services/WellOperationService/Race.cs b/AsbCloudInfrastructure/Services/WellOperationService/Race.cs new file mode 100644 index 00000000..65593f84 --- /dev/null +++ b/AsbCloudInfrastructure/Services/WellOperationService/Race.cs @@ -0,0 +1,21 @@ +using AsbCloudDb.Model; +using System; +using System.Collections.Generic; + +namespace AsbCloudInfrastructure.Services.WellOperationService +{ + class Race + { + public DateTime StartDate { get; set; } + public double StartWellDepth { get; set; } + public DateTime EndDate { get; set; } + public double EndWellDepth { get; set; } + public double DrillingTime { get; set; } + public double NonProductiveHours { get; set; } + public double DeltaDepth => EndWellDepth - StartWellDepth; + public double DeltaHoursTimeNoNpt => (EndDate - StartDate).TotalHours - NonProductiveHours; + public double Speed => DeltaDepth / (DeltaHoursTimeNoNpt + double.Epsilon); + + public List Operations { get; internal set; } + } +} diff --git a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs index 9b273efe..5f0e0725 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs @@ -12,21 +12,6 @@ using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services.WellOperationService { - class Race - { - public DateTime StartDate { get; set; } - public double StartWellDepth { get; set; } - public DateTime EndDate { get; set; } - public double EndWellDepth { get; set; } - public double DrillingTime { get; set; } - public double NonProductiveHours { get; set; } - public double DeltaDepth => EndWellDepth - StartWellDepth; - public double DeltaHoursTimeNoNpt => (EndDate - StartDate).TotalHours - NonProductiveHours; - public double Speed => DeltaDepth / (DeltaHoursTimeNoNpt + double.Epsilon); - - public List Operations { get; internal set; } - } - public class WellOperationsStatService : IWellOperationsStatService { private readonly IAsbCloudDbContext db; @@ -63,23 +48,15 @@ namespace AsbCloudInfrastructure.Services.WellOperationService .AsNoTracking() .ToListAsync(token); - var operations = wells - .SelectMany(w => w.WellOperations) - .OrderBy(o => o.DateStart) - .ThenBy(o => o.DepthEnd); + var statsWells = new List(wells.Count()); - var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token); - - var wellsIds = wells.Select(o => o.Id).Distinct(); - - var statsWells = new List(wellsIds.Count()); - - foreach (var idWell in wellsIds) + foreach (var well in wells) { - var statWellDto = await CalcStatWell(operations, idWell, token); + var statWellDto = await CalcStatWellAsync(well, token); statsWells.Add(statWellDto); } + var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token); var statClusterDto = new StatClusterDto { Id = idCluster, @@ -89,49 +66,59 @@ namespace AsbCloudInfrastructure.Services.WellOperationService return statClusterDto; } - public async Task GetStatWellAsync(int idWell, - CancellationToken token = default) + public async Task> GetWellsStatAsync(IEnumerable idWells, CancellationToken token) { - var operations = await db.WellOperations - .Where(o => o.IdWell == idWell) - .OrderBy(o => o.DateStart) // ускорит дальнейшие сортировки - .ThenBy(o => o.DepthEnd) + var wells = await db.Wells + .Include(w => w.WellOperations) + .Where(w => idWells.Contains(w.Id)) + .Where(w => w.WellOperations.Any()) .AsNoTracking() .ToListAsync(token); - if (!operations.Any()) - return null; + var statsWells = new List(wells.Count); - var statWellDto = await CalcStatWell(operations, idWell, token); + foreach (var well in wells) + { + var statWellDto = await CalcStatWellAsync(well, token); + statsWells.Add(statWellDto); + } + return statsWells; + } + + public async Task GetStatWellAsync(int idWell, + CancellationToken token = default) + { + var well = await db.Wells + .Include(w => w.WellOperations) + .FirstOrDefaultAsync(w => w.Id == idWell, token) + .ConfigureAwait(false); + + var statWellDto = await CalcStatWellAsync(well, token); return statWellDto; } - private async Task CalcStatWell(IEnumerable operations, int idWell, - CancellationToken token = default) + private async Task CalcStatWellAsync(Well well, CancellationToken token = default) { - var wellOperations = operations - .Where(o => o.IdWell == idWell); + var wellOperations = well.WellOperations + .OrderBy(o => o.DateStart) + .ThenBy(o => o.DepthEnd); - var well = await cacheWell.FirstOrDefaultAsync(w => w.Id == idWell, token); var wellType = await cacheWellType.FirstOrDefaultAsync(t => t.Id == well.IdWellType, token); - if (!wellOperations.Any()) - return new StatWellDto() - { - Id = idWell, - Caption = well.Caption, - WellType = wellType.Caption - }; - - var statWellDto = new StatWellDto + var statWellDto = new StatWellDto() { - Id = idWell, + Id = well.Id, Caption = well.Caption, - WellType = wellType?.Caption, - Companies = await wellService.GetCompaniesAsync(idWell, token), - Sections = CalcSectionsStats(wellOperations), - Total = GetStat(wellOperations), + WellType = wellType.Caption }; + + if (!wellOperations.Any()) + return statWellDto; + + statWellDto.Companies = await wellService.GetCompaniesAsync(well.Id, token); + statWellDto.Sections = CalcSectionsStats(wellOperations); + statWellDto.Total = GetStat(wellOperations); + return statWellDto; } @@ -412,7 +399,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService return merged; } - public static List> MergeArrays(IEnumerable array1, IEnumerable array2) + private static List> MergeArrays(IEnumerable array1, IEnumerable array2) { var a1 = array1.ToArray(); var a2 = array2.ToArray(); diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 490a8d41..c2419829 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -48,6 +48,9 @@ namespace AsbCloudInfrastructure.Services return wells.Select(w => From(w)); } + public bool IsCompanyInvolvedInWell(int idCompany, int idWell) + => cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany); + public async Task IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token) => await cacheRelationCompaniesWells.ContainsAsync(r => r.IdWell == idWell && r.IdCompany == idCompany, token).ConfigureAwait(false); diff --git a/AsbCloudWebApi/Controllers/WellOperationStatController.cs b/AsbCloudWebApi/Controllers/WellOperationStatController.cs index 18b9373f..28a97499 100644 --- a/AsbCloudWebApi/Controllers/WellOperationStatController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationStatController.cs @@ -3,6 +3,7 @@ using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -25,6 +26,12 @@ namespace AsbCloudWebApi.Controllers this.wellService = wellService; } + /// + /// Получает статстику по скважинам куста + /// + /// id куста + /// + /// [HttpGet] [Route("cluster/{idCluster}/stat")] [ProducesResponseType(typeof(StatClusterDto), (int)System.Net.HttpStatusCode.OK)] @@ -40,6 +47,29 @@ namespace AsbCloudWebApi.Controllers return Ok(result); } + /// + /// Получает статстику по списку скважин + /// + /// список скважин + /// + /// + [HttpGet] + [Route("wellsStats")] + [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] + public async Task GetWellsStatAsync([FromQuery]IEnumerable idWells, CancellationToken token = default) + { + var protectedIdWells = idWells.Where(CanUserAccessToWell); + var result = await operationsStatService.GetWellsStatAsync(protectedIdWells, token) + .ConfigureAwait(false); + return Ok(result); + } + + /// + /// Получает статистику по скважине + /// + /// id скважины + /// + /// [HttpGet] [Route("well/{idWell}/stat")] [ProducesResponseType(typeof(StatWellDto), (int)System.Net.HttpStatusCode.OK)] @@ -54,7 +84,12 @@ namespace AsbCloudWebApi.Controllers return Ok(result); } - + /// + /// Получает данные для графика глубина-днь + /// + /// + /// + /// [HttpGet] [Route("well/{idWell}/tvd")] [ProducesResponseType(typeof(IEnumerable>), (int)System.Net.HttpStatusCode.OK)] @@ -69,6 +104,12 @@ namespace AsbCloudWebApi.Controllers return Ok(result); } + private bool CanUserAccessToWell(int idWell) + { + int? idCompany = User.GetCompanyId(); + return idCompany is not null && wellService.IsCompanyInvolvedInWell((int)idCompany, idWell); + } + private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token = default) { int? idCompany = User.GetCompanyId();