DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/TrajectoryEditableRepository.cs

123 lines
4.5 KiB
C#
Raw Normal View History

2023-11-28 15:54:47 +05:00
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
{
2023-11-22 10:31:41 +05:00
/// <summary>
/// CRUD-репозиторий для работы с траекториями (плановыми и фактическими)
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="Tdto"></typeparam>
public class TrajectoryEditableRepository<TEntity, Tdto> : ITrajectoryEditableRepository<Tdto>
where TEntity : Trajectory
where Tdto : TrajectoryGeoDto
{
private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
2023-11-22 10:31:41 +05:00
public TrajectoryEditableRepository(IAsbCloudDbContext db, IWellService wellService)
{
this.db = db;
this.wellService = wellService;
}
2023-11-30 09:40:51 +05:00
public async Task<int> AddRangeAsync(IEnumerable<Tdto> trajectoryRows, CancellationToken token)
{
var idWell = trajectoryRows.First().IdWell;
if (!trajectoryRows.All(r => r.IdWell == idWell))
throw new ArgumentInvalidException(nameof(trajectoryRows), "Все строки должны относиться к одной скважине");
2023-09-29 12:06:46 +05:00
2023-01-11 14:59:54 +05:00
var offsetHours = wellService.GetTimezone(idWell).Hours;
var entities = trajectoryRows
.Select(e =>
{
2023-01-12 12:07:31 +05:00
var entity = Convert(e, offsetHours);
entity.Id = 0;
return entity;
});
db.Set<TEntity>().AddRange(entities);
2023-11-30 09:40:51 +05:00
return await db.SaveChangesAsync(token);
}
public async Task<int> AddAsync(Tdto trajectoryRow, CancellationToken token)
2023-01-11 14:59:54 +05:00
{
var offsetHours = wellService.GetTimezone(trajectoryRow.IdWell).Hours;
var entity = Convert(trajectoryRow, offsetHours);
2023-01-11 14:59:54 +05:00
entity.Id = 0;
db.Set<TEntity>().Add(entity);
2022-12-27 21:45:03 +05:00
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
2022-12-25 23:16:36 +05:00
public async Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token)
{
var query = db.Set<TEntity>()
2022-12-27 14:03:44 +05:00
.Where(e => ids.Contains(e.Id));
db.Set<TEntity>().RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
public async Task<int> DeleteByIdWellAsync(int idWell, CancellationToken token)
2022-12-27 00:02:49 +05:00
{
var query = db.Set<TEntity>()
2022-12-27 00:02:49 +05:00
.Where(e => e.IdWell == idWell);
db.Set<TEntity>().RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
2022-12-27 00:02:49 +05:00
}
2023-11-30 09:40:51 +05:00
public async Task<IEnumerable<Tdto>> GetAsync(int idWell, CancellationToken token)
{
2023-11-30 09:40:51 +05:00
var well = wellService.GetOrDefault(idWell)
?? throw new ArgumentInvalidException(nameof(idWell), "idWell doesn`t exist");
2023-09-29 12:06:46 +05:00
var offsetHours = well.Timezone.Hours;
var query = db.Set<TEntity>()
2022-12-27 14:03:44 +05:00
.AsNoTracking()
.Where(x => x.IdWell == well.Id);
var entities = await query
2023-01-12 12:07:31 +05:00
.OrderBy(e => e.WellboreDepth)
.ToArrayAsync(token);
2023-11-30 09:40:51 +05:00
var result = entities
2023-01-11 14:59:54 +05:00
.Select(r => Convert(r, offsetHours));
return result;
}
public async Task<int> UpdateAsync(Tdto row, CancellationToken token)
2023-01-12 10:32:54 +05:00
{
var offsetHours = wellService.GetTimezone(row.IdWell).Hours;
var entity = Convert(row, offsetHours);
db.Set<TEntity>().Update(entity);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
2023-11-30 09:40:51 +05:00
private static Tdto Convert(TEntity entity, double offsetHours)
{
var dto = entity.Adapt<Tdto>();
2023-01-11 14:59:54 +05:00
dto.UpdateDate = entity.UpdateDate.ToRemoteDateTime(offsetHours);
return dto;
2022-12-27 21:45:03 +05:00
}
2023-11-30 09:40:51 +05:00
private static TEntity Convert(Tdto dto, double offsetHours)
{
var entity = dto.Adapt<TEntity>();
2023-01-11 14:59:54 +05:00
entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(offsetHours);
2022-12-27 21:45:03 +05:00
return entity;
}
}
}