forked from ddrilling/AsbCloudServer
229 lines
9.5 KiB
C#
229 lines
9.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using AsbCloudApp.Data.ProcessMaps;
|
||
using AsbCloudApp.Data.ProcessMaps.Report;
|
||
using AsbCloudApp.Data.SAUB;
|
||
using AsbCloudApp.Exceptions;
|
||
using AsbCloudApp.Repositories;
|
||
using AsbCloudApp.Services;
|
||
using AsbCloudApp.Services.ProcessMaps.WellDrillingProcessMap;
|
||
using AsbCloudInfrastructure.Services.ProcessMaps.WellDrillingProcessMap.Report.Data;
|
||
|
||
namespace AsbCloudInfrastructure.Services.ProcessMaps.WellDrillingProcessMap.Report;
|
||
|
||
public class WellDrillingProcessMapReportService : IWellDrillingProcessMapReportService
|
||
{
|
||
private readonly IWellService wellService;
|
||
private readonly IWellDrillingProcessMapRepository wellDrillingProcessMapRepository;
|
||
private readonly ITelemetryDataSaubService telemetryDataSaubService;
|
||
private readonly IWellOperationRepository wellOperationRepository;
|
||
|
||
public WellDrillingProcessMapReportService(IWellService wellService,
|
||
IWellDrillingProcessMapRepository wellDrillingProcessMapRepository,
|
||
ITelemetryDataSaubService telemetryDataSaubService,
|
||
IWellOperationRepository wellOperationRepository)
|
||
{
|
||
this.wellService = wellService;
|
||
this.wellDrillingProcessMapRepository = wellDrillingProcessMapRepository;
|
||
this.telemetryDataSaubService = telemetryDataSaubService;
|
||
this.wellOperationRepository = wellOperationRepository;
|
||
}
|
||
|
||
public async Task<IEnumerable<WellDrillingProcessMapReportDto>> GetAsync(int idWell,
|
||
CancellationToken token)
|
||
{
|
||
var well = await wellService.GetOrDefaultAsync(idWell, token)
|
||
?? throw new ArgumentInvalidException(nameof(idWell), $"Скважина с Id: {idWell} не найдена");
|
||
|
||
if (!well.IdTelemetry.HasValue)
|
||
throw new ArgumentInvalidException(nameof(idWell), $"Скважина с Id: {idWell} не имеет телеметрии");
|
||
|
||
var wellDrillingProcessMaps = await wellDrillingProcessMapRepository.GetByIdWellAsync(idWell, token);
|
||
|
||
if (!wellDrillingProcessMaps.Any())
|
||
return Enumerable.Empty<WellDrillingProcessMapReportDto>();
|
||
|
||
var telemetryDataStat =
|
||
(await telemetryDataSaubService.GetTelemetryDataStatAsync(well.IdTelemetry.Value, token)).ToArray();
|
||
|
||
if (!telemetryDataStat.Any())
|
||
return Enumerable.Empty<WellDrillingProcessMapReportDto>();
|
||
|
||
var result = CalcByIntervals(wellDrillingProcessMaps, telemetryDataStat);
|
||
|
||
return result;
|
||
}
|
||
|
||
private IEnumerable<WellDrillingProcessMapReportDto> CalcByIntervals(
|
||
IEnumerable<WellDrillingProcessMapDto> wellDrillingProcessMaps,
|
||
TelemetryDataSaubStatDto[] telemetryDataStat)
|
||
{
|
||
var processMapIntervals = CalcDepthIntervals(wellDrillingProcessMaps);
|
||
|
||
var result = new List<WellDrillingProcessMapReportDto>(processMapIntervals.Count() * 4);
|
||
|
||
var telemetryIndexStart =
|
||
Array.FindIndex(telemetryDataStat, t => t.WellDepthMin >= processMapIntervals.First().DepthStart);
|
||
if (telemetryIndexStart < 0)
|
||
return Enumerable.Empty<WellDrillingProcessMapReportDto>();
|
||
|
||
IDictionary<int, string> sectionTypes = wellOperationRepository
|
||
.GetSectionTypes()
|
||
.ToDictionary(s => s.Id, s => s.Caption);
|
||
|
||
foreach (var interval in processMapIntervals)
|
||
{
|
||
var processMapPlanInterval = wellDrillingProcessMaps
|
||
.Where(p => p.DepthStart <= interval.DepthEnd && p.DepthEnd >= interval.DepthStart);
|
||
|
||
if (!processMapPlanInterval.Any())
|
||
continue;
|
||
|
||
var telemetryIndexEnd = Array.FindIndex(telemetryDataStat, telemetryIndexStart,
|
||
t => t.WellDepthMin >= interval.DepthEnd);
|
||
if (telemetryIndexEnd < 0)
|
||
telemetryIndexEnd = telemetryDataStat.Length - 1;
|
||
var telemetryDataInterval =
|
||
telemetryDataStat.AsSpan(telemetryIndexStart, telemetryIndexEnd - telemetryIndexStart);
|
||
|
||
IEnumerable<WellDrillingProcessMapReportDto> subIntervalsResult =
|
||
CalcSubIntervals(interval, processMapPlanInterval, telemetryDataInterval, sectionTypes);
|
||
|
||
result.AddRange(subIntervalsResult);
|
||
telemetryIndexStart = telemetryIndexEnd;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
private static IEnumerable<(double DepthStart, double DepthEnd)> CalcDepthIntervals(
|
||
IEnumerable<WellDrillingProcessMapDto> wellDrillingProcessMaps)
|
||
{
|
||
if (!wellDrillingProcessMaps.Any())
|
||
yield break;
|
||
|
||
var intervalStarts = wellDrillingProcessMaps
|
||
.OrderBy(i => i.DepthStart)
|
||
.Select(p => p.DepthStart)
|
||
.Distinct()
|
||
.ToArray();
|
||
|
||
for (var i = 1; i < intervalStarts.Length; i++)
|
||
yield return (intervalStarts[i - 1], intervalStarts[i]);
|
||
|
||
yield return (intervalStarts[^1], wellDrillingProcessMaps.Max(p => p.DepthEnd));
|
||
}
|
||
|
||
private static IEnumerable<WellDrillingProcessMapReportDto> CalcSubIntervals(
|
||
(double DepthStart, double DepthEnd) interval,
|
||
IEnumerable<WellDrillingProcessMapDto> wellDrillingProcessMapInterval,
|
||
Span<TelemetryDataSaubStatDto> telemetryDataInterval,
|
||
IDictionary<int, string> sectionTypes)
|
||
{
|
||
var telemetryDataIntervalLength = telemetryDataInterval.Length;
|
||
if (telemetryDataInterval.Length == 0)
|
||
return Enumerable.Empty<WellDrillingProcessMapReportDto>();
|
||
|
||
var result = new List<WellDrillingProcessMapReportDto>();
|
||
var telemetryIndexStart = 0;
|
||
var subInterval = interval;
|
||
|
||
for (var i = telemetryIndexStart + 1; i < telemetryDataIntervalLength; i++)
|
||
{
|
||
if (IsDifferent(telemetryDataInterval[telemetryIndexStart], telemetryDataInterval[i]))
|
||
{
|
||
subInterval.DepthEnd = telemetryDataInterval[i - 1].WellDepthMax;
|
||
var telemetryRowSpan = telemetryDataInterval[telemetryIndexStart..(i - 1)];
|
||
|
||
if (!telemetryRowSpan.IsEmpty)
|
||
{
|
||
var intervalReportRow = CalcSubIntervalReportRow(subInterval, wellDrillingProcessMapInterval,
|
||
telemetryRowSpan, sectionTypes);
|
||
result.Add(intervalReportRow);
|
||
}
|
||
|
||
telemetryIndexStart = i;
|
||
subInterval.DepthStart = subInterval.DepthEnd;
|
||
}
|
||
}
|
||
|
||
subInterval.DepthEnd = interval.DepthEnd;
|
||
var intervalReportRowLast = CalcSubIntervalReportRow(subInterval, wellDrillingProcessMapInterval,
|
||
telemetryDataInterval[telemetryIndexStart..telemetryDataIntervalLength], sectionTypes);
|
||
result.Add(intervalReportRowLast);
|
||
return result;
|
||
}
|
||
|
||
private static WellDrillingProcessMapReportDto CalcSubIntervalReportRow(
|
||
(double DepthStart, double DepthEnd) subInterval,
|
||
IEnumerable<WellDrillingProcessMapDto> wellDrillingProcessMaps,
|
||
Span<TelemetryDataSaubStatDto> telemetryRowSpan,
|
||
IDictionary<int, string> sectionTypes)
|
||
{
|
||
var telemetryStat = new TelemetryStat(telemetryRowSpan);
|
||
var processMapByMode = wellDrillingProcessMaps.FirstOrDefault(p => p.IdMode == telemetryStat.IdMode);
|
||
var processMapFirst = wellDrillingProcessMaps.First();
|
||
var idWellSectionType = processMapByMode?.IdWellSectionType ?? processMapFirst.IdWellSectionType;
|
||
|
||
var result = new WellDrillingProcessMapReportDto
|
||
{
|
||
IdWell = processMapByMode?.IdWell ?? processMapFirst.IdWell,
|
||
IdWellSectionType = idWellSectionType,
|
||
WellSectionTypeName = sectionTypes[idWellSectionType],
|
||
|
||
DepthStart = subInterval.DepthStart,
|
||
DepthEnd = subInterval.DepthEnd,
|
||
DateStart = telemetryStat.DateStart,
|
||
|
||
MechDrillingHours = telemetryStat.MechDrillingHours,
|
||
DrillingMode = telemetryStat.ModeName,
|
||
|
||
DeltaDepth = telemetryStat.DeltaDepth,
|
||
|
||
PressureDiff = telemetryStat.Pressure.MakeParams(processMapByMode?.Pressure.Plan),
|
||
AxialLoad = telemetryStat.AxialLoad.MakeParams(processMapByMode?.AxialLoad.Plan),
|
||
TopDriveTorque = telemetryStat.RotorTorque.MakeParams(processMapByMode?.TopDriveTorque.Plan),
|
||
SpeedLimit = telemetryStat.BlockSpeed.MakeParams(processMapByMode?.RopPlan),
|
||
|
||
Rop = telemetryStat.Rop,
|
||
UsagePlan = processMapByMode?.UsageSaub ?? telemetryStat.UsagePredictPlan,
|
||
UsageFact = telemetryStat.UsageSaub,
|
||
};
|
||
return result;
|
||
}
|
||
|
||
private static bool IsDifferent(TelemetryDataSaubStatDto intervalStart, TelemetryDataSaubStatDto current)
|
||
{
|
||
if (intervalStart.WellDepthMin > current.WellDepthMin)
|
||
return true;
|
||
|
||
if (intervalStart.IdMode != current.IdMode)
|
||
return true;
|
||
|
||
if (Math.Abs(intervalStart.PressureSp - current.PressureSp) > 5d)
|
||
return true;
|
||
|
||
if (Math.Abs(intervalStart.AxialLoadSp - current.AxialLoadSp) > 1d)
|
||
return true;
|
||
|
||
if (Math.Abs(intervalStart.RotorTorqueSp - current.RotorTorqueSp) > 5d)
|
||
return true;
|
||
|
||
var blockSpeedSpDiff = Math.Abs(intervalStart.BlockSpeedSp - current.BlockSpeedSp);
|
||
if (!(blockSpeedSpDiff > 5d))
|
||
return false;
|
||
|
||
switch (intervalStart.BlockSpeedSp)
|
||
{
|
||
case <= 30:
|
||
case > 30 when blockSpeedSpDiff > 15d:
|
||
case > 80 when blockSpeedSpDiff > 20d:
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
} |