DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/TelemetryWirelineRunOutRepository.cs
2023-01-11 16:00:11 +05:00

116 lines
4.1 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.SAUB;
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 timezoneOffset = telemetryService.GetTimezone(idTelemetry).Hours;
var entity = Convert(idTelemetry, dto, timezoneOffset);
var updatingItem = context.TelemetryWirelineRunOut
.Where(x => x.IdTelemetry == idTelemetry)
.FirstOrDefault();
if (updatingItem is null)
context.TelemetryWirelineRunOut.Add(entity);
else
context.TelemetryWirelineRunOut.Update(entity);
return await context.SaveChangesAsync(token);
}
/// <inheritdoc/>
public async Task<TelemetryWirelineRunOutDto?> GetOrDefaultAsync(int idWell, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(idWell, token).ConfigureAwait(false);
if (well is null)
return null;
return await GetOrDefaultAsync(well, token);
}
private async Task<TelemetryWirelineRunOutDto?> GetOrDefaultAsync(WellDto well, CancellationToken token)
{
var idTelemetry = telemetryService.GetOrDefaultIdTelemetryByIdWell(well.Id);
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 timezoneHours = well.Timezone.Hours;
return Convert(entity, well, timezoneHours);
}
/// <inheritdoc/>
public async Task<IEnumerable<TelemetryWirelineRunOutDto>> GetAllAsync(int idCompany, CancellationToken token)
{
var wells = await wellService.GetWellsByCompanyAsync(idCompany, token)
.ConfigureAwait(false);
var result = new List<TelemetryWirelineRunOutDto>(wells.Count());
foreach (var well in wells)
{
var dto = await GetOrDefaultAsync(well, token);
if (dto is not null)
result.Add(dto);
}
return result;
}
private static TelemetryWirelineRunOut Convert(int idTelemetry, TelemetryWirelineRunOutDto dto, double timezoneOffset)
{
var entity = dto.Adapt<TelemetryWirelineRunOut>();
entity.IdTelemetry = idTelemetry;
entity.DateTime = dto.DateTime.ToUtcDateTimeOffset(timezoneOffset);
return entity;
}
private static TelemetryWirelineRunOutDto Convert(TelemetryWirelineRunOut entity, WellDto well, double timezoneOffset)
{
var dto = entity.Adapt<TelemetryWirelineRunOutDto>();
dto.DateTime = entity.DateTime.ToRemoteDateTime(timezoneOffset);
dto.WellInfo = well;
return dto;
}
}
#nullable disable
}