using Yw.Basic; using Yw.PhartRelation; namespace IStation.Server { /// /// 能效分析单任务 /// [DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务 public class EpEtaAnalySingleJob : IJob { internal const string Instance = "Instance"; private Yw.Model.EtaAnalyConfigure _configure; /// /// /// public Task Execute(IJobExecutionContext context) { return Task.Run(() => { try { #region 任务数据 var dataMap = context.MergedJobDataMap; var jobModel = (Yw.Model.EtaAnalyConfigure)dataMap[Instance]; if (jobModel == null) { LogHelper.Info($"能效分析单任务中,任务数据传输失败!"); return; } var configure = jobModel; _configure = configure; #endregion EpEtaBaseAnalyContext analyContext = null; #region 自定义分析 if (configure.AnalyWay == Yw.Eta.eAnalyWay.Cus) { #region 基础验证 if (configure.ObjectType != IStation.DataType.LogicSite) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},分析方式配置错误:{configure.AnalyWay}"); return; } var logicSite = new Service.LogicSite().GetByID(configure.ObjectID); if (logicSite == null) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},未检索到站点信息"); return; } #endregion var analyCusContext = new EpEtaCustomAnalyContext(configure, logicSite); #region 测点验证 var allMonitorList = new Yw.Service.MonitorPoint().GetExSignalWithSignalTypeByBelongTypeAndBelongID(configure.ObjectType, configure.ObjectID); if (allMonitorList == null || allMonitorList.Count < 1) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},未检索到测点信息"); return; } allMonitorList = allMonitorList.Where(x => x.UseStatus == Yw.Model.eUseStatus.Enable).ToList(); if (allMonitorList.Count < 1) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},未检索到有效测点信息"); return; } allMonitorList = allMonitorList.Where(x => x.CronType == Yw.Monitor.eCronType.Real).ToList(); if (allMonitorList.Count < 1) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},未检索到有效实时测点信息"); return; } allMonitorList = allMonitorList.Where(x => x.SignalList.Exists(t => t.SignalType.FormatType == Yw.Monitor.eFormatType.Numeric || t.SignalType.FormatType == Yw.Monitor.eFormatType.Enum)).ToList(); if (allMonitorList.Count < 1) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},未检索到有效数据或枚举型测点信息"); return; } var allMonitorContextItemList = new List(); var serviceMonitorRecord = new Yw.Service.MonitorRealRecord(); foreach (var monitor in allMonitorList) { var monitorContextItem = new EpEtaMonitorAnalyContextItem(monitor); var signalList = monitor.SignalList.Where(t => t.SignalType.FormatType == Yw.Monitor.eFormatType.Numeric || t.SignalType.FormatType == Yw.Monitor.eFormatType.Enum).ToList(); foreach (var signal in signalList) { var signalContextItem = new EpEtaSignalAnalyContextItem(signal); var lastSignalRecord = serviceMonitorRecord.GetLastRecord(signal.ID); if (lastSignalRecord != null && Yw.Monitor.DataStatus.IsNormal(lastSignalRecord.DataStatus)) { signalContextItem.DataValue = double.Parse(lastSignalRecord.DataValue); monitorContextItem.SignalContextItemList.Add(signalContextItem); } } if (monitorContextItem.SignalContextItemList.Count > 0) { allMonitorContextItemList.Add(monitorContextItem); } } analyCusContext.MonitorContextItemList = allMonitorContextItemList; #endregion #region 机组验证 var serviceEquipment = new Yw.Service.Equipment(); var allEquipmentList = new Yw.Service.Equipment().GetByBelongTypeAndBelongID(configure.ObjectType, configure.ObjectID); if (allEquipmentList == null || allEquipmentList.Count < 1) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},未检索到设备信息"); return; } var allEnginePumpList = allEquipmentList.Where(x => x.Flags.Contains(Yw.Assets.Flags.机组)).OrderBy(x => x.SortCode).ToList(); if (allEnginePumpList == null || allEnginePumpList.Count < 1) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},未检索到机组信息"); return; } var serviceMonitorMapping = new Yw.Service.EquipmentMonitorMapping(); var servicePhartMapping = new Yw.Service.EquipmentPhartMapping(); var allEpContextItemList = new List(); foreach (var enginePump in allEnginePumpList) { var enginePumpContextItem = new EpEtaEnginePumpAnalyContextItem(enginePump); var allChildEquipmentList = serviceEquipment.GetChildAndSelfExPropListByID(enginePump.ID); var allChildEquipmentIds = allChildEquipmentList.Select(x => x.ID).Distinct().ToList(); var allMonitorMappingList = serviceMonitorMapping.GetByEquipmentIds(allChildEquipmentIds); if (allMonitorMappingList != null && allMonitorMappingList.Count > 0) { var allMonitorIds = allMonitorMappingList.Select(x => x.MonitorPointID).Distinct().ToList(); enginePumpContextItem.MonitorContextItemList = allMonitorContextItemList.Where(x => allMonitorIds.Contains(x.ID)).ToList(); } var pump = allChildEquipmentList.Where(x => x.Flags.Contains(Yw.Assets.Flags.水泵)).FirstOrDefault(); if (pump != null) { enginePumpContextItem.PumpContextItem = new EpEtaPumpAnalyContextItem(pump); var diagram = servicePhartMapping.GetTupleDefaultByEquipmentID (pump.ID, Yw.PhartRelation.eDiagramType.Feat, new List() { eGraphType.QH, eGraphType.QP, eGraphType.QE }); if (diagram != null) { enginePumpContextItem.PumpContextItem.DiagramContextItem = new EpEtaDiagramAnalyContextItem(diagram.Item2, diagram.Item3); } } allEpContextItemList.Add(enginePumpContextItem); } analyCusContext.EnginePumpContextItemList = allEpContextItemList; #endregion analyContext = analyCusContext; } #endregion #region 分析与保存 if (analyContext == null) { return; } var analyzer = EpEtaAnalyzerFactory.CreateAnalyzer(configure); if (analyzer == null) { return; } analyzer.Analy( analyContext, (etaRecordList) => { var etaLogicRecordList = etaRecordList?.Where(x => x is Yw.Model.EtaLogicRealRecord).Select(x => x as Yw.Model.EtaLogicRealRecord).ToList(); if (etaLogicRecordList != null && etaLogicRecordList.Count > 0) { var bol = new Yw.Service.EtaLogicRealRecord().InsertsLastRecord(etaLogicRecordList); if (!bol) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},业务能效记录保存失败"); } } var etaMultiRecordList = etaRecordList?.Where(x => x is Yw.Model.EtaMultiRealRecord).Select(x => x as Yw.Model.EtaMultiRealRecord).ToList(); if (etaMultiRecordList != null && etaMultiRecordList.Count > 0) { var bol = new Yw.Service.EtaMultiRealRecord().InsertsLastRecord(etaMultiRecordList); if (!bol) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},(多)能效记录保存失败"); } } var etaSingleRecordList = etaRecordList?.Where(x => x is Yw.Model.EtaSingleRealRecord).Select(x => x as Yw.Model.EtaSingleRealRecord).ToList(); if (etaSingleRecordList != null && etaSingleRecordList.Count > 0) { var bol = new Yw.Service.EtaSingleRealRecord().InsertsLastRecord(etaSingleRecordList); if (!bol) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},(单)能效记录保存失败"); } } LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},能效分析完成"); }, (monitorRecordList) => { var bol = new Yw.Service.MonitorRealRecord().InsertsLastRecord(monitorRecordList); if (!bol) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},定制测点记录保存失败!"); } else { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},定制测点记录保存成功!"); } } ); #endregion } catch (Exception ex) { LogHelper.Error($"能效分析单任务中,数据类型:{_configure.ObjectType} 数据id:{_configure.ObjectID}, 执行出错", ex); var e = new JobExecutionException(ex); throw e; } }); } } }