DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/PlannedTrajectoryService/PlannedTrajectoryService.cs

118 lines
4.5 KiB
C#
Raw Normal View History

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<int> AddRangeAsync(IEnumerable<PlannedTrajectoryDto> plannedTrajectoryRows, CancellationToken token)
{
2022-12-27 21:45:03 +05:00
var entitys = plannedTrajectoryRows
.Select(e => Convert(e));
2022-12-27 21:45:03 +05:00
db.PlannedTrajectorys.AddRange(entitys);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
public async Task<int> AddAsync(PlannedTrajectoryDto plannedTrajectoryRow, CancellationToken token)
{
var entity = Convert(plannedTrajectoryRow);
2022-12-27 21:45:03 +05:00
db.PlannedTrajectorys.Add(entity);
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)
{
2022-12-27 14:03:44 +05:00
var query = db.PlannedTrajectorys
.Where(e => ids.Contains(e.Id));
db.PlannedTrajectorys.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.PlannedTrajectorys
.Where(e => e.IdWell == idWell);
db.PlannedTrajectorys.RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
2022-12-27 00:02:49 +05:00
}
public async Task<IEnumerable<PlannedTrajectoryDto>> GetAsync(int idWell, CancellationToken token)
{
var well = wellService.GetOrDefault(idWell);
var timezone = wellService.GetTimezone(idWell);
if (well is null || well.Timezone is null)
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
var query = db.PlannedTrajectorys
2022-12-27 14:03:44 +05:00
.AsNoTracking()
.Where(x => x.IdWell == idWell);
var entities = await query
2022-12-26 19:22:29 +05:00
.OrderBy(e => e.UpdateDate)
2022-12-25 23:16:36 +05:00
.ToListAsync(token);
var result = entities
.Select(r => Convert(r));
return result;
}
public async Task<int> UpdateAsync(int idRow, PlannedTrajectoryDto row, CancellationToken token)
{
var entity = Convert(row, idRow);
db.PlannedTrajectorys.Update(entity);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
private PlannedTrajectoryDto Convert(AsbCloudDb.Model.PlannedTrajectory entity)
{
var timezone = wellService.GetTimezone(entity.IdWell);
var dto = entity.Adapt<PlannedTrajectoryDto>();
dto.UpdateDate = entity.UpdateDate.ToRemoteDateTime(timezone.Hours);
return dto;
2022-12-27 21:45:03 +05:00
}
private AsbCloudDb.Model.PlannedTrajectory Convert(PlannedTrajectoryDto dto)
{
var timezone = wellService.GetTimezone(dto.IdWell);
2022-12-27 21:45:03 +05:00
var entity = dto.Adapt<AsbCloudDb.Model.PlannedTrajectory>();
entity.IdWell = dto.IdWell;
entity.Id = 0;
entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(timezone.Hours);
entity.IdUser = dto.IdUser;
return entity;
}
private AsbCloudDb.Model.PlannedTrajectory Convert(PlannedTrajectoryDto dto, int idRow)
{
var timezone = wellService.GetTimezone(dto.IdWell);
var entity = dto.Adapt<AsbCloudDb.Model.PlannedTrajectory>();
entity.IdWell = dto.IdWell;
entity.Id = idRow;
entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(timezone.Hours);
entity.IdUser = dto.IdUser;
2022-12-27 21:45:03 +05:00
return entity;
}
}
#nullable disable
}