namespace IStation.Server { /// /// 能效分析单任务 /// [DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务 public class EMWSEtaAnalySingleJob : 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 EMWSEtaBaseAnalyContext 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 EMWSEtaCustomAnalyContext(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 EMWSEtaMonitorAnalyContextItem(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 EMWSEtaSignalAnalyContextItem(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 allWorkList = new Yw.Service.EMWSWorkSite().GetByBelongTypeAndBelongID(configure.ObjectType, configure.ObjectID); if (allWorkList == null || allWorkList.Count < 1) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},未检索到工位信息"); return; } var allWorkContextTreeList = new List(); var allWorkContextItemList = new List(); foreach (var work in allWorkList) { var workContextItem = new EMWSEtaWorkAnalyContextItem(work); allWorkContextItemList.Add(workContextItem); var parentWorkContextItem = allWorkContextItemList.Find(x => x.ID == workContextItem.ParentID); if (parentWorkContextItem == null) { allWorkContextTreeList.Add(workContextItem); } else { parentWorkContextItem.Children.Add(workContextItem); } } analyCusContext.WorkContextTreeList = allWorkContextTreeList; analyCusContext.WorkContextTreeItemList = allWorkContextItemList; var catalogList = new Yw.Service.SysCatalog().GetChildAndSelfByCode(Yw.Assets.Catalog.Pump); if (catalogList == null || catalogList.Count < 1) { LogHelper.Info($"能效分析单任务中,数据类型:{configure.ObjectType},数据id:{configure.ObjectID},为检索到泵分类信息"); return; } var catalogIds = catalogList.Select(x => x.ID).Distinct().ToList(); var serviceWorkMapping = new Yw.Service.EMWSWorkMapping(); var serviceWorkBinding = new Yw.Service.EMWSWorkBinding(); var serviceEquipment = new Yw.Service.Equipment(); var servicePhartMapping = new Yw.Service.EquipmentPhartMapping(); var serviceEquipmentMapping = new Yw.Service.EquipmentMonitorMapping(); foreach (var workContextItem in allWorkContextItemList) { var workMonitorMappingList = serviceWorkMapping.GetByWorkIDAndMappingType(workContextItem.ID, Yw.Monitor.DataType.MonitorPoint); if (workMonitorMappingList != null && workMonitorMappingList.Count > 0) { var workMonitorIds = workMonitorMappingList.Select(x => x.MappingID).ToList(); workContextItem.MonitorContextItemList = allMonitorContextItemList.Where(x => workMonitorIds.Contains(x.ID)).ToList(); } var workBinding = serviceWorkBinding.GetValidByWorkID(workContextItem.ID); if (workBinding != null && workBinding.BindingType == Yw.Assets.DataType.Equipment) { var equipmentExList = serviceEquipment.GetChildAndSelfExPropListByID(workBinding.BindingID); if (equipmentExList != null && equipmentExList.Count > 0) { var pumpEx = equipmentExList.Find(x => catalogIds.Contains(x.CatalogID)); if (pumpEx != null) { var graphList = servicePhartMapping.GetTupleDefaultByEquipmentID(pumpEx.ID)?.Item3; var pumpContextItem = new EMWSEtaPumpAnalyContextItem(pumpEx, graphList); var equipmentMappingList = serviceEquipmentMapping.GetByEquipmentIds(equipmentExList.Select(x => x.ID).Distinct().ToList()); if (equipmentMappingList != null && equipmentMappingList.Count > 0) { var equipmentMonitorIds = equipmentMappingList.Select(x => x.MonitorPointID).Distinct().ToList(); pumpContextItem.MonitorContextItemList = allMonitorContextItemList.Where(x => equipmentMonitorIds.Contains(x.ID)).ToList(); } workContextItem.PumpContextItem = pumpContextItem; } } } } #endregion analyContext = analyCusContext; } #endregion #region 分析与保存 if (analyContext == null) { return; } var analyzer = EMWSEtaAnalyzerFactory.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; } }); } } }