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

105 lines
3.3 KiB
C#
Raw Normal View History

2022-12-05 12:39:25 +05:00
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
{
2022-12-07 12:20:24 +05:00
#nullable enable
2022-12-07 15:04:36 +05:00
public class ProcessMapRepository : CrudWellRelatedRepositoryBase<ProcessMapDto, ProcessMap>,
2022-12-07 08:47:41 +05:00
IProcessMapRepository
2022-12-05 12:39:25 +05:00
{
private readonly IWellService wellService;
public ProcessMapRepository(IAsbCloudDbContext context, IWellService wellService)
2022-12-07 08:47:41 +05:00
: base(context, dbSet =>
dbSet.Include(x => x.Well)
.Include(x => x.WellSectionType)
)
2022-12-05 12:39:25 +05:00
{
this.wellService = wellService;
}
public async Task<IEnumerable<ProcessMapDto>> GetAllAsync(int idWell,
2022-12-07 10:52:35 +05:00
DateTime? updateFrom, CancellationToken token)
2022-12-05 12:39:25 +05:00
{
2022-12-07 10:52:35 +05:00
var entities = await BuildQuery(idWell, updateFrom)
2022-12-05 12:39:25 +05:00
.OrderBy(e => e.DepthStart)
.ThenBy(e => e.Id)
.ToListAsync(token)
.ConfigureAwait(false);
2022-12-07 10:52:35 +05:00
var dtos = entities.Select(Convert);
2022-12-05 12:39:25 +05:00
return dtos;
}
public override async Task<int> InsertAsync(ProcessMapDto dto,
2022-12-07 10:52:35 +05:00
CancellationToken token)
2022-12-05 12:39:25 +05:00
{
dto.LastUpdate = DateTime.UtcNow;
var result = await base.InsertAsync(dto, token);
return result;
}
public override async Task<int> UpdateAsync(ProcessMapDto dto,
2022-12-07 10:52:35 +05:00
CancellationToken token)
2022-12-05 12:39:25 +05:00
{
dto.LastUpdate = DateTime.UtcNow;
var result = await base.UpdateAsync(dto, token);
return result;
}
2022-12-07 10:52:35 +05:00
private IQueryable<ProcessMap> 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;
}
2022-12-05 12:39:25 +05:00
protected override ProcessMapDto Convert(ProcessMap entity)
{
2022-12-07 08:47:41 +05:00
var dto = entity.Adapt<ProcessMapDto>();
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
2022-12-05 12:39:25 +05:00
};
2022-12-07 08:47:41 +05:00
return dto;
2022-12-05 12:39:25 +05:00
}
}
2022-12-07 12:20:24 +05:00
#nullable disable
2022-12-05 12:39:25 +05:00
}