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-05-12 16:03:14 +05:00
|
|
|
|
private readonly ITelemetryTracker telemetryTracker;
|
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-05-14 17:02:29 +05:00
|
|
|
|
public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
2021-05-12 16:03:14 +05:00
|
|
|
|
this.telemetryTracker = telemetryTracker;
|
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-08-11 16:54:42 +05:00
|
|
|
|
public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, CancellationToken token)
|
2021-05-12 16:03:14 +05:00
|
|
|
|
{
|
2021-05-12 17:25:52 +05:00
|
|
|
|
var wells = new List<Well>();
|
2021-05-14 10:58:23 +05:00
|
|
|
|
IEnumerable<string> activeTelemetriesUids = telemetryTracker.GetTransmittingTelemetryUids();
|
2021-05-20 11:17:55 +05:00
|
|
|
|
if (activeTelemetriesUids.Any())
|
2021-05-12 17:25:52 +05:00
|
|
|
|
{
|
2021-08-11 12:11:21 +05:00
|
|
|
|
wells = await db.GetWellsForCompany(idCompany)
|
2021-05-12 17:25:52 +05:00
|
|
|
|
.Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid))
|
2021-08-11 10:16:01 +05:00
|
|
|
|
.AsNoTracking()
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-05-12 17:25:52 +05:00
|
|
|
|
}
|
2021-05-12 16:03:14 +05:00
|
|
|
|
return wells.Select(w => From(w));
|
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-04-02 17:28:07 +05:00
|
|
|
|
return wells.Select(w => From(w));
|
|
|
|
|
}
|
|
|
|
|
|
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-11 16:54:42 +05:00
|
|
|
|
public async Task<IEnumerable<WellOperationDto>> GetOperationsAsync(int idWell, CancellationToken token)
|
2021-07-27 14:43:30 +05:00
|
|
|
|
{
|
2021-08-11 12:11:21 +05:00
|
|
|
|
var entities = await db.WellOperations.Where(o => o.IdWell == idWell)
|
2021-08-11 10:16:01 +05:00
|
|
|
|
.AsNoTracking()
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-07-28 09:46:58 +05:00
|
|
|
|
|
|
|
|
|
var dtos = entities.Adapt<WellOperationDto>();
|
|
|
|
|
|
|
|
|
|
return dtos;
|
2021-07-27 14:43:30 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:03:14 +05:00
|
|
|
|
private static WellDto From(Well well)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
|
|
|
|
var wellDto = new WellDto
|
|
|
|
|
{
|
|
|
|
|
Id = well.Id,
|
|
|
|
|
Caption = well.Caption,
|
|
|
|
|
Cluster = well.Cluster.Caption,
|
|
|
|
|
Deposit = well.Cluster.Deposit.Caption,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return wellDto;
|
|
|
|
|
}
|
2021-08-25 11:13:56 +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);
|
|
|
|
|
|
|
|
|
|
var dto = entity.Adapt<WellDto>();
|
|
|
|
|
dto.Cluster = entity.Cluster?.Caption;
|
|
|
|
|
dto.Deposit = entity.Cluster?.Deposit?.Caption;
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
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);
|
|
|
|
|
var dto = entity.Adapt<WellDto>();
|
|
|
|
|
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-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
}
|