DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/WellService.cs

62 lines
1.9 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using System.Collections.Generic;
using System.Linq;
using System;
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 idCustomer)
{
var wells = new List<Well>();
IEnumerable<string> activeTelemetriesUids = telemetryTracker.GetTransmittingTelemetryUids();
2021-05-20 11:17:55 +05:00
if (activeTelemetriesUids.Any())
{
wells = db.GetWellsByCustomer(idCustomer)
.Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid))
.ToList();
}
return wells.Select(w => From(w));
}
public IEnumerable<WellDto> GetWellsByCustomer(int idCustomer)
{
var wells = db.GetWellsByCustomer(idCustomer).ToList();
return wells.Select(w => From(w));
}
public bool CheckWellOwnership(int idCustomer, int wellId)
=> cacheWells.Contains(w => w.Id == wellId && w.IdCustomer == idCustomer);
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;
}
}
}