DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/WellService.cs

79 lines
2.8 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Services
{
public class WellService : IWellService
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryTracker telemetryTracker;
private readonly CacheTable<RelationCompanyWell> cacheRelationCompaniesWells;
public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb)
{
this.db = db;
this.telemetryTracker = telemetryTracker;
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db);
}
public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, CancellationToken token)
{
var wells = new List<Well>();
IEnumerable<string> activeTelemetriesUids = telemetryTracker.GetTransmittingTelemetryUids();
if (activeTelemetriesUids.Any())
{
wells = await db.GetWellsForCompany(idCompany)
.Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid))
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
}
return wells.Select(w => From(w));
}
public async Task<IEnumerable<WellDto>> GetWellsByCompanyAsync(int idCompany, CancellationToken token)
{
var wells = await db.GetWellsForCompany(idCompany).ToListAsync(token);
return wells.Select(w => From(w));
}
public async Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token)
=> await cacheRelationCompaniesWells.ContainsAsync(r => r.IdWell == idWell &&
r.IdCompany == idCompany, token).ConfigureAwait(false);
public async Task<IEnumerable<WellOperationDto>> GetOperationsAsync(int idWell, CancellationToken token)
{
var entities = await db.WellOperations.Where(o => o.IdWell == idWell)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Adapt<WellOperationDto>();
return dtos;
}
private static WellDto From(Well well)
{
var wellDto = new WellDto
{
Id = well.Id,
Caption = well.Caption,
Cluster = well.Cluster.Caption,
Deposit = well.Cluster.Deposit.Caption,
};
return wellDto;
}
}
}