2021-10-12 12:02:28 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-10-11 15:21:26 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-10-12 12:02:28 +05:00
|
|
|
|
using Mapster;
|
2021-10-11 15:21:26 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
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;
|
|
|
|
|
|
2021-10-11 16:43:10 +05:00
|
|
|
|
public DrillParamsService(IAsbCloudDbContext context, ITelemetryService telemetryService)
|
|
|
|
|
: 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;
|
|
|
|
|
}
|
2021-10-12 12:02:28 +05:00
|
|
|
|
|
2021-10-12 11:30:07 +05:00
|
|
|
|
public async Task<DrillParamsDto> GetDefaultDrillParamsAsync(int idWell,
|
|
|
|
|
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,
|
|
|
|
|
DepthStart = startDepth,
|
|
|
|
|
DepthEnd = endDepth,
|
|
|
|
|
IdWellSectionType = 0,
|
|
|
|
|
AxialLoadMin = g.Min(t=> t.AxialLoad) ?? double.NaN,
|
|
|
|
|
AxialLoadAvg = g.Average(t => t.AxialLoad) ?? double.NaN,
|
|
|
|
|
AxialLoadMax = g.Max(t => t.AxialLoad) ?? double.NaN,
|
|
|
|
|
PressureMin = g.Min(t=> t.Pressure) ?? double.NaN,
|
|
|
|
|
PressureAvg = g.Average(t => t.Pressure) ?? double.NaN,
|
|
|
|
|
PressureMax = g.Max(t => t.Pressure) ?? double.NaN,
|
|
|
|
|
RotorTorqueMin = g.Min(t=> t.RotorTorque) ?? double.NaN,
|
|
|
|
|
RotorTorqueAvg = g.Average(t => t.RotorTorque) ?? double.NaN,
|
|
|
|
|
RotorTorqueMax = g.Max(t => t.RotorTorque) ?? double.NaN,
|
|
|
|
|
RotorSpeedMin = g.Min(t=> t.RotorSpeed) ?? double.NaN,
|
|
|
|
|
RotorSpeedAvg = g.Average(t => t.RotorSpeed) ?? double.NaN,
|
|
|
|
|
RotorSpeedMax = g.Max(t => t.RotorSpeed) ?? double.NaN,
|
|
|
|
|
FlowMin = g.Min(t => t.Flow) ?? double.NaN,
|
|
|
|
|
FlowAvg = g.Min(t => t.Flow) ?? double.NaN,
|
|
|
|
|
FlowMax = g.Min(t => t.Flow) ?? double.NaN
|
2021-10-12 11:30:07 +05:00
|
|
|
|
})
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.DefaultIfEmpty()
|
2021-10-13 15:56:23 +05:00
|
|
|
|
.OrderBy(t => t.AxialLoadMin)
|
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
|
|
|
|
}
|
2021-10-12 12:02:28 +05:00
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<DrillParamsDto>> GetAllAsync(int idWell,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
var entities = await (from p in db.DrillParams
|
|
|
|
|
where p.IdWell == idWell
|
|
|
|
|
orderby p.Id
|
|
|
|
|
select p)
|
2021-10-13 15:56:23 +05:00
|
|
|
|
.AsNoTracking()
|
2021-10-12 12:02:28 +05:00
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-10-13 15:56:23 +05:00
|
|
|
|
var dto = entities.Adapt<DrillParamsDto>();
|
2021-10-12 12:02:28 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
2021-10-13 12:27:40 +05:00
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<DrillParamsDto>> GetCompositeAllAsync(int idWell,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
var compositeWellDrillParams =
|
|
|
|
|
await (from p in db.DrillParams
|
|
|
|
|
from c in db.WellComposites
|
|
|
|
|
where c.IdWell == idWell &&
|
|
|
|
|
p.IdWell == c.IdWellSrc &&
|
|
|
|
|
p.IdWellSectionType == c.IdWellSectionType
|
|
|
|
|
select p)
|
2021-10-13 15:56:23 +05:00
|
|
|
|
.AsNoTracking()
|
2021-10-13 12:27:40 +05:00
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-10-13 15:56:23 +05:00
|
|
|
|
var compositeDrillParamsDtos = compositeWellDrillParams.Adapt<DrillParamsDto>();
|
2021-10-13 12:27:40 +05:00
|
|
|
|
|
|
|
|
|
return compositeDrillParamsDtos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 15:56:23 +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;
|
|
|
|
|
|
2021-10-13 15:56:23 +05:00
|
|
|
|
var result = await base.UpdateAsync(dtoId, dto, token).ConfigureAwait(false);
|
2021-10-13 12:27:40 +05:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-10-11 15:21:26 +05:00
|
|
|
|
}
|
|
|
|
|
}
|