forked from ddrilling/AsbCloudServer
129 lines
4.1 KiB
C#
129 lines
4.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Data.ProcessMaps;
|
|
using AsbCloudApp.Repositories;
|
|
using AsbCloudApp.Requests;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudDb.Model.ProcessMaps;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace AsbCloudInfrastructure.Repository;
|
|
|
|
public class WellDrillingProcessMapRepository : CrudWellRelatedRepositoryBase<WellDrillingProcessMapDto, WellDrillingProcessMap>,
|
|
IWellDrillingProcessMapRepository
|
|
{
|
|
private readonly IWellService wellService;
|
|
|
|
public WellDrillingProcessMapRepository(IAsbCloudDbContext context, IWellService wellService)
|
|
: base(context, dbSet =>
|
|
dbSet
|
|
.Include(p => p.WellSectionType)
|
|
.Include(p => p.Well))
|
|
{
|
|
this.wellService = wellService;
|
|
}
|
|
|
|
public async Task<IEnumerable<WellDrillingProcessMapDto>> GetAsync(WellDrillingProcessMapRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var requests = new[] { request };
|
|
|
|
var entities = await BuildQuery(requests)
|
|
.ToArrayAsync(cancellationToken);
|
|
|
|
return entities.Select(Convert);
|
|
}
|
|
|
|
public async Task<IEnumerable<WellDrillingProcessMapDto>> GetAsync(
|
|
IEnumerable<WellDrillingProcessMapRequest> requests,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var entities = await BuildQuery(requests)
|
|
.ToArrayAsync(cancellationToken);
|
|
|
|
return entities.Select(Convert);
|
|
}
|
|
|
|
private IQueryable<WellDrillingProcessMap> BuildQuery(IEnumerable<WellDrillingProcessMapRequest> requests)
|
|
{
|
|
var query = GetQuery();
|
|
|
|
foreach (var request in requests)
|
|
{
|
|
query = query.Where(p => p.IdWell == request.IdWell);
|
|
|
|
if (request.IdWellSectionType is not null)
|
|
query = query.Where(p => p.IdWellSectionType == request.IdWellSectionType);
|
|
|
|
if (request.UpdateFrom is not null)
|
|
{
|
|
var timezone = wellService.GetTimezone(request.IdWell);
|
|
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
|
|
|
|
query = query.Where(p => p.LastUpdate >= updateFromUtc);
|
|
}
|
|
}
|
|
|
|
return query.OrderBy(e => e.DepthStart)
|
|
.ThenBy(e => e.Id)
|
|
.AsNoTracking();
|
|
}
|
|
|
|
protected override WellDrillingProcessMapDto Convert(WellDrillingProcessMap entity)
|
|
{
|
|
var dto = entity.Adapt<WellDrillingProcessMapDto>();
|
|
dto.LastUpdate = entity.LastUpdate.ToRemoteDateTime(entity.Well.Timezone.Hours);
|
|
dto.AxialLoad = new PlanLimitDto
|
|
{
|
|
LimitMax = entity.AxialLoadLimitMax,
|
|
Plan = entity.AxialLoadPlan
|
|
};
|
|
dto.Flow = new PlanLimitDto
|
|
{
|
|
LimitMax = entity.FlowLimitMax,
|
|
Plan = entity.FlowPlan
|
|
};
|
|
dto.Pressure = new PlanLimitDto
|
|
{
|
|
LimitMax = entity.PressureLimitMax,
|
|
Plan = entity.PressurePlan
|
|
};
|
|
dto.TopDriveSpeed = new PlanLimitDto
|
|
{
|
|
LimitMax = entity.TopDriveSpeedLimitMax,
|
|
Plan = entity.TopDriveSpeedPlan
|
|
};
|
|
dto.TopDriveTorque = new PlanLimitDto
|
|
{
|
|
LimitMax = entity.TopDriveTorqueLimitMax,
|
|
Plan = entity.TopDriveTorquePlan
|
|
};
|
|
return dto;
|
|
}
|
|
|
|
protected override WellDrillingProcessMap Convert(WellDrillingProcessMapDto dto)
|
|
{
|
|
var entity = dto.Adapt<WellDrillingProcessMap>();
|
|
entity.AxialLoadPlan = dto.AxialLoad.Plan;
|
|
entity.AxialLoadLimitMax = dto.AxialLoad.LimitMax;
|
|
|
|
entity.FlowPlan = dto.Flow.Plan;
|
|
entity.FlowLimitMax = dto.Flow.LimitMax;
|
|
|
|
entity.PressurePlan = dto.Pressure.Plan;
|
|
entity.PressureLimitMax = dto.Pressure.LimitMax;
|
|
|
|
entity.TopDriveSpeedPlan = dto.TopDriveSpeed.Plan;
|
|
entity.TopDriveSpeedLimitMax = dto.TopDriveSpeed.LimitMax;
|
|
|
|
entity.TopDriveTorquePlan = dto.TopDriveTorque.Plan;
|
|
entity.TopDriveTorqueLimitMax = dto.TopDriveTorque.LimitMax;
|
|
|
|
return entity;
|
|
}
|
|
} |