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

121 lines
4.6 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
{
#nullable enable
public class PlannedTrajectoryRepository : IPlannedTrajectoryRepository
{
private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
public PlannedTrajectoryRepository(IAsbCloudDbContext db, IWellService wellService)
{
this.db = db;
this.wellService = wellService;
}
/// <inheritdoc/>
public async Task<int> AddRangeAsync(IEnumerable<PlannedTrajectoryDto> 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(PlannedTrajectoryDto 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<PlannedTrajectoryDto>> 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)
.ToListAsync(token);
var result = entities
.Select(r => Convert(r, offsetHours));
return result;
}
/// <inheritdoc/>
public async Task<int> UpdateAsync(PlannedTrajectoryDto 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 PlannedTrajectoryDto Convert(PlannedTrajectory entity, double offsetHours)
{
var dto = entity.Adapt<PlannedTrajectoryDto>();
dto.UpdateDate = entity.UpdateDate.ToRemoteDateTime(offsetHours);
return dto;
}
private PlannedTrajectory Convert(PlannedTrajectoryDto dto, double offsetHours)
{
var entity = dto.Adapt<PlannedTrajectory>();
entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(offsetHours);
return entity;
}
}
#nullable disable
}