forked from ddrilling/AsbCloudServer
116 lines
4.0 KiB
C#
116 lines
4.0 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Data.SAUB;
|
|
using AsbCloudApp.Repositories;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb;
|
|
using AsbCloudDb.Model;
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
|
{
|
|
#nullable enable
|
|
public class TelemetryWirelineRunOutRepository : ITelemetryWirelineRunOutRepository
|
|
{
|
|
private readonly IAsbCloudDbContext context;
|
|
private readonly ITelemetryService telemetryService;
|
|
private readonly IWellService wellService;
|
|
|
|
public TelemetryWirelineRunOutRepository(IAsbCloudDbContext context,
|
|
ITelemetryService telemetryService,
|
|
IWellService wellService)
|
|
{
|
|
this.context = context;
|
|
this.telemetryService = telemetryService;
|
|
this.wellService = wellService;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<int> AddOrUpdateAsync(string uid, TelemetryWirelineRunOutDto dto, CancellationToken token)
|
|
{
|
|
var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid);
|
|
var entity = Convert(idTelemetry, dto);
|
|
|
|
var updatingItem = context.TelemetryWirelineRunOut
|
|
.Where(x => x.IdTelemetry == idTelemetry)
|
|
.AsNoTracking()
|
|
.FirstOrDefault();
|
|
|
|
if (updatingItem is null)
|
|
context.TelemetryWirelineRunOut.Add(entity);
|
|
else
|
|
context.TelemetryWirelineRunOut.Upsert(entity);
|
|
|
|
return await context.SaveChangesAsync(token);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<TelemetryWirelineRunOutDto?> GetValueOrDefaultAsync(int idWell, CancellationToken token)
|
|
{
|
|
var well = await wellService.GetOrDefaultAsync(idWell, token).ConfigureAwait(false);
|
|
if (well is null)
|
|
return null;
|
|
|
|
var idTelemetry = telemetryService.GetOrDefaultIdTelemetryByIdWell(idWell);
|
|
if (idTelemetry is null)
|
|
return null;
|
|
|
|
var entity = await context.TelemetryWirelineRunOut
|
|
.Where(x => x.IdTelemetry == idTelemetry)
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(token)
|
|
.ConfigureAwait(false);
|
|
|
|
if (entity is null)
|
|
return null;
|
|
|
|
var timezone = telemetryService.GetTimezone((int)idTelemetry);
|
|
return Convert(entity, well, timezone.Hours);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<IEnumerable<TelemetryWirelineRunOutDto>> GetAllAsync(int idCompany, CancellationToken token)
|
|
{
|
|
var result = new List<TelemetryWirelineRunOutDto>();
|
|
var wells = await wellService.GetAllAsync(token)
|
|
.ConfigureAwait(false);
|
|
|
|
foreach (var well in wells)
|
|
{
|
|
bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync(idCompany, well.Id, token)
|
|
.ConfigureAwait(false);
|
|
|
|
if (!isCompanyOwnsWell)
|
|
continue;
|
|
|
|
var dto = await GetValueOrDefaultAsync(well.Id, token);
|
|
if (dto is not null)
|
|
result.Add(dto);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private static TelemetryWirelineRunOut Convert(int idTelemetry, TelemetryWirelineRunOutDto dto)
|
|
{
|
|
var entity = dto.Adapt<TelemetryWirelineRunOut>();
|
|
entity.IdTelemetry = idTelemetry;
|
|
entity.DateTime = entity.DateTime.ToUniversalTime();
|
|
return entity;
|
|
}
|
|
private static TelemetryWirelineRunOutDto Convert(TelemetryWirelineRunOut entity, WellDto well, double timezoneOffset)
|
|
{
|
|
var dto = entity.Adapt<TelemetryWirelineRunOutDto>();
|
|
dto.DateTime = entity.DateTime.ToUtcDateTimeOffset(timezoneOffset);
|
|
dto.WellInfo = well;
|
|
return dto;
|
|
}
|
|
}
|
|
#nullable disable
|
|
}
|