using AsbCloudApp.Data; 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 ProcessMapRepository : CrudWellRelatedRepositoryBase, IProcessMapRepository { private readonly IWellService wellService; public ProcessMapRepository(IAsbCloudDbContext context, IWellService wellService) : base(context, dbSet => dbSet.Include(x => x.Well) .Include(x => x.WellSectionType) ) { this.wellService = wellService; } public async Task> GetAllAsync(int idWell, DateTime? updateFrom, CancellationToken token) { var entities = await BuildQuery(idWell, updateFrom) .OrderBy(e => e.DepthStart) .ThenBy(e => e.Id) .ToListAsync(token) .ConfigureAwait(false); var dtos = entities.Select(Convert); return dtos; } public override async Task InsertAsync(ProcessMapDto dto, CancellationToken token) { dto.LastUpdate = DateTime.UtcNow; var result = await base.InsertAsync(dto, token); return result; } public override async Task UpdateAsync(ProcessMapDto dto, CancellationToken token) { dto.LastUpdate = DateTime.UtcNow; var result = await base.UpdateAsync(dto, token); return result; } private IQueryable BuildQuery(int idWell, DateTime? updateFrom) { var query = GetQuery().Where(e => e.IdWell == idWell); if (updateFrom is not null) { var timezone = wellService.GetTimezone(idWell); var updateFromUtc = updateFrom?.ToUtcDateTimeOffset(timezone.Hours); query.Where(e => e.LastUpdate >= updateFromUtc); } return query; } protected override ProcessMapDto Convert(ProcessMap entity) { var dto = entity.Adapt(); 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 { Fact = entity.FlowFact, Plan = entity.FlowPlan }; dto.TopDriveSpeed = new PlanFactDto { Fact = entity.TopDriveSpeedFact, Plan = entity.TopDriveSpeedPlan }; dto.TopDriveTorque = new PlanFactDto { Fact = entity.TopDriveTorqueFact, Plan = entity.TopDriveTorquePlan }; return dto; } } #nullable disable }