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

111 lines
3.6 KiB
C#
Raw Normal View History

2021-10-12 12:17:46 +05:00
using AsbCloudApp.Data;
2023-01-26 15:37:46 +05:00
using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Repositories;
2023-01-26 15:37:46 +05:00
using AsbCloudApp.Requests;
2021-10-12 12:17:46 +05:00
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
2023-01-26 15:37:46 +05:00
using System;
2021-10-12 12:17:46 +05:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
2021-10-12 12:17:46 +05:00
{
#nullable enable
public class WellCompositeRepository : IWellCompositeRepository
2021-10-12 12:17:46 +05:00
{
private readonly IAsbCloudDbContext db;
2023-01-26 15:37:46 +05:00
private readonly IProcessMapRepository processMapRepository;
2021-10-12 12:17:46 +05:00
2023-01-26 15:37:46 +05:00
public WellCompositeRepository(IAsbCloudDbContext db, IProcessMapRepository processMapRepository)
2021-10-12 12:17:46 +05:00
{
this.db = db;
2023-01-26 15:37:46 +05:00
this.processMapRepository = processMapRepository;
2021-10-12 12:17:46 +05:00
}
2022-10-19 15:00:18 +05:00
/// <inheritdoc/>
2021-10-12 12:17:46 +05:00
public async Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken token)
{
var entities = await db.WellComposites
2021-10-12 12:17:46 +05:00
.Where(c => c.IdWell == idWell)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
return entities.Select(Convert);
2021-10-12 12:17:46 +05:00
}
2022-10-19 15:00:18 +05:00
/// <inheritdoc/>
2021-10-12 12:17:46 +05:00
public Task<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token)
{
db.WellComposites.RemoveRange(db.WellComposites
2021-10-12 12:17:46 +05:00
.Where(c => c.IdWell == idWell));
2021-10-12 12:17:46 +05:00
var entities = wellComposites
.Select(w => Convert(idWell, w));
db.WellComposites.AddRange(entities);
return db.SaveChangesAsync(token);
2021-10-12 12:17:46 +05:00
}
2023-01-26 15:37:46 +05:00
/// <inheritdoc/>
2023-01-30 10:52:12 +05:00
public async Task<IEnumerable<ProcessMapDto>?> GetCompositeProcessMap(int idWell, CancellationToken token)
2023-01-26 15:37:46 +05:00
{
var dtos = await GetAsync(idWell, token);
2023-02-02 10:32:53 +05:00
2023-02-02 12:04:50 +05:00
var request = dtos.Select(x => new ProcessMapRequest {
IdWell = x.IdWellSrc,
IdWellSectionTypes = x.IdWellSectionType
});
var processMap = (await processMapRepository.GetProcessMaplAsync(request, token));
2023-01-26 15:37:46 +05:00
2023-01-30 10:52:12 +05:00
var result = processMap?.Select(x => new ProcessMapDto
{
IdWell = x.IdWell,
IdWellSectionType = x.IdWellSectionType,
RopPlan = x.RopPlan,
DepthStart = x.DepthStart,
DepthEnd = x.DepthEnd,
AxialLoad = new PlanFactDto
{
Plan = x.AxialLoad.Fact ?? x.AxialLoad.Plan,
},
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
});
2023-01-26 15:37:46 +05:00
return result;
}
2022-10-19 15:00:18 +05:00
private static WellComposite Convert(int idWell, WellCompositeDto dto)
{
var entity = dto.Adapt<WellComposite>();
entity.IdWell = idWell;
return entity;
}
2022-10-19 15:00:18 +05:00
private static WellCompositeDto Convert(WellComposite entity)
{
var dto = entity.Adapt<WellCompositeDto>();
return dto;
}
2021-10-12 12:17:46 +05:00
}
#nullable disable
2021-10-12 12:17:46 +05:00
}