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 cacheRelationCompaniesWells; public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb) { this.db = db; this.telemetryTracker = telemetryTracker; cacheRelationCompaniesWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); } public async Task> GetTransmittingWellsAsync(int idCompany) { var wells = new List(); IEnumerable activeTelemetriesUids = telemetryTracker.GetTransmittingTelemetryUids(); if (activeTelemetriesUids.Any()) { wells = await db.GetWellsForCompany(idCompany) .Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid)) .AsNoTracking() .ToListAsync(); } return wells.Select(w => From(w)); } public async Task> GetWellsByCompanyAsync(int idCompany) { var wells = await db.GetWellsForCompany(idCompany).ToListAsync(); return wells.Select(w => From(w)); } public async Task IsCompanyInvolvedInWellAsync(int idCompany, int idWell) => await cacheRelationCompaniesWells.ContainsAsync(r => r.IdWell == idWell && r.IdCompany == idCompany, new CancellationToken()); public async Task> GetOperationsAsync(int idWell) { var entities = await db.WellOperations.Where(o => o.IdWell == idWell) .AsNoTracking() .ToListAsync(); var dtos = entities.Adapt(); 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; } } }