2022-12-22 18:08:58 +05:00
|
|
|
|
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;
|
|
|
|
|
}
|
2023-01-11 14:59:54 +05:00
|
|
|
|
/// <inheritdoc/>
|
2022-12-29 04:25:08 +05:00
|
|
|
|
public async Task<int> AddRangeAsync(IEnumerable<PlannedTrajectoryDto> plannedTrajectoryRows, CancellationToken token)
|
2023-01-12 10:32:54 +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))
|
|
|
|
|
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
|
|
|
|
|
.Select(e => {
|
|
|
|
|
var entity = Convert(e, offsetHours);
|
|
|
|
|
entity.Id = 0;
|
|
|
|
|
return entity;});
|
|
|
|
|
|
|
|
|
|
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/>
|
2022-12-29 04:25:08 +05:00
|
|
|
|
public async Task<int> AddAsync(PlannedTrajectoryDto 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;
|
|
|
|
|
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/>
|
2022-12-29 04:25:08 +05:00
|
|
|
|
public async Task<IEnumerable<PlannedTrajectoryDto>> GetAsync(int idWell, CancellationToken token)
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
|
|
|
|
var well = wellService.GetOrDefault(idWell);
|
|
|
|
|
if (well is null || well.Timezone is null)
|
|
|
|
|
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
|
2023-01-12 10:32:54 +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()
|
|
|
|
|
.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)
|
2022-12-25 23:16:36 +05:00
|
|
|
|
.ToListAsync(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/>
|
|
|
|
|
public async Task<int> UpdateAsync(PlannedTrajectoryDto 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-01-11 14:59:54 +05:00
|
|
|
|
private PlannedTrajectoryDto Convert(AsbCloudDb.Model.PlannedTrajectory entity, double offsetHours)
|
|
|
|
|
{
|
2022-12-29 04:25:08 +05:00
|
|
|
|
var dto = entity.Adapt<PlannedTrajectoryDto>();
|
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-01-11 14:59:54 +05:00
|
|
|
|
private AsbCloudDb.Model.PlannedTrajectory Convert(PlannedTrajectoryDto dto, double offsetHours)
|
|
|
|
|
{
|
2022-12-29 04:25:08 +05:00
|
|
|
|
var entity = dto.Adapt<AsbCloudDb.Model.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-01-11 14:59:54 +05:00
|
|
|
|
}
|
2022-12-22 18:08:58 +05:00
|
|
|
|
}
|
|
|
|
|
#nullable disable
|
|
|
|
|
}
|