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 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 IEnumerable GetTransmittingWells(int idCompany) { var wells = new List(); IEnumerable activeTelemetriesUids = telemetryTracker.GetTransmittingTelemetryUids(); if (activeTelemetriesUids.Any()) { wells = db.GetWellsForCompany(idCompany) .Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid)) .AsNoTracking() .ToList(); } return wells.Select(w => From(w)); } public IEnumerable GetWellsByCompany(int idCompany) { var wells = db.GetWellsForCompany(idCompany).ToList(); return wells.Select(w => From(w)); } public bool IsCompanyInvolvedInWell(int idCompany, int idWell) => cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany); public IEnumerable GetOperations(int idWell) { var entities = db .WellOperations .Where(o => o.IdWell == idWell) .AsNoTracking() .ToList(); 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; } } }