From afd28017157f1ec120e03e704cb0b1bfdc7bec58 Mon Sep 17 00:00:00 2001 From: KharchenkoVladimir Date: Mon, 11 Oct 2021 15:21:26 +0500 Subject: [PATCH] CS2-94: Added main DrillParams controller and service methods --- AsbCloudApp/Data/DrillParamsDto.cs | 45 ++++++ AsbCloudApp/Services/IDrillParamsService.cs | 19 +++ .../Services/IDrillingParamsService.cs | 13 -- AsbCloudDb/Model/AsbCloudDbContext.cs | 23 +-- AsbCloudDb/Model/DrillParams.cs | 16 +- AsbCloudDb/Model/WellSectionType.cs | 4 + AsbCloudInfrastructure/DependencyInjection.cs | 2 +- .../Services/DrillParamsService.cs | 118 +++++++++++++++ .../Services/DrillingParamsService.cs | 14 -- .../Controllers/DrillParamsController.cs | 137 ++++++++++++++++++ .../Controllers/DrillingParamsController.cs | 22 --- 11 files changed, 343 insertions(+), 70 deletions(-) create mode 100644 AsbCloudApp/Data/DrillParamsDto.cs create mode 100644 AsbCloudApp/Services/IDrillParamsService.cs delete mode 100644 AsbCloudApp/Services/IDrillingParamsService.cs create mode 100644 AsbCloudInfrastructure/Services/DrillParamsService.cs delete mode 100644 AsbCloudInfrastructure/Services/DrillingParamsService.cs create mode 100644 AsbCloudWebApi/Controllers/DrillParamsController.cs delete mode 100644 AsbCloudWebApi/Controllers/DrillingParamsController.cs diff --git a/AsbCloudApp/Data/DrillParamsDto.cs b/AsbCloudApp/Data/DrillParamsDto.cs new file mode 100644 index 00000000..cd1bda14 --- /dev/null +++ b/AsbCloudApp/Data/DrillParamsDto.cs @@ -0,0 +1,45 @@ +namespace AsbCloudApp.Data +{ + public class DrillParamsDto + { + public int Id { get; set; } + + public int IdWell { get; set; } + + public string WellSectionTypeName { get; set; } + + public double DepthStart { get; set; } + + public double DepthEnd { get; set; } + + public double AxialLoadMin { get; set; } + + public double AxialLoadAvg { get; set; } + + public double AxialLoadMax { get; set; } + + public double PressureMin { get; set; } + + public double PressureAvg { get; set; } + + public double PressureMax { get; set; } + + public double TopDriveTorqueMin { get; set; } + + public double TopDriveTorqueAvg { get; set; } + + public double TopDriveTorqueMax { get; set; } + + public double TopDriveSpeedMin { get; set; } + + public double TopDriveSpeedAvg { get; set; } + + public double TopDriveSpeedMax { get; set; } + + public double ConsumptionMin { get; set; } + + public double ConsumptionAvg { get; set; } + + public double ConsumptionMax { get; set; } + } +} \ No newline at end of file diff --git a/AsbCloudApp/Services/IDrillParamsService.cs b/AsbCloudApp/Services/IDrillParamsService.cs new file mode 100644 index 00000000..d33d1530 --- /dev/null +++ b/AsbCloudApp/Services/IDrillParamsService.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.Data; + +namespace AsbCloudApp.Services +{ + public interface IDrillParamsService + { + Task GetDefaultDrillParamsAsync(int idWell, double startDepth, + double endDepth, CancellationToken token = default); + + // Task>GetDrillParamsAsync(int idWell, + // DrillParamsDto drillParamsDto, CancellationToken token = default); + + Task SaveDrillParamsAsync(int idWell, DrillParamsDto drillParamsDto, + CancellationToken token = default); + } +} diff --git a/AsbCloudApp/Services/IDrillingParamsService.cs b/AsbCloudApp/Services/IDrillingParamsService.cs deleted file mode 100644 index 4192a696..00000000 --- a/AsbCloudApp/Services/IDrillingParamsService.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AsbCloudApp.Services -{ - public interface IDrillingParamsService - { - - } -} diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index 920d8fac..8e654622 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -180,18 +180,19 @@ namespace AsbCloudDb.Model entity.HasIndex(d => d.DateStart); }); - //modelBuilder.Entity(entity => - //{ - // entity.HasOne(r => r.Well) - // .WithOne(w => w.) - // .HasForeignKey(r => r) - // .HasConstraintName("t_relation_company_well_t_well_id_fk"); + modelBuilder.Entity(entity => + { + // entity.HasOne(d => d.Well) + // .WithOne(p => p.) + // .HasForeignKey(d => d.IdTelemetry) + // .OnDelete(DeleteBehavior.SetNull) + // .HasConstraintName("t_well_t_telemetry_id_fk"); - // entity.HasOne(r => r.WellSectionType) - // .WithOne(w => w.RelationCompaniesWells) - // .HasForeignKey(r => r.IdCompany) - // .HasConstraintName("t_relation_company_well_t_company_id_fk"); - //}); + entity.HasOne(r => r.WellSectionType) + .WithMany(w => w.DrillParamsCollection) + .HasForeignKey(r => r.IdWellSectionType) + .HasConstraintName("t_drill_params_t_well_section_type_id_fk"); + }); FillData(modelBuilder); } diff --git a/AsbCloudDb/Model/DrillParams.cs b/AsbCloudDb/Model/DrillParams.cs index 77bd1b47..f401bf9a 100644 --- a/AsbCloudDb/Model/DrillParams.cs +++ b/AsbCloudDb/Model/DrillParams.cs @@ -1,9 +1,7 @@ using Microsoft.EntityFrameworkCore; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Text.Json.Serialization; -#nullable disable namespace AsbCloudDb.Model { @@ -16,8 +14,14 @@ namespace AsbCloudDb.Model [Column("well_id"), Comment("Id скважины")] public int IdWell { get; set; } + + [Column("depth_start"), Comment("Стартовая глубина")] + public double DepthStart { get; set; } - [Column("wellsection_type_id"), Comment("Id с типом секции скважины")] + [Column("depth_end"), Comment("Глубина окончания интервала")] + public double DepthEnd { get; set; } + + [Column("id_wellsection_type"), Comment("Id с типом секции скважины")] public int IdWellSectionType { get; set; } [Column("axial_load_min"), Comment("Минимальная нагрузка")] @@ -38,12 +42,6 @@ namespace AsbCloudDb.Model [Column("pressure_max"), Comment("Максимальное давление")] public double PressureMax { get; set; } - [Column("depth_start"), Comment("Минимальный момент на ВСП")] - public double DepthStart { get; set; } - - [Column("depth_end"), Comment("Максимальный момент на ВСП")] - public double DepthEnd { get; set; } - [Column("top_drive_min"), Comment("Минимальный момент на ВСП")] public double TopDriveTorqueMin { get; set; } diff --git a/AsbCloudDb/Model/WellSectionType.cs b/AsbCloudDb/Model/WellSectionType.cs index 5570f138..72447c6f 100644 --- a/AsbCloudDb/Model/WellSectionType.cs +++ b/AsbCloudDb/Model/WellSectionType.cs @@ -21,5 +21,9 @@ namespace AsbCloudDb.Model [JsonIgnore] [InverseProperty(nameof(WellOperation.WellSectionType))] public virtual ICollection WellOperations { get; set; } + + [JsonIgnore] + [InverseProperty(nameof(DrillParams.WellSectionType))] + public virtual ICollection DrillParamsCollection { get; set; } } } diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 7798dfe7..1e87aaa2 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -42,7 +42,7 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); // admin crud services: services.AddTransient, CrudServiceBase>(); diff --git a/AsbCloudInfrastructure/Services/DrillParamsService.cs b/AsbCloudInfrastructure/Services/DrillParamsService.cs new file mode 100644 index 00000000..a1c49eb2 --- /dev/null +++ b/AsbCloudInfrastructure/Services/DrillParamsService.cs @@ -0,0 +1,118 @@ +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using Microsoft.EntityFrameworkCore; +using Mapster; + +namespace AsbCloudInfrastructure.Services +{ + public class DrillParamsService : IDrillParamsService + { + private readonly IAsbCloudDbContext db; + private readonly ITelemetryService telemetryService; + + public DrillParamsService(IAsbCloudDbContext db, ITelemetryService telemetryService) + { + this.db = db; + this.telemetryService = telemetryService; + } + public async Task GetDefaultDrillParamsAsync(int idWell, double startDepth, + double endDepth, CancellationToken token = default) + { + var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell); + + if (idTelemetry is null) + return null; + + var axialLoads = await GetDrillParamsQuery(idWell, startDepth, endDepth, + (telemetry) => telemetry.AxialLoad, token); + + var pressures = await GetDrillParamsQuery(idWell, startDepth, endDepth, + (telemetry) => telemetry.Pressure, token); + + // var topDriveTorques = await (from telemetry in db.TelemetryDataSaub + // where telemetry.IdTelemetry == idTelemetry && + // telemetry.WellDepth >= startDepth && + // telemetry.WellDepth <= endDepth + // group telemetry.Pressure by true into g + // select new { Min = g.Min(), Avg = g.Average(), Max = g.Max() }) + // .DefaultIfEmpty() + // .FirstOrDefaultAsync(token); + // + // var topDriveSpeeds = await (from telemetry in db.TelemetryDataSaub + // where telemetry.IdTelemetry == idTelemetry && + // telemetry.WellDepth >= startDepth && + // telemetry.WellDepth <= endDepth + // group telemetry.Pressure by true into g + // select new { Min = g.Min(), Avg = g.Average(), Max = g.Max() }) + // .DefaultIfEmpty() + // .FirstOrDefaultAsync(token); + + // var consumptions = await (from telemetry in db.TelemetryDataSaub + // where telemetry.IdTelemetry == idTelemetry && + // telemetry.WellDepth >= startDepth && + // telemetry.WellDepth <= endDepth + // group telemetry.Pressure by true into g + // select new { Min = g.Min(), Avg = g.Average(), Max = g.Max() }) + // .DefaultIfEmpty() + // .FirstOrDefaultAsync(token); + + var drillParamsDto = new DrillParamsDto() + { + IdWell = idWell, + DepthStart = startDepth, + DepthEnd = endDepth, + AxialLoadMin = axialLoads.Min, + AxialLoadAvg = axialLoads.Avg, + AxialLoadMax = axialLoads.Max, + PressureMin = pressures.Min, + PressureAvg = pressures.Avg, + PressureMax = pressures.Max, + // TopDriveTorqueMin = topDriveTorques.Min ?? 0, + // TopDriveTorqueAvg = topDriveTorques.Avg ?? 0, + // TopDriveTorqueMax = topDriveTorques.Max ?? 0, + // TopDriveSpeedMin = topDriveSpeeds.Min ?? 0, + // TopDriveSpeedAvg = topDriveSpeeds.Avg ?? 0, + // TopDriveSpeedMax = topDriveSpeeds.Max ?? 0, + // ConsumptionMin = consumptions.Min ?? 0, + // ConsumptionAvg = consumptions.Avg ?? 0, + // ConsumptionMax = consumptions.Max ?? 0, + }; + + return drillParamsDto; + } + + public Task SaveDrillParamsAsync(int idWell, DrillParamsDto drillParamsDto, + CancellationToken token = default) + { + var entity = drillParamsDto.Adapt(); + + db.DrillParams.Add(entity); + + return db.SaveChangesAsync(token); + } + + private async Task<(double Min, double Avg, double Max)> GetDrillParamsQuery(int idTelemetry, + double startDepth, double endDepth, Func func, + CancellationToken token = default) + { + var res = await (from telemetry in db.TelemetryDataSaub + where telemetry.IdTelemetry == idTelemetry && + telemetry.WellDepth >= startDepth && + telemetry.WellDepth <= endDepth + group telemetry by telemetry.IdTelemetry into g + select new { Min = g.Min(func), Avg = g.Average(func), Max = g.Max(func) }) + .DefaultIfEmpty() + .AsNoTracking() + .FirstOrDefaultAsync(token) + .ConfigureAwait(false); + + return (res.Min ?? 0, res.Avg ?? 0, res.Max ?? 0); + } + + } +} diff --git a/AsbCloudInfrastructure/Services/DrillingParamsService.cs b/AsbCloudInfrastructure/Services/DrillingParamsService.cs deleted file mode 100644 index 016a340e..00000000 --- a/AsbCloudInfrastructure/Services/DrillingParamsService.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using AsbCloudApp.Services; - -namespace AsbCloudInfrastructure.Services -{ - public class DrillingParamsService : IDrillingParamsService - { - - } -} diff --git a/AsbCloudWebApi/Controllers/DrillParamsController.cs b/AsbCloudWebApi/Controllers/DrillParamsController.cs new file mode 100644 index 00000000..8e7f1678 --- /dev/null +++ b/AsbCloudWebApi/Controllers/DrillParamsController.cs @@ -0,0 +1,137 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using AsbCloudApp.Data; +using AsbCloudApp.Services; + +namespace AsbCloudWebApi.Controllers +{ + /// + /// Контроллер для режимов бурения + /// + [Route("/drillParams")] + [ApiController] + public class DrillParamsController : ControllerBase + { + private readonly IDrillParamsService drillParamsService; + + public DrillParamsController(IDrillParamsService drillParamsService) + { + this.drillParamsService = drillParamsService; + } + + /// + /// Возвращает автоматически расчитанные значения для режимов бурения + /// + /// id скважины + /// Стартовая глубина + /// Конечная глубина + /// Токен отмены задачи + /// Значения по умолчанию для режимов бурения + [HttpGet("idWell/autoParams")] + [ProducesResponseType(typeof(DrillParamsDto), (int) System.Net.HttpStatusCode.OK)] + public async Task GetDefaultDrillParamsAsync(int idWell, double startDepth, + double endDepth, CancellationToken token = default) + { + var idCompany = User.GetCompanyId(); + + if (idCompany is null) + return Forbid(); + + var dto = await drillParamsService.GetDefaultDrillParamsAsync(idWell, startDepth, endDepth, token); + + return Ok(dto); + } + + // /// + // /// Возвращает значения для режимов бурения gj ctrwbzv yf crdf;byt + // /// + // /// id скважины + // /// Токен отмены задачи + // /// Список параметров для режимов бурения на скважине + // [HttpGet("idWell")] + // [ProducesResponseType(typeof(IEnumerable), (int) System.Net.HttpStatusCode.OK)] + // public async Task GetDrillParamsAsync(int idWell, + // CancellationToken token = default) + // { + // var idCompany = User.GetCompanyId(); + // + // if (idCompany is null) + // return Forbid(); + // + // var dto = await drillParamsService.GetDrillParamsAsync(idWell, token); + // + // return Ok(dto); + // } + + /// + /// Сохраняет значения для режимов бурения + /// + /// id скважины + /// Параметры режимов бурений для секции + /// Токен отмены задачи + /// + [HttpPost("idWell")] + [ProducesResponseType(typeof(int), (int) System.Net.HttpStatusCode.OK)] + public async Task SaveDrillParamsAsync(int idWell, + DrillParamsDto drillParamsDto, CancellationToken token = default) + { + var idCompany = User.GetCompanyId(); + + if (idCompany is null) + return Forbid(); + + var result = await drillParamsService.SaveDrillParamsAsync(idWell, + drillParamsDto, token); + + return Ok(result); + } + + // /// + // /// Изменяет значения для режимов бурения + // /// + // /// id скважины + // /// Параметры режимов бурений для секции + // /// Токен отмены задачи + // /// + // [HttpPut("idWell")] + // [ProducesResponseType(typeof(int), (int) System.Net.HttpStatusCode.OK)] + // public async Task EditDrillParamsAsync(int idWell, + // DrillParamsDto drillParamsDto, CancellationToken token = default) + // { + // var idCompany = User.GetCompanyId(); + // + // if (idCompany is null) + // return Forbid(); + // + // var result = await drillParamsService.EditDrillParamsAsync(idWell, + // drillParamsDto, token); + // + // return Ok(result); + // } + + // /// + // /// Удаляет значения для режимов бурения + // /// + // /// id скважины + // /// Параметры режимов бурений для секции + // /// Токен отмены задачи + // /// + // [HttpDelete("idWell")] + // [ProducesResponseType(typeof(int), (int) System.Net.HttpStatusCode.OK)] + // public async Task DeleteDrillParamsAsync(int idWell, + // DrillParamsDto drillParamsDto, CancellationToken token = default) + // { + // var idCompany = User.GetCompanyId(); + // + // if (idCompany is null) + // return Forbid(); + // + // var result = await drillParamsService.DeleteDrillParamsAsync(idWell, + // drillParamsDto, token); + // + // return Ok(result); + // } + } +} diff --git a/AsbCloudWebApi/Controllers/DrillingParamsController.cs b/AsbCloudWebApi/Controllers/DrillingParamsController.cs deleted file mode 100644 index 2aa1cafa..00000000 --- a/AsbCloudWebApi/Controllers/DrillingParamsController.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using AsbCloudApp.Services; - -namespace AsbCloudWebApi.Controllers -{ - [Route("/mode")] - [ApiController] - public class DrillingParamsController : ControllerBase - { - private readonly IDrillingParamsService modeService; - - public DrillingParamsController(IDrillingParamsService modeService) - { - this.modeService = modeService; - } - } -}