2022-04-11 18:00:34 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-10-11 15:21:26 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2022-06-16 12:33:05 +05:00
|
|
|
|
using AsbCloudInfrastructure.Repository;
|
2021-10-12 12:02:28 +05:00
|
|
|
|
using Mapster;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-10-11 15:21:26 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
2022-09-20 09:47:54 +05:00
|
|
|
|
#nullable enable
|
2021-10-11 16:43:10 +05:00
|
|
|
|
public class DrillParamsService : CrudServiceBase<DrillParamsDto, DrillParams>, IDrillParamsService
|
2021-10-11 15:21:26 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
|
|
|
|
public DrillParamsService(IAsbCloudDbContext context, ITelemetryService telemetryService)
|
2021-10-11 16:43:10 +05:00
|
|
|
|
: base(context)
|
2021-10-11 15:21:26 +05:00
|
|
|
|
{
|
2021-10-12 11:30:07 +05:00
|
|
|
|
this.db = context;
|
2021-10-11 15:21:26 +05:00
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2022-09-20 10:52:57 +05:00
|
|
|
|
public async Task<DrillParamsDto?> GetDefaultDrillParamsAsync(int idWell,
|
2021-10-12 11:30:07 +05:00
|
|
|
|
double startDepth, double endDepth, CancellationToken token = default)
|
2021-10-11 15:21:26 +05:00
|
|
|
|
{
|
|
|
|
|
var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell);
|
|
|
|
|
|
|
|
|
|
if (idTelemetry is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-10-13 15:56:23 +05:00
|
|
|
|
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()
|
2021-10-12 11:30:07 +05:00
|
|
|
|
{
|
2021-10-13 15:56:23 +05:00
|
|
|
|
IdWell = idWell,
|
2022-09-20 10:52:57 +05:00
|
|
|
|
Depth = new MinMaxDto<double>
|
|
|
|
|
{
|
2022-09-20 09:47:54 +05:00
|
|
|
|
Min = endDepth,
|
|
|
|
|
Max = startDepth
|
|
|
|
|
},
|
2021-10-13 15:56:23 +05:00
|
|
|
|
IdWellSectionType = 0,
|
2022-09-20 10:52:57 +05:00
|
|
|
|
AxialLoad = new MinMaxExtendedViewDto
|
|
|
|
|
{
|
2022-09-20 09:47:54 +05:00
|
|
|
|
Min = g.Min(t => t.AxialLoad) ?? double.NaN,
|
|
|
|
|
Avg = g.Average(t => t.AxialLoad) ?? double.NaN,
|
|
|
|
|
Max = g.Max(t => t.AxialLoad) ?? double.NaN
|
|
|
|
|
},
|
2022-09-20 10:52:57 +05:00
|
|
|
|
Pressure = new MinMaxExtendedViewDto
|
|
|
|
|
{
|
2022-09-20 09:47:54 +05:00
|
|
|
|
Min = g.Min(t => t.Pressure) ?? double.NaN,
|
|
|
|
|
Avg = g.Average(t => t.Pressure) ?? double.NaN,
|
|
|
|
|
Max = g.Max(t => t.Pressure) ?? double.NaN
|
|
|
|
|
},
|
2022-09-20 10:52:57 +05:00
|
|
|
|
RotorTorque = new MinMaxExtendedViewDto
|
|
|
|
|
{
|
2022-09-20 09:47:54 +05:00
|
|
|
|
Min = g.Min(t => t.RotorTorque) ?? double.NaN,
|
|
|
|
|
Avg = g.Average(t => t.RotorTorque) ?? double.NaN,
|
|
|
|
|
Max = g.Max(t => t.RotorTorque) ?? double.NaN
|
|
|
|
|
},
|
2022-09-20 10:52:57 +05:00
|
|
|
|
RotorSpeed = new MinMaxExtendedViewDto
|
|
|
|
|
{
|
2022-09-20 09:47:54 +05:00
|
|
|
|
Min = g.Min(t => t.RotorSpeed) ?? double.NaN,
|
|
|
|
|
Avg = g.Average(t => t.RotorSpeed) ?? double.NaN,
|
|
|
|
|
Max = g.Max(t => t.RotorSpeed) ?? double.NaN
|
|
|
|
|
},
|
2022-09-20 10:52:57 +05:00
|
|
|
|
Flow = new MinMaxExtendedViewDto
|
|
|
|
|
{
|
2022-09-20 09:47:54 +05:00
|
|
|
|
Min = g.Min(t => t.Flow) ?? double.NaN,
|
|
|
|
|
Avg = g.Min(t => t.Flow) ?? double.NaN,
|
|
|
|
|
Max = g.Min(t => t.Flow) ?? double.NaN
|
|
|
|
|
}
|
2021-10-12 11:30:07 +05:00
|
|
|
|
})
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.DefaultIfEmpty()
|
2022-09-20 09:47:54 +05:00
|
|
|
|
.OrderBy(t => t.AxialLoad.Min)
|
2021-10-12 11:30:07 +05:00
|
|
|
|
.FirstOrDefaultAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-10-11 15:21:26 +05:00
|
|
|
|
|
2021-10-13 15:56:23 +05:00
|
|
|
|
return drillParamsDto;
|
2021-10-11 15:21:26 +05:00
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-12 12:02:28 +05:00
|
|
|
|
public async Task<IEnumerable<DrillParamsDto>> GetAllAsync(int idWell,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2022-09-20 15:09:01 +05:00
|
|
|
|
var entities = await db.DrillParams
|
|
|
|
|
.Where(p => p.IdWell == idWell)
|
|
|
|
|
.OrderBy(p=> p.Id)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToArrayAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var dtos = entities.Select(p =>
|
|
|
|
|
{
|
|
|
|
|
var dto = new DrillParamsDto
|
|
|
|
|
{
|
|
|
|
|
IdWell = p.IdWell,
|
|
|
|
|
Id = p.Id,
|
|
|
|
|
IdWellSectionType = p.IdWellSectionType,
|
|
|
|
|
Depth = new MinMaxDto<double> { Max = p.PressureMax, Min = p.PressureMin },
|
|
|
|
|
Pressure = MakeMinMaxExtended(p.PressureAvg, p.PressureMax, p.PressureMin),
|
|
|
|
|
AxialLoad = MakeMinMaxExtended(p.AxialLoadAvg, p.AxialLoadMax, p.AxialLoadMin),
|
|
|
|
|
Flow = MakeMinMaxExtended(p.FlowAvg, p.FlowMax, p.FlowMin),
|
|
|
|
|
RotorSpeed = MakeMinMaxExtended(p.RotorSpeedAvg, p.RotorSpeedMax, p.RotorSpeedMin),
|
|
|
|
|
RotorTorque = MakeMinMaxExtended(p.RotorTorqueAvg, p.RotorTorqueMax, p.RotorTorqueMin)
|
|
|
|
|
};
|
|
|
|
|
return dto;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return dtos;
|
2021-10-12 12:02:28 +05:00
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-13 12:27:40 +05:00
|
|
|
|
public async Task<IEnumerable<DrillParamsDto>> GetCompositeAllAsync(int idWell,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2022-09-20 12:29:55 +05:00
|
|
|
|
var allDrillParamsQuery = db.WellComposites
|
|
|
|
|
.Where(c => c.IdWell == idWell)
|
2022-09-20 09:47:54 +05:00
|
|
|
|
.Join(db.DrillParams,
|
2022-09-20 12:29:55 +05:00
|
|
|
|
c => c.IdWellSrc,
|
|
|
|
|
p => p.IdWell,
|
|
|
|
|
(c, p) => p);
|
|
|
|
|
|
|
|
|
|
var allDrillParams = await allDrillParamsQuery
|
2022-09-20 09:47:54 +05:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2022-09-20 12:29:55 +05:00
|
|
|
|
var compositeWellDrillParamsQuery = db.WellComposites
|
2022-09-20 09:47:54 +05:00
|
|
|
|
.Where(c => c.IdWell == idWell)
|
|
|
|
|
.Join(db.DrillParams,
|
|
|
|
|
c => new { IdWell = c.IdWellSrc, IdSection = c.IdWellSectionType },
|
|
|
|
|
p => new { IdWell = p.IdWell, IdSection = p.IdWellSectionType },
|
2022-09-20 12:29:55 +05:00
|
|
|
|
(c, p) => p);
|
|
|
|
|
|
|
|
|
|
var compositeWellDrillParams = await compositeWellDrillParamsQuery
|
2022-09-20 09:47:54 +05:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var result = compositeWellDrillParams.Select(x => Convert(x, allDrillParams));
|
2022-09-19 15:55:53 +05:00
|
|
|
|
return result;
|
2021-10-13 12:27:40 +05:00
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-13 12:27:40 +05:00
|
|
|
|
public async Task<int> InsertAsync(int idWell, DrillParamsDto dto,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
dto.IdWell = idWell;
|
|
|
|
|
|
2021-10-13 15:56:23 +05:00
|
|
|
|
var result = await base.InsertAsync(dto, token).ConfigureAwait(false);
|
2021-10-13 12:27:40 +05:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> InsertRangeAsync(int idWell, IEnumerable<DrillParamsDto> dtos,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
foreach (var dto in dtos)
|
|
|
|
|
dto.IdWell = idWell;
|
|
|
|
|
|
2021-10-13 15:56:23 +05:00
|
|
|
|
var result = await base.InsertRangeAsync(dtos, token).ConfigureAwait(false);
|
2021-10-13 12:27:40 +05:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-10-14 17:57:25 +05:00
|
|
|
|
|
|
|
|
|
public async Task<int> SaveAsync(int idWell, IEnumerable<DrillParamsDto> 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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
public async Task<int> UpdateAsync(int idWell, int dtoId, DrillParamsDto dto,
|
2021-10-13 12:27:40 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
dto.IdWell = idWell;
|
|
|
|
|
|
2022-06-09 11:19:52 +05:00
|
|
|
|
var result = await base.UpdateAsync(dto, token).ConfigureAwait(false);
|
2021-10-13 12:27:40 +05:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-09-19 15:55:53 +05:00
|
|
|
|
|
2022-09-20 09:47:54 +05:00
|
|
|
|
private static DrillParamsDto Convert(DrillParams entity, IEnumerable<DrillParams> drillParams)
|
2022-09-19 15:55:53 +05:00
|
|
|
|
{
|
|
|
|
|
return new DrillParamsDto
|
|
|
|
|
{
|
2022-09-20 09:47:54 +05:00
|
|
|
|
Id = entity.Id,
|
|
|
|
|
IdWellSectionType = entity.IdWellSectionType,
|
2022-09-20 15:09:01 +05:00
|
|
|
|
AxialLoad = MakeMinMaxExtended(entity.AxialLoadAvg, entity.AxialLoadMax, entity.AxialLoadMin, drillParams.Select(x => (x.AxialLoadMin, x.AxialLoadMax))),
|
2022-09-20 10:52:57 +05:00
|
|
|
|
Depth = new MinMaxDto<double> {
|
2022-09-20 09:47:54 +05:00
|
|
|
|
Min = entity.DepthEnd,
|
|
|
|
|
Max = entity.DepthStart
|
|
|
|
|
},
|
2022-09-20 15:09:01 +05:00
|
|
|
|
Flow = MakeMinMaxExtended(entity.FlowAvg, entity.FlowMax, entity.FlowMin, drillParams.Select(x => (x.FlowMin, x.FlowMax))),
|
2022-09-20 09:47:54 +05:00
|
|
|
|
IdWell = entity.IdWell,
|
2022-09-20 15:09:01 +05:00
|
|
|
|
Pressure = MakeMinMaxExtended(entity.PressureAvg, entity.PressureMax, entity.PressureMin, drillParams.Select(x => (x.PressureMin, x.PressureMax))),
|
|
|
|
|
RotorSpeed = MakeMinMaxExtended(entity.RotorSpeedAvg, entity.RotorSpeedMax, entity.RotorSpeedMin, drillParams.Select(x => (x.RotorSpeedMin, x.RotorSpeedMax))),
|
|
|
|
|
RotorTorque = MakeMinMaxExtended(entity.RotorTorqueAvg, entity.RotorTorqueMax, entity.RotorTorqueMin, drillParams.Select(x => (x.RotorTorqueMin, x.RotorTorqueMax)))
|
2022-09-19 15:55:53 +05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 15:09:01 +05:00
|
|
|
|
private static MinMaxExtendedViewDto MakeMinMaxExtended(double avg, double max, double min, IEnumerable<(double min, double max)>? allDrillParams = null)
|
2022-09-20 10:52:57 +05:00
|
|
|
|
=> new MinMaxExtendedViewDto {
|
|
|
|
|
Avg = avg,
|
|
|
|
|
Max = max,
|
|
|
|
|
Min = min,
|
2022-09-20 15:09:01 +05:00
|
|
|
|
IsMax = allDrillParams?.Any(mx => mx.max > max) ?? false,
|
|
|
|
|
IsMin = allDrillParams?.Any(mn => mn.min < min) ?? false
|
|
|
|
|
};
|
2021-10-11 15:21:26 +05:00
|
|
|
|
}
|
2022-09-20 09:47:54 +05:00
|
|
|
|
#nullable disable
|
2021-10-11 15:21:26 +05:00
|
|
|
|
}
|