DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/TrajectoryPlanRepository.cs
2023-05-30 11:21:07 +05:00

120 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
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
{
public class TrajectoryPlanRepository : ITrajectoryPlanRepository
{
private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
public TrajectoryPlanRepository(IAsbCloudDbContext db, IWellService wellService)
{
this.db = db;
this.wellService = wellService;
}
/// <inheritdoc/>
public async Task<int> AddRangeAsync(IEnumerable<TrajectoryGeoPlanDto> plannedTrajectoryRows, CancellationToken token)
{
var idWell = plannedTrajectoryRows.First().IdWell;
if (!plannedTrajectoryRows.All(r => r.IdWell == idWell))
throw new ArgumentInvalidException("Все строки должны относиться к одной скважине", nameof(plannedTrajectoryRows));
var offsetHours = wellService.GetTimezone(idWell).Hours;
var entities = plannedTrajectoryRows
.Select(e =>
{
var entity = Convert(e, offsetHours);
entity.Id = 0;
return entity;
});
db.PlannedTrajectories.AddRange(entities);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<int> AddAsync(TrajectoryGeoPlanDto plannedTrajectoryRow, CancellationToken token)
{
var offsetHours = wellService.GetTimezone(plannedTrajectoryRow.IdWell).Hours;
var entity = Convert(plannedTrajectoryRow, offsetHours);
entity.Id = 0;
db.PlannedTrajectories.Add(entity);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token)
{
var query = db.PlannedTrajectories
.Where(e => ids.Contains(e.Id));
db.PlannedTrajectories.RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<int> DeleteByIdWellAsync(int idWell, CancellationToken token)
{
var query = db.PlannedTrajectories
.Where(e => e.IdWell == idWell);
db.PlannedTrajectories.RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<IEnumerable<TrajectoryGeoPlanDto>> GetAsync(int idWell, CancellationToken token)
{
var well = wellService.GetOrDefault(idWell);
if (well is null || well.Timezone is null)
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
var offsetHours = well.Timezone.Hours;
var query = db.PlannedTrajectories
.AsNoTracking()
.Where(x => x.IdWell == idWell);
var entities = await query
.OrderBy(e => e.WellboreDepth)
.ToArrayAsync(token);
var result = entities
.Select(r => Convert(r, offsetHours));
return result;
}
/// <inheritdoc/>
public async Task<int> UpdateAsync(TrajectoryGeoPlanDto row, CancellationToken token)
{
var offsetHours = wellService.GetTimezone(row.IdWell).Hours;
var entity = Convert(row, offsetHours);
db.PlannedTrajectories.Update(entity);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
private TrajectoryGeoPlanDto Convert(PlannedTrajectory entity, double offsetHours)
{
var dto = entity.Adapt<TrajectoryGeoPlanDto>();
dto.UpdateDate = entity.UpdateDate.ToRemoteDateTime(offsetHours);
return dto;
}
private PlannedTrajectory Convert(TrajectoryGeoPlanDto dto, double offsetHours)
{
var entity = dto.Adapt<PlannedTrajectory>();
entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(offsetHours);
return entity;
}
}
}