Shuxia Ning
2025-01-17 a0bce3b366451b3ca94e676eb98dd7b415375c14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using Yw.Basic;
using Yw.PhartRelation;
 
namespace IStation.Server
{
    /// <summary>
    /// 能效分析单任务
    /// </summary>
    [DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务
    public class EpEtaAnalySingleJob : IJob
    {
 
        internal const string Instance = "Instance";
 
        private Yw.Model.EtaAnalyConfigure _configure;
 
        /// <summary>
        /// 
        /// </summary>
        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<EpEtaMonitorAnalyContextItem>();
                        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<EpEtaEnginePumpAnalyContextItem>();
                        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<Yw.PhartRelation.eGraphType>() { 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;
                }
 
            });
 
 
        }
 
 
 
 
 
 
    }
}