2021-04-02 17:28:07 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-01-18 11:04:15 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
2022-12-28 17:38:53 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2023-02-15 17:57:32 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2022-06-16 12:33:05 +05:00
|
|
|
|
using AsbCloudInfrastructure.Repository;
|
2021-07-28 09:46:58 +05:00
|
|
|
|
using Mapster;
|
2022-06-06 15:43:47 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-11-17 16:09:51 +05:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using System;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-08-11 12:11:21 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
2022-12-01 15:56:11 +05:00
|
|
|
|
public class WellService : CrudCacheRepositoryBase<WellDto, Well>, IWellService
|
2021-12-22 11:41:18 +05:00
|
|
|
|
{
|
2021-10-15 12:24:04 +05:00
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2022-12-01 15:56:11 +05:00
|
|
|
|
private readonly ICrudRepository<CompanyTypeDto> companyTypesService;
|
2022-01-05 17:50:45 +05:00
|
|
|
|
private readonly ITimezoneService timezoneService;
|
2023-06-29 15:54:54 +05:00
|
|
|
|
private readonly WellInfoService wellInfoService;
|
2022-12-28 17:38:53 +05:00
|
|
|
|
private readonly IWellOperationRepository wellOperationRepository;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2021-12-30 17:05:44 +05:00
|
|
|
|
public ITelemetryService TelemetryService => telemetryService;
|
|
|
|
|
|
2022-06-15 14:57:37 +05:00
|
|
|
|
private static IQueryable<Well> MakeQueryWell(DbSet<Well> dbSet)
|
2022-06-06 15:43:47 +05:00
|
|
|
|
=> dbSet
|
|
|
|
|
.Include(w => w.Cluster)
|
|
|
|
|
.ThenInclude(c => c.Deposit)
|
|
|
|
|
.Include(w => w.Telemetry)
|
|
|
|
|
.Include(w => w.WellType)
|
|
|
|
|
.Include(w => w.RelationCompaniesWells)
|
2022-11-17 16:09:51 +05:00
|
|
|
|
.ThenInclude(r => r.Company);
|
2022-06-06 15:43:47 +05:00
|
|
|
|
|
2023-06-29 15:54:54 +05:00
|
|
|
|
public WellService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService, ITimezoneService timezoneService, WellInfoService wellInfoService)
|
2022-11-17 16:09:51 +05:00
|
|
|
|
: base(db, memoryCache, MakeQueryWell)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-10-15 12:24:04 +05:00
|
|
|
|
this.telemetryService = telemetryService;
|
2022-01-05 17:50:45 +05:00
|
|
|
|
this.timezoneService = timezoneService;
|
2023-06-29 15:54:54 +05:00
|
|
|
|
this.wellInfoService = wellInfoService;
|
2022-12-28 17:38:53 +05:00
|
|
|
|
this.wellOperationRepository = new WellOperationRepository(db, memoryCache, this);
|
2022-12-01 15:56:11 +05:00
|
|
|
|
companyTypesService = new CrudCacheRepositoryBase<CompanyTypeDto, CompanyType>(dbContext, memoryCache);
|
2021-05-12 16:03:14 +05:00
|
|
|
|
}
|
2021-10-20 12:52:31 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
private Task<IEnumerable<RelationCompanyWell>> GetCacheRelationCompanyWellAsync(CancellationToken token)
|
2023-02-21 18:01:03 +05:00
|
|
|
|
{
|
2023-02-22 09:40:02 +05:00
|
|
|
|
return memoryCache.GetOrCreateBasicAsync(
|
|
|
|
|
dbContext.Set<RelationCompanyWell>()
|
2023-02-21 18:01:03 +05:00
|
|
|
|
.Include(r => r.Company)
|
|
|
|
|
.Include(r => r.Well)
|
2023-02-22 09:40:02 +05:00
|
|
|
|
, token);
|
2023-02-21 18:01:03 +05:00
|
|
|
|
}
|
2022-06-06 15:43:47 +05:00
|
|
|
|
|
|
|
|
|
private void DropCacheRelationCompanyWell()
|
2023-02-21 18:01:03 +05:00
|
|
|
|
=> memoryCache.DropBasic<RelationCompanyWell>();
|
2022-06-06 15:43:47 +05:00
|
|
|
|
|
2023-05-19 17:57:07 +05:00
|
|
|
|
public DateTime GetLastTelemetryDate(int idWell)
|
2021-10-20 12:52:31 +05:00
|
|
|
|
{
|
2022-06-16 12:33:05 +05:00
|
|
|
|
var well = GetOrDefault(idWell);
|
2022-06-15 14:57:37 +05:00
|
|
|
|
|
2021-10-21 15:57:20 +05:00
|
|
|
|
if (well?.IdTelemetry is null)
|
2023-05-19 17:57:07 +05:00
|
|
|
|
return DateTime.MinValue;
|
2021-10-20 12:52:31 +05:00
|
|
|
|
|
2023-05-19 17:57:07 +05:00
|
|
|
|
var datesRange = telemetryService.GetDatesRange(well.IdTelemetry.Value);
|
|
|
|
|
return datesRange.To;
|
2021-10-20 12:52:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 16:27:14 +05:00
|
|
|
|
/// <inheritdoc/>
|
2023-02-15 17:57:32 +05:00
|
|
|
|
public async Task<IEnumerable<DepositBranchDto>> GetWellTreeAsync(int idCompany, CancellationToken token)
|
2021-10-21 15:57:20 +05:00
|
|
|
|
{
|
2023-02-15 17:57:32 +05:00
|
|
|
|
var wells = await GetEntitiesAsync(new() { IdCompany = idCompany }, token);
|
2022-06-06 15:43:47 +05:00
|
|
|
|
|
2023-02-15 17:57:32 +05:00
|
|
|
|
var groupedWells = wells
|
|
|
|
|
.GroupBy(w => w.Cluster)
|
|
|
|
|
.GroupBy(g => g.Key.Deposit);
|
2021-12-22 11:41:18 +05:00
|
|
|
|
|
2023-02-15 17:57:32 +05:00
|
|
|
|
var depositTree = groupedWells.Select(
|
|
|
|
|
gDeposit => new DepositBranchDto
|
|
|
|
|
{
|
|
|
|
|
Id = gDeposit.Key.Id,
|
|
|
|
|
Caption = gDeposit.Key.Caption,
|
|
|
|
|
Latitude = gDeposit.Key.Latitude,
|
|
|
|
|
Longitude = gDeposit.Key.Longitude,
|
|
|
|
|
Clusters = gDeposit.Select(gCluster => new ClusterBranchDto
|
|
|
|
|
{
|
|
|
|
|
Id = gCluster.Key.Id,
|
|
|
|
|
Caption = gCluster.Key.Caption,
|
|
|
|
|
Latitude = gCluster.Key.Latitude ?? gDeposit.Key.Latitude,
|
|
|
|
|
Longitude = gCluster.Key.Longitude ?? gDeposit.Key.Longitude,
|
|
|
|
|
Wells = gCluster.Select(well =>
|
|
|
|
|
{
|
2023-06-29 15:54:54 +05:00
|
|
|
|
var dto = wellInfoService.FirstOrDefault(w => w.Id == well.Id);
|
|
|
|
|
dto ??= well.Adapt<WellMapInfoWithTelemetryStat>();
|
2023-02-16 16:27:14 +05:00
|
|
|
|
dto.Latitude ??= gCluster.Key.Latitude ?? gDeposit.Key.Latitude;
|
|
|
|
|
dto.Longitude ??= gCluster.Key.Longitude ?? gDeposit.Key.Longitude;
|
2023-02-15 17:57:32 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
return depositTree;
|
|
|
|
|
}
|
2022-06-15 14:57:37 +05:00
|
|
|
|
|
2023-02-15 17:57:32 +05:00
|
|
|
|
public async Task<IEnumerable<WellDto>> GetAsync(WellRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var wells = await GetEntitiesAsync(request, token);
|
|
|
|
|
var wellsDtos = wells.Select(Convert);
|
2022-12-05 09:36:02 +05:00
|
|
|
|
return wellsDtos;
|
2021-10-21 15:57:20 +05:00
|
|
|
|
}
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2023-02-15 17:57:32 +05:00
|
|
|
|
private async Task<IEnumerable<Well>> GetEntitiesAsync(WellRequest request, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var wells = await GetCacheAsync(token);
|
|
|
|
|
|
2023-02-16 16:27:14 +05:00
|
|
|
|
if (request.Ids?.Any() == true)
|
|
|
|
|
wells = wells.Where(well => request.Ids.Contains(well.Id));
|
|
|
|
|
|
2023-02-15 17:57:32 +05:00
|
|
|
|
if (request.IdCompany.HasValue)
|
|
|
|
|
wells = wells.Where(well => well.RelationCompaniesWells.Any(r => r.IdCompany == request.IdCompany.Value));
|
|
|
|
|
|
|
|
|
|
if (request.IdState.HasValue)
|
|
|
|
|
wells = wells.Where(well => well.IdState == request.IdState.Value);
|
|
|
|
|
|
|
|
|
|
return wells;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 09:36:02 +05:00
|
|
|
|
public override async Task<int> InsertAsync(WellDto dto, CancellationToken token = default)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-12-22 11:41:18 +05:00
|
|
|
|
if (dto.IdWellType is < 1 or > 2)
|
2022-01-18 11:04:15 +05:00
|
|
|
|
throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto));
|
2021-12-22 11:41:18 +05:00
|
|
|
|
|
|
|
|
|
if (dto.IdState is < 0 or > 2)
|
2022-01-18 11:04:15 +05:00
|
|
|
|
throw new ArgumentInvalidException("Текущее состояние работы скважины указано неправильно.", nameof(dto));
|
2021-12-22 11:41:18 +05:00
|
|
|
|
|
2022-11-16 17:07:47 +05:00
|
|
|
|
if (dto.Id != 0 && (await GetCacheAsync(token)).Any(w => w.Id == dto.Id))
|
2022-01-18 11:04:15 +05:00
|
|
|
|
throw new ArgumentInvalidException($"Нельзя повторно добавить скважину с id: {dto.Id}", nameof(dto));
|
2021-12-22 11:41:18 +05:00
|
|
|
|
|
|
|
|
|
var entity = Convert(dto);
|
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var result = await base.InsertAsync(dto, token);
|
2021-12-22 11:41:18 +05:00
|
|
|
|
|
|
|
|
|
if (dto.Companies.Any())
|
|
|
|
|
{
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var newRelations = dto.Companies.Select(c => new RelationCompanyWell { IdWell = result, IdCompany = c.Id });
|
|
|
|
|
dbContext.RelationCompaniesWells.AddRange(newRelations);
|
|
|
|
|
await dbContext.SaveChangesAsync(token);
|
|
|
|
|
DropCacheRelationCompanyWell();
|
2021-12-22 11:41:18 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
return result;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
2021-12-21 11:52:53 +05:00
|
|
|
|
|
2022-12-05 09:36:02 +05:00
|
|
|
|
public override Task<int> InsertRangeAsync(IEnumerable<WellDto> dtos, CancellationToken token)
|
2021-12-21 11:52:53 +05:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 09:36:02 +05:00
|
|
|
|
public override async Task<int> UpdateAsync(WellDto dto,
|
2021-10-18 16:30:14 +05:00
|
|
|
|
CancellationToken token = default)
|
2021-10-18 12:38:49 +05:00
|
|
|
|
{
|
2021-10-18 16:30:14 +05:00
|
|
|
|
if (dto.IdWellType is < 1 or > 2)
|
2022-01-18 11:04:15 +05:00
|
|
|
|
throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto));
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
if (dto.IdState is < 0 or > 2)
|
2022-01-18 11:04:15 +05:00
|
|
|
|
throw new ArgumentInvalidException("Текущее состояние работы скважины указано неправильно.", nameof(dto));
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var oldRelations = (await GetCacheRelationCompanyWellAsync(token))
|
2022-06-09 11:19:52 +05:00
|
|
|
|
.Where(r => r.IdWell == dto.Id);
|
2021-12-22 11:41:18 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
if (dto.Companies.Count() != oldRelations.Count() ||
|
2021-12-22 11:41:18 +05:00
|
|
|
|
dto.Companies.Any(c => !oldRelations.Any(oldC => oldC.IdCompany == c.Id)))
|
|
|
|
|
{
|
2022-06-06 15:43:47 +05:00
|
|
|
|
dbContext.RelationCompaniesWells
|
|
|
|
|
.RemoveRange(dbContext.RelationCompaniesWells
|
2022-06-09 11:19:52 +05:00
|
|
|
|
.Where(r => r.IdWell == dto.Id));
|
2022-06-06 15:43:47 +05:00
|
|
|
|
|
2022-06-09 11:19:52 +05:00
|
|
|
|
var newRelations = dto.Companies.Select(c => new RelationCompanyWell { IdWell = dto.Id, IdCompany = c.Id });
|
2022-06-06 15:43:47 +05:00
|
|
|
|
dbContext.RelationCompaniesWells.AddRange(newRelations);
|
2021-12-22 11:41:18 +05:00
|
|
|
|
}
|
2021-10-18 12:38:49 +05:00
|
|
|
|
|
2022-06-09 11:19:52 +05:00
|
|
|
|
var result = await base.UpdateAsync(dto, token);
|
2021-12-21 11:52:53 +05:00
|
|
|
|
return result;
|
2021-10-18 12:38:49 +05:00
|
|
|
|
}
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token)
|
2022-06-06 15:43:47 +05:00
|
|
|
|
=> (await GetCacheRelationCompanyWellAsync(token))
|
|
|
|
|
.Any(r => r.IdWell == idWell && r.IdCompany == idCompany);
|
2021-08-09 15:41:42 +05:00
|
|
|
|
|
2021-08-25 11:13:56 +05:00
|
|
|
|
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
2022-12-05 09:36:02 +05:00
|
|
|
|
var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false);
|
|
|
|
|
return entity!.Caption;
|
2021-08-25 11:13:56 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var relations = (await GetCacheRelationCompanyWellAsync(token))
|
|
|
|
|
.Where(r => r.IdWell == idWell);
|
2021-12-21 11:52:53 +05:00
|
|
|
|
var dtos = relations.Select(r => Convert(r.Company));
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
public string GetStateText(int state)
|
|
|
|
|
{
|
|
|
|
|
return state switch
|
|
|
|
|
{
|
|
|
|
|
1 => "В работе",
|
|
|
|
|
2 => "Завершена",
|
2021-12-30 17:05:44 +05:00
|
|
|
|
_ => "Неизвестно",
|
2021-10-20 12:52:31 +05:00
|
|
|
|
};
|
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-11-23 11:41:27 +05:00
|
|
|
|
public async Task<IEnumerable<int>> GetClusterWellsIdsAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
2022-06-16 12:33:05 +05:00
|
|
|
|
var well = await GetOrDefaultAsync(idWell, token);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
|
|
|
|
if (well is null)
|
2023-02-15 17:57:32 +05:00
|
|
|
|
return Enumerable.Empty<int>();
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var cache = await GetCacheAsync(token);
|
|
|
|
|
|
2022-11-16 17:07:47 +05:00
|
|
|
|
var clusterWellsIds = cache
|
2022-06-06 15:43:47 +05:00
|
|
|
|
.Where((w) => w.IdCluster == well.IdCluster)
|
|
|
|
|
.Select(w => w.Id);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
return clusterWellsIds;
|
2021-11-23 11:41:27 +05:00
|
|
|
|
}
|
2021-10-21 15:57:20 +05:00
|
|
|
|
|
2021-12-22 11:41:18 +05:00
|
|
|
|
protected override Well Convert(WellDto dto)
|
|
|
|
|
{
|
2022-06-06 17:00:53 +05:00
|
|
|
|
var entity = dto.Adapt<Well>();
|
2022-01-10 11:45:14 +05:00
|
|
|
|
|
|
|
|
|
entity.IdTelemetry = entity.IdTelemetry ?? dto.IdTelemetry ?? dto.Telemetry?.Id;
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
if (dto.Timezone is null)
|
2022-06-06 15:43:47 +05:00
|
|
|
|
entity.Timezone = GetTimezone(dto.Id)
|
|
|
|
|
.Adapt<SimpleTimezone>();
|
2022-01-10 11:45:14 +05:00
|
|
|
|
|
2021-12-22 11:41:18 +05:00
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 11:52:53 +05:00
|
|
|
|
protected override WellDto Convert(Well entity)
|
2021-10-21 15:57:20 +05:00
|
|
|
|
{
|
2021-12-21 11:52:53 +05:00
|
|
|
|
var dto = base.Convert(entity);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
|
|
|
|
if (entity.Timezone is null)
|
2022-06-06 15:43:47 +05:00
|
|
|
|
dto.Timezone = GetTimezone(entity.Id);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
2022-12-28 17:38:53 +05:00
|
|
|
|
dto.StartDate = wellOperationRepository.FirstOperationDate(entity.Id)?.ToRemoteDateTime(dto.Timezone.Hours);
|
2023-02-17 15:30:17 +05:00
|
|
|
|
dto.WellType = entity.WellType.Caption;
|
|
|
|
|
dto.Cluster = entity.Cluster.Caption;
|
|
|
|
|
dto.Deposit = entity.Cluster.Deposit.Caption;
|
2022-06-15 14:57:37 +05:00
|
|
|
|
if (entity.IdTelemetry is not null)
|
2023-05-19 17:57:07 +05:00
|
|
|
|
dto.LastTelemetryDate = telemetryService.GetDatesRange(entity.IdTelemetry.Value).To;
|
2022-06-06 15:43:47 +05:00
|
|
|
|
dto.Companies = entity.RelationCompaniesWells
|
|
|
|
|
.Select(r => Convert(r.Company))
|
|
|
|
|
.ToList();
|
2021-12-21 11:52:53 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CompanyDto Convert(Company entity)
|
|
|
|
|
{
|
|
|
|
|
var dto = entity.Adapt<CompanyDto>();
|
2022-04-11 18:00:34 +05:00
|
|
|
|
dto.CompanyTypeCaption = entity.CompanyType?.Caption
|
2023-02-15 17:57:32 +05:00
|
|
|
|
?? companyTypesService.GetOrDefault(entity.IdCompanyType)?.Caption
|
|
|
|
|
?? string.Empty;
|
2021-12-21 11:52:53 +05:00
|
|
|
|
return dto;
|
2021-10-21 15:57:20 +05:00
|
|
|
|
}
|
2021-12-30 17:05:44 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
public async Task EnshureTimezonesIsSetAsync(CancellationToken token)
|
2021-12-30 17:05:44 +05:00
|
|
|
|
{
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var cache = await GetCacheAsync(token);
|
2022-11-16 17:07:47 +05:00
|
|
|
|
if (!cache.Any(w => w.Timezone is null))
|
2022-06-06 15:43:47 +05:00
|
|
|
|
return;
|
2022-01-10 17:39:33 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var defaultTimeZone = new SimpleTimezone
|
2022-01-05 17:50:45 +05:00
|
|
|
|
{
|
2022-06-06 15:43:47 +05:00
|
|
|
|
Hours = 5,
|
|
|
|
|
IsOverride = false,
|
2022-06-06 17:00:53 +05:00
|
|
|
|
TimezoneId = "Assumed",
|
2022-06-06 15:43:47 +05:00
|
|
|
|
};
|
2021-12-30 17:05:44 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
await dbSet.Where(w => w.Timezone == null)
|
|
|
|
|
.ForEachAsync(w => w.Timezone = defaultTimeZone, token);
|
|
|
|
|
|
|
|
|
|
await dbContext.SaveChangesAsync(token);
|
|
|
|
|
DropCache();
|
2022-01-10 17:39:33 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
public SimpleTimezoneDto GetTimezone(int idWell)
|
|
|
|
|
{
|
2022-06-16 12:33:05 +05:00
|
|
|
|
var well = GetOrDefault(idWell);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
if (well == null)
|
2022-01-18 11:04:15 +05:00
|
|
|
|
throw new ArgumentInvalidException($"idWell: {idWell} does not exist.", nameof(idWell));
|
2022-01-05 17:50:45 +05:00
|
|
|
|
return GetTimezone(well);
|
|
|
|
|
}
|
2021-12-30 17:05:44 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
private SimpleTimezoneDto GetTimezone(WellDto wellDto)
|
2022-01-10 17:39:33 +05:00
|
|
|
|
{
|
2022-06-06 15:43:47 +05:00
|
|
|
|
if (wellDto.Timezone is not null)
|
|
|
|
|
return wellDto.Timezone;
|
2022-01-10 17:39:33 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
if (wellDto.Telemetry is not null)
|
2022-01-05 17:50:45 +05:00
|
|
|
|
{
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var timezone = telemetryService.GetTimezone(wellDto.Telemetry.Id);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
if (timezone is not null)
|
|
|
|
|
return timezone;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var well = GetQuery().FirstOrDefault(w => w.Id == wellDto.Id);
|
2023-02-15 17:57:32 +05:00
|
|
|
|
|
|
|
|
|
if (well is not null)
|
2021-12-30 17:05:44 +05:00
|
|
|
|
{
|
2023-02-15 17:57:32 +05:00
|
|
|
|
var point = GetCoordinates(well);
|
|
|
|
|
if (point is not null)
|
2022-01-05 17:50:45 +05:00
|
|
|
|
{
|
2023-02-15 17:57:32 +05:00
|
|
|
|
if (point.Timezone is not null)
|
|
|
|
|
{
|
|
|
|
|
return point.Timezone.Adapt<SimpleTimezoneDto>();
|
|
|
|
|
}
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
2023-02-15 17:57:32 +05:00
|
|
|
|
if (point.Latitude is not null & point.Longitude is not null)
|
2022-01-05 17:50:45 +05:00
|
|
|
|
{
|
2023-04-18 16:16:11 +05:00
|
|
|
|
var timezone = timezoneService.GetOrDefaultByCoordinates(point.Latitude!.Value, point.Longitude!.Value);
|
2023-02-15 17:57:32 +05:00
|
|
|
|
if (timezone is not null)
|
|
|
|
|
{
|
|
|
|
|
return timezone;
|
|
|
|
|
}
|
2022-01-05 17:50:45 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-30 17:05:44 +05:00
|
|
|
|
}
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
throw new Exception($"Can't find timezone for well {wellDto.Caption} id: {wellDto.Id}");
|
2022-01-05 17:50:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static AsbCloudDb.Model.IMapPoint GetCoordinates(Well well)
|
|
|
|
|
{
|
2022-04-11 18:00:34 +05:00
|
|
|
|
if (well is null)
|
2022-01-05 17:50:45 +05:00
|
|
|
|
throw new ArgumentNullException(nameof(well));
|
|
|
|
|
|
|
|
|
|
if (well.Latitude is not null & well.Longitude is not null)
|
|
|
|
|
return well;
|
|
|
|
|
|
|
|
|
|
var cluster = well.Cluster;
|
|
|
|
|
|
|
|
|
|
if (cluster.Latitude is not null & cluster.Longitude is not null)
|
|
|
|
|
return cluster;
|
|
|
|
|
|
|
|
|
|
if (cluster.Deposit is null)
|
|
|
|
|
throw new Exception($"Can't find coordinates of well by cluster {cluster.Caption} id: {cluster.Id}");
|
|
|
|
|
|
|
|
|
|
var deposit = cluster.Deposit;
|
|
|
|
|
|
|
|
|
|
if (deposit.Latitude is not null & deposit.Longitude is not null)
|
|
|
|
|
return deposit;
|
|
|
|
|
|
|
|
|
|
throw new Exception($"Can't find coordinates of well by deposit {deposit.Caption} id: {deposit.Id}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DatesRangeDto GetDatesRange(int idWell)
|
|
|
|
|
{
|
2022-06-16 12:33:05 +05:00
|
|
|
|
var well = GetOrDefault(idWell);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
if (well is null)
|
|
|
|
|
throw new Exception($"Well id: {idWell} does not exist.");
|
|
|
|
|
|
|
|
|
|
if (well.IdTelemetry is null)
|
2022-11-21 16:58:37 +05:00
|
|
|
|
throw new KeyNotFoundException($"Well id: {idWell} does not contain telemetry.");
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
|
|
|
|
return telemetryService.GetDatesRange((int)well.IdTelemetry);
|
2021-12-30 17:05:44 +05:00
|
|
|
|
}
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2023-02-15 17:57:32 +05:00
|
|
|
|
}
|