DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/WellCompositeRepository.cs
2023-01-26 15:37:46 +05:00

118 lines
4.1 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
#nullable enable
public class WellCompositeRepository : IWellCompositeRepository
{
private readonly IAsbCloudDbContext db;
private readonly IProcessMapRepository processMapRepository;
public WellCompositeRepository(IAsbCloudDbContext db, IProcessMapRepository processMapRepository)
{
this.db = db;
this.processMapRepository = processMapRepository;
}
/// <inheritdoc/>
public async Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken token)
{
var entities = await db.WellComposites
.Where(c => c.IdWell == idWell)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
return entities.Select(Convert);
}
/// <inheritdoc/>
public Task<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token)
{
db.WellComposites.RemoveRange(db.WellComposites
.Where(c => c.IdWell == idWell));
var entities = wellComposites
.Select(w => Convert(idWell, w));
db.WellComposites.AddRange(entities);
return db.SaveChangesAsync(token);
}
/// <inheritdoc/>
public async Task<IEnumerable<ProcessMapDto>> GetCompositeProcessMap(int idWell, CancellationToken token)
{
var result = new List<ProcessMapDto>();
var dtos = await GetAsync(idWell, token);
foreach (var dto in dtos)
{
var processMaps = (await processMapRepository.GetByRequesProcessMaplAsync(new WellCompositeRequest
{
IdWell = dto.IdWellSrc,
IdWellSectionType = dto.IdWellSectionType
}
, token))?
.Where(x => x.IdWellSectionType == dto.IdWellSectionType)
.Select(x => new ProcessMapDto
{
IdWell = dto.IdWell,
IdWellSectionType = dto.IdWellSectionType,
RopPlan = x.RopPlan,
DepthStart = x.DepthStart,
DepthEnd = x.DepthEnd,
AxialLoad = new PlanFactDto
{
Plan = x.AxialLoad.Fact ?? 0
},
Flow = new PlanFactDto
{
Plan = x.Flow.Fact ?? x.Flow.Plan
},
Pressure = new PlanFactDto
{
Plan = x.Pressure.Fact ?? x.Pressure.Plan
},
TopDriveSpeed = new PlanFactDto
{
Plan = x.TopDriveSpeed.Fact ?? x.TopDriveSpeed.Plan
},
TopDriveTorque = new PlanFactDto
{
Plan = x.TopDriveTorque.Fact ?? x.TopDriveTorque.Plan
},
LastUpdate = DateTime.UtcNow
});
if (processMaps is not null)
result.AddRange(processMaps);
}
return result;
}
private static WellComposite Convert(int idWell, WellCompositeDto dto)
{
var entity = dto.Adapt<WellComposite>();
entity.IdWell = idWell;
return entity;
}
private static WellCompositeDto Convert(WellComposite entity)
{
var dto = entity.Adapt<WellCompositeDto>();
return dto;
}
}
#nullable disable
}