forked from ddrilling/AsbCloudServer
123 lines
4.4 KiB
C#
123 lines
4.4 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Repositories;
|
|
using AsbCloudApp.Requests;
|
|
using AsbCloudDb;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudDb.Model.Subsystems;
|
|
using AsbCloudInfrastructure.Services;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Org.BouncyCastle.Asn1.Ocsp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
|
{
|
|
#nullable enable
|
|
public class LimitingParameterRepository : ILimitingParameterRepository
|
|
{
|
|
private readonly IAsbCloudDbContext context;
|
|
|
|
public LimitingParameterRepository(IAsbCloudDbContext context)
|
|
{
|
|
this.context = context;
|
|
}
|
|
public async Task<IEnumerable<LimitingParameterDto>> GetInfosAsync(LimitingParameterRequest request, WellDto wellDto, CancellationToken token)
|
|
{
|
|
var query = BuildQuery(request, wellDto);
|
|
|
|
if (query is null)
|
|
return Enumerable.Empty<LimitingParameterDto>();
|
|
|
|
var data = (await query.ToListAsync(token))
|
|
.GroupBy(x => x.IdFeedRegulator);
|
|
|
|
List<LimitingParameterDto> result = new List<LimitingParameterDto>();
|
|
foreach (var item in data)
|
|
{
|
|
var trimData = TrimLimitingParameters(item, request);
|
|
|
|
var allItemDepths = trimData.Sum(x => x.DepthEnd - x.DepthStart);
|
|
var allItemDates = trimData.Sum(x => {
|
|
if (x.DateEnd is not null && x.DateStart is not null)
|
|
return (x.DateEnd.Value - x.DateStart.Value).TotalMinutes;
|
|
else return default(long);
|
|
});
|
|
|
|
result.Add(new LimitingParameterDto {
|
|
IdWell = wellDto.Id,
|
|
IdFeedRegulator = item.Key,
|
|
NumberInclusions = trimData.Count(),
|
|
Depth = allItemDepths.HasValue ? allItemDepths.Value : 0,
|
|
TimeMinutes = (float)allItemDates
|
|
});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private IEnumerable<LimitingParameter> TrimLimitingParameters(IEnumerable<LimitingParameter> data, LimitingParameterRequest request)
|
|
{
|
|
var result = data.Select((x) =>
|
|
{
|
|
if (request.GtDate.HasValue && x.DateStart < request.GtDate.Value)
|
|
{
|
|
x.DepthStart = x.GetDepth(request.GtDate.Value);
|
|
x.DateStart = request.GtDate.Value;
|
|
}
|
|
if (request.LtDate.HasValue && x.DateEnd > request.LtDate.Value)
|
|
{
|
|
x.DepthEnd = x.GetDepth(request.LtDate.Value);
|
|
x.DateEnd = request.LtDate.Value;
|
|
}
|
|
|
|
if (request.GtDepth.HasValue && x.DepthStart < request.GtDepth.Value)
|
|
{
|
|
x.DateStart = x.GetDate(request.GtDepth.Value);
|
|
x.DepthStart = (float)request.GtDepth.Value;
|
|
}
|
|
if (request.LtDepth.HasValue && x.DepthEnd > request.LtDepth.Value)
|
|
{
|
|
x.DateEnd = x.GetDate(request.LtDepth.Value);
|
|
x.DepthEnd = (float)request.LtDepth.Value;
|
|
}
|
|
return x;
|
|
}).ToList();
|
|
return result;
|
|
}
|
|
|
|
private IQueryable<LimitingParameter>? BuildQuery(LimitingParameterRequest request, WellDto wellDto)
|
|
{
|
|
var query = context.LimitingParameter
|
|
.OrderBy(x => x.Id)
|
|
.Where(x => x.IdTelemetry == wellDto.IdTelemetry)
|
|
.AsNoTracking();
|
|
|
|
if (request.GtDate.HasValue)
|
|
{
|
|
var gtDate = request.GtDate.Value.ToUtcDateTimeOffset(wellDto.Timezone.Hours);
|
|
query = query.Where(x => x.DateEnd >= gtDate);
|
|
}
|
|
|
|
if (request.LtDate.HasValue)
|
|
{
|
|
var ltDate = request.LtDate.Value.ToUtcDateTimeOffset(wellDto.Timezone.Hours);
|
|
query = query.Where(x => x.DateStart <= ltDate);
|
|
}
|
|
|
|
if (request.GtDepth.HasValue)
|
|
query = query.Where(x => x.DepthEnd >= request.GtDepth.Value);
|
|
|
|
if (request.LtDepth.HasValue)
|
|
query = query.Where(x => x.DepthStart <= request.LtDepth.Value);
|
|
|
|
return query;
|
|
}
|
|
}
|
|
#nullable disable
|
|
}
|