DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/PlannedTrajectoryService/PlannedTrajectoryService.cs
2022-12-27 21:45:03 +05:00

115 lines
4.4 KiB
C#

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(int idWell, int idUser,IEnumerable<PlannedTrajectoryDto> plannedTrajectoryRows, CancellationToken token)
{
var timezone = wellService.GetTimezone(idWell);
var entitys = plannedTrajectoryRows
.Select(e => Convert(e, timezone, idWell, idUser));
db.PlannedTrajectorys.AddRange(entitys);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
public async Task<int> AddAsync(int idWell, int idUser, PlannedTrajectoryDto plannedTrajectoryRow, CancellationToken token)
{
var timezone = wellService.GetTimezone(idWell);
var entity = Convert(plannedTrajectoryRow, timezone, idWell, idUser);
db.PlannedTrajectorys.Add(entity);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
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);
}
public async Task DeleteAllByIdWellAsync(int idWell, CancellationToken token)
{
var query = db.PlannedTrajectorys
.AsNoTracking()
.Where(e => e.IdWell == idWell);
var ids = await query
.Select(r => r.IdWell)
.ToListAsync(token);
var result = await DeleteRangeAsync(ids, token);
}
public async Task<IEnumerable<PlannedTrajectoryDto>> GetOrDefaultAsync(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
.AsNoTracking()
.Where(x => x.IdWell == idWell);
var entities = await query
.OrderBy(e => e.UpdateDate)
.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.UpdateDate = 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;
}
private static AsbCloudDb.Model.PlannedTrajectory Convert(PlannedTrajectoryDto dto, SimpleTimezoneDto timezone, int idWell, int idUser)
{
var entity = dto.Adapt<AsbCloudDb.Model.PlannedTrajectory>();
entity.IdWell = idWell;
entity.Id = default;
entity.UpdateDate = DateTime.Today.ToUtcDateTimeOffset(timezone.Hours);
entity.IdUser = idUser;
return entity;
}
}
#nullable disable
}