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 : CrudCacheServiceBase, IWellService { private readonly ITelemetryService telemetryService; private readonly CacheTable cacheRelationCompaniesWells; public WellService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService) :base(db, cacheDb) { this.telemetryService = telemetryService; cacheRelationCompaniesWells = cacheDb.GetCachedTable((AsbCloudDbContext)db, nameof(RelationCompanyWell.Company), nameof(RelationCompanyWell.Well)); } public DateTime GetLastTelemetryDate(int idWell) { var well = Cache.FirstOrDefault(w => w.Id == idWell); if (well?.IdTelemetry is null) return DateTime.MinValue; var lastTelemetryDate = telemetryService.GetLastTelemetryDate((int)well.IdTelemetry); return lastTelemetryDate; } public async Task> GetWellsByCompanyAsync(int idCompany, CancellationToken token) { var relations = await cacheRelationCompaniesWells .WhereAsync(r => r.IdCompany == idCompany, token); var dtos = relations.Select(r => Convert(r.Well)); return dtos; } public override Task InsertAsync(WellDto newItem, CancellationToken token = default) { throw new NotImplementedException(); implement this //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } public override Task InsertRangeAsync(IEnumerable dtos, CancellationToken token) { throw new NotImplementedException(); } public override async Task UpdateAsync(int idWell, WellDto dto, CancellationToken token = default) { if (dto.IdWellType is < 1 or > 2) throw new ArgumentException("Тип скважины указан неправильно.", nameof(dto)); if (dto.IdState is < 0 or > 2) throw new ArgumentException("Текущее состояние работы скважины указано неправильно.", nameof(dto)); if(dto.Id != idWell) throw new ArgumentException($"Нельзя поменять id для скважины: {idWell} => {dto.Id}.", nameof(dto)); var entity = Convert(dto); var oldRelations = await cacheRelationCompaniesWells.FirstOrDefaultAsync(w => w.Id == idWell, token); var result = await Cache.UpsertAsync(entity, token); return result; } 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 GetWellCaptionByIdAsync(int idWell, CancellationToken token) { var entity = await Cache.FirstOrDefaultAsync(w => w.Id == idWell, token).ConfigureAwait(false); var dto = Convert(entity); return dto.Caption; } public async Task> GetCompaniesAsync(int idWell, CancellationToken token) { var relations = await cacheRelationCompaniesWells.WhereAsync(r => r.IdWell == idWell, token); var dtos = relations.Select(r => Convert(r.Company)); return dtos; } private IEnumerable GetCompanies(int idWell) { var relations = cacheRelationCompaniesWells.Where(r => r.IdWell == idWell); var dtos = relations.Select(r => Convert(r.Company)); return dtos; } public string GetStateText(int state) { return state switch { 1 => "В работе", 2 => "Завершена", _ => "Незвестно", }; } public async Task> GetClusterWellsIdsAsync(int idWell, CancellationToken token) { var well = await Cache.FirstOrDefaultAsync(w => w.Id == idWell, token) .ConfigureAwait(false); if (well is null) return null; var clusterWells = await Cache.WhereAsync(w => w.IdCluster == well.IdCluster, token) .ConfigureAwait(false); return clusterWells.Select(w => w.Id); } protected override WellDto Convert(Well entity) { var dto = base.Convert(entity); dto.Cluster = entity.Cluster?.Caption; dto.Deposit = entity.Cluster?.Deposit?.Caption; dto.LastTelemetryDate = GetLastTelemetryDate(entity.Id); dto.Companies = GetCompanies(entity.Id); return dto; } private CompanyDto Convert(Company entity) { var dto = entity.Adapt(); return dto; } } }