DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/LimitingParameterRepository.cs
2022-11-23 11:19:52 +05:00

73 lines
2.4 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
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<LimitingParameterDataDto>> GetStatOrDefaultAsync(LimitingParameterRequest request, WellDto wellDto, CancellationToken token)
{
var query = BuildQuery(request, wellDto);
if (query is null)
return Enumerable.Empty<LimitingParameterDataDto>();
var data = (await query.ToListAsync(token))
.Select(x => new LimitingParameterDataDto {
IdWell = wellDto.Id,
IdTelemetry = x.IdTelemetry,
IdFeedRegulator = x.IdFeedRegulator,
DateStart = x.DateStart,
DateEnd = x.DateEnd,
DepthStart = x.DepthStart,
DepthEnd = x.DepthEnd
});
return data;
}
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
}