using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Repository; using Mapster; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services { #nullable enable public class DrillParamsService : CrudServiceBase, IDrillParamsService { private readonly IAsbCloudDbContext db; private readonly ITelemetryService telemetryService; public DrillParamsService(IAsbCloudDbContext context, ITelemetryService telemetryService) : base(context) { this.db = context; 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 drillParamsDto = 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 DrillParamsDto() { IdWell = idWell, Depth = new MinMaxDto { Min = endDepth, Max = startDepth }, IdWellSectionType = 0, AxialLoad = new MinMaxExtendedViewDto { Min = g.Min(t => t.AxialLoad) ?? double.NaN, Avg = g.Average(t => t.AxialLoad) ?? double.NaN, Max = g.Max(t => t.AxialLoad) ?? double.NaN }, Pressure = new MinMaxExtendedViewDto { Min = g.Min(t => t.Pressure) ?? double.NaN, Avg = g.Average(t => t.Pressure) ?? double.NaN, Max = g.Max(t => t.Pressure) ?? double.NaN }, RotorTorque = new MinMaxExtendedViewDto { Min = g.Min(t => t.RotorTorque) ?? double.NaN, Avg = g.Average(t => t.RotorTorque) ?? double.NaN, Max = g.Max(t => t.RotorTorque) ?? double.NaN }, RotorSpeed = new MinMaxExtendedViewDto { Min = g.Min(t => t.RotorSpeed) ?? double.NaN, Avg = g.Average(t => t.RotorSpeed) ?? double.NaN, Max = g.Max(t => t.RotorSpeed) ?? double.NaN }, Flow = new MinMaxExtendedViewDto { Min = g.Min(t => t.Flow) ?? double.NaN, Avg = g.Min(t => t.Flow) ?? double.NaN, Max = g.Min(t => t.Flow) ?? double.NaN } }) .AsNoTracking() .DefaultIfEmpty() .OrderBy(t => t.AxialLoad.Min) .FirstOrDefaultAsync(token) .ConfigureAwait(false); return drillParamsDto; } public async Task> GetAllAsync(int idWell, CancellationToken token = default) { var entities = await (from p in db.DrillParams where p.IdWell == idWell orderby p.Id select p) .AsNoTracking() .ToListAsync(token) .ConfigureAwait(false); var dto = entities.Adapt>(); return dto; } public async Task> GetCompositeAllAsync(int idWell, CancellationToken token = default) { var allDrillParams = await db.WellComposites .Join(db.DrillParams, c => new { IdWell = c.IdWellSrc, IdSection = c.IdWellSectionType }, p => new { IdWell = p.IdWell, IdSection = p.IdWellSectionType }, (c, p) => p) .AsNoTracking() .ToListAsync(token) .ConfigureAwait(false); var compositeWellDrillParams = await db.WellComposites .Where(c => c.IdWell == idWell) .Join(db.DrillParams, c => new { IdWell = c.IdWellSrc, IdSection = c.IdWellSectionType }, p => new { IdWell = p.IdWell, IdSection = p.IdWellSectionType }, (c, p) => p) .AsNoTracking() .ToListAsync(token) .ConfigureAwait(false); var result = compositeWellDrillParams.Select(x => Convert(x, allDrillParams)); return result; } public async Task InsertAsync(int idWell, DrillParamsDto dto, CancellationToken token = default) { dto.IdWell = idWell; var result = await base.InsertAsync(dto, token).ConfigureAwait(false); return result; } public async Task InsertRangeAsync(int idWell, IEnumerable dtos, CancellationToken token = default) { foreach (var dto in dtos) dto.IdWell = idWell; var result = await base.InsertRangeAsync(dtos, token).ConfigureAwait(false); return result; } public async Task SaveAsync(int idWell, IEnumerable dtos, CancellationToken token = default) { db.DrillParams.RemoveRange(db.DrillParams.Where(d => d.IdWell == idWell)); foreach (var dto in dtos) dto.IdWell = idWell; var result = await base.InsertRangeAsync(dtos, token).ConfigureAwait(false); return result; } public async Task UpdateAsync(int idWell, int dtoId, DrillParamsDto dto, CancellationToken token = default) { dto.IdWell = idWell; var result = await base.UpdateAsync(dto, token).ConfigureAwait(false); return result; } private static DrillParamsDto Convert(DrillParams entity, IEnumerable drillParams) { return new DrillParamsDto { Id = entity.Id, IdWellSectionType = entity.IdWellSectionType, AxialLoad = GetMinMaxExtended(entity, drillParams.Select(x => (x.AxialLoadMin, x.AxialLoadMax))), Depth = new MinMaxDto { Min = entity.DepthEnd, Max = entity.DepthStart }, Flow = GetMinMaxExtended(entity, drillParams.Select(x => (x.FlowMin, x.FlowMax))), IdWell = entity.IdWell, Pressure = GetMinMaxExtended(entity, drillParams.Select(x => (x.PressureMin, x.PressureMax))), RotorSpeed = GetMinMaxExtended(entity, drillParams.Select(x => (x.RotorSpeedMin, x.RotorSpeedMax))), RotorTorque = GetMinMaxExtended(entity, drillParams.Select(x => (x.RotorTorqueMin, x.RotorTorqueMax))) }; } private static MinMaxExtendedViewDto GetMinMaxExtended(DrillParams x, IEnumerable<(double min, double max)> allDrillParams) { return new MinMaxExtendedViewDto { Avg = x.AxialLoadAvg, Max = x.AxialLoadMax, Min = x.AxialLoadMin, IsMax = allDrillParams.Max(mx => mx.max) < x.AxialLoadMax, IsMin = allDrillParams.Min(mn => mn.min) > x.AxialLoadMin }; } } #nullable disable }