DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeService.cs

363 lines
14 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Data.DetectedOperation;
using AsbCloudApp.Data.Subsystems;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudApp.Services.Subsystems;
using AsbCloudDb;
using AsbCloudDb.Model;
using AsbCloudDb.Model.Subsystems;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.Subsystems
{
#nullable enable
internal class SubsystemOperationTimeService : ISubsystemOperationTimeService
{
private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
private readonly ICrudService<SubsystemDto> subsystemService;
private readonly IDetectedOperationService detectedOperationService;
public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudService<SubsystemDto> subsystemService, IDetectedOperationService detectedOperationService)
{
this.db = db;
this.wellService = wellService;
this.subsystemService = subsystemService;
this.detectedOperationService = detectedOperationService;
}
/// <inheritdoc/>
public async Task<int> DeleteAsync(SubsystemOperationTimeRequest request, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
if (well?.IdTelemetry is null || well.Timezone is null)
return 0;
var query = BuildQuery(request);
if (query is null)
return 0;
db.SubsystemOperationTimes.RemoveRange(query);
return await db.SaveChangesAsync(token);
}
/// <inheritdoc/>
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;
var query = BuildQuery(request);
if (query is null)
return null;
IEnumerable<SubsystemOperationTime> data = await query.ToListAsync(token);
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeInner)
{
if (request.GtDate is not null)
data = data.Where(o => o.DateStart >= request.GtDate.Value);
if (request.LtDate is not null)
data = data.Where(o => o.DateEnd <= request.LtDate.Value);
}
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);
}
var dtos = data.Select(o => Convert(o, well));
return dtos;
}
/// <inheritdoc/>
public async Task<IEnumerable<SubsystemStatDto>?> GetStatAsync(SubsystemOperationTimeRequest request, CancellationToken token)
{
request.SelectMode = SubsystemOperationTimeRequest.SelectModeTrim;
var data = await GetOperationTimeAsync(request, token);
if (data is null)
return null;
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);
if(detectedOperations?.Any() != true)
return null;
var depthInterval = GetDepthInterval(detectedOperations);
var statList = CalcStat(data,depthInterval);
return statList;
}
private static IEnumerable<SubsystemOperationTime> Trim(IEnumerable<SubsystemOperationTime> data, DateTimeOffset? gtDate, DateTimeOffset? ltDate)
{
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)
{
item.DateEnd = ltDate.Value;
item.DepthEnd = null;
}
return item;
});
return items;
}
private IEnumerable<SubsystemStatDto> CalcStat(IEnumerable<SubsystemOperationTimeDto> dtos, (double depthIntervalRotor, double depthIntervalSlide) depthInterval)
{
var groupedDataSubsystems = dtos
.GroupBy(o => o.IdSubsystem);
var periodGroupTotal = dtos.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
var result = groupedDataSubsystems.Select(g =>
{
var depthIntervalSubsystem = GetDepthIntervalSubsystem(g.Key, depthInterval);
var periodGroup = g.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
var periodGroupDepth = g.Sum(o => o.DepthEnd - o.DepthStart);
var subsystemStat = new SubsystemStatDto()
{
IdSubsystem = g.Key,
SubsystemName = subsystemService.GetOrDefault(g.Key)?.Name ?? "unknown",
UsedTimeHours = periodGroup,
KUsage = periodGroupDepth / depthIntervalSubsystem,
SumDepthInterval = periodGroupDepth,
OperationCount = g.Count()
};
if(subsystemStat.KUsage > 1)
subsystemStat.KUsage = 1;
return subsystemStat;
});
return result;
}
private (double depthIntervalRotor, double depthIntervalSlide) GetDepthInterval (IEnumerable<DetectedOperationDto> detectedOperations)
{
var depthIntervalRotor = detectedOperations.Where(o => o.IdCategory == 1)
.Sum(o => o.DepthEnd - o.DepthStart);
var depthIntervalSlide = detectedOperations.Where(o => o.IdCategory == 3)
.Sum(o => o.DepthEnd - o.DepthStart);
var depthInterval = (depthIntervalRotor, depthIntervalSlide);
return depthInterval;
}
private double GetDepthIntervalSubsystem(int idSubsystem, (double depthIntervalRotor, double depthIntervalSlide) depthInterval)
{
var depthIntervalSubsystem = 0d;
//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;
}
private async Task<IEnumerable<WellDto>> GetActiveWellByCompany(int idCompany, CancellationToken token)
{
var listWell = await wellService.GetWellsByCompanyAsync(idCompany, token);
var active = listWell.Where(w => w.IdState == 1);
return active;
}
public async Task<IEnumerable<SubsystemActiveWellStatDto>?> GetStatByActiveWell(int idCompany, DateTime? gtDate, DateTime? ltDate, CancellationToken token)
{
var activeWell = await GetActiveWellByCompany(idCompany, token);
var telemetryIds = activeWell.Select(w => w.IdTelemetry).Distinct();
var firstWell = activeWell.FirstOrDefault();
if (firstWell == null)
return null;
var query = db.SubsystemOperationTimes
.Where(o => telemetryIds.Contains(o.IdTelemetry))
.AsNoTracking();
if (gtDate is not null)
{
var beginUTC = gtDate.Value.ToUtcDateTimeOffset(firstWell.Timezone.Hours);
query = query.Where(d => d.DateStart >= beginUTC);
}
else
{
var beginUTC = DateTime.Today.AddDays(-1).ToUtcDateTimeOffset(firstWell.Timezone.Hours);
query = query.Where(o => o.DateStart >= beginUTC );
}
if (ltDate is not null)
{
var endUTC = ltDate.Value.ToUtcDateTimeOffset(firstWell.Timezone.Hours);
query = query.Where(d => d.DateEnd <= endUTC);
}
else
{
var endUTC = DateTime.Today.ToUtcDateTimeOffset(firstWell.Timezone.Hours);
query = query.Where(o => o.DateEnd <= endUTC);
}
var result = new List<SubsystemActiveWellStatDto>();
var subsystemsOperationTime = await query.ToListAsync(token);
var groupingSubsystemsOperationTime = subsystemsOperationTime.GroupBy(g => g.IdTelemetry);
foreach (var group in groupingSubsystemsOperationTime)
{
var well = activeWell.Where(w => w.IdTelemetry == group.Key).FirstOrDefault();
if (well != null)
{
var wellStat = new SubsystemActiveWellStatDto()
{
ActiveWell = well,
listSubsystemStat = new List<SubsystemStatDto>()
};
var detectedOperationsRequest = new DetectedOperationRequest()
{
IdWell = well.Id,
IdsCategories = new List<int>() { 1, 3 },
LtDate = ltDate,
GtDate = gtDate,
};
var detectedOperations = await detectedOperationService.GetOperationsAsync(detectedOperationsRequest, token);
if (detectedOperations is not null && detectedOperations.Any())
{
var depthInterval = GetDepthInterval(detectedOperations);
var groupSubsystem = group.GroupBy(g => g.IdSubsystem);
foreach (var subsystem in groupSubsystem)
{
var dto = subsystem.Select(s => s.Adapt<SubsystemOperationTimeDto>());
var subsystemStat = CalcStat(dto, depthInterval);
wellStat.listSubsystemStat.Concat(subsystemStat);
//wellStat.Saub = subsystemStat.FirstOrDefault(s=>s.IdSubsystem == idSubsystemSaub)
}
result.Add(wellStat);
}
}
}
return result;
}
/// <inheritdoc/>
public async Task<DatesRangeDto?> GetDateRangeOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
{
var query = BuildQuery(request);
if (query is null)
{
return null;
}
var result = await query
.GroupBy(o => o.IdTelemetry)
.Select(g => new DatesRangeDto
{
From = g.Min(o => o.DateStart).DateTime,
To = g.Max(o => o.DateEnd).DateTime
})
.FirstOrDefaultAsync(token);
return result;
}
private IQueryable<SubsystemOperationTime>? BuildQuery(SubsystemOperationTimeRequest request)
{
var well = wellService.GetOrDefault(request.IdWell);
if (well?.IdTelemetry is null || well.Timezone is null)
return null;
var query = db.SubsystemOperationTimes
.Include(o => o.Subsystem)
.Where(o => o.IdTelemetry == well.IdTelemetry)
.AsNoTracking();
if (request.IdsSubsystems?.Any() == true)
query = query.Where(o => request.IdsSubsystems.Contains(o.IdSubsystem));
// # 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);
}
if (request.LtDate.HasValue)
{
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);
else
query = query.Take(3000);
return query;
}
private static SubsystemOperationTimeDto Convert(SubsystemOperationTime operationTime, WellDto well)
{
var dto = operationTime.Adapt<SubsystemOperationTimeDto>();
dto.DateStart = operationTime.DateStart.ToRemoteDateTime(well.Timezone.Hours);
dto.DateEnd = operationTime.DateEnd.ToRemoteDateTime(well.Timezone.Hours);
return dto;
}
}
#nullable disable
}