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;
|
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;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.Subsystems
|
|
|
|
|
{
|
2022-08-02 15:26:33 +05:00
|
|
|
|
#nullable enable
|
2022-07-18 18:51:49 +05:00
|
|
|
|
internal class SubsystemOperationTimeService : ISubsystemOperationTimeService
|
2022-07-14 03:47:11 +05:00
|
|
|
|
{
|
|
|
|
|
|
2022-07-18 18:51:49 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly IWellService wellService;
|
2022-08-05 17:10:56 +05:00
|
|
|
|
private readonly ICrudService<SubsystemDto> subsystemService;
|
2022-09-21 13:06:40 +05:00
|
|
|
|
private readonly IDetectedOperationService detectedOperationService;
|
|
|
|
|
public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudService<SubsystemDto> subsystemService, IDetectedOperationService detectedOperationService)
|
2022-07-18 18:51:49 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
2022-08-01 13:55:51 +05:00
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.subsystemService = subsystemService;
|
2022-09-21 13:06:40 +05:00
|
|
|
|
this.detectedOperationService = detectedOperationService;
|
2022-08-19 00:00:30 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2022-07-18 18:51:49 +05:00
|
|
|
|
public async Task<int> DeleteAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
|
|
|
|
{
|
2022-08-01 13:55:51 +05:00
|
|
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
|
|
|
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
|
|
|
|
return 0;
|
|
|
|
|
var query = BuildQuery(request);
|
2022-08-15 01:17:00 +05:00
|
|
|
|
if (query is null)
|
|
|
|
|
return 0;
|
2022-08-01 13:55:51 +05:00
|
|
|
|
db.SubsystemOperationTimes.RemoveRange(query);
|
|
|
|
|
return await db.SaveChangesAsync(token);
|
2022-07-18 18:51:49 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 18:01:39 +05:00
|
|
|
|
/// <inheritdoc/>
|
2022-08-15 01:17:00 +05:00
|
|
|
|
public async Task<IEnumerable<SubsystemOperationTimeDto>?> GetOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
|
|
|
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
|
|
|
|
return null;
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2022-08-15 01:17:00 +05:00
|
|
|
|
var query = BuildQuery(request);
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2022-08-01 13:55:51 +05:00
|
|
|
|
if (query is null)
|
2022-08-19 00:00:30 +05:00
|
|
|
|
return null;
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
|
|
|
|
IEnumerable<SubsystemOperationTime> data = await query.ToListAsync(token);
|
|
|
|
|
|
2022-08-19 00:00:30 +05:00
|
|
|
|
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeInner)
|
2022-08-02 15:26:33 +05:00
|
|
|
|
{
|
|
|
|
|
if (request.GtDate is not null)
|
2022-09-07 18:01:39 +05:00
|
|
|
|
data = data.Where(o => o.DateStart >= request.GtDate.Value);
|
2022-08-02 15:26:33 +05:00
|
|
|
|
|
|
|
|
|
if (request.LtDate is not null)
|
2022-09-07 18:01:39 +05:00
|
|
|
|
data = data.Where(o => o.DateEnd <= request.LtDate.Value);
|
2022-08-02 15:26:33 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
else if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeTrim)
|
|
|
|
|
{
|
|
|
|
|
var begin = request.GtDate?.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
|
|
|
var end = request.LtDate?.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
|
|
|
data = Trim(data, begin, end);
|
2022-08-02 15:26:33 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
|
|
|
|
var dtos = data.Select(o => Convert(o, well));
|
2022-08-01 13:55:51 +05:00
|
|
|
|
return dtos;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2022-08-03 16:35:21 +05:00
|
|
|
|
public async Task<IEnumerable<SubsystemStatDto>?> GetStatAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
2022-08-02 15:26:33 +05:00
|
|
|
|
{
|
2022-08-16 02:22:16 +05:00
|
|
|
|
request.SelectMode = SubsystemOperationTimeRequest.SelectModeTrim;
|
2022-08-02 15:26:33 +05:00
|
|
|
|
var data = await GetOperationTimeAsync(request, token);
|
2022-08-15 01:17:00 +05:00
|
|
|
|
if (data is null)
|
|
|
|
|
return null;
|
2022-09-22 16:26:17 +05:00
|
|
|
|
|
|
|
|
|
var detectedOperationsRequest = new DetectedOperationRequest()
|
|
|
|
|
{
|
|
|
|
|
IdWell = request.IdWell,
|
|
|
|
|
IdsCategories = new List<int>() {
|
|
|
|
|
1,3
|
|
|
|
|
},
|
|
|
|
|
LtDate = request.LtDate,
|
|
|
|
|
GtDate = request.GtDate,
|
|
|
|
|
};
|
|
|
|
|
var detectedOperations = await detectedOperationService.GetOperationsAsync(detectedOperationsRequest, token);
|
2022-09-22 17:13:53 +05:00
|
|
|
|
if(detectedOperations?.Any() != true)
|
|
|
|
|
return null;
|
2022-09-22 16:26:17 +05:00
|
|
|
|
var depthInterval = GetDepthInterval(detectedOperations);
|
|
|
|
|
|
|
|
|
|
var statList = CalcStat(data,depthInterval,request, token);
|
2022-08-01 13:55:51 +05:00
|
|
|
|
return statList;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
|
|
|
|
private static IEnumerable<SubsystemOperationTime> Trim(IEnumerable<SubsystemOperationTime> data, DateTimeOffset? gtDate, DateTimeOffset? ltDate)
|
2022-08-19 00:00:30 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var items = data.Select((item) =>
|
|
|
|
|
{
|
|
|
|
|
if (gtDate.HasValue && item.DateStart < gtDate.Value)
|
|
|
|
|
{
|
|
|
|
|
item.DateStart = gtDate.Value;
|
|
|
|
|
item.DepthStart = null;
|
|
|
|
|
}
|
|
|
|
|
if (ltDate.HasValue && item.DateEnd > ltDate.Value)
|
2022-08-02 15:26:33 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
item.DateEnd = ltDate.Value;
|
|
|
|
|
item.DepthEnd = null;
|
|
|
|
|
}
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return items;
|
2022-08-02 15:26:33 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2022-09-22 16:26:17 +05:00
|
|
|
|
private IEnumerable<SubsystemStatDto> CalcStat(IEnumerable<SubsystemOperationTimeDto> dtos, (double depthIntervalRotor, double depthIntervalSlide) depthInterval, SubsystemOperationTimeRequest request, CancellationToken token)
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var groupedDataSubsystems = dtos
|
|
|
|
|
.GroupBy(o => o.IdSubsystem);
|
|
|
|
|
var periodGroupTotal = dtos.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
2022-09-22 16:32:59 +05:00
|
|
|
|
|
2022-09-21 13:06:40 +05:00
|
|
|
|
var result = groupedDataSubsystems.Select(g =>
|
2022-09-07 18:01:39 +05:00
|
|
|
|
{
|
2022-09-22 16:32:59 +05:00
|
|
|
|
var depthIntervalSubsystem = GetDepthIntervalSubsystem(g.Key, depthInterval);
|
2022-09-21 13:06:40 +05:00
|
|
|
|
var periodGroup = g.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
2022-09-22 16:32:59 +05:00
|
|
|
|
var periodGroupDepth = g.Sum(o => o.DepthEnd - o.DepthStart);
|
2022-08-03 16:35:21 +05:00
|
|
|
|
var subsystemStat = new SubsystemStatDto()
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
2022-09-21 13:06:40 +05:00
|
|
|
|
IdSubsystem = g.Key,
|
|
|
|
|
SubsystemName = subsystemService.GetOrDefault(g.Key)?.Name ?? "unknown",
|
|
|
|
|
UsedTimeHours = periodGroup,
|
2022-09-22 16:26:17 +05:00
|
|
|
|
KUsage = periodGroupDepth / depthIntervalSubsystem,
|
2022-09-21 13:06:40 +05:00
|
|
|
|
SumDepthInterval = periodGroupDepth,
|
|
|
|
|
OperationCount = g.Count()
|
2022-08-01 13:55:51 +05:00
|
|
|
|
};
|
2022-09-29 17:56:59 +05:00
|
|
|
|
if(subsystemStat.KUsage > 1)
|
|
|
|
|
subsystemStat.KUsage = 1;
|
2022-09-07 18:01:39 +05:00
|
|
|
|
return subsystemStat;
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-01 13:55:51 +05:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-09-22 17:13:53 +05:00
|
|
|
|
private (double depthIntervalRotor, double depthIntervalSlide) GetDepthInterval (IEnumerable<DetectedOperationDto> detectedOperations)
|
2022-09-21 13:06:40 +05:00
|
|
|
|
{
|
2022-09-22 17:13:53 +05:00
|
|
|
|
|
|
|
|
|
var depthIntervalRotor = detectedOperations.Where(o => o.IdCategory == 1)
|
|
|
|
|
.Sum(o => o.DepthEnd - o.DepthStart);
|
|
|
|
|
var depthIntervalSlide = detectedOperations.Where(o => o.IdCategory == 3)
|
2022-09-16 13:34:14 +05:00
|
|
|
|
.Sum(o => o.DepthEnd - o.DepthStart);
|
2022-09-22 17:13:53 +05:00
|
|
|
|
var depthInterval = (depthIntervalRotor, depthIntervalSlide);
|
|
|
|
|
|
2022-09-22 16:26:17 +05:00
|
|
|
|
return depthInterval;
|
2022-09-16 13:34:14 +05:00
|
|
|
|
}
|
2022-09-22 16:32:59 +05:00
|
|
|
|
private double GetDepthIntervalSubsystem(int idSubsystem, (double depthIntervalRotor, double depthIntervalSlide) depthInterval)
|
|
|
|
|
{
|
2022-09-22 17:13:53 +05:00
|
|
|
|
var depthIntervalSubsystem = 0d;
|
2022-09-22 16:32:59 +05:00
|
|
|
|
//AKB - MSE
|
|
|
|
|
if (idSubsystem == 1 | idSubsystem == 2)
|
|
|
|
|
{
|
|
|
|
|
depthIntervalSubsystem = depthInterval.depthIntervalRotor + depthInterval.depthIntervalSlide;
|
|
|
|
|
}
|
|
|
|
|
//Spin
|
|
|
|
|
if (idSubsystem == 65536)
|
|
|
|
|
{
|
|
|
|
|
depthIntervalSubsystem = depthInterval.depthIntervalSlide;
|
|
|
|
|
}
|
|
|
|
|
//Torque
|
|
|
|
|
if (idSubsystem == 65537)
|
|
|
|
|
{
|
|
|
|
|
depthIntervalSubsystem = depthInterval.depthIntervalRotor;
|
|
|
|
|
}
|
|
|
|
|
return depthIntervalSubsystem;
|
|
|
|
|
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2022-10-21 15:52:51 +05:00
|
|
|
|
/// <inheritdoc/>
|
2022-10-25 09:42:08 +05:00
|
|
|
|
public async Task<DatesRangeDto?> GetDateRangeOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var query = BuildQuery(request);
|
|
|
|
|
if (query is not null)
|
|
|
|
|
{
|
|
|
|
|
var result = await query
|
|
|
|
|
.GroupBy(o => o.IdSubsystem)
|
|
|
|
|
.Select(g => new DatesRangeDto
|
2022-10-21 15:52:51 +05:00
|
|
|
|
{
|
2022-10-25 09:42:08 +05:00
|
|
|
|
From = g.Min(o => o.DateStart).DateTime,
|
|
|
|
|
To = g.Max(o => o.DateEnd).DateTime
|
2022-10-21 15:52:51 +05:00
|
|
|
|
})
|
2022-10-25 09:42:08 +05:00
|
|
|
|
.FirstOrDefaultAsync(token);
|
|
|
|
|
return result;
|
2022-10-20 14:26:36 +05:00
|
|
|
|
}
|
2022-10-25 09:42:08 +05:00
|
|
|
|
return null;
|
2022-10-20 12:44:24 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-01 13:55:51 +05:00
|
|
|
|
private IQueryable<SubsystemOperationTime>? BuildQuery(SubsystemOperationTimeRequest request)
|
|
|
|
|
{
|
2022-08-17 00:29:32 +05:00
|
|
|
|
var well = wellService.GetOrDefault(request.IdWell);
|
|
|
|
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
2022-08-01 13:55:51 +05:00
|
|
|
|
return null;
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2022-08-01 13:55:51 +05:00
|
|
|
|
var query = db.SubsystemOperationTimes
|
2022-09-07 18:01:39 +05:00
|
|
|
|
.Include(o => o.Subsystem)
|
|
|
|
|
.Where(o => o.IdTelemetry == well.IdTelemetry)
|
|
|
|
|
.AsNoTracking();
|
|
|
|
|
|
2022-08-21 20:58:55 +05:00
|
|
|
|
if (request.IdsSubsystems?.Any() == true)
|
2022-08-01 13:55:51 +05:00
|
|
|
|
query = query.Where(o => request.IdsSubsystems.Contains(o.IdSubsystem));
|
|
|
|
|
|
2022-09-07 18:01:39 +05:00
|
|
|
|
// # Dates range condition
|
|
|
|
|
// [GtDate LtDate]
|
|
|
|
|
// [DateStart DateEnd] [DateStart DateEnd]
|
|
|
|
|
if (request.GtDate.HasValue)
|
2022-08-21 20:58:55 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
DateTimeOffset gtDate = request.GtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
|
|
|
query = query.Where(o => o.DateEnd >= gtDate);
|
2022-08-19 00:00:30 +05:00
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2022-09-07 18:01:39 +05:00
|
|
|
|
if (request.LtDate.HasValue)
|
2022-08-21 20:58:55 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
DateTimeOffset ltDate = request.LtDate.Value.ToUtcDateTimeOffset(well.Timezone.Hours);
|
|
|
|
|
query = query.Where(o => o.DateStart <= ltDate);
|
2022-08-21 20:58:55 +05:00
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2022-09-07 18:01:39 +05:00
|
|
|
|
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);
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
|
|
|
|
if (request?.SortFields?.Any() == true)
|
|
|
|
|
{
|
|
|
|
|
query = query.SortBy(request.SortFields);
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-09-07 18:01:39 +05:00
|
|
|
|
{
|
2022-08-01 13:55:51 +05:00
|
|
|
|
query = query
|
|
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
|
.ThenBy(o => o.DepthStart);
|
2022-09-07 18:01:39 +05:00
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
|
|
|
|
if (request?.Skip > 0)
|
|
|
|
|
query = query.Skip((int)request.Skip);
|
|
|
|
|
|
|
|
|
|
if (request?.Take > 0)
|
|
|
|
|
query = query.Take((int)request.Take);
|
|
|
|
|
else
|
|
|
|
|
query = query.Take(3000);
|
|
|
|
|
|
|
|
|
|
return query;
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
|
|
|
|
private static SubsystemOperationTimeDto Convert(SubsystemOperationTime operationTime, WellDto well)
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
2022-08-15 01:17:00 +05:00
|
|
|
|
var dto = operationTime.Adapt<SubsystemOperationTimeDto>();
|
|
|
|
|
dto.DateStart = operationTime.DateStart.ToRemoteDateTime(well.Timezone.Hours);
|
|
|
|
|
dto.DateEnd = operationTime.DateEnd.ToRemoteDateTime(well.Timezone.Hours);
|
2022-08-01 13:55:51 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
2022-10-21 15:52:51 +05:00
|
|
|
|
|
|
|
|
|
|
2022-07-14 03:47:11 +05:00
|
|
|
|
}
|
2022-08-02 15:26:33 +05:00
|
|
|
|
#nullable disable
|
2022-07-14 03:47:11 +05:00
|
|
|
|
}
|