DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/WellService.cs
2021-07-21 15:29:19 +05:00

62 lines
2.0 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AsbCloudInfrastructure.Services
{
public class WellService : IWellService
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryTracker telemetryTracker;
private readonly CacheTable<Well> cacheWells;
public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb)
{
this.db = db;
this.telemetryTracker = telemetryTracker;
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
public IEnumerable<WellDto> GetTransmittingWells(int idCompany)
{
var wells = new List<Well>();
IEnumerable<string> activeTelemetriesUids = telemetryTracker.GetTransmittingTelemetryUids();
if (activeTelemetriesUids.Any())
{
wells = db.GetWellsForCompany(idCompany)
.Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid))
.ToList();
}
return wells.Select(w => From(w));
}
public IEnumerable<WellDto> GetWellsByCompany(int idCompany)
{
var wells = db.GetWellsForCompany(idCompany).ToList();
return wells.Select(w => From(w));
}
public bool CheckWellOwnership(int idCompany, int wellId)
=> cacheWells.Contains(w => w.Id == wellId && (w.Companies.FirstOrDefault(c => c.Id == idCompany) != null));
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;
}
}
}