using AsbCloudApp.Data; using AsbCloudApp.Exceptions; 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.Services.PlannedTrajectory { #nullable enable public class PlannedTrajectoryService : IPlannedTrajectoryService { private readonly IAsbCloudDbContext db; private readonly IWellService wellService; public PlannedTrajectoryService(IAsbCloudDbContext db, IWellService wellService) { this.db = db; this.wellService = wellService; } public async Task AddAsync(int idWell, int idUser,IEnumerable plannedTrajectoryRows, CancellationToken token) { var timezone = wellService.GetTimezone(idWell); foreach (var dto in plannedTrajectoryRows) { var entity = dto.Adapt(); entity.IdWell = idWell; entity.Id = default; entity.LoadDate = DateTime.Today.ToUtcDateTimeOffset(timezone.Hours); entity.IdUser = idUser; db.PlannedTrajectorys.Add(entity); } return await db.SaveChangesAsync(token) .ConfigureAwait(false); } public async Task DeleteRangeAsync(IEnumerable ids, CancellationToken token) { var query = db.PlannedTrajectorys.Where(e => ids.Contains(e.Id)); db.PlannedTrajectorys.RemoveRange(query); return await db.SaveChangesAsync(token) .ConfigureAwait(false); } public async Task?> GetCoordinatesAsync(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 query = db.PlannedTrajectorys .Where(x => x.IdWell == idWell); var entities = await query .OrderBy(e => e.LoadDate) .ToListAsync(token); var result = entities .Select(r => Convert(r)); return result; } public async Task UpdateAsync(int idWell, int idUser, int idRow, PlannedTrajectoryDto row, CancellationToken token) { var timezone = wellService.GetTimezone(idWell); var entity = row.Adapt(); entity.IdWell = idWell; entity.Id = idUser; entity.LoadDate = DateTime.Today.ToUtcDateTimeOffset(timezone.Hours); entity.IdUser = idUser; db.PlannedTrajectorys.Update(entity); return await db.SaveChangesAsync(token) .ConfigureAwait(false); } private static PlannedTrajectoryDto Convert(AsbCloudDb.Model.PlannedTrajectory entity) { var dto = entity.Adapt(); return dto; } } #nullable disable }