forked from ddrilling/AsbCloudServer
253 lines
9.9 KiB
C#
253 lines
9.9 KiB
C#
using AsbCloudApp.Data;
|
|
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;
|
|
public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudService<SubsystemDto> subsystemService)
|
|
{
|
|
this.db = db;
|
|
this.wellService = wellService;
|
|
this.subsystemService = subsystemService;
|
|
}
|
|
|
|
/// <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 statList = CalcStat(data, request);
|
|
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, SubsystemOperationTimeRequest request)
|
|
{
|
|
var groupedDataSubsystems = dtos
|
|
.GroupBy(o => o.IdSubsystem);
|
|
|
|
var periodGroupTotal = dtos.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
|
|
|
var gtDate = request.GtDate ?? dtos.Min(o => o.DateStart);
|
|
var ltDate = request.LtDate ?? dtos.Max(o => o.DateEnd);
|
|
|
|
var periodRequest = (ltDate - gtDate).TotalHours;
|
|
|
|
var result = groupedDataSubsystems.Select(item =>
|
|
{
|
|
var periodGroup = item.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
|
var periodGroupDepth = item.Sum(o => o.DepthEnd - o.DepthStart);
|
|
var depthChange = GetDepthChange(item.Key, gtDate, ltDate);
|
|
var subsystemStat = new SubsystemStatDto()
|
|
{
|
|
IdSubsystem = item.Key,
|
|
SubsystemName = subsystemService.GetOrDefault(item.Key)?.Name ?? "unknown",
|
|
UsedTimeHours = periodGroup,
|
|
//условие добавлено что бы избежать деление на ноль
|
|
//чисто теоретически такое событие может произойти только в случае
|
|
//если не было определенных операций по какому то из режимов
|
|
// тогда в этом поле статистики будет null
|
|
KUsage = depthChange==0? double.NaN : periodGroupDepth / depthChange,
|
|
SumDepthChange = periodGroupDepth,
|
|
OperationCount = item.Count()
|
|
};
|
|
return subsystemStat;
|
|
});
|
|
|
|
return result;
|
|
}
|
|
private double GetDepthChange (int idSubsystem, DateTime gtDate, DateTime ltDate)
|
|
{
|
|
double depthChange = 0;
|
|
var detectOperations = db.DetectedOperations
|
|
.Where(a => a.DateStart >= gtDate & a.DateEnd <= ltDate)
|
|
.ToList();
|
|
//AKB - Rotor+Slide
|
|
if (idSubsystem == 1)
|
|
{
|
|
depthChange = detectOperations.Where(a => a.IdCategory == 1 & a.IdCategory == 3)
|
|
.Sum(o => o.DepthEnd - o.DepthStart);
|
|
return depthChange;
|
|
}
|
|
//Mse - all detect operations
|
|
if (idSubsystem == 2)
|
|
{
|
|
depthChange = detectOperations
|
|
.Sum(o => o.DepthEnd - o.DepthStart);
|
|
return depthChange;
|
|
}
|
|
//Slide
|
|
if (idSubsystem == 65536)
|
|
{
|
|
depthChange = detectOperations.Where(o => o.IdCategory == 3)
|
|
.Sum(o => o.DepthEnd - o.DepthStart);
|
|
return depthChange;
|
|
}
|
|
//Rotor
|
|
if (idSubsystem == 65537)
|
|
{
|
|
depthChange = detectOperations.Where(o => o.IdCategory == 1)
|
|
.Sum(o => o.DepthEnd - o.DepthStart);
|
|
return depthChange;
|
|
}
|
|
|
|
return depthChange;
|
|
}
|
|
|
|
private IQueryable<SubsystemOperationTime>? BuildQuery(SubsystemOperationTimeRequest request)
|
|
{
|
|
if (request.SelectMode > 0 && request.GtDate is null && request.LtDate is null)
|
|
return null;
|
|
|
|
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
|
|
}
|