2022-08-15 01:17:00 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-09-21 15:06:47 +05:00
|
|
|
|
using AsbCloudApp.Data.DetectedOperation;
|
2022-08-15 01:17:00 +05:00
|
|
|
|
using AsbCloudApp.Data.Subsystems;
|
2023-09-15 16:48:19 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudApp.Services.Subsystems;
|
2022-08-01 13:55:51 +05:00
|
|
|
|
using AsbCloudDb;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2022-08-01 13:55:51 +05:00
|
|
|
|
using AsbCloudDb.Model.Subsystems;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-07-14 03:47:11 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-08-01 13:55:51 +05:00
|
|
|
|
using System.Linq;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
using System.Threading;
|
2022-07-14 03:47:11 +05:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.Subsystems;
|
|
|
|
|
|
2023-11-28 17:32:14 +05:00
|
|
|
|
/// Todo: Выделить репозиторий
|
2023-10-04 16:41:19 +05:00
|
|
|
|
internal class SubsystemOperationTimeService : ISubsystemOperationTimeService
|
2022-07-14 03:47:11 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
private readonly ICrudRepository<SubsystemDto> subsystemService;
|
|
|
|
|
private readonly IDetectedOperationService detectedOperationService;
|
|
|
|
|
public const int IdSubsystemAKB = 1;
|
|
|
|
|
public const int IdSubsystemAKBRotor = 11;
|
|
|
|
|
public const int IdSubsystemAKBSlide = 12;
|
|
|
|
|
public const int IdSubsystemMSE = 2;
|
|
|
|
|
public const int IdSubsystemSpin = 65536;
|
|
|
|
|
public const int IdSubsystemTorque = 65537;
|
|
|
|
|
|
|
|
|
|
public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudRepository<SubsystemDto> subsystemService, IDetectedOperationService detectedOperationService)
|
2022-07-14 03:47:11 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
this.db = db;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.subsystemService = subsystemService;
|
|
|
|
|
this.detectedOperationService = detectedOperationService;
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<int> DeleteAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} does not exist");
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var query = BuildQuery(request, well);
|
|
|
|
|
db.SubsystemOperationTimes.RemoveRange(query);
|
|
|
|
|
return await db.SaveChangesAsync(token);
|
|
|
|
|
}
|
2022-08-02 15:26:33 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<IEnumerable<SubsystemOperationTimeDto>> GetOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} does not exist");
|
|
|
|
|
|
|
|
|
|
var dtos = await GetOperationTimeAsync(request, well, token);
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
private async Task<IEnumerable<SubsystemOperationTimeDto>> GetOperationTimeAsync(SubsystemOperationTimeRequest request, WellDto well, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = BuildQuery(request, well);
|
|
|
|
|
IEnumerable<SubsystemOperationTime> data = await query.ToListAsync(token);
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeInner)
|
2022-08-02 15:26:33 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
if (request.GtDate is not null)
|
|
|
|
|
data = data.Where(o => o.DateStart >= request.GtDate.Value);
|
2022-09-22 16:26:17 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
if (request.LtDate is not null)
|
|
|
|
|
data = data.Where(o => o.DateEnd <= request.LtDate.Value);
|
2022-07-18 18:51:49 +05:00
|
|
|
|
}
|
2023-10-04 16:41:19 +05:00
|
|
|
|
else if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeTrim)
|
2022-08-19 00:00:30 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var begin = request.GtDate?.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
|
|
|
var end = request.LtDate?.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
|
|
|
data = TrimOperation(data, begin, end);
|
|
|
|
|
}
|
2023-07-17 17:17:11 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var dtos = data.Select(o => Convert(o, well.Timezone.Hours));
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
2023-07-17 15:50:14 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<IEnumerable<SubsystemStatDto>> GetStatAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} does not exist");
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
request.SelectMode = SubsystemOperationTimeRequest.SelectModeTrim;
|
|
|
|
|
var subsystemsTimes = await GetOperationTimeAsync(request, well, token);
|
|
|
|
|
if (subsystemsTimes is null)
|
|
|
|
|
return Enumerable.Empty<SubsystemStatDto>();
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var detectedOperationSummaryRequest = new DetectedOperationSummaryRequest()
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
IdsTelemetries = new[] {well.IdTelemetry!.Value},
|
|
|
|
|
IdsOperationCategories = WellOperationCategory.MechanicalDrillingSubIds,
|
2023-08-23 15:35:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
GeDateStart = request.GtDate,
|
|
|
|
|
LeDateStart = request.LtDate,
|
|
|
|
|
|
|
|
|
|
GeDepthStart = request.GtDepth,
|
|
|
|
|
LeDepthStart = request.LtDepth,
|
|
|
|
|
};
|
|
|
|
|
var operationsSummaries = await detectedOperationService.GetOperationSummaryAsync(detectedOperationSummaryRequest, token);
|
|
|
|
|
if(!operationsSummaries.Any())
|
|
|
|
|
return Enumerable.Empty<SubsystemStatDto>();
|
|
|
|
|
|
|
|
|
|
var statList = CalcStat(subsystemsTimes, operationsSummaries);
|
|
|
|
|
return statList;
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
private static IEnumerable<SubsystemOperationTime> TrimOperation(IEnumerable<SubsystemOperationTime> data, DateTimeOffset? gtDate, DateTimeOffset? ltDate)
|
|
|
|
|
{
|
|
|
|
|
if (!ltDate.HasValue && !gtDate.HasValue)
|
|
|
|
|
return data.Select(d => d.Adapt<SubsystemOperationTime>());
|
2023-08-31 14:12:01 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var items = data.Select((item) =>
|
|
|
|
|
{
|
|
|
|
|
var operationTime = item.Adapt<SubsystemOperationTime>();
|
|
|
|
|
if (!(item.DepthStart.HasValue && item.DepthEnd.HasValue))
|
|
|
|
|
return operationTime;
|
2022-09-22 17:13:53 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var dateDiff = (item.DateEnd - item.DateStart).TotalSeconds;
|
|
|
|
|
var depthDiff = item.DepthEnd.Value - item.DepthStart.Value;
|
|
|
|
|
var a = depthDiff / dateDiff;
|
|
|
|
|
var b = item.DepthStart.Value;
|
2023-09-15 16:48:19 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
if (gtDate.HasValue && item.DateStart < gtDate.Value)
|
2022-09-22 16:32:59 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
operationTime.DateStart = gtDate.Value;
|
|
|
|
|
var x = (gtDate.Value - item.DateStart).TotalSeconds;
|
|
|
|
|
operationTime.DepthStart = (float)(a * x + b);
|
2022-09-22 16:32:59 +05:00
|
|
|
|
}
|
2023-10-04 16:41:19 +05:00
|
|
|
|
if (ltDate.HasValue && item.DateEnd > ltDate.Value)
|
2022-09-22 16:32:59 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
operationTime.DateEnd = ltDate.Value;
|
|
|
|
|
var x = (ltDate.Value - item.DateStart).TotalSeconds;
|
|
|
|
|
operationTime.DepthEnd = (float)(a * x + b);
|
2022-09-22 16:32:59 +05:00
|
|
|
|
}
|
2023-10-04 16:41:19 +05:00
|
|
|
|
return operationTime;
|
|
|
|
|
});
|
2022-09-22 16:32:59 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
return items;
|
|
|
|
|
}
|
2022-11-02 15:49:07 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
private IEnumerable<SubsystemStatDto> CalcStat(
|
|
|
|
|
IEnumerable<SubsystemOperationTimeDto> subsystemsTimes,
|
|
|
|
|
IEnumerable<OperationsSummaryDto> operationsSummaries)
|
|
|
|
|
{
|
|
|
|
|
var groupedSubsystemsTimes = subsystemsTimes
|
|
|
|
|
.OrderBy(o => o.Id)
|
|
|
|
|
.GroupBy(o => o.IdSubsystem);
|
2023-02-16 16:27:14 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var periodGroupTotal = subsystemsTimes.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
2023-02-16 16:27:14 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var result = groupedSubsystemsTimes.Select(g =>
|
2023-02-16 16:27:14 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var periodGroup = g.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
|
|
|
|
var periodGroupDepth = g.Sum(o => o.DepthEnd - o.DepthStart);
|
|
|
|
|
var (sumOprationsDepth, sumOprationsDurationHours) = AggregateOperationsSummaries(g.Key, operationsSummaries);
|
|
|
|
|
var subsystemStat = new SubsystemStatDto()
|
|
|
|
|
{
|
|
|
|
|
IdSubsystem = g.Key,
|
|
|
|
|
SubsystemName = subsystemService.GetOrDefault(g.Key)?.Name ?? "unknown",
|
|
|
|
|
UsedTimeHours = periodGroup,
|
|
|
|
|
SumOperationDepthInterval = sumOprationsDepth,
|
|
|
|
|
SumOperationDurationHours = sumOprationsDurationHours,
|
|
|
|
|
SumDepthInterval = periodGroupDepth,
|
|
|
|
|
KUsage = periodGroupDepth / sumOprationsDepth,
|
|
|
|
|
OperationCount = g.Count(),
|
|
|
|
|
};
|
|
|
|
|
if (subsystemStat.KUsage > 1)
|
|
|
|
|
subsystemStat.KUsage = 1;
|
|
|
|
|
return subsystemStat;
|
|
|
|
|
});
|
2022-10-31 18:19:14 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var apdParts = result.Where(x => x.IdSubsystem == 11 || x.IdSubsystem == 12);
|
|
|
|
|
if (apdParts.Any())
|
2022-10-25 22:18:28 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var apdSum = new SubsystemStatDto()
|
2022-10-25 09:42:08 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
IdSubsystem = IdSubsystemAKB,
|
|
|
|
|
SubsystemName = "АПД",
|
|
|
|
|
UsedTimeHours = apdParts.Sum(part => part.UsedTimeHours),
|
|
|
|
|
SumOperationDepthInterval = apdParts.Sum(part => part.SumOperationDepthInterval),
|
|
|
|
|
SumOperationDurationHours = apdParts.Sum(part => part.SumOperationDurationHours),
|
|
|
|
|
SumDepthInterval = apdParts.Sum(part => part.SumDepthInterval),
|
|
|
|
|
OperationCount = apdParts.Sum(part => part.OperationCount),
|
|
|
|
|
};
|
|
|
|
|
apdSum.KUsage = apdSum.SumDepthInterval / apdSum.SumOperationDepthInterval;
|
|
|
|
|
if (apdSum.KUsage > 1)
|
|
|
|
|
apdSum.KUsage = 1;
|
|
|
|
|
result = result.Append(apdSum).OrderBy(m => m.IdSubsystem);
|
2022-10-20 12:44:24 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static (double SumDepth, double SumDurationHours) AggregateOperationsSummaries(int idSubsystem, IEnumerable<OperationsSummaryDto> operationsSummaries)
|
|
|
|
|
=> idSubsystem switch
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
IdSubsystemAKBRotor or IdSubsystemTorque => CalcOperationSummariesByCategories(operationsSummaries, WellOperationCategory.IdRotor),
|
|
|
|
|
IdSubsystemAKBSlide or IdSubsystemSpin => CalcOperationSummariesByCategories(operationsSummaries, WellOperationCategory.IdSlide),
|
|
|
|
|
IdSubsystemAKB or IdSubsystemMSE => CalcOperationSummariesByCategories(operationsSummaries, WellOperationCategory.IdRotor, WellOperationCategory.IdSlide),
|
|
|
|
|
_ => throw new ArgumentException($"idSubsystem: {idSubsystem} does not supported in this method", nameof(idSubsystem)),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static (double SumDepth, double SumDurationHours) CalcOperationSummariesByCategories(
|
|
|
|
|
IEnumerable<OperationsSummaryDto> operationsSummaries,
|
|
|
|
|
params int[] idsOperationCategories)
|
|
|
|
|
{
|
|
|
|
|
var filtered = operationsSummaries.Where(sum => idsOperationCategories.Contains(sum.IdCategory));
|
|
|
|
|
var sumDepth = filtered.Sum(summ => summ.SumDepthIntervals);
|
|
|
|
|
var sumDurationHours = filtered.Sum(summ => summ.SumDurationHours);
|
|
|
|
|
return (sumDepth, sumDurationHours);
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatByActiveWells(int idCompany, DateTime? gtDate, DateTime? ltDate, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var activeWells = await wellService.GetAsync(new() { IdCompany = idCompany, IdState = 1 }, token);
|
|
|
|
|
var result = await GetStatAsync(activeWells, gtDate, ltDate, token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatByActiveWells(IEnumerable<int> wellIds, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var activeWells = await wellService.GetAsync(new() { Ids = wellIds, IdState = 1 }, token);
|
|
|
|
|
var result = await GetStatAsync(activeWells, null, null, token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
private async Task<IEnumerable<SubsystemActiveWellStatDto>> GetStatAsync(IEnumerable<WellDto> wells, DateTime? gtDate, DateTime? ltDate, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (!wells.Any())
|
|
|
|
|
return Enumerable.Empty<SubsystemActiveWellStatDto>();
|
|
|
|
|
|
|
|
|
|
var hoursOffset = wells
|
|
|
|
|
.FirstOrDefault(well => well.Timezone is not null)
|
|
|
|
|
?.Timezone.Hours
|
|
|
|
|
?? 5d;
|
|
|
|
|
|
|
|
|
|
var beginUTC = gtDate.HasValue
|
|
|
|
|
? gtDate.Value.ToUtcDateTimeOffset(hoursOffset)
|
|
|
|
|
: db.SubsystemOperationTimes.Min(s => s.DateStart)
|
|
|
|
|
.DateTime
|
|
|
|
|
.ToUtcDateTimeOffset(hoursOffset);
|
|
|
|
|
|
|
|
|
|
var endUTC = ltDate.HasValue
|
|
|
|
|
? ltDate.Value.ToUtcDateTimeOffset(hoursOffset)
|
|
|
|
|
: db.SubsystemOperationTimes.Max(s => s.DateEnd)
|
|
|
|
|
.DateTime
|
|
|
|
|
.ToUtcDateTimeOffset(hoursOffset);
|
|
|
|
|
|
|
|
|
|
IEnumerable<int> idsTelemetries = wells
|
|
|
|
|
.Where(w => w.IdTelemetry is not null)
|
|
|
|
|
.Select(w => w.IdTelemetry!.Value)
|
|
|
|
|
.Distinct();
|
|
|
|
|
|
|
|
|
|
var query = db.SubsystemOperationTimes
|
|
|
|
|
.Where(o => idsTelemetries.Contains(o.IdTelemetry) &&
|
|
|
|
|
o.DateStart >= beginUTC &&
|
|
|
|
|
o.DateEnd <= endUTC)
|
|
|
|
|
.AsNoTracking();
|
|
|
|
|
|
|
|
|
|
var subsystemsOperationTime = await query.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var operationSummaries = await detectedOperationService
|
|
|
|
|
.GetOperationSummaryAsync(new ()
|
2022-08-21 20:58:55 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
IdsTelemetries = idsTelemetries,
|
|
|
|
|
IdsOperationCategories = WellOperationCategory.MechanicalDrillingSubIds,
|
|
|
|
|
GeDateStart = beginUTC,
|
|
|
|
|
LeDateEnd = endUTC,
|
|
|
|
|
}, token);
|
|
|
|
|
|
|
|
|
|
var result = wells
|
|
|
|
|
.Select(well => {
|
|
|
|
|
var dtos = subsystemsOperationTime
|
|
|
|
|
.Where(s => s.IdTelemetry == well.IdTelemetry)
|
|
|
|
|
.Select(s => Convert(s, well.Timezone.Hours));
|
|
|
|
|
|
|
|
|
|
var wellStat = new SubsystemActiveWellStatDto{ Well = well };
|
|
|
|
|
|
|
|
|
|
var telemetryOperationSummaries = operationSummaries.Where(summ => summ.IdTelemetry == well.IdTelemetry);
|
|
|
|
|
if (telemetryOperationSummaries.Any())
|
|
|
|
|
{
|
|
|
|
|
var subsystemStat = CalcStat(dtos, telemetryOperationSummaries);
|
|
|
|
|
if (subsystemStat.Any())
|
|
|
|
|
{
|
|
|
|
|
wellStat.SubsystemAKB = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemAKB);
|
|
|
|
|
wellStat.SubsystemMSE = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemMSE);
|
|
|
|
|
wellStat.SubsystemSpinMaster = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemSpin);
|
|
|
|
|
wellStat.SubsystemTorqueMaster = subsystemStat.FirstOrDefault(s => s.IdSubsystem == IdSubsystemTorque);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
return wellStat;
|
|
|
|
|
});
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<DatesRangeDto?> GetDateRangeOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} does not exist");
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
var query = BuildQuery(request, well);
|
|
|
|
|
if (query is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var result = await query
|
|
|
|
|
.GroupBy(o => o.IdTelemetry)
|
|
|
|
|
.Select(g => new DatesRangeDto
|
2022-09-07 18:01:39 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
From = g.Min(o => o.DateStart).DateTime,
|
|
|
|
|
To = g.Max(o => o.DateEnd).DateTime
|
|
|
|
|
})
|
|
|
|
|
.FirstOrDefaultAsync(token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
private IQueryable<SubsystemOperationTime> BuildQuery(SubsystemOperationTimeRequest request, WellDto well)
|
|
|
|
|
{
|
|
|
|
|
var idTelemetry = well.IdTelemetry
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(request.IdWell), $"Well Id: {request.IdWell} has no telemetry");
|
|
|
|
|
|
|
|
|
|
var query = db.SubsystemOperationTimes
|
|
|
|
|
.Include(o => o.Subsystem)
|
|
|
|
|
.Where(o => o.IdTelemetry == idTelemetry)
|
|
|
|
|
.AsNoTracking();
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
if (request.IdsSubsystems.Any())
|
|
|
|
|
query = query.Where(o => request.IdsSubsystems.Contains(o.IdSubsystem));
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
// # Dates range condition
|
|
|
|
|
// [GtDate LtDate]
|
|
|
|
|
// [DateStart DateEnd] [DateStart DateEnd]
|
|
|
|
|
if (request.GtDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
DateTimeOffset gtDate = request.GtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
|
|
|
query = query.Where(o => o.DateEnd >= gtDate);
|
2022-08-01 13:55:51 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2023-10-04 16:41:19 +05:00
|
|
|
|
if (request.LtDate.HasValue)
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
2023-10-04 16:41:19 +05:00
|
|
|
|
DateTimeOffset ltDate = request.LtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
|
|
|
query = query.Where(o => o.DateStart <= ltDate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.GtDepth.HasValue)
|
|
|
|
|
query = query.Where(o => o.DepthEnd >= request.GtDepth.Value);
|
|
|
|
|
|
|
|
|
|
if (request.LtDepth.HasValue)
|
|
|
|
|
query = query.Where(o => o.DepthStart <= request.LtDepth.Value);
|
|
|
|
|
|
|
|
|
|
if (request?.SortFields?.Any() == true)
|
|
|
|
|
{
|
|
|
|
|
query = query.SortBy(request.SortFields);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
query = query
|
|
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
|
.ThenBy(o => o.DepthStart);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request?.Skip > 0)
|
|
|
|
|
query = query.Skip((int)request.Skip);
|
|
|
|
|
|
|
|
|
|
if (request?.Take > 0)
|
|
|
|
|
query = query.Take((int)request.Take);
|
|
|
|
|
|
|
|
|
|
return query;
|
2022-07-14 03:47:11 +05:00
|
|
|
|
}
|
2023-10-04 16:41:19 +05:00
|
|
|
|
|
|
|
|
|
private static SubsystemOperationTimeDto Convert(SubsystemOperationTime operationTime, double? timezoneHours = null)
|
|
|
|
|
{
|
|
|
|
|
var dto = operationTime.Adapt<SubsystemOperationTimeDto>();
|
|
|
|
|
var hours = timezoneHours ?? operationTime.Telemetry.TimeZone.Hours;
|
|
|
|
|
dto.DateStart = operationTime.DateStart.ToRemoteDateTime(hours);
|
|
|
|
|
dto.DateEnd = operationTime.DateEnd.ToRemoteDateTime(hours);
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
2022-07-14 03:47:11 +05:00
|
|
|
|
}
|