DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/LimitingParameterService.cs

120 lines
4.6 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
2022-11-23 11:19:52 +05:00
using System;
using System.Collections.Generic;
2022-11-23 11:19:52 +05:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
#nullable enable
public class LimitingParameterService : ILimitingParameterService
{
private readonly ILimitingParameterRepository limitingParameterRepository;
private readonly IWellService wellService;
private readonly Dictionary<int, string> feedRegulatorData = new ()
{
{ 0, "Нет ограничения" },
{ 1, "МСП" },
{ 2, "Давление" },
{ 3, "Осевая нагрузка" },
{ 4, "Момент" }
};
public LimitingParameterService(ILimitingParameterRepository limitingParameterRepository,
IWellService wellService)
{
this.limitingParameterRepository = limitingParameterRepository;
this.wellService = wellService;
}
2022-11-25 11:56:21 +05:00
public async Task<IEnumerable<LimitingParameterDto>> GetStatAsync(LimitingParameterRequest request, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
if (well?.IdTelemetry is null || well.Timezone is null)
2022-11-25 11:56:21 +05:00
return Enumerable.Empty<LimitingParameterDto>();
2022-11-25 11:56:21 +05:00
var data = (await limitingParameterRepository.GetLimitingParametersAsync(request, well, token))
2022-11-23 11:19:52 +05:00
.GroupBy(x => x.IdFeedRegulator);
2022-11-25 11:56:21 +05:00
List<LimitingParameterDto> result = new List<LimitingParameterDto>(data.Count());
2022-11-23 11:19:52 +05:00
foreach (var item in data)
{
var trimData = TrimLimitingParameters(item, request);
var allItemDepths = trimData.Sum(x => x.DepthEnd - x.DepthStart);
var allItemDates = trimData.Sum(x => (x.DateEnd - x.DateStart).TotalMinutes);
result.Add(new LimitingParameterDto
{
IdWell = well.Id,
IdFeedRegulator = item.Key,
Depth = allItemDepths,
TotalMinutes = (float)allItemDates,
NumberInclusions = item.Count(),
NameFeedRegulator = feedRegulatorData.GetValueOrDefault(item.Key) ?? $"Id: {item.Key}"
2022-11-23 11:19:52 +05:00
});
}
return result;
}
public Dictionary<int, string> GetLimitingParameteraNames() //TODO: Перенести получение ограничений в репозиторий
{
return feedRegulatorData;
}
2022-11-23 11:19:52 +05:00
private IEnumerable<LimitingParameterDataDto> TrimLimitingParameters(IEnumerable<LimitingParameterDataDto> data, LimitingParameterRequest request)
{
var result = data.Select((x) =>
{
if (request.GtDate.HasValue && x.DateStart < request.GtDate.Value)
{
x.DepthStart = GetDepth(request.GtDate.Value, x);
x.DateStart = request.GtDate.Value;
}
if (request.LtDate.HasValue && x.DateEnd > request.LtDate.Value)
{
x.DepthEnd = GetDepth(request.LtDate.Value, x);
x.DateEnd = request.LtDate.Value;
}
if (request.GtDepth.HasValue && x.DepthStart < request.GtDepth.Value)
{
x.DateStart = GetDate(request.GtDepth.Value, x);
x.DepthStart = (float)request.GtDepth.Value;
}
if (request.LtDepth.HasValue && x.DepthEnd > request.LtDepth.Value)
{
x.DateEnd = GetDate(request.LtDepth.Value, x);
x.DepthEnd = (float)request.LtDepth.Value;
}
return x;
2022-11-25 11:56:21 +05:00
});
2022-11-23 11:19:52 +05:00
return result;
}
private float GetDepth(DateTimeOffset date, LimitingParameterDataDto dto)
{
var a = (date - dto.DateStart).TotalSeconds;
var b = (dto.DateEnd - dto.DateStart).TotalSeconds;
var c = dto.DepthEnd - dto.DepthStart;
var result = dto.DepthStart + (a / b) * c;
return (float)result;
}
private DateTime GetDate(double depth, LimitingParameterDataDto dto)
2022-11-23 11:19:52 +05:00
{
var a = depth - dto.DepthStart;
var b = dto.DepthEnd - dto.DepthStart;
var c = (dto.DateEnd - dto.DateStart);
var result = dto.DateStart + (a / b) * c;
return result;
}
}
#nullable disable
}