using Quartz;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using IStation.Service;
|
using System.Diagnostics;
|
|
namespace IStation.Server
|
{
|
/// <summary>
|
/// 泵站能效分析任务
|
/// </summary>
|
[DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务
|
public class EtaAnalyCronJob : IJob
|
{
|
/// <summary>
|
/// 客户标识
|
/// </summary>
|
public long CorpID { get; set; }
|
|
/// <summary>
|
/// 分析频率
|
/// </summary>
|
public int Frequency { get; set; }
|
|
/// <summary>
|
///
|
/// </summary>
|
public Task Execute(IJobExecutionContext context)
|
{
|
|
return Task.Run(() =>
|
{
|
try
|
{
|
//var stopWatch = new Stopwatch();
|
//stopWatch.Start();
|
LogHelper.Info($"客户标识:{this.CorpID}, 开始能效分析!");
|
|
//获取客户信息
|
var corpration = new Service.Corpration().GetByID(CorpID);
|
if (corpration == null)
|
{
|
LogHelper.Info($"能效分析任务中,客户标识:{CorpID},未检测到客户信息!");
|
return;
|
}
|
|
//能效分析上下文
|
var analy_context = new Model.EtaCorpAnalyContext();
|
analy_context.ID = CorpID;
|
analy_context.Name = corpration.ShortName;
|
analy_context.DataTime = DateTime.Now;
|
analy_context.Duration = Frequency;
|
analy_context.TagName = corpration.TagName;
|
analy_context.UseStatus = corpration.UseStatus;
|
analy_context.LogicCatalogContextItems = new List<Model.EtaLogicCatalogAnalyContextItem>();
|
|
//获取业务类别
|
var logic_catalog_list = new Service.LogicCatalog().GetByCorpID(CorpID);
|
if (logic_catalog_list == null || logic_catalog_list.Count < 1)
|
{
|
LogHelper.Info($"能效分析任务中,客户标识:{CorpID},未检测到业务类别信息!");
|
return;
|
}
|
|
//遍历业务类别
|
foreach (var logicCataog in logic_catalog_list)
|
{
|
var logicCatalogContextItem = new Model.EtaLogicCatalogAnalyContextItem();
|
logicCatalogContextItem.ID = logicCataog.ID;
|
logicCatalogContextItem.Name = logicCataog.Name;
|
logicCatalogContextItem.TagName = logicCataog.TagName;
|
logicCatalogContextItem.SortCode = logicCataog.SortCode;
|
logicCatalogContextItem.UseStatus = logicCataog.UseStatus;
|
|
logicCatalogContextItem.LogicTreeContextItems = new List<Model.EtaLogicTreeAnalyContextItem>();
|
analy_context.LogicCatalogContextItems.Add(logicCatalogContextItem);
|
|
//获取业务清单
|
var logicTreeList = new Service.LogicTree().GetByCatalogID(CorpID, logicCataog.ID);
|
//遍历业务清单
|
if (logicTreeList != null && logicTreeList.Count > 0)
|
{
|
var logicTreeContextItemList = new List<Model.EtaLogicTreeAnalyContextItem>();
|
foreach (var logicTree in logicTreeList)
|
{
|
var logicTreeContextItem = new Model.EtaLogicTreeAnalyContextItem();
|
logicTreeContextItem.TreeID = logicTree.ID;
|
logicTreeContextItem.ParentID = logicTree.ParentIds.LastOrDefault();
|
logicTreeContextItem.ParentIds = logicTree.ParentIds;
|
logicTreeContextItem.LogicType = logicTree.LogicType;
|
logicTreeContextItem.LogicID = logicTree.LogicID;
|
logicTreeContextItem.SortCode = logicTree.SortCode;
|
logicTreeContextItem.Children = new List<Model.EtaLogicTreeAnalyContextItem>();
|
|
var logicTreeParentContextItem = logicTreeContextItemList.Find(t => t.TreeID == logicTreeContextItem.ParentID);
|
if (logicTreeParentContextItem != null)
|
{
|
logicTreeParentContextItem.Children.Add(logicTreeContextItem);
|
}
|
logicTreeContextItemList.Add(logicTreeContextItem);
|
|
//业务区域
|
if (logicTree.LogicType == IStation.ObjectType.LogicArea)
|
{
|
IStation.Model.LogicArea logicArea = new Service.LogicArea().GetByID(CorpID, logicTree.LogicID);
|
if (logicArea == null)
|
continue;
|
|
//构建 上下文
|
logicTreeContextItem.LogicName = logicArea.Name;
|
logicTreeContextItem.ObjectID = logicArea.ID;
|
logicTreeContextItem.LogicContextItem = new Model.EtaLogicAreaAnalyContextItem(logicArea);
|
}
|
//泵站
|
else if (logicTree.LogicType == IStation.ObjectType.Station)
|
{
|
#region 泵站
|
var station = new Service.Station().GetByID(CorpID, logicTree.LogicID);
|
if (station == null)
|
continue;
|
if (station.ID == 1564533764212789248)
|
{
|
var a = 1;
|
}
|
|
//构建泵站上下文
|
logicTreeContextItem.LogicName = station.Name;
|
logicTreeContextItem.ObjectID = station.ID;
|
var stationContextItem = new Model.EtaStationAnalyContextItem(station);
|
logicTreeContextItem.LogicContextItem = stationContextItem;
|
|
|
//获取管路
|
var pipeLineList = new Service.PipeLine().GetByBelongTypeAndBelongID(CorpID, IStation.ObjectType.Station, station.ID);
|
if (pipeLineList != null && pipeLineList.Count > 0)
|
{
|
var pipeLineIds = pipeLineList.Select(x => x.ID).ToList();
|
//管路绑定
|
var pipeBindlingList = new Service.PipeLineBinding().GetUseByPipeLineIds(CorpID, pipeLineIds);
|
//测点映射
|
var monitorPointMappingList = new Service.MonitorPointMapping().GetByObjectTypeAndObjectIds(CorpID, IStation.ObjectType.PipeLine, pipeLineIds);
|
//测点列表
|
var monitorPointList = new Service.MonitorPoint().GetByIds(CorpID,
|
monitorPointMappingList?.Select(x => x.MonitorPointID).Distinct().ToList(), Model.eMonitorType.General, Model.Monitor.eCronType.Real);
|
//最近测点记录
|
//var monitorRecordList = new Service.MonitorRealRecord().GetLastNormalRecord(CorpID,monitorPointList?.Select(x=>x.ID).ToList());
|
var monitorRecordList = MonitorRealRecordHelper.GetLastNormalRealRecord(CorpID, monitorPointList?.Select(x => x.ID).ToList());
|
//遍历管路
|
foreach (var pipeLine in pipeLineList)
|
{
|
var pipeLineContextItem = new Model.EtaPipeLineAnalyContextItem();
|
pipeLineContextItem.ID = pipeLine.ID;
|
pipeLineContextItem.Name = pipeLine.Name;
|
pipeLineContextItem.Catalog = pipeLine.Catalog;
|
pipeLineContextItem.HeadID = pipeLine.HeadID;
|
pipeLineContextItem.TailID = pipeLine.TailID;
|
pipeLineContextItem.Direction = pipeLine.Direction;
|
pipeLineContextItem.SerialNO = pipeLine.SerialNO;
|
pipeLineContextItem.Flags = pipeLine.Flags?.ToList();
|
pipeLineContextItem.TagName = pipeLine.TagName;
|
pipeLineContextItem.UseStatus = pipeLine.UseStatus;
|
pipeLineContextItem.ProductContextItem = null;
|
pipeLineContextItem.MonitorPointContextItems = new List<Model.EtaMonitorPointAnalyContextitem>();
|
stationContextItem.PipeLineContextItems.Add(pipeLineContextItem);
|
|
//构造测点上下文项
|
var monitorPointMappingItemList = monitorPointMappingList?.Where(x => x.ObjectID == pipeLine.ID).ToList();
|
if (monitorPointMappingItemList != null && monitorPointMappingItemList.Count > 0)
|
{
|
foreach (var monitorPointMapping in monitorPointMappingItemList)
|
{
|
var monitorPoint = monitorPointList?.Find(t => t.ID == monitorPointMapping.MonitorPointID);
|
if (monitorPoint == null)
|
continue;
|
|
var signal = new Service.Signal().GetFirstByMonitorPointID(CorpID, monitorPoint.ID);
|
if (signal == null)
|
continue;
|
|
var record = monitorRecordList?.Find(t => t.MonitorPointID == monitorPoint.ID);
|
if (record == null)
|
continue;
|
|
//暂时未进行时间步长验证
|
if (double.TryParse(record.DataValue, out double dataValue))
|
{
|
var monitorPointContextItem = new Model.EtaMonitorPointAnalyContextitem(monitorPoint);
|
monitorPointContextItem.PipeLineID = pipeLine.ID;
|
monitorPointContextItem.SignalType = signal.SignalType;
|
monitorPointContextItem.DataValue = dataValue;
|
pipeLineContextItem.MonitorPointContextItems.Add(monitorPointContextItem);
|
}
|
}
|
}
|
|
//构建设备绑定项
|
switch (pipeLine.Catalog)
|
{
|
case IStation.PipeLine.EnginePump:
|
{
|
var productBinding = pipeBindlingList?.Find(t => t.PipeLineID == pipeLine.ID && t.BindingType == IStation.ObjectType.Product);
|
if (productBinding == null)
|
continue;
|
|
var product = new Service.Product().GetByID(CorpID, productBinding.BindingID);
|
if (product == null)
|
{
|
continue;
|
}
|
if (product.Catalog == IStation.Product.Catalog_JiBeng)
|
{//机泵
|
var enginePumpContextItem = new Model.EtaEnginePumpAnalyContextItem(product);
|
enginePumpContextItem.PipeLineID = pipeLine.ID;
|
|
var pump = new Service.Product().GetChildPumpByEnginePumpID(CorpID, product.ID);
|
if (pump != null)
|
{
|
pipeLineContextItem.ProductContextItem = enginePumpContextItem;
|
enginePumpContextItem.RatedParas = pump.RatedParas;
|
var pumpCurve = new Service.PumpCurveExMapping().GetDefaultWorkingByPumpID(CorpID, pump.ID);
|
enginePumpContextItem.SetCurveInfo(pumpCurve);
|
}
|
}
|
}
|
break;
|
default: break;
|
}
|
}
|
}
|
#endregion
|
}
|
}
|
var logicTreeRootContextItemList = logicTreeContextItemList.Where(x => x.ParentID == 0).OrderBy(x => x.SortCode).ToList();
|
logicCatalogContextItem.LogicTreeContextItems.AddRange(logicTreeRootContextItemList);
|
}
|
}
|
|
//stopWatch.Stop();
|
//var elea = stopWatch.Elapsed;
|
|
#region 计算
|
|
var calculator = EtaCalculationFactory.Create(this.CorpID);
|
if (calculator == null)
|
{
|
LogHelper.Info($"能效分析任务中,客户标识:{this.CorpID}, 创建计算器失败!");
|
return;
|
}
|
var analyBol = calculator.Calculate(analy_context, (singleList, multiList, logicList, error_info) =>
|
{
|
if (!string.IsNullOrEmpty(error_info))
|
{
|
LogHelper.Info($"能效分析任务中,客户标识:{this.CorpID}, 错误信息:{error_info}");
|
return;
|
}
|
|
if (singleList != null && singleList.Count > 0)
|
{
|
var bol = new Service.EtaSingleRealRecord().InsertsLastRecord(singleList);
|
if (!bol)
|
{
|
LogHelper.Info($"能效分析任务中,客户标识:{this.CorpID}, (单)记录保存失败!");
|
}
|
}
|
if (multiList != null && multiList.Count > 0)
|
{
|
var bol = new Service.EtaMultiRealRecord().InsertsLastRecord(multiList);
|
if (!bol)
|
{
|
LogHelper.Info($"能效分析任务中,客户标识:{this.CorpID}, (多)记录保存失败!");
|
}
|
}
|
if (logicList != null && logicList.Count > 0)
|
{
|
var bol = new Service.EtaLogicRealRecord().InsertsLastRecord(logicList);
|
if (!bol)
|
{
|
LogHelper.Info($"能效分析任务中,客户标识:{this.CorpID}, (业务)记录保存失败!");
|
}
|
}
|
|
});
|
#endregion
|
|
LogHelper.Info($"客户标识:{this.CorpID}, 结束能效分析!");
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error($"能效实时分析任务中,客户标识:{this.CorpID}, 执行出错", ex);
|
var e = new JobExecutionException(ex);
|
throw e;
|
}
|
|
});
|
|
|
}
|
|
|
|
|
|
|
}
|
}
|