using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services.Cache; using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services { public class WellService : IWellService { private readonly IAsbCloudDbContext db; private readonly ITelemetryService telemetryService; private readonly CacheTable cacheRelationCompaniesWells; private readonly CacheTable cacheWells; public WellService(IAsbCloudDbContext db, ITelemetryService telemetryService, CacheDb cacheDb) { this.db = db; this.telemetryService = telemetryService; cacheRelationCompaniesWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); cacheWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); } public async Task> GetTransmittingWellsAsync(int idCompany, CancellationToken token) => await telemetryService.GetTransmittingWellsAsync(idCompany, token); public async Task> GetWellsByCompanyAsync(int idCompany, CancellationToken token) { var wells = await db.GetWellsForCompany(idCompany).ToListAsync(token); return wells.Select(w => new WellDto { Id = w.Id, Caption = w.Caption, Cluster = w.Cluster.Caption, Deposit = w.Cluster.Deposit.Caption, }); } public async Task UpdateWellAsync(int idWell, string caption = default, double latitude = default, double longitude = default, int idWellType = default, int state = default, CancellationToken token = default) { var well = await db.Wells .FirstOrDefaultAsync(w => w.Id == idWell, token) .ConfigureAwait(false); if (well is null) return 0; if (caption != default) well.Caption = caption; if (latitude != default) well.Latitude = latitude; if (longitude != default) well.Longitude = longitude; if (idWellType != default) well.IdWellType = idWellType; if (state < 3) well.State = state; else throw new ArgumentException("Недопустимое значение состояния работы скважины"); db.Wells.Update(well); return await db.SaveChangesAsync(token); } public bool IsCompanyInvolvedInWell(int idCompany, int idWell) => cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany); public async Task IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token) => await cacheRelationCompaniesWells.ContainsAsync(r => r.IdWell == idWell && r.IdCompany == idCompany, token).ConfigureAwait(false); public async Task GetAsync(int idWell, CancellationToken token) { var entity = await db.Wells .Include(w => w.Cluster) .ThenInclude(c => c.Deposit) .FirstOrDefaultAsync(w => w.Id == idWell, token) .ConfigureAwait(false); var dto = entity.Adapt(); dto.Cluster = entity.Cluster?.Caption; dto.Deposit = entity.Cluster?.Deposit?.Caption; return dto; } public async Task GetWellCaptionByIdAsync(int idWell, CancellationToken token) { var entity = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token).ConfigureAwait(false); var dto = entity.Adapt(); return dto.Caption; } public async Task> GetCompaniesAsync(int idWell, CancellationToken token) { var well = await db.Wells .Include(w => w.RelationCompaniesWells) .ThenInclude(r => r.Company) .FirstOrDefaultAsync(w => w.Id == idWell, token) .ConfigureAwait(false); var companies = well.RelationCompaniesWells.Select(r => r.Company); return companies.Adapt(); } } }