Shuxia Ning
2024-08-06 1d5b344c8be498c9989f3fe2e9846b1dcc919cb8
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using Quartz;
 
namespace IStation.Server
{
    /// <summary>
    /// 调度验证辅助类
    /// </summary>
    [DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务
    public class ScheduleValidationHelperJob : IJob
    {
        private static readonly Service.ScheduleRequest _service_schedule_request = new();
        private static readonly Service.ScheduleScada _service_schedule_scada = new();
        private static readonly Service.ScheduleRule _service_schedule_rule = new();
        private static readonly Service.ScheduleConclusion _service_schedule_conclusion = new();
        private static readonly Service.SchedulePump _service_schedule_pump = new();
 
        private static readonly Service.Station _service_station = new();
        private static readonly Service.ScheduleConfig _service_schedule_config = new();
 
 
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                var resuest_id = Yw.YitIdHelper.NextId();
                var resuest_time = DateTime.Now;
 
                try
                { 
                    var station_info = _service_station.Get();
                    if (station_info == null)
                    {
                        Log.Info(resuest_id, "调度验证计划任务中,Station文件缺失!");
                        return;
                    }
 
 
                    GlobalHelper.GetRealTimeZyScadaList(out List<Model.ZyScada> zy_scada_list);
                    if (zy_scada_list == null || !zy_scada_list.Any())
                    {
                        Log.Info(resuest_id, "调度验证计划任务中,接收Scada数据为空,自动跳过当前数据!");
                        return;
                    }
 
                    Log.Debug(resuest_id, Yw.JsonHelper.Object2Json(zy_scada_list));
 
                    var zy_scada_jd1_flow = zy_scada_list.Find(x => x.Code == GlobalHelper.嘉定1线_瞬时流量_长江管网图)?.Value ?? 0;
                    var zy_scada_jd2_flow = zy_scada_list.Find(x => x.Code == GlobalHelper.嘉定2线_瞬时流量_长江管网图)?.Value ?? 0;
                    var zy_scada_jd3_flow = zy_scada_list.Find(x => x.Code == GlobalHelper.嘉定3线_瞬时流量_长江管网图)?.Value ?? 0;
 
                    var zy_scada_jd1_pressure = zy_scada_list.Find(x => x.Code == GlobalHelper.嘉定1线_压力_长江管网图)?.Value ?? 0;
                    var zy_scada_jd2_pressure = zy_scada_list.Find(x => x.Code == GlobalHelper.嘉定2线_压力_长江管网图)?.Value ?? 0;
                    var zy_scada_jd3_pressure = zy_scada_list.Find(x => x.Code == GlobalHelper.嘉定3线_压力_长江管网图)?.Value ?? 0;
 
                    var zy_scada_dn2400_flow = zy_scada_list.Find(x => x.Code == GlobalHelper.DN2400总管_瞬时流量_长江管网图)?.Value ?? 0;
                    var zy_scada_dn2700_flow = zy_scada_list.Find(x => x.Code == GlobalHelper.DN2700总管_瞬时流量_长江管网图)?.Value ?? 0;
 
                    var zy_scada_dn2400_pressure = zy_scada_list.Find(x => x.Code == GlobalHelper.DN2400_出厂压力_长江管网图)?.Value ?? 0;
                    var zy_scada_dn2700_pressure = zy_scada_list.Find(x => x.Code == GlobalHelper.DN2700_出厂压力_长江管网图)?.Value ?? 0;
 
 
                    var pressure_list1 = new List<double?>() { zy_scada_jd1_pressure, zy_scada_jd2_pressure, zy_scada_jd3_pressure };
                    var pressure_list2 = new List<double?>() { zy_scada_dn2400_pressure, zy_scada_dn2700_pressure };
 
                    var target_flow1 = zy_scada_jd1_flow + zy_scada_jd2_flow + zy_scada_jd3_flow;
                    var target_pressure1 = (pressure_list1.Max() ?? 0) / 1000;
 
                    var target_flow2 = zy_scada_dn2400_flow + zy_scada_dn2700_flow;
                    var target_pressure2 = pressure_list2.Max() ?? 0;
                    Log.Info(resuest_id, $"target_flow1:{target_flow1},target_pressure1:{target_pressure1},target_flow2:{target_flow2},target_pressure2:{target_pressure2}");
 
 
                    var target_head1 = Curve.PumpCalculateHelper.Mpa2M(target_pressure1);
                    var target_head2 = Curve.PumpCalculateHelper.Mpa2M(target_pressure2);
 
 
                    var station1 = station_info.Station1;
                    var station2 = station_info.Station2;
                    var station1_same_type_flag_group_first = GlobalHelper.Station1SameTypeFlagGroupFirst;
                    var station2_same_type_flag_group_first = GlobalHelper.Station2SameTypeFlagGroupFirst;
                    var station1_schedule_config = _service_schedule_config.GetStation1();
                    var station2_schedule_config = _service_schedule_config.GetStation2();
                    var station1_flag_list = GlobalHelper.Station1FlagList;
                    var station2_flag_list = GlobalHelper.Station2FlagList;
 
                    GlobalHelper.GetStationOpenFlagList(zy_scada_list, out List<int> station1_open_flag_list, out List<int> station2_open_flag_list);
                    GlobalHelper.GetStationMaintenanceFlagList(zy_scada_list, out List<int> station1_maintenance_flag_list, out List<int> station2_maintenance_flag_list);
                    GlobalHelper.GetFlagInletWaterLevelDict(zy_scada_list, out Dictionary<int, double> station1_flag_inlet_water_level_dict, out Dictionary<int, double> station2_flag_inlet_water_level_dict);
 
                    if (station1_maintenance_flag_list != null && station1_maintenance_flag_list.Any())
                    {
                        Log.Info(resuest_id, $"调度验证计划任务中,1输水:{Yw.Untity.IntListHelper.ToString(station1_maintenance_flag_list)}");
                        station1_schedule_config.MustCloseFlagList.AddRange(station1_maintenance_flag_list);
                    }
 
                    if (station2_maintenance_flag_list != null && station2_maintenance_flag_list.Any())
                    {
                        Log.Info(resuest_id, $"调度验证计划任务中,2输水:{Yw.Untity.IntListHelper.ToString(station2_maintenance_flag_list)}");
                        station2_schedule_config.MustCloseFlagList.AddRange(station2_maintenance_flag_list);
                    }
 
                    var msg = "调度验证计划任务中,计算结束!";
                    var helper = new Algorithm.ScheduleHelper();
 
                    helper.Initial(station1_open_flag_list, station1_schedule_config);
                    var optimal_combine1 = helper.GetOptAnaCombine(station1, station1_same_type_flag_group_first, station1_flag_inlet_water_level_dict, target_flow1, target_head1);
 
 
                    helper.Initial(station2_open_flag_list, station2_schedule_config);
                    var optimal_combine2 = helper.GetOptAnaCombine(station2, station2_same_type_flag_group_first, station2_flag_inlet_water_level_dict, target_flow2, target_head2);
 
                    var schedule_request = new Model.ScheduleRequest
                    {
                        ID = resuest_id,
                        TargetFlow1 = target_flow1,
                        TargetPressure1 = target_pressure1,
                        TargetFlow2 = target_flow2,
                        TargetPressure2 = target_pressure2,
                        ScheduleStatus1 = optimal_combine1 != null,
                        ScheduleStatus2 = optimal_combine2 != null,
                        TotalTimeSpent = (DateTime.Now - resuest_time).TotalSeconds,
                        ReceptionTime = resuest_time
                    };
 
                    Model.ScheduleConclusion schedule_conclusion1 = null;
                    Model.ScheduleConclusion schedule_conclusion2 = null;
                    List<Model.SchedulePump> schedule_pump_list1 = null;
                    List<Model.SchedulePump> schedule_pump_list2 = null;
 
                    if (optimal_combine1 == null && optimal_combine2 == null)
                    {
                        Log.Info(resuest_id, "调度验证计划任务中,厂内调度方案无法计算!");
                    }
                    else
                    {
                        if (optimal_combine1 != null)
                        {
                            optimal_combine1.Round();
                            schedule_conclusion1 = new Model.ScheduleConclusion
                            {
                                ID = Yw.YitIdHelper.NextId(),
                                RequestID = resuest_id,
                                Station = IStation.eDockingStation.Ch1s,
                                TotalFlow = optimal_combine1.TotalFlow,
                                TotalHead = optimal_combine1.TotalHead,
                                TotalPower = optimal_combine1.TotalPower,
                                TotalEfficiency = optimal_combine1.TotalEfficiency,
                                WP = optimal_combine1.WP,
                                UWP = optimal_combine1.UWP,
                                Flags = Yw.Untity.IntListHelper.ToString(optimal_combine1.Flags),
                                MeritRatio = optimal_combine1.MeritRatio
                            };
                            schedule_pump_list1 = new List<Model.SchedulePump>();
                            foreach (var fre_pump in optimal_combine1.AnaFrePumps)
                            {
                                var schedule_pump = new Model.SchedulePump
                                {
                                    RequestID = resuest_id,
                                    Station = IStation.eDockingStation.Ch1s,
                                    Flag = fre_pump.Flag,
                                    Flow = fre_pump.Flow,
                                    Head = fre_pump.Head,
                                    Power = fre_pump.Power,
                                    Efficiency = fre_pump.Efficiency,
                                    Frequency = fre_pump.Frequency,
                                    Speed = fre_pump.Speed
                                };
                                schedule_pump_list1.Add(schedule_pump);
 
                            }
 
                        }
                        else
                        {
                            msg += $"  1输水调度计算失败,无法满足目标流量:{target_flow1},目标压力:{target_pressure1}!";
                        }
 
                        if (optimal_combine2 != null)
                        {
                            optimal_combine2.Round();
                            schedule_conclusion2 = new Model.ScheduleConclusion
                            {
                                ID = Yw.YitIdHelper.NextId(),
                                RequestID = resuest_id,
                                Station = IStation.eDockingStation.Ch2s,
                                TotalFlow = optimal_combine2.TotalFlow,
                                TotalHead = optimal_combine2.TotalHead,
                                TotalPower = optimal_combine2.TotalPower,
                                TotalEfficiency = optimal_combine2.TotalEfficiency,
                                WP = optimal_combine2.WP,
                                UWP = optimal_combine2.UWP,
                                Flags = Yw.Untity.IntListHelper.ToString(optimal_combine2.Flags),
                                MeritRatio = optimal_combine2.MeritRatio
                            };
                            schedule_pump_list2 = new List<Model.SchedulePump>();
 
                            foreach (var fre_pump in optimal_combine2.AnaFrePumps)
                            {
 
                                var flag = fre_pump.Flag;
                                var schedule_pump = new Model.SchedulePump
                                {
                                    RequestID = resuest_id,
                                    Station = IStation.eDockingStation.Ch2s,
                                    Flag = flag,
                                    Flow = fre_pump.Flow,
                                    Head = fre_pump.Head,
                                    Power = fre_pump.Power,
                                    Efficiency = fre_pump.Efficiency,
                                    Frequency = fre_pump.Frequency,
                                    Speed = fre_pump.Speed
                                };
                                schedule_pump_list2.Add(schedule_pump);
                            }
 
                        }
                        else
                        {
                            msg += $"  2输水调度计算失败,无法满足目标流量:{target_flow2},目标压力:{target_pressure2}!";
                        }
                    }
 
                    Log.Info(resuest_id, msg);
 
                    var bol = false;
                    bol = _service_schedule_request.Insert(schedule_request) > 0;
                    if (!bol)
                    {
                        Log.Info(resuest_id, "调度验证计划任务中,schedule_request 插入异常");
                        Log.Debug(resuest_id, Yw.JsonHelper.Object2Json(schedule_request));
                    }
 
                    if (station1_schedule_config != null)
                    {
                        var schedule_rule1 = new Model.ScheduleRule
                        {
                            RequestID = resuest_id,
                            Station = eDockingStation.Ch1s,
                            Config = Yw.JsonHelper.Object2Json(station1_schedule_config)
                        };
                        bol = _service_schedule_rule.Insert(schedule_rule1) > 0;
                        if (!bol)
                        {
                            Log.Info(resuest_id, "调度验证计划任务中,schedule_config1 插入异常");
                            Log.Debug(resuest_id, Yw.JsonHelper.Object2Json(schedule_rule1));
                        }
                    }
 
                    if (station2_schedule_config != null)
                    {
                        var schedule_rule2 = new Model.ScheduleRule
                        {
                            RequestID = resuest_id,
                            Station = eDockingStation.Ch2s,
                            Config = Yw.JsonHelper.Object2Json(station2_schedule_config)
                        };
                        bol = _service_schedule_rule.Insert(schedule_rule2) > 0;
                        if (!bol)
                        {
                            Log.Info(resuest_id, "调度验证计划任务中,schedule_config2 插入异常");
                            Log.Debug(resuest_id, Yw.JsonHelper.Object2Json(schedule_rule2));
                        }
                    }
 
                    if (zy_scada_list != null && zy_scada_list.Any())
                    {
                        var scada_list = zy_scada_list
                            .OrderBy(x => x.Code)
                            .Select(x => new Model.ScheduleScada()
                            {
                                RequestID = resuest_id,
                                Time = x.Time,
                                Code = x.Code,
                                Tag = x.Tag,
                                Value = x.Value,
 
                            }).ToList();
                        bol = _service_schedule_scada.Inserts(scada_list);
                        if (!bol)
                        {
                            Log.Info(resuest_id, "调度验证计划任务中,scada_list 插入异常");
                            Log.Debug(resuest_id, Yw.JsonHelper.Object2Json(scada_list));
                        }
                    }
                    if (schedule_conclusion1 != null)
                    {
                        bol = _service_schedule_conclusion.Insert(schedule_conclusion1) > 0;
                        if (!bol)
                        {
                            Log.Info(resuest_id, "调度验证计划任务中,schedule_conclusion1 插入异常");
                            Log.Debug(resuest_id, Yw.JsonHelper.Object2Json(schedule_conclusion1));
                        }
                    }
                    if (schedule_conclusion2 != null)
                    {
                        bol = _service_schedule_conclusion.Insert(schedule_conclusion2) > 0;
                        if (!bol)
                        {
                            Log.Info(resuest_id, "调度验证计划任务中,schedule_conclusion2 插入异常");
                            Log.Debug(resuest_id, Yw.JsonHelper.Object2Json(schedule_conclusion2));
                        }
                    }
 
                    if (schedule_pump_list1 != null)
                    {
                        bol = _service_schedule_pump.Inserts(schedule_pump_list1);
                        if (!bol)
                        {
                            Log.Info(resuest_id, "调度验证计划任务中,schedule_pump_list1 插入异常");
                            Log.Debug(resuest_id, Yw.JsonHelper.Object2Json(schedule_pump_list1));
                        }
                    }
 
                    if (schedule_pump_list2 != null)
                    {
                        bol = _service_schedule_pump.Inserts(schedule_pump_list2);
                        if (!bol)
                        {
                            Log.Info(resuest_id, "调度验证计划任务中,schedule_pump_list2 插入异常");
                            Log.Debug(resuest_id, Yw.JsonHelper.Object2Json(schedule_pump_list2));
                        }
                    }
 
                    Log.Info(resuest_id, $"调度验证计划任务中,调度验证,[{resuest_time:G}]验证成功!");
 
                }
                catch (Exception ex)
                {
                    Log.Error(resuest_id, "调度验证计划任务中,出错", ex);
                    var e = new JobExecutionException(ex);
                    throw e;
                }
 
            });
        }
 
 
 
    }
}