DD.WellWorkover.Cloud/AsbCloudInfrastructure/ReportDataSourcePgCloud.cs

135 lines
5.0 KiB
C#

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<int, Event> events;
private readonly Dictionary<int, TelemetryUser> users;
private readonly Dictionary<int, string> categories = new Dictionary<int, string>
{
{1, "Авария"},
{2, "Предупреждение"},
{3, "Информация"},
};
public ReportDataSourcePgCloud(AsbCloudDbContext context, int wellId)
{
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 == wellId);
idTelemetry = well?.IdTelemetry;
if (idTelemetry is null)
throw new ArgumentException($"Well {wellId} doesn't contain telemetry", nameof(wellId));
events = context.Events
.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.Messages
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.DataSaubBases
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<DataSaubReport> GetDataSaubItems(DateTime begin, DateTime end)
=> from item in context.DataSaubBases
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<MessageReport> GetMessages(DateTime begin, DateTime end)
=> from item in context.Messages
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;
}
}