ningshuxia
2022-10-08 afedb8fd4e17a5a911deee3dae04a10a93e6a39a
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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;
                }
 
            });
 
 
        }
 
 
 
 
 
 
    }
}