using AsbCloudDb.Model; using AsbSaubReport.Model; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; namespace AsbSaubReport { public class ReportDataSourcePgCloud : IReportDataSource { private readonly AsbCloudDbContext context; private readonly int? idTelemetry; private readonly WellInfoReport info; private readonly Dictionary events; private readonly Dictionary users; private readonly Dictionary categories = new Dictionary { {1, "Авария"}, {2, "Предупреждение"}, {3, "Информация"}, }; public ReportDataSourcePgCloud(AsbCloudDbContext context, int idWell) { this.context = context; var well = context.Wells .Include(w => w.Cluster) .ThenInclude(c => c.Deposit) .Include(w => w.RelationCompaniesWells) .ThenInclude(r => r.Company) .Include(w => w.Telemetry) .FirstOrDefault(w => w.Id == idWell); idTelemetry = well?.IdTelemetry; if (idTelemetry is null) throw new ArgumentException($"Well {idWell} doesn't contain telemetry", nameof(idWell)); events = context.TelemetryEvents .Where(e => e.IdTelemetry == idTelemetry) .ToDictionary(e => e.IdEvent, e => e); users = context.TelemetryUsers .Where(u => u.IdTelemetry == idTelemetry) .ToDictionary(u => u.IdUser, u => u); info = new WellInfoReport { Deposit = well.Cluster.Deposit.Caption, Cluster = well.Cluster.Caption, Well = well.Caption, Customer = well.RelationCompaniesWells.FirstOrDefault(c => c.Company.IdCompanyType == 1)?.Company.Caption, DrillingStartDate = well.Telemetry?.Info?.DrillingStartDate ?? default, TimeZoneId = well.Telemetry?.Info?.TimeZoneId ?? default, }; } public AnalyzeResult Analyze() { var messagesQuery = from item in context.TelemetryMessages where item.IdTelemetry == idTelemetry select item; var messagesCount = messagesQuery.Count(); var messagesMinDate = messagesQuery.Min(e => e.Date); var messagesMaxDate = messagesQuery.Max(e => e.Date); var dataQuery = from item in context.TelemetryDataSaub where item.IdTelemetry == idTelemetry select item; var dataMinDate = dataQuery.Min(e => e.Date); var dataMaxDate = dataQuery.Max(e => e.Date); var result = new AnalyzeResult { MaxDate = dataMinDate < messagesMinDate ? dataMinDate : messagesMinDate, MinDate = dataMaxDate > messagesMaxDate ? dataMaxDate : messagesMaxDate, MessagesCount = messagesCount, }; return result; } public IQueryable GetDataSaubItems(DateTime begin, DateTime end) => from item in context.TelemetryDataSaub where item.IdTelemetry == idTelemetry && item.Date >= begin && item.Date <= end select new DataSaubReport { Id = item.Id, Date = item.Date, Mode = item.Mode, WellDepth = item.WellDepth, BitDepth = item.BitDepth, BlockPosition = item.BlockPosition, BlockSpeed = item.BlockSpeed, BlockSpeedSp = item.BlockSpeedSp, BlockSpeedSpDevelop = item.BlockSpeedSpDevelop, Pressure = item.Pressure, PressureSp = item.PressureSp, AxialLoad = item.AxialLoad, AxialLoadSp = item.AxialLoadSp, AxialLoadLimitMax = item.AxialLoadLimitMax, HookWeight = item.HookWeight, RotorTorque = item.RotorTorque, RotorTorqueSp = item.RotorTorqueSp, RotorSpeed = item.RotorSpeed, Flow = item.Flow, PressureSpDevelop = item.PressureSpDevelop, }; public IQueryable GetMessages(DateTime begin, DateTime end) => from item in context.TelemetryMessages where item.IdTelemetry == idTelemetry && item.Date >= begin && item.Date <= end select new MessageReport { Id = item.Id, Date = item.Date, Category = categories[events[item.IdEvent].IdCategory], User = item.IdTelemetryUser == null ? "" : users[(int)item.IdTelemetryUser].MakeDisplayName(), Text = events[item.IdEvent].MakeMessageText(item) }; public WellInfoReport GetWellInfo() => info; } }