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

85 lines
3.3 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> AddAsync(int idWell, int idUser,IEnumerable<PlannedTrajectoryDto> plannedTrajectoryRows, CancellationToken token)
{
var timezone = wellService.GetTimezone(idWell);
foreach (var dto in plannedTrajectoryRows)
{
var entity = dto.Adapt<AsbCloudDb.Model.PlannedTrajectory>();
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);
}
2022-12-25 23:16:36 +05:00
public async Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token)
{
var query = db.PlannedTrajectorys.Where(e => ids.Contains(e.Id));
db.PlannedTrajectorys.RemoveRange(query);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
2022-12-25 23:16:36 +05:00
public async Task<IEnumerable<PlannedTrajectoryDto>?> 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
2022-12-25 23:16:36 +05:00
.Where(x => x.IdWell == idWell);
var entities = await query
.OrderBy(e => e.LoadDate)
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 idWell, int idUser, int idRow, PlannedTrajectoryDto row, CancellationToken token)
{
var timezone = wellService.GetTimezone(idWell);
var entity = row.Adapt<AsbCloudDb.Model.PlannedTrajectory>();
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<PlannedTrajectoryDto>();
return dto;
}
}
#nullable disable
}