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 offsetHours = wellService.GetTimezone(idWell).Hours; var entities = trajectoryRows .Select(e => { var entity = Convert(e, offsetHours); entity.Id = 0; return entity; }); db.Set().AddRange(entities); return await db.SaveChangesAsync(token); } public async Task AddAsync(Tdto trajectoryRow, CancellationToken token) { var offsetHours = wellService.GetTimezone(trajectoryRow.IdWell).Hours; var entity = Convert(trajectoryRow, offsetHours); 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 offsetHours = wellService.GetTimezone(row.IdWell).Hours; var entity = Convert(row, offsetHours); 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.ToRemoteDateTime(offsetHours); return dto; } private static TEntity Convert(Tdto dto, double offsetHours) { var entity = dto.Adapt(); entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(offsetHours); return entity; } } }