using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data.ProcessMap; using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; using AsbCloudApp.Services; namespace AsbCloudInfrastructure.Services.ProcessMap.ProcessMapWellboreDevelopment; public class ProcessMapWellboreDevelopmentService : IProcessMapWellboreDevelopmentService { private readonly ITelemetryService telemetryService; private readonly IWellService wellService; private readonly IProcessMapWellboreDevelopmentRepository processMapWellboreDevelopmentRepository; public ProcessMapWellboreDevelopmentService(ITelemetryService telemetryService, IWellService wellService, IProcessMapWellboreDevelopmentRepository processMapWellboreDevelopmentRepository) { this.telemetryService = telemetryService; this.wellService = wellService; this.processMapWellboreDevelopmentRepository = processMapWellboreDevelopmentRepository; } public async Task InsertAsync(ProcessMapWellboreDevelopmentDto processMapWellboreDevelopment, CancellationToken cancellationToken) { var well = await wellService.GetOrDefaultAsync(processMapWellboreDevelopment.IdWell, cancellationToken); if (well is null) throw new ArgumentInvalidException($"Скважины с Id: {processMapWellboreDevelopment.IdWell} не существует", nameof(processMapWellboreDevelopment.IdWell)); if (processMapWellboreDevelopment.DepthStart > processMapWellboreDevelopment.DepthEnd) throw new ArgumentInvalidException("Значение стартовой глубины не может превышать конечную глубину", nameof(processMapWellboreDevelopment.DepthStart)); processMapWellboreDevelopment.LastUpdate = DateTimeOffset.UtcNow; return await processMapWellboreDevelopmentRepository.InsertAsync(processMapWellboreDevelopment, cancellationToken); } public async Task UpdateAsync(ProcessMapWellboreDevelopmentDto processMapWellboreDevelopment, CancellationToken cancellationToken) { var well = await wellService.GetOrDefaultAsync(processMapWellboreDevelopment.IdWell, cancellationToken); if (well is null) throw new ArgumentInvalidException($"Скважины с Id: {processMapWellboreDevelopment.IdWell} не существует", nameof(processMapWellboreDevelopment.IdWell)); if (processMapWellboreDevelopment.DepthStart > processMapWellboreDevelopment.DepthEnd) throw new ArgumentInvalidException("Значение стартовой глубины не может превышать конечную глубину", nameof(processMapWellboreDevelopment.DepthStart)); processMapWellboreDevelopment.LastUpdate = DateTimeOffset.UtcNow; var result = await processMapWellboreDevelopmentRepository.UpdateAsync(processMapWellboreDevelopment, cancellationToken); if (result == ICrudRepository.ErrorIdNotFound) throw new ArgumentInvalidException($"Проработки с Id: {processMapWellboreDevelopment.Id} не существует", nameof(processMapWellboreDevelopment.Id)); return result; } public async Task> GetByTelemetryAsync(string uid, DateTime updateFrom, CancellationToken cancellationToken) { var idWell = telemetryService.GetIdWellByTelemetryUid(uid); if (!idWell.HasValue) throw new ArgumentInvalidException($"Неправильный телеметрии. Uid: {uid}", nameof(uid)); return await processMapWellboreDevelopmentRepository.GetAllAsync(idWell.Value, updateFrom, cancellationToken); } }