2022-08-15 01:17:00 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
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;
|
|
|
|
|
public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudService<SubsystemDto> subsystemService)
|
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-07-18 18:51:49 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-15 01:17:00 +05:00
|
|
|
|
private async Task<IEnumerable<SubsystemDto>?> GetSubsystemByIdWellAsync(int idWell, CancellationToken token)
|
2022-08-05 17:10:56 +05:00
|
|
|
|
{
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(idWell, token);
|
|
|
|
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
|
|
|
|
return null;
|
|
|
|
|
var wellSubsystem = await db.SubsystemOperationTimes
|
|
|
|
|
.Include(e => e.Subsystem)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(o => o.IdTelemetry == well.IdTelemetry)
|
|
|
|
|
.DistinctBy(o => o.IdSubsystem)
|
|
|
|
|
.Select(d => new SubsystemDto
|
|
|
|
|
{
|
|
|
|
|
Id = d.Subsystem.Id,
|
|
|
|
|
Name = d.Subsystem.Name,
|
|
|
|
|
Description = d.Subsystem.Description,
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.ToListAsync(token);
|
|
|
|
|
return wellSubsystem;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
}
|
2022-08-15 01:17:00 +05:00
|
|
|
|
public async Task<IEnumerable<SubsystemDto>?> GetSubsystemAsync(int? idWell, CancellationToken token)
|
2022-08-05 17:10:56 +05:00
|
|
|
|
{
|
2022-08-15 01:17:00 +05:00
|
|
|
|
if (idWell.HasValue)
|
2022-08-05 17:10:56 +05:00
|
|
|
|
{
|
2022-08-15 01:17:00 +05:00
|
|
|
|
var subsystemWell = await GetSubsystemByIdWellAsync(idWell.Value, token);
|
|
|
|
|
return subsystemWell;
|
|
|
|
|
}
|
|
|
|
|
var subsystem = await subsystemService.GetAllAsync(token);
|
|
|
|
|
return subsystem;
|
2022-08-05 17:10:56 +05:00
|
|
|
|
}
|
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-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;
|
|
|
|
|
var query = BuildQuery(request);
|
2022-08-01 13:55:51 +05:00
|
|
|
|
if (query is null)
|
|
|
|
|
return null;
|
2022-08-02 15:26:33 +05:00
|
|
|
|
var data = await query.ToListAsync(token);
|
|
|
|
|
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeInner)
|
|
|
|
|
{
|
|
|
|
|
if (request.GtDate is not null)
|
|
|
|
|
query = query.Where(o => o.DateStart >= request.GtDate.Value || o.DateEnd >= request.GtDate.Value);
|
|
|
|
|
|
|
|
|
|
if (request.LtDate is not null)
|
|
|
|
|
query = query.Where(o => o.DateEnd <= request.LtDate.Value || o.DateStart <= request.LtDate.Value);
|
|
|
|
|
}
|
|
|
|
|
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeTrim)
|
|
|
|
|
{
|
|
|
|
|
var begin = request.GtDate ?? throw new ArgumentNullException(nameof(request.GtDate));
|
|
|
|
|
var end = request.GtDate ?? throw new ArgumentNullException(nameof(request.LtDate));
|
|
|
|
|
data = Trim(data, begin,end );
|
|
|
|
|
}
|
2022-08-15 01:17:00 +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-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
|
|
|
|
{
|
|
|
|
|
var data = await GetOperationTimeAsync(request, token);
|
2022-08-15 01:17:00 +05:00
|
|
|
|
if (data is null)
|
|
|
|
|
return null;
|
2022-08-02 15:26:33 +05:00
|
|
|
|
var statList = CalcStat(data, request);
|
2022-08-01 13:55:51 +05:00
|
|
|
|
return statList;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
}
|
2022-08-02 15:26:33 +05:00
|
|
|
|
private List<SubsystemOperationTime> Trim(List<SubsystemOperationTime> data, DateTime gtDate, DateTime ltDate)
|
|
|
|
|
{
|
2022-08-15 01:17:00 +05:00
|
|
|
|
var itemsToTrim = data.Where(q => q.DateStart == gtDate & q.DateEnd == ltDate)
|
2022-08-02 15:26:33 +05:00
|
|
|
|
.Select(o => new SubsystemOperationTime
|
|
|
|
|
{
|
|
|
|
|
Id = o.Id,
|
|
|
|
|
DateStart = gtDate < o.DateStart ? gtDate : o.DateStart,
|
|
|
|
|
DateEnd = ltDate > o.DateEnd ? ltDate : o.DateEnd,
|
|
|
|
|
IdSubsystem = o.IdSubsystem,
|
|
|
|
|
IdTelemetry = o.IdTelemetry,
|
|
|
|
|
DepthEnd = o.DepthEnd,
|
|
|
|
|
DepthStart = o.DepthStart,
|
|
|
|
|
Subsystem = o.Subsystem,
|
|
|
|
|
Telemetry = o.Telemetry
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
return itemsToTrim;
|
|
|
|
|
}
|
2022-08-03 16:35:21 +05:00
|
|
|
|
private IEnumerable<SubsystemStatDto> CalcStat(IEnumerable<SubsystemOperationTimeDto> groupedData, SubsystemOperationTimeRequest request)
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
2022-08-03 16:35:21 +05:00
|
|
|
|
var result = new List<SubsystemStatDto>();
|
2022-08-05 17:10:56 +05:00
|
|
|
|
var groupedDataSubsystems = groupedData
|
|
|
|
|
.GroupBy(x => x.IdSubsystem);
|
2022-08-02 15:26:33 +05:00
|
|
|
|
var periodGroupTotal = groupedData
|
|
|
|
|
.GroupBy(x => x.IdSubsystem)
|
|
|
|
|
.Sum(g => g.Sum(o=> (o.DateEnd - o.DateStart).TotalHours));
|
2022-08-05 17:10:56 +05:00
|
|
|
|
foreach (var item in groupedDataSubsystems)
|
|
|
|
|
{
|
2022-08-02 15:26:33 +05:00
|
|
|
|
var periodGroup = item.Sum(g => (g.DateEnd - g.DateStart).TotalHours);
|
2022-08-05 17:10:56 +05:00
|
|
|
|
var gtDate = request.GtDate ?? item.Min(d => d.DateStart);
|
|
|
|
|
var ltDate = request.LtDate ?? item.Max(d => d.DateEnd);
|
|
|
|
|
var periodRequest = item.Where(o => o.DateStart>=gtDate && o.DateEnd <= ltDate)
|
2022-08-02 15:26:33 +05:00
|
|
|
|
.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
2022-08-01 13:55:51 +05:00
|
|
|
|
int idSubsystem = item.First().IdSubsystem;
|
2022-08-03 16:35:21 +05:00
|
|
|
|
var subsystemStat = new SubsystemStatDto()
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
|
|
|
|
IdSubsystem = idSubsystem,
|
2022-08-15 01:17:00 +05:00
|
|
|
|
Subsystem = subsystemService.GetOrDefault(idSubsystem)?.Name ?? "unknown",
|
2022-08-02 15:26:33 +05:00
|
|
|
|
UsedTimeHours = TimeSpan.FromHours(periodGroup),
|
|
|
|
|
KUsage = 1d*periodGroup / periodRequest,
|
|
|
|
|
K2 = 1d*periodGroup / periodGroupTotal,
|
2022-08-01 13:55:51 +05:00
|
|
|
|
};
|
|
|
|
|
result.Add(subsystemStat);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
private IQueryable<SubsystemOperationTime>? BuildQuery(SubsystemOperationTimeRequest request)
|
|
|
|
|
{
|
|
|
|
|
var idTelemetry = wellService.GetOrDefault(request.IdWell)?.IdTelemetry;
|
|
|
|
|
if (idTelemetry is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var query = db.SubsystemOperationTimes
|
|
|
|
|
.Include(o => o.Subsystem)
|
|
|
|
|
.Where(o => o.IdTelemetry == idTelemetry);
|
|
|
|
|
|
|
|
|
|
if(request.IdsSubsystems?.Any() == true)
|
|
|
|
|
query = query.Where(o => request.IdsSubsystems.Contains(o.IdSubsystem));
|
|
|
|
|
|
2022-08-02 15:26:33 +05:00
|
|
|
|
|
|
|
|
|
if (request.GtDate is not null)
|
|
|
|
|
query = query.Where(o => o.DateStart >= request.GtDate.Value);
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
2022-08-02 15:26:33 +05:00
|
|
|
|
if (request.LtDate is not null)
|
|
|
|
|
query = query.Where(o => o.DateEnd <= request.LtDate.Value);
|
|
|
|
|
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
|
|
|
|
if (request.GtDepth is not null)
|
|
|
|
|
query = query.Where(o => o.DepthStart >= request.GtDepth.Value);
|
|
|
|
|
|
|
|
|
|
if (request.LtDepth is not null)
|
|
|
|
|
query = query.Where(o => o.DepthEnd <= 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;
|
|
|
|
|
}
|
2022-08-15 01:17:00 +05:00
|
|
|
|
private 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-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
|
|
|
|
}
|