2022-12-22 18:08:58 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Exceptions;
|
2023-02-13 09:10:48 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2022-12-22 18:08:58 +05:00
|
|
|
|
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;
|
|
|
|
|
|
2023-02-13 09:10:48 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2023-05-30 11:21:07 +05:00
|
|
|
|
public class TrajectoryPlanRepository : ITrajectoryPlanRepository
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly IWellService wellService;
|
2023-05-30 11:21:07 +05:00
|
|
|
|
public TrajectoryPlanRepository(IAsbCloudDbContext db, IWellService wellService)
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
}
|
2023-01-11 14:59:54 +05:00
|
|
|
|
/// <inheritdoc/>
|
2023-05-30 11:21:07 +05:00
|
|
|
|
public async Task<int> AddRangeAsync(IEnumerable<TrajectoryGeoPlanDto> plannedTrajectoryRows, CancellationToken token)
|
2023-02-13 09:10:48 +05:00
|
|
|
|
{
|
2023-01-11 14:59:54 +05:00
|
|
|
|
var idWell = plannedTrajectoryRows.First().IdWell;
|
2023-01-12 10:32:54 +05:00
|
|
|
|
if (!plannedTrajectoryRows.All(r => r.IdWell == idWell))
|
2023-09-29 12:06:46 +05:00
|
|
|
|
throw new ArgumentInvalidException(nameof(plannedTrajectoryRows), "Все строки должны относиться к одной скважине");
|
|
|
|
|
|
2023-01-11 14:59:54 +05:00
|
|
|
|
var offsetHours = wellService.GetTimezone(idWell).Hours;
|
2023-01-12 12:07:31 +05:00
|
|
|
|
var entities = plannedTrajectoryRows
|
2023-02-13 09:10:48 +05:00
|
|
|
|
.Select(e =>
|
|
|
|
|
{
|
2023-01-12 12:07:31 +05:00
|
|
|
|
var entity = Convert(e, offsetHours);
|
|
|
|
|
entity.Id = 0;
|
2023-02-13 09:10:48 +05:00
|
|
|
|
return entity;
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-12 12:07:31 +05:00
|
|
|
|
db.PlannedTrajectories.AddRange(entities);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return await db.SaveChangesAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:59:54 +05:00
|
|
|
|
/// <inheritdoc/>
|
2023-05-30 11:21:07 +05:00
|
|
|
|
public async Task<int> AddAsync(TrajectoryGeoPlanDto plannedTrajectoryRow, CancellationToken token)
|
2023-01-11 14:59:54 +05:00
|
|
|
|
{
|
|
|
|
|
var offsetHours = wellService.GetTimezone(plannedTrajectoryRow.IdWell).Hours;
|
|
|
|
|
var entity = Convert(plannedTrajectoryRow, offsetHours);
|
|
|
|
|
entity.Id = 0;
|
2023-02-13 09:10:48 +05:00
|
|
|
|
db.PlannedTrajectories.Add(entity);
|
2022-12-27 21:45:03 +05:00
|
|
|
|
return await db.SaveChangesAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:59:54 +05:00
|
|
|
|
/// <inheritdoc/>
|
2022-12-25 23:16:36 +05:00
|
|
|
|
public async Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token)
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
2023-01-11 14:59:54 +05:00
|
|
|
|
var query = db.PlannedTrajectories
|
2022-12-27 14:03:44 +05:00
|
|
|
|
.Where(e => ids.Contains(e.Id));
|
2023-01-11 14:59:54 +05:00
|
|
|
|
db.PlannedTrajectories.RemoveRange(query);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return await db.SaveChangesAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:59:54 +05:00
|
|
|
|
/// <inheritdoc/>
|
2022-12-29 04:25:08 +05:00
|
|
|
|
public async Task<int> DeleteByIdWellAsync(int idWell, CancellationToken token)
|
2022-12-27 00:02:49 +05:00
|
|
|
|
{
|
2023-01-11 14:59:54 +05:00
|
|
|
|
var query = db.PlannedTrajectories
|
2022-12-27 00:02:49 +05:00
|
|
|
|
.Where(e => e.IdWell == idWell);
|
2023-01-11 14:59:54 +05:00
|
|
|
|
db.PlannedTrajectories.RemoveRange(query);
|
2022-12-29 04:25:08 +05:00
|
|
|
|
return await db.SaveChangesAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2022-12-27 00:02:49 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:59:54 +05:00
|
|
|
|
/// <inheritdoc/>
|
2023-05-30 11:21:07 +05:00
|
|
|
|
public async Task<IEnumerable<TrajectoryGeoPlanDto>> GetAsync(int idWell, CancellationToken token)
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
2023-09-29 12:06:46 +05:00
|
|
|
|
var well = wellService.GetOrDefault(idWell)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(idWell), "idWell doesn`t exist");
|
|
|
|
|
|
2023-02-13 09:10:48 +05:00
|
|
|
|
var offsetHours = well.Timezone.Hours;
|
2023-01-11 14:59:54 +05:00
|
|
|
|
var query = db.PlannedTrajectories
|
2022-12-27 14:03:44 +05:00
|
|
|
|
.AsNoTracking()
|
2023-02-13 09:10:48 +05:00
|
|
|
|
.Where(x => x.IdWell == idWell);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
var entities = await query
|
2023-01-12 12:07:31 +05:00
|
|
|
|
.OrderBy(e => e.WellboreDepth)
|
2023-05-17 11:42:14 +05:00
|
|
|
|
.ToArrayAsync(token);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
var result = entities
|
2023-01-11 14:59:54 +05:00
|
|
|
|
.Select(r => Convert(r, offsetHours));
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:59:54 +05:00
|
|
|
|
/// <inheritdoc/>
|
2023-05-30 11:21:07 +05:00
|
|
|
|
public async Task<int> UpdateAsync(TrajectoryGeoPlanDto row, CancellationToken token)
|
2023-01-12 10:32:54 +05:00
|
|
|
|
{
|
|
|
|
|
var offsetHours = wellService.GetTimezone(row.IdWell).Hours;
|
|
|
|
|
var entity = Convert(row, offsetHours);
|
2023-01-11 14:59:54 +05:00
|
|
|
|
db.PlannedTrajectories.Update(entity);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return await db.SaveChangesAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 11:21:07 +05:00
|
|
|
|
private TrajectoryGeoPlanDto Convert(PlannedTrajectory entity, double offsetHours)
|
2023-02-13 09:10:48 +05:00
|
|
|
|
{
|
2023-05-30 11:21:07 +05:00
|
|
|
|
var dto = entity.Adapt<TrajectoryGeoPlanDto>();
|
2023-01-11 14:59:54 +05:00
|
|
|
|
dto.UpdateDate = entity.UpdateDate.ToRemoteDateTime(offsetHours);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return dto;
|
2022-12-27 21:45:03 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 11:21:07 +05:00
|
|
|
|
private PlannedTrajectory Convert(TrajectoryGeoPlanDto dto, double offsetHours)
|
2023-02-13 09:10:48 +05:00
|
|
|
|
{
|
|
|
|
|
var entity = dto.Adapt<PlannedTrajectory>();
|
2023-01-11 14:59:54 +05:00
|
|
|
|
entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(offsetHours);
|
2022-12-27 21:45:03 +05:00
|
|
|
|
return entity;
|
2023-02-13 09:10:48 +05:00
|
|
|
|
}
|
2022-12-22 18:08:58 +05:00
|
|
|
|
}
|
|
|
|
|
}
|