using Quartz;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using IStation.Calculation;
|
|
|
namespace IStation.Server
|
{
|
/// <summary>
|
/// 常规日分析计划任务
|
/// </summary>
|
[DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务
|
public class GeneralDayAnalyCronJob : IJob
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public Task Execute(IJobExecutionContext context)
|
{
|
return Task.Run(() =>
|
{
|
try
|
{
|
#region 当前时间
|
|
var dtNow = DateTime.Now;
|
|
#endregion
|
|
#region 加载目标客户
|
|
var corpIds = CorpHelper.GetCorpIds();
|
if (corpIds == null || corpIds.Count < 1)
|
{
|
LogHelper.Error("常规日分析计划任务中,未检索到目标客户信息");
|
return;
|
}
|
|
#endregion
|
|
#region 加载日分析测点
|
|
var monitorList = new Service.MonitorPoint().GetByMonitorTypeAndCronTypeAndSourceType
|
(corpIds, Model.eMonitorType.General, Model.Monitor.eCronType.EachDay, Model.Monitor.eSourceType.Analyse);
|
if (monitorList == null || monitorList.Count < 1)
|
{
|
LogHelper.Info($"常规日分析计划任务中,未检索到常规日分析测点信息");
|
return;
|
}
|
monitorList = monitorList.Where(x => x.UseStatus == Model.eUseStatus.Enable).ToList();
|
if (monitorList.Count < 1)
|
{
|
LogHelper.Info($"常规日分析计划任务中,未检索到有效常规日分析测点信息");
|
return;
|
}
|
|
#endregion
|
|
#region 遍历分析
|
|
foreach (var monitor in monitorList)
|
{
|
var signal = new Service.Signal().GetFirstExSignalTypeByMonitorPointID(monitor.CorpID, monitor.ID);
|
if (signal == null)
|
{
|
LogHelper.Error($"常规日分析计划任务中,客户标识:{monitor.CorpID},测点标识:{monitor.ID} 未检索到信号信息!");
|
continue;
|
}
|
|
var cronModel = Model.Monitor.DaySlot.ToModel(monitor.CronParas);
|
if (cronModel == null)
|
{
|
cronModel = new Model.Monitor.DaySlot();
|
}
|
|
var startTime = cronModel.GetStartTime(dtNow);
|
var endTime = cronModel.GetEndTime(dtNow);
|
|
#region 计算原始值
|
|
var getMonitorPoint = new Func<long, Model.MonitorPoint>(x => new Service.MonitorPoint().GetByID(monitor.CorpID, x));
|
var getSignal = new Func<long, Model.Signal_SignalType>(x=> new Service.Signal().GetFirstExSignalTypeByMonitorPointID(monitor.CorpID, x));
|
var getRecordList = new Func<long, long,Model.Monitor.eCronType, List<Model.MonitorBasicRecordContent>>((x, y,z)=>{
|
switch (z)
|
{
|
case Model.Monitor.eCronType.Real:
|
return new Service.MonitorRealRecord().GetBasicContentBySignalIDOfTimeRange(monitor.CorpID, x, y, startTime, endTime);
|
case Model.Monitor.eCronType.EachHour:
|
return new Service.MonitorHourRecord().GetBasicContentBySignalIDOfHourRange
|
(monitor.CorpID,x,y,startTime.Date,startTime.Hour+1,endTime.AddHours(-1).Date,endTime.AddHours(-1).Hour+1);
|
default:return default;
|
}
|
});
|
var srcValue = MonitorFormulaCalcuHelper.Statistical(monitor.SourceParas,getMonitorPoint,getSignal,getRecordList,out string msg);
|
if (!string.IsNullOrEmpty(msg))
|
{
|
LogHelper.Error($"常规日分析计划任务中,客户标识:{monitor.CorpID},测点标识:{monitor.ID}, {msg}!");
|
continue;
|
}
|
|
#endregion
|
|
#region 数据处理
|
|
var dataStatus = new List<string>();
|
var dataValue = MonitorHandleHelper.Handle(monitor, signal,
|
() => new Service.MonitorDayRecord().GetLastNormalRecord(monitor.CorpID, monitor.ID, signal.ID),
|
dtNow,
|
srcValue.ToString(),
|
dataStatus);
|
|
var analyRecord = MonitorHandleHelper.BuildMonitorRecord(monitor, signal, dtNow, srcValue.ToString(), dtNow, dataValue, dataStatus);
|
var result = new Service.MonitorDayRecord().InsertLastRecord(analyRecord as Model.MonitorDayRecordPure);
|
if (result)
|
{
|
result = new RabbitMqExChangeHelper().Push(ConfigHelper.RunExchangeName, new List<Model.MonitorBasicRecord>() { analyRecord });
|
if (!result)
|
{
|
LogHelper.Error($"常规日分析计划任务中,客户标识:{monitor.CorpID},测点标识:{monitor.ID} 数据分析记录推入运行监测通道失败!");
|
break;
|
}
|
LogHelper.Error($"常规日分析计划任务中,客户标识:{monitor.CorpID},测点标识:{monitor.ID} 数据分析完成!");
|
}
|
else
|
{
|
LogHelper.Error($"常规日分析计划任务中,客户标识:{monitor.CorpID},测点标识:{monitor.ID} 数据分析记录保存失败!");
|
break;
|
}
|
|
#endregion
|
}
|
|
#endregion
|
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error("常规日分析计划任务中,出错", ex);
|
var e = new JobExecutionException(ex);
|
throw e;
|
}
|
});
|
}
|
}
|
}
|