DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/LimitingParameterRepository.cs

80 lines
3.0 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
public class LimitingParameterRepository : ILimitingParameterRepository
{
private readonly IAsbCloudDbContext context;
public LimitingParameterRepository(IAsbCloudDbContext context)
{
this.context = context;
}
2022-11-25 11:56:21 +05:00
public async Task<IEnumerable<LimitingParameterDataDto>> GetLimitingParametersAsync(LimitingParameterRequest request, WellDto wellDto, CancellationToken token)
{
var timezoneOffset = wellDto.Timezone.Hours;
var IdTelemetry = wellDto.IdTelemetry!.Value;
return await GetLimitingParametersAsync(request, IdTelemetry, timezoneOffset, token);
}
public async Task<IEnumerable<LimitingParameterDataDto>> GetLimitingParametersAsync(LimitingParameterRequest request, int idTelemetry, double timezoneHours, CancellationToken token)
{
var timezoneSpan = TimeSpan.FromHours(timezoneHours);
var query = BuildQuery(request, idTelemetry, timezoneHours);
2022-11-24 11:15:50 +05:00
var data = (await query.ToListAsync(token))
.Select(x => new LimitingParameterDataDto
{
IdWell = request.IdWell,
2022-11-24 11:15:50 +05:00
IdTelemetry = x.IdTelemetry,
IdFeedRegulator = x.IdFeedRegulator,
DateStart = DateTime.SpecifyKind(x.DateStart.UtcDateTime + timezoneSpan, DateTimeKind.Unspecified),
DateEnd = DateTime.SpecifyKind(x.DateEnd.UtcDateTime + timezoneSpan, DateTimeKind.Unspecified),
2022-11-24 11:15:50 +05:00
DepthStart = x.DepthStart,
DepthEnd = x.DepthEnd
});
return data;
}
private IQueryable<LimitingParameter> BuildQuery(LimitingParameterRequest request, int idTelemetry, double timezoneHours)
{
var query = context.LimitingParameter
2022-11-24 11:15:50 +05:00
.OrderBy(x => x.Id)
.Where(x => x.IdTelemetry == idTelemetry)
.AsNoTracking();
if (request.GtDate.HasValue)
{
var gtDate = request.GtDate.Value.ToUtcDateTimeOffset(timezoneHours);
query = query.Where(x => x.DateEnd >= gtDate);
}
if (request.LtDate.HasValue)
{
var ltDate = request.LtDate.Value.ToUtcDateTimeOffset(timezoneHours);
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;
}
}
}