2021-04-02 17:28:07 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2021-05-14 17:02:29 +05:00
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
2021-07-28 09:46:58 +05:00
|
|
|
|
using Mapster;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
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
|
|
|
|
|
{
|
|
|
|
|
public class WellService : IWellService
|
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-10-15 12:24:04 +05:00
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2021-07-21 17:23:57 +05:00
|
|
|
|
private readonly CacheTable<RelationCompanyWell> cacheRelationCompaniesWells;
|
2021-08-25 11:13:56 +05:00
|
|
|
|
private readonly CacheTable<Well> cacheWells;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2021-10-15 12:24:04 +05:00
|
|
|
|
public WellService(IAsbCloudDbContext db, ITelemetryService telemetryService, CacheDb cacheDb)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
2021-10-15 12:24:04 +05:00
|
|
|
|
this.telemetryService = telemetryService;
|
2021-07-21 17:23:57 +05:00
|
|
|
|
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db);
|
2021-08-25 11:13:56 +05:00
|
|
|
|
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
|
2021-05-12 16:03:14 +05:00
|
|
|
|
}
|
2021-10-20 12:52:31 +05:00
|
|
|
|
|
|
|
|
|
public DateTime GetLastTelemetryDate(int idWell)
|
|
|
|
|
{
|
|
|
|
|
var well = cacheWells.FirstOrDefault(w => w.Id == idWell);
|
|
|
|
|
|
2021-10-21 15:57:20 +05:00
|
|
|
|
if (well?.IdTelemetry is null)
|
2021-10-25 16:32:55 +05:00
|
|
|
|
return DateTime.MinValue;
|
2021-10-20 12:52:31 +05:00
|
|
|
|
|
2021-10-25 16:32:55 +05:00
|
|
|
|
var lastTelemetryDate = telemetryService.GetLastTelemetryDate((int)well.IdTelemetry);
|
|
|
|
|
return lastTelemetryDate;
|
2021-10-20 12:52:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 15:57:20 +05:00
|
|
|
|
public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
2021-12-07 15:55:11 +05:00
|
|
|
|
var activeTelemetryIds = telemetryService.GetTransmittingTelemetries()
|
2021-10-21 15:57:20 +05:00
|
|
|
|
.Select(t => t.Id);
|
|
|
|
|
|
|
|
|
|
var wells = await (from w in db.GetWellsForCompany(idCompany)
|
|
|
|
|
where w.IdTelemetry != null &&
|
|
|
|
|
activeTelemetryIds.Contains((int)w.IdTelemetry)
|
|
|
|
|
select w)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return wells.Select(Convert);
|
|
|
|
|
}
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<IEnumerable<WellDto>> GetWellsByCompanyAsync(int idCompany, CancellationToken token)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-08-11 16:54:42 +05:00
|
|
|
|
var wells = await db.GetWellsForCompany(idCompany).ToListAsync(token);
|
2021-10-21 15:57:20 +05:00
|
|
|
|
return wells.Select(Convert);
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
2021-10-18 12:38:49 +05:00
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
public async Task<int?> UpdateWellAsync(int idWell, WellParamsDto 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)
|
2021-10-27 17:48:19 +05:00
|
|
|
|
throw new ArgumentException("Тип скважины указан неправильно.", nameof(dto));
|
2021-10-18 16:30:14 +05:00
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
if (dto.IdState is < 0 or > 2)
|
2021-10-27 17:48:19 +05:00
|
|
|
|
throw new ArgumentException("Текущее состояние работы скважины указано неправильно.", nameof(dto));
|
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
var entity = await db.Wells
|
2021-10-18 12:38:49 +05:00
|
|
|
|
.FirstOrDefaultAsync(w => w.Id == idWell, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
if (entity is null)
|
2021-10-19 11:17:16 +05:00
|
|
|
|
throw new ArgumentException("Тип секции указан неправильно.", nameof(idWell));
|
2021-10-18 12:38:49 +05:00
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
entity.Caption = dto.Caption;
|
|
|
|
|
entity.Latitude = dto.Latitude;
|
|
|
|
|
entity.Longitude = dto.Longitude;
|
|
|
|
|
entity.IdWellType = dto.IdWellType;
|
|
|
|
|
entity.IdState = dto.IdState;
|
2021-10-18 12:38:49 +05:00
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
db.Wells.Update(entity);
|
2021-10-18 12:38:49 +05:00
|
|
|
|
|
|
|
|
|
return await db.SaveChangesAsync(token);
|
|
|
|
|
}
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2021-10-12 16:07:08 +05:00
|
|
|
|
public bool IsCompanyInvolvedInWell(int idCompany, int idWell)
|
|
|
|
|
=> cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany);
|
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
=> await cacheRelationCompaniesWells.ContainsAsync(r => r.IdWell == idWell &&
|
2021-08-11 17:26:02 +05:00
|
|
|
|
r.IdCompany == idCompany, token).ConfigureAwait(false);
|
2021-08-09 15:41:42 +05:00
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
public async Task<WellDto> GetAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var entity = await db.Wells
|
2021-09-10 11:28:57 +05:00
|
|
|
|
.Include(w => w.Cluster)
|
|
|
|
|
.ThenInclude(c => c.Deposit)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
.FirstOrDefaultAsync(w => w.Id == idWell, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-10-20 16:18:07 +05:00
|
|
|
|
|
2021-10-21 15:57:20 +05:00
|
|
|
|
if (entity is null)
|
|
|
|
|
return null;
|
2021-10-20 16:18:07 +05:00
|
|
|
|
|
2021-10-21 15:57:20 +05:00
|
|
|
|
var dto = Convert(entity);
|
2021-10-20 16:18:07 +05:00
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
2021-11-22 14:14:44 +05:00
|
|
|
|
|
2021-08-25 11:13:56 +05:00
|
|
|
|
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var entity = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token).ConfigureAwait(false);
|
2021-10-21 15:57:20 +05:00
|
|
|
|
var dto = Convert(entity);
|
2021-08-25 11:13:56 +05:00
|
|
|
|
return dto.Caption;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var well = await db.Wells
|
2021-09-10 11:28:57 +05:00
|
|
|
|
.Include(w => w.RelationCompaniesWells)
|
|
|
|
|
.ThenInclude(r => r.Company)
|
2021-08-25 11:13:56 +05:00
|
|
|
|
.FirstOrDefaultAsync(w => w.Id == idWell, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
var companies = well.RelationCompaniesWells.Select(r => r.Company);
|
|
|
|
|
return companies.Adapt<CompanyDto>();
|
|
|
|
|
}
|
2021-10-20 12:52:31 +05:00
|
|
|
|
|
|
|
|
|
public string GetStateText(int state)
|
|
|
|
|
{
|
|
|
|
|
return state switch
|
|
|
|
|
{
|
|
|
|
|
1 => "В работе",
|
|
|
|
|
2 => "Завершена",
|
|
|
|
|
_ => "Незвестно",
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-11-23 11:41:27 +05:00
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<int>> GetClusterWellsIdsAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
2021-12-07 11:34:06 +05:00
|
|
|
|
var well = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token)
|
2021-11-23 11:41:27 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (well is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-12-07 11:34:06 +05:00
|
|
|
|
var clusterWells = await cacheWells.WhereAsync(w => w.IdCluster == well.IdCluster, token)
|
2021-11-23 11:41:27 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return clusterWells.Select(w => w.Id);
|
|
|
|
|
}
|
2021-10-21 15:57:20 +05:00
|
|
|
|
|
|
|
|
|
private WellDto Convert(Well well)
|
|
|
|
|
{
|
|
|
|
|
return new WellDto
|
|
|
|
|
{
|
|
|
|
|
Id = well.Id,
|
|
|
|
|
Caption = well.Caption,
|
2021-11-23 11:41:27 +05:00
|
|
|
|
IdCluster = well.IdCluster,
|
2021-11-22 14:14:44 +05:00
|
|
|
|
Cluster = well.Cluster?.Caption,
|
|
|
|
|
Deposit = well.Cluster?.Deposit?.Caption,
|
2021-10-27 17:48:19 +05:00
|
|
|
|
LastTelemetryDate = GetLastTelemetryDate(well.Id),
|
|
|
|
|
IdWellType = well.IdWellType ?? default,
|
|
|
|
|
IdState = well.IdState,
|
|
|
|
|
Latitude = well.Latitude,
|
|
|
|
|
Longitude = well.Longitude,
|
2021-10-21 15:57:20 +05:00
|
|
|
|
};
|
|
|
|
|
}
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
}
|