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; namespace AsbCloudInfrastructure.Repository; /// /// CRUD-репозиторий для работы с траекториями (плановыми и фактическими) /// /// /// public class TrajectoryEditableRepository : ITrajectoryEditableRepository where TEntity : Trajectory where Tdto : TrajectoryGeoDto { private readonly IAsbCloudDbContext db; private readonly IWellService wellService; public TrajectoryEditableRepository(IAsbCloudDbContext db, IWellService wellService) { this.db = db; this.wellService = wellService; } public async Task AddRangeAsync(IEnumerable trajectoryRows, CancellationToken token) { var idWell = trajectoryRows.First().IdWell; if (!trajectoryRows.All(r => r.IdWell == idWell)) throw new ArgumentInvalidException(nameof(trajectoryRows), "Все строки должны относиться к одной скважине"); var entities = trajectoryRows .Select(e => { var entity = Convert(e); entity.Id = 0; return entity; }); db.Set().AddRange(entities); return await db.SaveChangesAsync(token); } public async Task AddAsync(Tdto trajectoryRow, CancellationToken token) { var entity = Convert(trajectoryRow); entity.Id = 0; db.Set().Add(entity); return await db.SaveChangesAsync(token) .ConfigureAwait(false); } public async Task DeleteRangeAsync(IEnumerable ids, CancellationToken token) { var query = db.Set() .Where(e => ids.Contains(e.Id)); db.Set().RemoveRange(query); return await db.SaveChangesAsync(token) .ConfigureAwait(false); } public async Task DeleteByIdWellAsync(int idWell, CancellationToken token) { var query = db.Set() .Where(e => e.IdWell == idWell); db.Set().RemoveRange(query); return await db.SaveChangesAsync(token) .ConfigureAwait(false); } public async Task> GetAsync(int idWell, CancellationToken token) { var well = wellService.GetOrDefault(idWell) ?? throw new ArgumentInvalidException(nameof(idWell), "idWell doesn`t exist"); var offsetHours = well.Timezone.Hours; var query = db.Set() .AsNoTracking() .Where(x => x.IdWell == well.Id); var entities = await query .OrderBy(e => e.WellboreDepth) .ToArrayAsync(token); var result = entities .Select(r => Convert(r, offsetHours)); return result; } public async Task UpdateAsync(Tdto row, CancellationToken token) { var entity = Convert(row); db.Set().Update(entity); return await db.SaveChangesAsync(token) .ConfigureAwait(false); } private static Tdto Convert(TEntity entity, double offsetHours) { var dto = entity.Adapt(); dto.UpdateDate = entity.UpdateDate.ToOffset(TimeSpan.FromHours(offsetHours)); return dto; } private static TEntity Convert(Tdto dto) { var entity = dto.Adapt(); entity.UpdateDate = DateTimeOffset.UtcNow; return entity; } }