DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/ProcessMaps/Report/ProcessMapReportDataSaubStatService.cs

357 lines
17 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMapPlan;
using AsbCloudApp.Data.ProcessMaps.Report;
using AsbCloudApp.Exceptions;
2024-02-13 16:35:01 +05:00
using AsbCloudApp.Extensions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudApp.Services.ProcessMaps.WellDrilling;
using AsbCloudDb.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.ProcessMaps.Report
{
public class ProcessMapReportDataSaubStatService : IProcessMapReportDataSaubStatService
{
private readonly IWellService wellService;
private readonly IChangeLogRepository<ProcessMapPlanDrillingDto, ProcessMapPlanBaseRequestWithWell> processMapPlanBaseRepository;
private readonly IDataSaubStatRepository dataSaubStatRepository;
private readonly IWellOperationRepository wellOperationRepository;
2024-02-13 17:29:20 +05:00
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
public ProcessMapReportDataSaubStatService(IWellService wellService,
IChangeLogRepository<ProcessMapPlanDrillingDto, ProcessMapPlanBaseRequestWithWell> processMapPlanBaseRepository,
IDataSaubStatRepository dataSaubStatRepository,
2024-02-13 17:29:20 +05:00
IWellOperationRepository wellOperationRepository,
IWellOperationCategoryRepository wellOperationCategoryRepository
)
{
this.wellService = wellService;
this.processMapPlanBaseRepository = processMapPlanBaseRepository;
this.dataSaubStatRepository = dataSaubStatRepository;
this.wellOperationRepository = wellOperationRepository;
2024-02-13 17:29:20 +05:00
this.wellOperationCategoryRepository = wellOperationCategoryRepository;
}
public async Task<IEnumerable<ProcessMapReportDataSaubStatDto>> GetAsync(int idWell, DataSaubStatRequest request, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(idWell, token)
?? throw new ArgumentInvalidException(nameof(idWell), $"Скважина с Id: {idWell} не найдена");
if (!well.IdTelemetry.HasValue)
return Enumerable.Empty<ProcessMapReportDataSaubStatDto>();
var requestProcessMapPlan = new ProcessMapPlanBaseRequestWithWell(idWell);
var processMapPlanWellDrillings = await processMapPlanBaseRepository.Get(requestProcessMapPlan, token);
if (!processMapPlanWellDrillings.Any())
return Enumerable.Empty<ProcessMapReportDataSaubStatDto>();
2024-02-13 16:35:01 +05:00
var geDepth = processMapPlanWellDrillings.Min(p => p.DepthStart);
var leDepth = processMapPlanWellDrillings.Max(p => p.DepthEnd);
var requestWellOperationFact = new WellOperationRequest()
{
IdWell = idWell,
2024-02-13 16:35:01 +05:00
OperationType = WellOperation.IdOperationTypeFact,
GeDepth = geDepth,
LeDepth = leDepth
};
var wellOperations = await wellOperationRepository
.GetAsync(requestWellOperationFact, token);
if (!wellOperations.Any())
return Enumerable.Empty<ProcessMapReportDataSaubStatDto>();
2024-02-13 16:35:01 +05:00
var dataSaubStats =
(await dataSaubStatRepository.GetAsync(well.IdTelemetry.Value, geDepth, leDepth, token)).ToArray();
if (!dataSaubStats.Any())
return Enumerable.Empty<ProcessMapReportDataSaubStatDto>();
2024-02-14 11:33:35 +05:00
var wellOperationCategories = wellOperationCategoryRepository.Get(false);
var wellSectionTypes = wellOperationRepository.GetSectionTypes();
2024-02-13 16:35:01 +05:00
2024-02-14 11:33:35 +05:00
var result = CalcByIntervals(
request,
processMapPlanWellDrillings,
dataSaubStats,
wellOperations,
wellOperationCategories,
wellSectionTypes);
return result;
}
private IEnumerable<ProcessMapReportDataSaubStatDto> CalcByIntervals(
DataSaubStatRequest request,
IEnumerable<ProcessMapPlanDrillingDto> processMapPlanWellDrillings,
Span<DataSaubStatDto> dataSaubStats,
2024-02-14 11:33:35 +05:00
IEnumerable<WellOperationDto> wellOperations,
IEnumerable<WellOperationCategoryDto> wellOperationCategories,
IEnumerable<WellSectionTypeDto> wellSectionTypes
)
{
var list = new List<ProcessMapReportDataSaubStatDto>();
var firstElemInInterval = dataSaubStats[0];
2024-02-13 17:29:20 +05:00
int GetSection(DataSaubStatDto data)
=> wellOperations.MinBy(o => data.DateStart - o.DateStart)!.IdWellSectionType;
ProcessMapPlanDrillingDto? GetProcessMapPlan(int idWellSectionType, DataSaubStatDto data)
=> processMapPlanWellDrillings
.Where(p => p.IdWellSectionType == idWellSectionType)
.Where(p => p.DepthStart <= data.DepthStart)
.Where(p => p.DepthEnd >= data.DepthStart)
2024-02-14 13:12:25 +05:00
.Where(p => IsModeMatchOperationCategory(p.IdMode, data.IdCategory))
2024-02-13 17:29:20 +05:00
.WhereActualAtMoment(data.DateStart)
.FirstOrDefault();
var idWellSectionType = GetSection(firstElemInInterval);
var prevProcessMapPlan = GetProcessMapPlan(idWellSectionType, firstElemInInterval);
var indexStart = 0;
2024-02-13 17:29:20 +05:00
for (var i = 1; i < dataSaubStats.Length; i++)
{
var currentElem = dataSaubStats[i];
2024-02-13 17:29:20 +05:00
idWellSectionType = GetSection(currentElem);
var processMapPlan = GetProcessMapPlan(idWellSectionType, currentElem);
if (IsNewInterval(currentElem, firstElemInInterval, request) || i == dataSaubStats.Length - 1 || processMapPlan != prevProcessMapPlan)
{
2024-02-13 17:29:20 +05:00
prevProcessMapPlan = processMapPlan;
var length = i - indexStart;
2024-02-13 16:35:01 +05:00
var span = dataSaubStats.Slice(indexStart, length);
indexStart = i;
firstElemInInterval = currentElem;
2024-02-13 16:35:01 +05:00
var firstElemInSpan = span[0];
var lastElemInISpan = span[^1];
2024-02-14 11:33:35 +05:00
var wellOperationCategoryName = wellOperationCategories
.Where(c => c.Id == firstElemInSpan.IdCategory)
.FirstOrDefault()?.Name ?? string.Empty;
var wellSectionTypeName = wellSectionTypes
.Where(c => c.Id == idWellSectionType)
.FirstOrDefault()?.Caption ?? string.Empty;
2024-02-14 13:12:25 +05:00
var elem = CalcStat(processMapPlan, span, wellOperationCategoryName, wellSectionTypeName);
2024-02-13 16:35:01 +05:00
if (elem is not null)
list.Add(elem);
}
}
return list;
}
2024-02-13 17:29:20 +05:00
private bool IsModeMatchOperationCategory(int idMode, int idCategory)
{
2024-02-14 13:12:25 +05:00
return (idMode == 1 && idCategory == 5003) || (idMode == 2 && idCategory == 5002);
2024-02-13 17:29:20 +05:00
}
private ProcessMapReportDataSaubStatDto? CalcStat(
2024-02-13 17:29:20 +05:00
ProcessMapPlanDrillingDto? processMapPlanFilteredByDepth,
2024-02-14 11:33:35 +05:00
Span<DataSaubStatDto> span,
string wellOperationCategoryName,
string wellSectionTypeName
)
{
var firstElemInInterval = span[0];
var lastElemInInterval = span[^1];
var deltaDepth = lastElemInInterval.DepthEnd - firstElemInInterval.DepthStart;
2024-02-13 16:35:01 +05:00
var aggregatedValues = CalcAggregate(span);
return new ProcessMapReportDataSaubStatDto()
{
2024-02-13 16:35:01 +05:00
DateStart = firstElemInInterval.DateStart.DateTime,
2024-02-14 11:33:35 +05:00
WellSectionTypeName = wellSectionTypeName,
DepthStart = firstElemInInterval.DepthStart,
DepthEnd = lastElemInInterval.DepthEnd,
DeltaDepth = deltaDepth,
2024-02-13 16:35:01 +05:00
DrilledTime = aggregatedValues.DrilledTime,
DrillingMode = wellOperationCategoryName,
PressureDiff = new ProcessMapReportDataSaubStatParamsDto()
{
2024-02-14 11:33:35 +05:00
SetpointPlan = processMapPlanFilteredByDepth?.DeltaPressurePlan,
SetpointFact = firstElemInInterval.PressureSp - firstElemInInterval.PressureIdle,
FactWavg = aggregatedValues.Pressure,
2024-02-14 11:33:35 +05:00
Limit = processMapPlanFilteredByDepth?.DeltaPressureLimitMax,
SetpointUsage = aggregatedValues.SetpointUsagePressure
},
AxialLoad = new ProcessMapReportDataSaubStatParamsDto()
{
2024-02-14 11:33:35 +05:00
SetpointPlan = processMapPlanFilteredByDepth?.AxialLoadPlan,
SetpointFact = aggregatedValues.AxialLoadSp,
FactWavg = aggregatedValues.AxialLoad,
2024-02-14 11:33:35 +05:00
Limit = processMapPlanFilteredByDepth?.AxialLoadLimitMax,
SetpointUsage = aggregatedValues.SetpointUsageAxialLoad
},
TopDriveTorque = new ProcessMapReportDataSaubStatParamsDto()
{
2024-02-14 11:33:35 +05:00
SetpointPlan = processMapPlanFilteredByDepth?.TopDriveTorquePlan,
SetpointFact = aggregatedValues.RotorTorqueSp,
FactWavg = aggregatedValues.RotorTorque,
FactMax = aggregatedValues.RotorTorqueMax,
2024-02-14 11:33:35 +05:00
Limit = processMapPlanFilteredByDepth?.TopDriveTorqueLimitMax,
SetpointUsage = aggregatedValues.SetpointUsageRotorTorque
},
SpeedLimit = new ProcessMapReportDataSaubStatParamsDto
{
2024-02-14 11:33:35 +05:00
SetpointPlan = processMapPlanFilteredByDepth?.RopPlan,
SetpointFact = aggregatedValues.BlockSpeedSp,
2024-02-13 16:35:01 +05:00
FactWavg = deltaDepth / aggregatedValues.DrilledTime,
SetpointUsage = aggregatedValues.SetpointUsageRopPlan
},
Turnover = new ProcessMapReportDataSaubStatParamsDto
{
2024-02-14 11:33:35 +05:00
SetpointPlan = processMapPlanFilteredByDepth?.TopDriveSpeedPlan,
FactWavg = aggregatedValues.RotorSpeed,
FactMax = aggregatedValues.RotorSpeedMax
},
Flow = new ProcessMapReportDataSaubStatParamsDto
{
2024-02-14 11:33:35 +05:00
SetpointPlan = processMapPlanFilteredByDepth?.FlowPlan,
FactWavg = aggregatedValues.MaxFlow,
2024-02-14 11:33:35 +05:00
Limit = processMapPlanFilteredByDepth?.FlowLimitMax,
},
Rop = new PlanFactDto<double?>
{
2024-02-14 13:12:25 +05:00
Plan = processMapPlanFilteredByDepth?.RopPlan,
2024-02-13 16:35:01 +05:00
Fact = deltaDepth / aggregatedValues.DrilledTime
},
};
}
private (
double Pressure,
double AxialLoadSp,
double AxialLoad,
double RotorTorqueSp,
double RotorTorque,
double RotorTorqueMax,
double BlockSpeedSp,
double RotorSpeed,
double RotorSpeedMax,
double MaxFlow,
double SetpointUsagePressure,
double SetpointUsageAxialLoad,
double SetpointUsageRotorTorque,
2024-02-13 16:35:01 +05:00
double SetpointUsageRopPlan,
double DrilledTime
) CalcAggregate(Span<DataSaubStatDto> span)
{
var sumPressure = 0.0;
var sumAxialLoadSp = 0.0;
var sumAxialLoad = 0.0;
var sumRotorTorqueSp = 0.0;
var sumRotorTorque = 0.0;
var sumBlockSpeedSp = 0.0;
var sumRotorSpeed = 0.0;
var maxFlow = 0.0;
var maxRotorTorque = 0.0;
var maxRotorSpeed = 0.0;
var sumDiffDepthByPressure = 0.0;
var sumDiffDepthByAxialLoad = 0.0;
var sumDiffDepthByRotorTorque = 0.0;
var sumDiffDepthByRopPlan = 0.0;
var diffDepthTotal = 0.0;
2024-02-13 16:35:01 +05:00
var drilledTime = 0.0;
for (var i = 0; i < span.Length; i++)
{
var diffDepth = span[i].DepthEnd - span[i].DepthStart;
2024-02-14 10:51:20 +05:00
sumPressure += diffDepth * (span[i].Pressure - (span[i].PressureIdle ?? 0.0));
sumAxialLoadSp += diffDepth * (span[i].AxialLoadSp ?? 0);
sumAxialLoad += diffDepth * span[i].AxialLoad;
sumRotorTorqueSp += diffDepth * (span[i].RotorTorqueSp ?? 0);
sumRotorTorque += diffDepth * span[i].RotorTorque;
sumBlockSpeedSp += diffDepth * (span[i].BlockSpeedSp ?? 0);
sumRotorSpeed += diffDepth * span[i].RotorSpeed;
maxFlow = span[i].Flow > maxFlow ? span[i].Flow : maxFlow;
maxRotorTorque = span[i].RotorTorque > maxRotorTorque ? span[i].RotorTorque : maxRotorTorque;
maxRotorSpeed = span[i].RotorSpeed > maxRotorSpeed ? span[i].RotorSpeed : maxRotorSpeed;
if (span[i].IdFeedRegulator == LimitingParameterDto.Pressure)
2024-02-14 10:51:20 +05:00
sumDiffDepthByPressure += diffDepth;
if (span[i].IdFeedRegulator == LimitingParameterDto.AxialLoad)
2024-02-14 10:51:20 +05:00
sumDiffDepthByAxialLoad += diffDepth;
if (span[i].IdFeedRegulator == LimitingParameterDto.RotorTorque)
2024-02-14 10:51:20 +05:00
sumDiffDepthByRotorTorque += diffDepth;
if (span[i].IdFeedRegulator == LimitingParameterDto.RopPlan)
2024-02-14 10:51:20 +05:00
sumDiffDepthByRopPlan += diffDepth;
diffDepthTotal += diffDepth;
2024-02-13 16:35:01 +05:00
drilledTime += (span[i].DateEnd - span[i].DateStart).TotalHours;
}
return (
Pressure: sumPressure / diffDepthTotal,
AxialLoadSp: sumAxialLoadSp / diffDepthTotal,
AxialLoad: sumAxialLoad / diffDepthTotal,
RotorTorqueSp: sumRotorTorqueSp / diffDepthTotal,
RotorTorque: sumRotorTorque / diffDepthTotal,
RotorTorqueMax: maxRotorTorque,
BlockSpeedSp: sumBlockSpeedSp / diffDepthTotal,
RotorSpeed: sumRotorSpeed / diffDepthTotal,
RotorSpeedMax: maxRotorSpeed,
MaxFlow: maxFlow,
SetpointUsagePressure: sumDiffDepthByPressure / diffDepthTotal,
SetpointUsageAxialLoad: sumDiffDepthByAxialLoad / diffDepthTotal,
SetpointUsageRotorTorque: sumDiffDepthByRotorTorque / diffDepthTotal,
2024-02-13 16:35:01 +05:00
SetpointUsageRopPlan: sumDiffDepthByRopPlan / diffDepthTotal,
DrilledTime: drilledTime
);
}
private double CalcRopPlan(ProcessMapPlanDrillingDto[] processMapPlanFilteredByDepth)
{
var sumRopPlan = 0.0;
var diffDepthTotal = 0.0;
for (var i = 0; i < processMapPlanFilteredByDepth.Length; i++)
{
var diffDepth = processMapPlanFilteredByDepth[i].DepthEnd - processMapPlanFilteredByDepth[i].DepthStart;
sumRopPlan += diffDepth * processMapPlanFilteredByDepth[i].RopPlan;
diffDepthTotal += diffDepth;
}
return sumRopPlan / diffDepthTotal;
}
private bool IsNewInterval(DataSaubStatDto currentElem, DataSaubStatDto firstElem, DataSaubStatRequest request)
{
bool isNewElemBySpeed(double currentSpeed, double firstSpeed)
{
//2. Изменение уставки скорости подачи от первого значения в начале интервала при условии:
//скорость > 80 м/ч => изменение уставки на ± 20 м/ч;
//скорость > 30 м/ч => изменение уставки на ± 15 м/ч;
//скорость <= 30 м/ч => изменение уставки на ± 5 м/ч;
if (firstSpeed > 80)
return Math.Abs(currentSpeed - firstSpeed) >= 20;
else if (firstSpeed > 30)
return Math.Abs(currentSpeed - firstSpeed) >= 15;
else
return Math.Abs(currentSpeed - firstSpeed) >= 5;
}
var isNewElem = (currentElem.IdCategory != firstElem.IdCategory)
|| (Math.Abs(currentElem.Pressure - firstElem.Pressure) >= request.DeltaPressure)
|| (Math.Abs(currentElem.AxialLoad - firstElem.AxialLoad) >= request.DeltaAxialLoad)
|| (Math.Abs(currentElem.RotorTorque - firstElem.RotorTorque) >= request.DeltaRotorTorque)
|| (Math.Abs((currentElem.AxialLoadSp ?? 0) - (firstElem.AxialLoadSp ?? 0)) >= request.DeltaAxialLoadSp)
|| (Math.Abs((currentElem.RotorTorqueSp ?? 0) - (firstElem.RotorTorqueSp ?? 0)) >= request.DeltaRotorTorqueSp)
|| (isNewElemBySpeed(currentElem.Speed, firstElem.Speed));
return isNewElem;
}
}
}