ningshuxia
2025-04-16 ed113213fc94c3d9886ea08dfddd09d08d9ba7d5
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
using DevExpress.XtraEditors;
using IStation.Epanet;
using IStation.Epanet.Enums;
using IStation.Model;
using IStation.WinFrmUI;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime;
using System.Text;
using static IStation.WinFrmUI.Monitor.ModelCorrectionCombinePage;
 
namespace IStation.Test
    public class Station1CombineHelper
    {
        private static string _model_file = System.IO.Path.Combine
        (SettingsD.File.RootDirectory, SettingsD.File.DataFolder, "ch2_v3_20240801(Clear).inp");
 
 
        static double _min_flow = 0;
        static double _max_flow = 100000;
        static double _space_flow = 500;
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public static void Start()
        {
 
            var projectId = 661070185922629;
            IStation.SettingsD.Project.ID = projectId;
            var monitorDataSourcesId = 663976941449285;//2024-202504
            var stationIndex = 1;
            var stationId = 462958406303813;
            var dtStart = new DateTime(2025, 4, 1);
            var dtEnd = new DateTime(2025, 5, 1);
 
            var bll = new BLL.StationSignalRecordPacket();
            var bllEquipment = new BLL.Equipment();
            var bllCurve = new BLL.PumpCurve();
 
 
            var pumpList = bllEquipment.GetPumpListByBelongTypeAndBelongID(IStation.ObjectType.Station, stationId);
            var flagList = pumpList.Select(x => x.SortCode).OrderBy(x => x).ToList();
            var flagPumpDict = pumpList.ToDictionary(x => x.SortCode, x => x.RatedParas);
 
            var flagQhCurveDict = GetFlagCurveDict();
            var flagHzQhCurveDict = GetFlagHzCurveDict();
 
            var pipe_flow_id_list = GlobalHelperW.GetPipeFlowIdList(stationIndex);
            var pipe_pressure_id_list = GlobalHelperW.GetPipePressureIdList(stationIndex);
 
            //var packet_list = bll.Get(monitorDataSourcesId, stationId);
            //var allStationRecordList = packet_list.SelectMany(x => x.StationSignalRecords).ToList();
            var packet_list = bll.Get(monitorDataSourcesId, stationId,2025,2);
            var allStationRecordList = packet_list.StationSignalRecords.Where(x=>x.Time.Day==11).ToList();
 
            var temp_id_build = new StringBuilder(31);
            var err = EpanetMethods.ENopen(_model_file, "", "");
            if ((int)err > 6)
            {
                throw new Exception($"ENopen:{err}");
            }
 
            err = EpanetMethods.ENopenH();
            if ((int)err > 6)
            {
                throw new Exception($"ENopenH:{err}");
            }
 
            var curve_point_count = 100;
            var flag_qh_curve_dict = GetFlagCurveDict(); 
 
            var pump_model_mapping_list = AnalysisHelper.GetPumpModelMappingList();
            var model_mapping_list = AnalysisHelper.GetStationMappingList();
 
            foreach (var item in pump_model_mapping_list)
            {
                var flag = item.Flag;
 
                if (EpanetMethods.ENgetlinkindex(item.PumpId, out int pump_index) != ErrorCode.Ok)
                    throw new Exception($"ENgetlinkindex:{err}");
 
                if (!string.IsNullOrEmpty(item.FlowId))
                {
                    if (EpanetMethods.ENgetlinkindex(item.FlowId, out int flow_index) != ErrorCode.Ok)
                        throw new Exception($"ENgetlinkindex:{err}");
                    item.FlowIndex = flow_index;
                }
 
                if (EpanetMethods.ENgetnodeindex(item.PressureId, out int pressure_index) != ErrorCode.Ok)
                    throw new Exception($"ENgetnodeindex:{err}");
 
                if (EpanetMethods.ENgetcurveindex(item.CurveId, out int curve_index) != ErrorCode.Ok)
                    throw new Exception($"ENgetcurveindex:{err}");
 
                item.PumpIndex = pump_index;
                item.PressureIndex = pressure_index;
                item.CurveIndex = curve_index;
 
 
                var qh = flag_qh_curve_dict[flag];
                var fit_point_list = qh.GetFitPoints(curve_point_count);
                var flow_list = fit_point_list.Select(x => (float)x.X).ToArray();
                var head_list = fit_point_list.Select(x => (float)x.Y).ToArray();
 
 
                var cek = CheckFitPointList(fit_point_list, curve_point_count);
                if (!cek)
                {
                    throw new Exception($"CheckFitPointList: false");
                }
                err = EpanetMethods.ENsetcurve(item.CurveIndex, flow_list, head_list, curve_point_count);
                if (err != 0)
                {
                    throw new Exception($"ENsetcurve:{err}");
                }
            }
 
            foreach (var item in model_mapping_list)
            {
 
                if (EpanetMethods.ENgetlinkindex(item.FlowId, out int flow_index) != ErrorCode.Ok)
                    throw new Exception($"ENgetlinkindex:{err}");
 
                if (EpanetMethods.ENgetnodeindex(item.PressureId, out int pressure_index) != ErrorCode.Ok)
                    throw new Exception($"ENgetnodeindex:{err}");
 
                item.PressureIndex = pressure_index;
                item.FlowIndex = flow_index;
 
            }
 
            var pump_model_mapping_dict = pump_model_mapping_list.ToDictionary(x => x.Flag, y => y);
            var pattern_id_list = AnalysisHelper.GetPatternIdList();
            var pattern_id_dict = new Dictionary<string, int>();
            foreach (var id in pattern_id_list)
            {
                if (EpanetMethods.ENgetpatternindex(id, out int index) != ErrorCode.Ok)
                    continue;
                pattern_id_dict.Add(id, index);
            }
 
             
            var vm_combine_list = new List<ModelCombineViewModel>();
            var vm_diff_list = new List<ModelDiffViewModel>();
            foreach (var station_record in allStationRecordList)
            {
                var model_record_dict = station_record.ModelRecordDict;
                var pipe_flow_err = true;
                var pipe_pressure_err = true;
                foreach (var id in pipe_flow_id_list)
                {
                    if (model_record_dict[id] < 1)
                    {
                        pipe_flow_err = false;
                        break;
                    }
                }
                foreach (var id in pipe_pressure_id_list)
                {
                    var value = model_record_dict[id];
                    if (value < 1 || value > 40)
                    {
                        pipe_pressure_err = false;
                        break;
                    }
                }
                if (!pipe_flow_err || !pipe_pressure_err)
                {
                    continue;
                }
 
 
                var time = station_record.Time.ToString("G");
                var pump_record_list = station_record.PumpSignalRecords;
                var total_flow = station_record.TotalFlow;
                var total_pressure = station_record.TotalPressure;
                var pump_total_flow = pump_record_list.Sum(x => x.FlowRate);
                var diff_flow = station_record.DiffFlow;
                //if (total_pressure < 20|| total_pressure>60)
                //{
                //    continue;
                //}
                if (total_pressure < 0)
                {
                    continue;
                }
                if (total_flow <= 0 && pump_total_flow <= 0)
                {
                    continue;
                }
                if (pump_total_flow > 0 && Math.Abs(diff_flow) > 2000)
                {
                    continue;
                }
                //if (station_record.Time.Month==4&& station_record.Time.Day==17)
                //{
                //    continue;
                //}
 
 
                //if (station_record.Time > new DateTime(2024,4,29)&& station_record.Time < new DateTime(2024, 5, 15))
                //{
                //    continue;
                //}
 
                var pump_run_flags = pump_record_list.Select(x => x.Flag).OrderBy(x => x).ToList();
                if (pump_record_list.Exists(x => x.Flag != 15 && x.Flag != 16 && x.Rpm < 1))
                {
                    continue;
                }
 
                //if (pump_record_list.Exists(x => x.FlowRate < 1))
                //{
                //    continue;
                //}
 
                if (pump_record_list.Exists(x => x.OutletPressure > 50))
                {
                    continue;
                }
 
                if (flagHzQhCurveDict.Any())
                {
                    var error = false;
                    foreach (var pump_record in pump_record_list)
                    {
                        var flag = pump_record.Flag;
                        var pump = flagPumpDict[flag];
                        var mapping = pump_model_mapping_dict[flag];
                        var rpm_ratio = pump_record.Rpm / pump.Nr;
                        if (!pump.IsBp)
                            rpm_ratio = 1;
                        if (rpm_ratio <= 0.1)
                            continue;
 
                        int hz0 = (int)Math.Round(rpm_ratio * 50, 0);
                        if (hz0 > 50)
                            hz0 = 50;
                        if (hz0<25)
                        {
                            error = true;
                            break;
                        }
 
                        var updateCurvePtList = flagHzQhCurveDict[flag][hz0];
                        if (updateCurvePtList == null)
                        {
                            throw new Exception($"ana_factor:is null");
                        }
 
                        var x_list = updateCurvePtList.Select(x => (float)x.X).ToArray();
                        var y_list = updateCurvePtList.Select(x => (float)x.Y).ToArray();
                        err = EpanetMethods.ENsetcurve(mapping.CurveIndex, x_list, y_list, curve_point_count);
                        if ((int)err > 6)
                        {
                            throw new Exception($"ENsetcurve:{err}");
                        }
                    }
                    if (error)
                    {
                        continue;
                    }
                }
 
                foreach (var pattern in pattern_id_dict)
                {
                    var pattern_id = pattern.Key;
                    var pattern_index = pattern.Value;
                    var pattern_value = 0f;
                    if (model_record_dict.ContainsKey(pattern_id))
                    {
                        pattern_value = (float)model_record_dict[pattern_id];
                    }
                    err = EpanetMethods.ENsetpattern(pattern_index, new float[] { pattern_value }, 1);
                    if ((int)err > 6)
                    {
                        throw new Exception($"ENsetpattern:{err}");
                    }
                }
 
                err = EpanetMethods.ENinitH(0);
                if ((int)err > 6)
                {
                    throw new Exception($"ENinitH:{err}");
                }
 
                err = EpanetMethods.ENrunH(out _);
                if ((int)err > 6)
                {
                    throw new Exception($"ENrunH:{err}");
                }
                var flags = pump_record_list.Select(x => x.Flag).OrderBy(x => x).ToList();
 
                var vm_combine = new ModelCombineViewModel();
                vm_combine.Time = time;
                vm_combine.RunFlags = IStation.Untity.IntListHelper.ToString(flags);
                vm_combine.ModelDeviation = new List<Tuple<int, double, double>>();
                vm_combine_list.Add(vm_combine);
 
                foreach (var pump_record in pump_record_list)
                {
                    var flag = pump_record.Flag;
                    var pump = flagPumpDict[flag];
                    var qh = flag_qh_curve_dict[flag];
                    var mapping = pump_model_mapping_dict[flag];
                    var rpm = pump_record.Rpm;
                    if (!pump.IsBp)
                        rpm = pump.Nr;
                    if (rpm == 0)
                        break;
 
                    var hz = Math.Round(rpm / pump.Nr * 50, 2);
                    if (hz > 50)
                        hz = 50;
                    var hz0 = Math.Round(hz, 0);
                    var wl = pump_record.WaterLevel;
                    var inlet_pressure = Model.CurveCalcuHelper.Mpa2M(pump_record.InletPressure);
                    var outlet_pressure = Model.CurveCalcuHelper.Mpa2M(pump_record.OutletPressure);
                    var pressure_diff = outlet_pressure - inlet_pressure;
                    var flow = pump_record.FlowRate;
                    var curve_head = pump_record.Head;
 
 
                    err = EpanetMethods.ENgetlinkvalue(mapping.PumpIndex, LinkValue.Flow, out float model_flow);
                    if ((int)err > 6)
                    {
                        throw new Exception($"ENgetnodevalue:{err}");
                    }
 
                    err = EpanetMethods.ENgetnodevalue(mapping.PressureIndex, NodeValue.Pressure, out float model_outlet_pressure);
                    if ((int)err > 6)
                    {
                        throw new Exception($"ENgetnodevalue:{err}");
                    }
 
                    model_flow = Math.Abs(model_flow);
                    model_outlet_pressure = Math.Abs(model_outlet_pressure);
                    var model_pressure_diff = model_outlet_pressure - inlet_pressure;
 
                    var flow_diff = flow - model_flow;
                    var pressure_diff_dev = pressure_diff - model_pressure_diff;
                     
 
                    var calc_flow = flow > 0 ? flow : model_flow;
                    var dif_flow = flow - model_flow;
 
                    vm_combine.ScadaTotalFlow += flow < 1 ? model_flow : flow;
                    vm_combine.ModelTotalFlow += model_flow;
                    vm_combine.ModelDeviation.Add(new Tuple<int, double, double>(flag, calc_flow, pressure_diff_dev));
                }
 
 
                foreach (var mapping in model_mapping_list)
                {
                    if (!model_record_dict.ContainsKey(mapping.ScadaPressureId))
                    {
                        continue;
                    }
                    if (!model_record_dict.ContainsKey(mapping.ScadaFlowId))
                    {
                        continue;
                    }
                    var flow = model_record_dict[mapping.ScadaFlowId];
                    var outlet_pressure = model_record_dict[mapping.ScadaPressureId];
 
                    err = EpanetMethods.ENgetlinkvalue(mapping.FlowIndex, LinkValue.Flow, out float model_flow);
                    if ((int)err > 6)
                    {
                        throw new Exception($"ENgetnodevalue:{err}");
                    }
 
                    err = EpanetMethods.ENgetnodevalue(mapping.PressureIndex, NodeValue.Pressure, out float model_outlet_pressure);
                    if ((int)err > 6)
                    {
                        throw new Exception($"ENgetnodevalue:{err}");
                    }
 
                    var flow_diff = flow - model_flow;
                    var pressure_diff = outlet_pressure - model_outlet_pressure;
 
                    var vm = new ModelDiffViewModel();
                    vm.Date = station_record.Time;
                    vm.Time = time;
                    vm.Name = mapping.Name;
                    vm.ScadaFlow = Math.Round(flow, 1);
                    vm.ScadaPressure = Math.Round(outlet_pressure, 3);
                    vm.MonitorFlow = Math.Round(model_flow, 1);
                    vm.MonitorPressure = Math.Round(model_outlet_pressure, 3);
                    vm.FlowDiff = Math.Round(flow_diff, 1);
                    vm.PressureDiff = Math.Round(pressure_diff, 3);
                    vm_diff_list.Add(vm);
                }
            }
 
            err = EpanetMethods.ENcloseH();
            if ((int)err > 6)
            {
                throw new Exception($"ENcloseH:{err}");
            }
            err = EpanetMethods.ENclose();
            if ((int)err > 6)
            {
                throw new Exception($"ENclose:{err}");
            }
 
            if (!vm_combine_list.Any())
            { 
                return;
            }
            var run_flags_flow_pressure_diff_dict = vm_combine_list
             .Select(x => new Tuple<string, double, List<Tuple<int, double, double>>>(x.RunFlags, x.ScadaTotalFlow, x.ModelDeviation))
             .ToList();
 
 
            var ana_dev_list = GetAnalysisDeviationDtoList(flagPumpDict, run_flags_flow_pressure_diff_dict, _min_flow, _max_flow, _space_flow);
 
            SaveAnalysisDeviationDtoList(stationIndex,ana_dev_list);
        }
 
        public static List<AnalysisHelper.AnalysisDeviationDto> GetAnalysisDeviationDtoList(Dictionary<int, Model.Pump> flag_pump_dict, List<Tuple<string, double, List<Tuple<int, double, double>>>> run_flags_flow_pressure_diff_dict, double minQ = 0, double maxQ = 100000, double spaceQ = 1000)
        {
            // 检查输入参数是否为空
            if (flag_pump_dict == null || !flag_pump_dict.Any() || run_flags_flow_pressure_diff_dict == null || !run_flags_flow_pressure_diff_dict.Any())
            {
                return new List<AnalysisHelper.AnalysisDeviationDto>();
            }
 
            var resultList = new List<AnalysisHelper.AnalysisDeviationDto>();
 
            // 遍历流量区间
            for (double currentMinQ = minQ; currentMinQ <= maxQ; currentMinQ += spaceQ)
            {
                double currentMaxQ = currentMinQ + spaceQ;
                var validItems = run_flags_flow_pressure_diff_dict
                   .Where(x => x.Item2 >= currentMinQ && x.Item2 <= currentMaxQ)
                   .ToList();
 
                // 如果当前区间没有有效数据,跳过本次循环
                if (!validItems.Any())
                {
                    continue;
                }
 
                // 按运行标志分组
                var groupedByRunFlags = validItems.GroupBy(x => x.Item1);
                foreach (var runFlagsGroup in groupedByRunFlags)
                {
                    var analysisDeviationDto = CreateAnalysisDeviationDto(runFlagsGroup, currentMinQ, currentMaxQ);
                    if (IsValidDeviation(analysisDeviationDto.PressureDiff))
                    {
                        resultList.Add(analysisDeviationDto);
                    }
                }
            }
 
            return resultList;
        }
 
        private static AnalysisHelper.AnalysisDeviationDto CreateAnalysisDeviationDto(IGrouping<string, Tuple<string, double, List<Tuple<int, double, double>>>> runFlagsGroup, double currentMinQ, double currentMaxQ)
        {
            var vm = new AnalysisHelper.AnalysisDeviationDto
            {
                RunFlags = IStation.Untity.IntListHelper.ToList(runFlagsGroup.Key),
                MinFlow = currentMinQ,
                MaxFlow = currentMaxQ
            };
 
            var devDict = new Dictionary<int, double>();
            var allTuples = runFlagsGroup.SelectMany(x => x.Item3).ToList();
            var groupedByFlag = allTuples.GroupBy(x => x.Item1);
 
            foreach (var flagGroup in groupedByFlag)
            {
                var headDiffArray = flagGroup.Select(x => x.Item3).ToArray();
                var (filterStdDevPopArray, stdDevPop) = AnalysisHelper.GetFilterBySTDP(headDiffArray);
                var stdDevPopHeadAvg = filterStdDevPopArray.Average();
                var headDiffAvg = Math.Round(stdDevPopHeadAvg, 5);
                devDict.Add(flagGroup.Key, headDiffAvg);
            }
 
            vm.Count = runFlagsGroup.Count();
            vm.PressureDiff = devDict;
 
            return vm;
        }
 
        private static bool IsValidDeviation(Dictionary<int, double> devDict)
        {
            foreach (var dev in devDict)
            {
                if (Math.Abs(dev.Value) > 5)
                {
                    return false;
                }
            }
            return true;
        }
 
 
 
 
        //public static List<AnalysisHelper.AnalysisDeviationDto> GetAnalysisDeviationDtoList(Dictionary<int, Model.Pump> flag_pump_dict, List<Tuple<string, double, List<Tuple<int, double, double>>>> run_flags_flow_pressure_diff_dict, double minQ = 0, double maxQ = 100000, double spaceQ = 1000)
        //{
        //    if (flag_pump_dict == null || !flag_pump_dict.Any())
        //    {
        //        return default;
        //    }
        //    if (run_flags_flow_pressure_diff_dict == null || !run_flags_flow_pressure_diff_dict.Any())
        //    {
        //        return default;
        //    }
 
        //    var list = new List<AnalysisHelper.AnalysisDeviationDto>();
        //    var error_value = false;
        //    for (double current_minQ = minQ; current_minQ <= maxQ; current_minQ += spaceQ)
        //    {
        //        var current_maxQ = current_minQ + spaceQ;
        //        var combine_list = run_flags_flow_pressure_diff_dict.Where(x => x.Item2 >= current_minQ && x.Item2 <= current_maxQ).ToList();
        //        if (combine_list == null || !combine_list.Any())
        //            continue;
        //        var group_by_run_flags = combine_list.GroupBy(x => x.Item1);
        //        foreach (var item_run_flags in group_by_run_flags)
        //        { 
        //            var flags = item_run_flags.Key;
        //            var vm = new AnalysisHelper.AnalysisDeviationDto
        //            {
        //                RunFlags = IStation.Untity.IntListHelper.ToList(flags),
        //                MinFlow = current_minQ,
        //                MaxFlow = current_maxQ
        //            };
        //            var dev_dict = new Dictionary<int, double>();
        //            List<Tuple<int, double, double>> tuple_list = item_run_flags.SelectMany(x => x.Item3).ToList();
        //            var group_by_flag = tuple_list.GroupBy(x => x.Item1);
        //            foreach (var flag_item in group_by_flag)
        //            {
        //                var flag = flag_item.Key;
        //                var head_diff_array = flag_item.Select(x => x.Item3).ToArray();
        //                var filter_std_dev_pop_tuple = AnalysisHelper.GetFilterBySTDP(head_diff_array);
        //                var filter_std_dev_pop_array = filter_std_dev_pop_tuple.Item1;
        //                var std_dev_pop = filter_std_dev_pop_tuple.Item2;
        //                var std_dev_pop_head_avg = filter_std_dev_pop_array.Average();
        //                var head_diff_avg = std_dev_pop_head_avg;
        //                head_diff_avg = Math.Round(head_diff_avg, 5);
        //                dev_dict.Add(flag, head_diff_avg);
        //            }
        //            vm.Count = item_run_flags.Count();
        //            vm.PressureDiff = dev_dict;
        //            error_value = false;
        //            foreach (var dev in dev_dict)
        //            {
        //                if (Math.Abs(dev.Value) > 3)
        //                {
        //                    error_value = true;
        //                    break;
        //                }
        //            }
        //            if (error_value)
        //            {
        //                continue;
        //            }
        //            list.Add(vm);
        //        }
        //    }
 
        //    return list;
        //}
 
        public static bool SaveAnalysisDeviationDtoList(int stationIndex, List<AnalysisHelper.AnalysisDeviationDto> all_list)
        {
 
            var root_folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "分析");
            if (!Directory.Exists(root_folder))
            {
                Directory.CreateDirectory(root_folder);
            }
 
            List<AnalysisHelper.AnalysisDeviationDto> exist_list = null;
            var fileName = root_folder + "\\" + "AnalysisDeviation.json";
            if (File.Exists(fileName))
            {
                var exist_json = File.ReadAllText(fileName);
                var all_exist_list = JsonHelper.Json2Object<List<AnalysisHelper.AnalysisDeviationDto>>(exist_json);
                exist_list = all_exist_list?.Where(x => x.Station != stationIndex).ToList();
                File.Delete(fileName);
            }
            if (exist_list != null)
            {
                all_list.AddRange(exist_list);
            }
            all_list = all_list.OrderBy(x => x.Station).ThenBy(x => x.MinFlow).ToList();
            var json = JsonHelper.Object2Json(all_list);
            File.WriteAllText(fileName, json);
            return true;
        }
 
 
 
 
 
        static  bool  CheckFitPointList(List<CurvePoint> fit_point_list, int nPoints)
        {
            var arr = fit_point_list.ToArray();
            var max_y = fit_point_list[0].Y;
            for (int j = 1; j < nPoints; j++)
            {
                if (arr[j].Y > max_y)
                {
                    return false;
                }
                else
                {
                    max_y = fit_point_list.Take(j).Max(x => x.Y);
                }
            }
            return true;
        }
 
 
        private static Dictionary<int, Model.CurveExpress> GetFlagCurveDict()
        {
            var dict = new Dictionary<int, Model.CurveExpress>();
            var bll_curve = new BLL.PumpCurve();
            var station_list = new BLL.Station().GetAll();
            foreach (var station in station_list)
            {
                var eq_list = new BLL.Equipment().GetPumpListByBelongTypeAndBelongID(IStation.ObjectType.Station, station.ID);
                if (eq_list == null || !eq_list.Any())
                {
                    return default;
                }
 
                foreach (var eq in eq_list)
                {
                    Model.CurveExpress qh = null;
                    var curve_info = bll_curve.GetDefaultWorkingByPumpID(eq.ID)?.CurveInfo;
                    if (curve_info != null)
                    {
                        qh = curve_info.CurveQH;
                    }
                    dict.Add(eq.SortCode, qh);
                }
            }
 
            return dict;
        }
 
 
 
        private static Dictionary<int,Dictionary<int, List<CurvePoint>>> GetFlagHzCurveDict()
        {
            var fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "pumpcsv");
 
            var fileNameList = Directory.GetFiles(fullPath).Select(x => Path.GetFileNameWithoutExtension(x)).ToList();
            if (fileNameList == null || fileNameList.Count() < 1)
                return default;
 
            var dict=new Dictionary<int, Dictionary<int, List<CurvePoint>>>();
            var group = fileNameList.Where(x => x.Contains("_update_curve")).GroupBy(x => x.Substring(0, 2));
            foreach (var flagItem in group)
            {
                var flag=int.Parse(flagItem.Key);
                var files = flagItem.OrderBy(x => x).ToList();
                var fileInfoList = new List<(int Hz, string FileName)>();
 
                dict[flag] = new Dictionary<int, List<CurvePoint>>();
                foreach (var fileName in files)
                {
                    var hz = (int.Parse(fileName.Substring(3, 2)));
                    if (hz>50)
                    {
                        continue;
                    }
                    fileInfoList.Add((hz, fileName));
                }
                foreach (var item in fileInfoList)
                {
                    var updateCurvePtList = new List<CurvePoint>(); 
                    var updateCurveFile = Path.Combine(fullPath, $"{item.FileName}.csv");
                    using (var fs = new FileStream(updateCurveFile, FileMode.Open, FileAccess.Read))
                    using (var sr = new StreamReader(fs, Encoding.UTF8))
                    {
                        var strLine = string.Empty;
                        sr.ReadLine();
                        while (!string.IsNullOrEmpty(strLine = sr.ReadLine()))
                        {
                            var strList = strLine.Split(',');
                            var x = double.Parse(strList[0]);
                            var y = double.Parse(strList[1]);
                            updateCurvePtList.Add(new CurvePoint(x, y));
                        }
                    }
                    dict[flag][item.Hz]= updateCurvePtList;
                }
              
            }
 
            return dict;
        }
 
 
 
    }
 
 
}