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

119 lines
3.8 KiB
C#
Raw Normal View History

using AsbCloudApp.Data.Trajectory;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudDb.Model.Trajectory;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure.Repository;
/// <summary>
/// CRUD-репозиторий для работы с траекториями (плановыми и фактическими)
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="Tdto"></typeparam>
public class TrajectoryEditableRepository<TEntity, Tdto> : ITrajectoryEditableRepository<Tdto>
where TEntity : Trajectory
where Tdto : TrajectoryGeoDto
{
2024-08-19 10:01:07 +05:00
private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
public TrajectoryEditableRepository(IAsbCloudDbContext db, IWellService wellService)
{
2024-08-19 10:01:07 +05:00
this.db = db;
this.wellService = wellService;
}
2023-11-30 09:40:51 +05:00
2024-08-19 10:01:07 +05:00
public async Task<int> AddRangeAsync(IEnumerable<Tdto> trajectoryRows, CancellationToken token)
{
var idWell = trajectoryRows.First().IdWell;
if (!trajectoryRows.All(r => r.IdWell == idWell))
throw new ArgumentInvalidException(nameof(trajectoryRows), "Все строки должны относиться к одной скважине");
2023-09-29 12:06:46 +05:00
2024-08-19 10:01:07 +05:00
var entities = trajectoryRows
.Select(e =>
{
var entity = Convert(e);
entity.Id = 0;
return entity;
});
2024-08-19 10:01:07 +05:00
db.Set<TEntity>().AddRange(entities);
return await db.SaveChangesAsync(token);
}
2024-08-19 10:01:07 +05:00
public async Task<int> AddAsync(Tdto trajectoryRow, CancellationToken token)
{
var entity = Convert(trajectoryRow);
entity.Id = 0;
db.Set<TEntity>().Add(entity);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
2022-12-27 21:45:03 +05:00
2024-08-19 10:01:07 +05:00
public async Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token)
{
var query = db.Set<TEntity>()
.Where(e => ids.Contains(e.Id));
db.Set<TEntity>().RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
2024-08-19 10:01:07 +05:00
public async Task<int> DeleteByIdWellAsync(int idWell, CancellationToken token)
{
var query = db.Set<TEntity>()
.Where(e => e.IdWell == idWell);
db.Set<TEntity>().RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
2022-12-27 00:02:49 +05:00
2024-08-19 10:01:07 +05:00
public async Task<IEnumerable<Tdto>> GetAsync(int idWell, CancellationToken token)
{
var well = wellService.GetOrDefault(idWell)
?? throw new ArgumentInvalidException(nameof(idWell), "idWell doesn`t exist");
2023-09-29 12:06:46 +05:00
2024-08-19 10:01:07 +05:00
var offsetHours = well.Timezone.Hours;
var query = db.Set<TEntity>()
.AsNoTracking()
.Where(x => x.IdWell == well.Id);
var entities = await query
.OrderBy(e => e.WellboreDepth)
.ToArrayAsync(token);
2023-11-30 09:40:51 +05:00
2024-08-19 10:01:07 +05:00
var result = entities
.Select(r => Convert(r, offsetHours));
return result;
}
2024-08-19 10:01:07 +05:00
public async Task<int> UpdateAsync(Tdto row, CancellationToken token)
{
var entity = Convert(row);
db.Set<TEntity>().Update(entity);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
2024-08-19 10:01:07 +05:00
private static Tdto Convert(TEntity entity, double offsetHours)
{
var dto = entity.Adapt<Tdto>();
dto.UpdateDate = entity.UpdateDate.ToOffset(TimeSpan.FromHours(offsetHours));
return dto;
}
2022-12-27 21:45:03 +05:00
2024-08-19 10:01:07 +05:00
private static TEntity Convert(Tdto dto)
{
var entity = dto.Adapt<TEntity>();
entity.UpdateDate = DateTimeOffset.UtcNow;
return entity;
}
}