DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/ProcessMapRepository.cs
2023-02-02 14:30:27 +05:00

160 lines
5.4 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using DocumentFormat.OpenXml.Spreadsheet;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
#nullable enable
public class ProcessMapRepository : CrudWellRelatedRepositoryBase<ProcessMapDto, ProcessMap>,
IProcessMapRepository
{
private readonly IWellService wellService;
public ProcessMapRepository(IAsbCloudDbContext context, IWellService wellService)
: base(context, dbSet =>
dbSet.Include(x => x.Well)
.Include(x => x.WellSectionType)
)
{
this.wellService = wellService;
}
public async Task<IEnumerable<ProcessMapDto>> GetAllAsync(int idWell,
DateTime? updateFrom, CancellationToken token)
{
var requests = new[]
{
new ProcessMapRequest {
IdWell = idWell,
UpdateFrom = updateFrom
}
};
var entities = await BuildQuery(requests)
.OrderBy(e => e.DepthStart)
.ThenBy(e => e.Id)
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Select(Convert);
return dtos;
}
public async Task<IEnumerable<ProcessMapDto>> GetProcessMapAsync(IEnumerable<ProcessMapRequest> requests, CancellationToken token)
{
var entities = await BuildQuery(requests)
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Select(Convert).ToList();
return dtos;
}
public override async Task<int> InsertAsync(ProcessMapDto dto,
CancellationToken token)
{
dto.LastUpdate = DateTime.UtcNow;
var result = await base.InsertAsync(dto, token);
return result;
}
public override async Task<int> UpdateAsync(ProcessMapDto dto,
CancellationToken token)
{
dto.LastUpdate = DateTime.UtcNow;
var result = await base.UpdateAsync(dto, token);
return result;
}
private IQueryable<ProcessMap> BuildQuery(IEnumerable<ProcessMapRequest> requests)
{
var query = GetQuery();
Func<ProcessMap, bool>? p = null;
foreach (var request in requests)
{
var p2 = (ProcessMap map) => map.IdWell == request.IdWell;
if (request.IdWellSectionType is not null)
p2 = (ProcessMap map) => p2(map) && map.IdWellSectionType == request.IdWellSectionType;
if (request.UpdateFrom is not null)
{
var timezone = wellService.GetTimezone(request.IdWell);
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
p2 = (ProcessMap map) => p2(map) && map.LastUpdate >= updateFromUtc;
}
p = p is null
? p2
: (ProcessMap map) => p(map) || p2(map);
}
if(p is not null)
query.Where(p);
return query;
}
protected override ProcessMapDto Convert(ProcessMap entity)
{
var dto = entity.Adapt<ProcessMapDto>();
dto.LastUpdate = entity.LastUpdate.ToRemoteDateTime(entity.Well.Timezone.Hours);
dto.AxialLoad = new PlanFactDto
{
Fact = entity.AxialLoadFact,
Plan = entity.AxialLoadPlan
};
dto.Flow = new PlanFactDto
{
Fact = entity.FlowFact,
Plan = entity.FlowPlan
};
dto.Pressure = new PlanFactDto
{
Fact = entity.PressureFact,
Plan = entity.PressurePlan
};
dto.TopDriveSpeed = new PlanFactDto
{
Fact = entity.TopDriveSpeedFact,
Plan = entity.TopDriveSpeedPlan
};
dto.TopDriveTorque = new PlanFactDto
{
Fact = entity.TopDriveTorqueFact,
Plan = entity.TopDriveTorquePlan
};
return dto;
}
protected override ProcessMap Convert(ProcessMapDto dto)
{
var entity = dto.Adapt<ProcessMap>();
entity.AxialLoadPlan = dto.AxialLoad.Plan;
entity.AxialLoadFact = dto.AxialLoad.Fact;
entity.FlowPlan = dto.Flow.Plan;
entity.FlowFact = dto.Flow.Fact;
entity.PressurePlan = dto.Pressure.Plan;
entity.PressureFact = dto.Pressure.Fact;
entity.TopDriveSpeedPlan = dto.TopDriveSpeed.Plan;
entity.TopDriveSpeedFact = dto.TopDriveSpeed.Fact;
entity.TopDriveTorquePlan = dto.TopDriveTorque.Plan;
entity.TopDriveTorqueFact = dto.TopDriveTorque.Fact;
return entity;
}
}
#nullable disable
}