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

160 lines
5.4 KiB
C#
Raw Normal View History

2022-12-05 12:39:25 +05:00
using AsbCloudApp.Data;
2022-12-27 14:30:52 +05:00
using AsbCloudApp.Data.ProcessMap;
2023-02-02 12:04:50 +05:00
using AsbCloudApp.Repositories;
2023-01-26 15:37:46 +05:00
using AsbCloudApp.Requests;
2022-12-05 12:39:25 +05:00
using AsbCloudApp.Services;
using AsbCloudDb.Model;
2023-02-02 14:30:27 +05:00
using DocumentFormat.OpenXml.Spreadsheet;
2022-12-05 12:39:25 +05:00
using Mapster;
using Microsoft.EntityFrameworkCore;
2023-02-02 14:30:27 +05:00
using Org.BouncyCastle.Asn1.Ocsp;
2022-12-05 12:39:25 +05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
2022-12-07 12:20:24 +05:00
#nullable enable
2022-12-07 15:04:36 +05:00
public class ProcessMapRepository : CrudWellRelatedRepositoryBase<ProcessMapDto, ProcessMap>,
2022-12-07 08:47:41 +05:00
IProcessMapRepository
2022-12-05 12:39:25 +05:00
{
private readonly IWellService wellService;
public ProcessMapRepository(IAsbCloudDbContext context, IWellService wellService)
2022-12-07 08:47:41 +05:00
: base(context, dbSet =>
dbSet.Include(x => x.Well)
.Include(x => x.WellSectionType)
)
2022-12-05 12:39:25 +05:00
{
this.wellService = wellService;
}
public async Task<IEnumerable<ProcessMapDto>> GetAllAsync(int idWell,
2022-12-07 10:52:35 +05:00
DateTime? updateFrom, CancellationToken token)
2022-12-05 12:39:25 +05:00
{
2023-02-02 14:30:27 +05:00
var requests = new[]
2023-02-02 10:32:53 +05:00
{
new ProcessMapRequest {
IdWell = idWell,
UpdateFrom = updateFrom
}
};
2023-02-02 14:30:27 +05:00
var entities = await BuildQuery(requests)
2022-12-05 12:39:25 +05:00
.OrderBy(e => e.DepthStart)
.ThenBy(e => e.Id)
.ToListAsync(token)
.ConfigureAwait(false);
2022-12-07 10:52:35 +05:00
var dtos = entities.Select(Convert);
2022-12-05 12:39:25 +05:00
return dtos;
}
2023-02-02 14:30:27 +05:00
public async Task<IEnumerable<ProcessMapDto>> GetProcessMapAsync(IEnumerable<ProcessMapRequest> requests, CancellationToken token)
2023-01-26 15:37:46 +05:00
{
2023-02-02 12:04:50 +05:00
var entities = await BuildQuery(requests)
2023-02-02 10:32:53 +05:00
.ToListAsync(token)
.ConfigureAwait(false);
2023-01-26 15:37:46 +05:00
var dtos = entities.Select(Convert).ToList();
return dtos;
}
2022-12-05 12:39:25 +05:00
public override async Task<int> InsertAsync(ProcessMapDto dto,
2022-12-07 10:52:35 +05:00
CancellationToken token)
2022-12-05 12:39:25 +05:00
{
dto.LastUpdate = DateTime.UtcNow;
var result = await base.InsertAsync(dto, token);
return result;
}
public override async Task<int> UpdateAsync(ProcessMapDto dto,
2022-12-07 10:52:35 +05:00
CancellationToken token)
2022-12-05 12:39:25 +05:00
{
dto.LastUpdate = DateTime.UtcNow;
var result = await base.UpdateAsync(dto, token);
return result;
}
2023-02-02 14:30:27 +05:00
private IQueryable<ProcessMap> BuildQuery(IEnumerable<ProcessMapRequest> requests)
2022-12-07 10:52:35 +05:00
{
2023-02-02 10:32:53 +05:00
var query = GetQuery();
2023-02-02 14:30:27 +05:00
Func<ProcessMap, bool>? p = null;
foreach (var request in requests)
2022-12-07 10:52:35 +05:00
{
2023-02-02 14:30:27 +05:00
var p2 = (ProcessMap map) => map.IdWell == request.IdWell;
2023-02-02 10:32:53 +05:00
2023-02-02 14:30:27 +05:00
if (request.IdWellSectionType is not null)
p2 = (ProcessMap map) => p2(map) && map.IdWellSectionType == request.IdWellSectionType;
2023-02-02 10:32:53 +05:00
2023-02-02 14:30:27 +05:00
if (request.UpdateFrom is not null)
2023-02-02 10:32:53 +05:00
{
2023-02-02 14:30:27 +05:00
var timezone = wellService.GetTimezone(request.IdWell);
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
p2 = (ProcessMap map) => p2(map) && map.LastUpdate >= updateFromUtc;
2023-02-02 10:32:53 +05:00
}
2023-02-02 14:30:27 +05:00
p = p is null
? p2
: (ProcessMap map) => p(map) || p2(map);
2022-12-07 10:52:35 +05:00
}
2023-02-02 14:30:27 +05:00
if(p is not null)
query.Where(p);
2022-12-07 10:52:35 +05:00
return query;
}
2022-12-05 12:39:25 +05:00
protected override ProcessMapDto Convert(ProcessMap entity)
{
2022-12-07 08:47:41 +05:00
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
{
2022-12-14 09:10:01 +05:00
Fact = entity.PressureFact,
Plan = entity.PressurePlan
2022-12-07 08:47:41 +05:00
};
dto.TopDriveSpeed = new PlanFactDto
{
Fact = entity.TopDriveSpeedFact,
Plan = entity.TopDriveSpeedPlan
};
dto.TopDriveTorque = new PlanFactDto
{
Fact = entity.TopDriveTorqueFact,
Plan = entity.TopDriveTorquePlan
2022-12-05 12:39:25 +05:00
};
2022-12-07 08:47:41 +05:00
return dto;
2022-12-05 12:39:25 +05:00
}
2022-12-14 09:10:01 +05:00
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;
}
2022-12-05 12:39:25 +05:00
}
2022-12-07 12:20:24 +05:00
#nullable disable
2022-12-05 12:39:25 +05:00
}