duheng
2024-03-28 6c93aedfeeb66023f9506083517aee1a4e18e24d
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
using IStation.CalcModel.Tide;
using IStation.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
 
namespace IStation.Common
{
    public class PumpHistoryDataFileHelper
    {
 
        public class RunParasData
        {
            /// <summary>
            /// 数据日期
            /// </summary>
            public DateTime DateTime { get; set; }
            /// <summary>
            /// 泵运行状态
            /// </summary>
            public List<RunParasBlock> Pump1 { get; set; }
            public List<RunParasBlock> Pump2 { get; set; }
            public List<RunParasBlock> Pump3 { get; set; }
            public List<RunParasBlock> Pump4 { get; set; }
            public List<RunParasBlock> Pump5 { get; set; }
            /// <summary>
            /// 总取水量
            /// </summary>
            public double FlowIn { get; set; }
            /// <summary>
            /// 总用电量
            /// </summary>
            public double Electric { get; set; }
            /// <summary>
            /// 总电费
            /// </summary>
            public double Money { get; set; }
        }
        public class RunParasBlock
        {
            /// <summary>
            /// 开始时间
            /// </summary>
            public DateTime StartTime { get; set; }
            /// <summary>
            /// 结束时间
            /// </summary>
            public DateTime EndTime { get; set; }
            /// <summary>
            /// 取水量
            /// </summary>
            public double FlowIn { get; set; }
            /// <summary>
            /// 用电量
            /// </summary>
            public double Electric { get; set; }
            /// <summary>
            /// 电费
            /// </summary>
            public double Money { get; set; }
        }
 
 
        List<ZyModel.RealScadaData> allFlows = null;
        List<ZyModel.RealScadaData> allEles = null;
 
        //取水量
        private double? GetPumpFlowIndexByStartTime(int start_index, int pump_index, DateTime time)
        {
            for (int i = 0; i < allFlows[pump_index].MonitorRecords.Count; i++)
            {
                var v = allFlows[pump_index].MonitorRecords[i];
                if (v.Time >= time && v.Value != null)
                {
                    return v.Value;
                }
            }
 
            return -1;
        }
 
        //电能
        private double? GetPumpEleIndexByStartTime(int start_index, int pump_index, DateTime time)
        {
            for (int i = 0; i < allEles[pump_index].MonitorRecords.Count; i++)
            {
                var v = allEles[pump_index].MonitorRecords[i];
                if (v.Time >= time && v.Value != null)
                {
                    return v.Value;
                }
            }
 
            return -1;
        }
        private double GetByEndTime(int index, DateTime time)
        {
            foreach (var v in allFlows[index].MonitorRecords)
            {
                if (v.Time >= time && v.Value != null)
                    return v.Value.Value;
            }
 
            return -1;
        }
 
 
        /// <summary>
        /// 初始方法获取所有数据
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        public void GetHistoryData()
        {
            List<RunParasData> runParasDatasList = new List<RunParasData>();
 
            DateTime startDay = new DateTime(2022, 11, 1);
            DateTime endDay = new DateTime(2023, 11, 14);
 
            allFlows = GetAllWaterData();  //取水量
            allEles = GetAllEleData();           //有功电能
            //var allRunStaus = GetAllRunStatusData();
            var allRunstatus = getPumpIsOpen();//运行参数
            var pumprun1 = allRunstatus.Where(x => x.Item1 == 1).ToList();
            var pumprun2 = allRunstatus.Where(x => x.Item1 == 2).ToList();
            var pumprun3 = allRunstatus.Where(x => x.Item1 == 3).ToList();
            var pumprun4 = allRunstatus.Where(x => x.Item1 == 4).ToList();
            var pumprun5 = allRunstatus.Where(x => x.Item1 == 5).ToList();
            List<RunParasBlock> runParasBlockpump1 = new List<RunParasBlock>();
            List<RunParasBlock> runParasBlockpump2 = new List<RunParasBlock>();
            List<RunParasBlock> runParasBlockpump3 = new List<RunParasBlock>();
            List<RunParasBlock> runParasBlockpump4 = new List<RunParasBlock>();
            List<RunParasBlock> runParasBlockpump5 = new List<RunParasBlock>();
            RunParasData day_sum = new RunParasData();
            for (DateTime day = startDay; day <= endDay; day = day.AddDays(1))
            {
                day_sum = new RunParasData();
                var next_day = day.AddDays(1);
                day_sum.DateTime = day;
 
                foreach (var time_block in pumprun1)
                {
                    if (time_block.Item2 >= day && time_block.Item3 <= next_day)
                    {
                        RunParasBlock block = new RunParasBlock();
                        block.EndTime = time_block.Item3;
                        block.StartTime = time_block.Item2;
                        double? start_flow_in = GetPumpFlowIndexByStartTime(0, 0, time_block.Item2);
                        double? end_flow = GetPumpFlowIndexByStartTime(0, 0, time_block.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.FlowIn = (double)(end_flow - start_flow_in);  //取水量
                        }
                        double? start_ELe = GetPumpEleIndexByStartTime(0, 0, time_block.Item2);
                        double? end_Ele = GetPumpEleIndexByStartTime(0, 0, time_block.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.Electric = (double)(end_Ele - start_ELe);  //用电量
                        }
                        if(time_block.Item2==new DateTime(2023, 3, 22)) 
                        { }
                        block.Money = GetMoney(time_block.Item2, time_block.Item3, 0);
                        if (time_block.Item1 == 1)
                        {
                            runParasBlockpump1.Add(block);
                        }
                        if (time_block.Item1 == 2)
                        {
                            runParasBlockpump2.Add(block);
                        }
                        if (time_block.Item1 == 3)
                        {
                            runParasBlockpump3.Add(block);
                        }
                        if (time_block.Item1 == 4)
                        {
                            runParasBlockpump4.Add(block);
                        }
                        if (time_block.Item1 == 5)
                        {
                            runParasBlockpump5.Add(block);
                        }
                    }
                }
                foreach (var item in pumprun2)
                {
                    if (item.Item2 >= day && item.Item3 <= next_day)
                    {
                        RunParasBlock block = new RunParasBlock();
                        block.EndTime = item.Item3;
                        block.StartTime = item.Item2;
                        double? start_flow_in = GetPumpFlowIndexByStartTime(1, 1, item.Item2);
                        double? end_flow = GetPumpFlowIndexByStartTime(1, 1, item.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.FlowIn = (double)(end_flow - start_flow_in);  //取水量
                        }
                        double? start_ELe = GetPumpEleIndexByStartTime(1, 1, item.Item2);
                        double? end_Ele = GetPumpEleIndexByStartTime(1, 1, item.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.Electric = (double)(end_Ele - start_ELe);  //用电量
                        }
                        block.Money = GetMoney(item.Item2, item.Item3, 1);
                        if (item.Item1 == 1)
                        {
                            runParasBlockpump1.Add(block);
                        }
                        if (item.Item1 == 2)
                        {
                            runParasBlockpump2.Add(block);
                        }
                        if (item.Item1 == 3)
                        {
                            runParasBlockpump3.Add(block);
                        }
                        if (item.Item1 == 4)
                        {
                            runParasBlockpump4.Add(block);
                        }
                        if (item.Item1 == 5)
                        {
                            runParasBlockpump5.Add(block);
                        }
                    }
                }
                foreach (var item in pumprun3)
                {
                    if (item.Item2 >= day && item.Item3 <= next_day)
                    {
                        RunParasBlock block = new RunParasBlock();
                        block.EndTime = item.Item3;
                        block.StartTime = item.Item2;
                        double? start_flow_in = GetPumpFlowIndexByStartTime(2, 2, item.Item2);
                        double? end_flow = GetPumpFlowIndexByStartTime(2, 2, item.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.FlowIn = (double)(end_flow - start_flow_in);  //取水量
                        }
                        double? start_ELe = GetPumpEleIndexByStartTime(2, 2, item.Item2);
                        double? end_Ele = GetPumpEleIndexByStartTime(2, 2, item.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.Electric = (double)(end_Ele - start_ELe);  //用电量
                        }
                        block.Money = GetMoney(item.Item2, item.Item3, 2);
                        if (item.Item1 == 1)
                        {
                            runParasBlockpump1.Add(block);
                        }
                        if (item.Item1 == 2)
                        {
                            runParasBlockpump2.Add(block);
                        }
                        if (item.Item1 == 3)
                        {
                            runParasBlockpump3.Add(block);
                        }
                        if (item.Item1 == 4)
                        {
                            runParasBlockpump4.Add(block);
                        }
                        if (item.Item1 == 5)
                        {
                            runParasBlockpump5.Add(block);
                        }
                    }
                }
                foreach (var item in pumprun4)
                {
                    if (item.Item2 >= day && item.Item3 <= next_day)
                    {
                        RunParasBlock block = new RunParasBlock();
                        block.EndTime = item.Item3;
                        block.StartTime = item.Item2;
                        double? start_flow_in = GetPumpFlowIndexByStartTime(3, 3, item.Item2);
                        double? end_flow = GetPumpFlowIndexByStartTime(3, 3, item.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.FlowIn = (double)(end_flow - start_flow_in);  //取水量
                        }
                        double? start_ELe = GetPumpEleIndexByStartTime(3, 3, item.Item2);
                        double? end_Ele = GetPumpEleIndexByStartTime(3, 3, item.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.Electric = (double)(end_Ele - start_ELe);  //用电量
                        }
                        block.Money = GetMoney(item.Item2, item.Item3, 3);
 
                        if (item.Item1 == 1)
                        {
                            runParasBlockpump1.Add(block);
                        }
                        if (item.Item1 == 2)
                        {
                            runParasBlockpump2.Add(block);
                        }
                        if (item.Item1 == 3)
                        {
                            runParasBlockpump3.Add(block);
                        }
                        if (item.Item1 == 4)
                        {
                            runParasBlockpump4.Add(block);
                        }
                        if (item.Item1 == 5)
                        {
                            runParasBlockpump5.Add(block);
                        }
                    }
                }
                foreach (var item in pumprun5)
                {
                    if (item.Item2 >= day && item.Item3 <= next_day)
                    {
                        RunParasBlock block = new RunParasBlock();
                        block.EndTime = item.Item3;
                        block.StartTime = item.Item2;
                        double? start_flow_in = GetPumpFlowIndexByStartTime(4, 4, item.Item2);
                        double? end_flow = GetPumpFlowIndexByStartTime(4, 4, item.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.FlowIn = (double)(end_flow - start_flow_in);  //取水量
                        }
                        double? start_ELe = GetPumpEleIndexByStartTime(4, 4, item.Item2);
                        double? end_Ele = GetPumpEleIndexByStartTime(4, 4, item.Item3);
                        if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
                        {
                            block.Electric = (double)(end_Ele - start_ELe);  //用电量
                        }
                        block.Money = GetMoney(item.Item2, item.Item3, 4);
 
                        if (item.Item1 == 1)
                        {
                            runParasBlockpump1.Add(block);
                        }
                        if (item.Item1 == 2)
                        {
                            runParasBlockpump2.Add(block);
                        }
                        if (item.Item1 == 3)
                        {
                            runParasBlockpump3.Add(block);
                        }
                        if (item.Item1 == 4)
                        {
                            runParasBlockpump4.Add(block);
                        }
                        if (item.Item1 == 5)
                        {
                            runParasBlockpump5.Add(block);
                        }
                    }
                }
                day_sum.Pump1 = runParasBlockpump1;
                day_sum.Pump2 = runParasBlockpump2;
                day_sum.Pump3 = runParasBlockpump3;
                day_sum.Pump4 = runParasBlockpump4;
                day_sum.Pump5 = runParasBlockpump5;
                foreach (var item in runParasBlockpump1)
                {
                    day_sum.Electric += item.Electric;
                    day_sum.FlowIn += item.FlowIn;
                    day_sum.Money += item.Money;
                }
                foreach (var item in runParasBlockpump2)
                {
                    day_sum.Electric += item.Electric;
                    day_sum.FlowIn += item.FlowIn;
                    day_sum.Money += item.Money;
                }
                foreach (var item in runParasBlockpump3)
                {
                    day_sum.Electric += item.Electric;
                    day_sum.FlowIn += item.FlowIn;
                    day_sum.Money += item.Money;
                }
                foreach (var item in runParasBlockpump4)
                {
                    day_sum.Electric += item.Electric;
                    day_sum.FlowIn += item.FlowIn;
                    day_sum.Money += item.Money;
                }
                foreach (var item in runParasBlockpump5)
                {
                    day_sum.Electric += item.Electric;
                    day_sum.FlowIn += item.FlowIn;
                    day_sum.Money += item.Money;
                }
                runParasDatasList.Add(day_sum);
                var json = JsonHelper.Object2Json(runParasDatasList);
 
 
                runParasBlockpump1 = new List<RunParasBlock>();
                runParasBlockpump2 = new List<RunParasBlock>();
                runParasBlockpump3 = new List<RunParasBlock>();
                runParasBlockpump4 = new List<RunParasBlock>();
                runParasBlockpump5 = new List<RunParasBlock>();
            }
            DateTime date = new DateTime(2022, 11, 1);
            DateTime currenttime = new DateTime(2022, 11, 1);
 
            //  int daysInMonth = DateTime.DaysInMonth(date.Year, date.Month);
            //  var path = "C:\\Users\\ZKC\\Desktop\\新建文件夹 (2)";
            int processedDays = 0;
            for (int k = 0; k < 30; k++)
            {
                int daysInMonth = DateTime.DaysInMonth(date.Year, date.Month); // 获取当前月份的天数
                 var path22 = $"C:\\Users\\ZKC\\Desktop\\新建文件夹 (2)\\{date.ToString("yyyy-MM")}";
                var filepath = Directory.CreateDirectory(path22);
                 int i = 0;
             
                foreach (var item in runParasDatasList.Skip(processedDays)) // 跳过已处理的天数
                {
                    string jsonpath = string.Concat(path22, $"\\{currenttime.ToString("dd")}");
                    string json = JsonHelper.Object2Json(item);
                    File.WriteAllText($"{jsonpath}.json", json);
                    currenttime = currenttime.AddDays(1);
                    i++;
                    if (i == daysInMonth)
                        break;
                }
 
                processedDays += i; // 更新已处理的天数
                date = date.AddMonths(1); // 更新日期到下一个月份
                currenttime = date; // 重置 currenttime 为新月份的第一
            }
 
        }
 
 
 
 
 
        /*
                /// <summary>
                ///取水量
                /// </summary>
                /// <returns></returns>
                public   List<ZyModel.StatusScadaData> GetWater()
                {
                    var tagDict = new List<string>
                    {
                           "_0402010204012101001",//"二取水1号泵累计流量" },
                           "_0402010204012203002",//"二取水2号泵累计流量" },
                           "_0402010204012303002",// "二取水3号泵累计流量" },
                           "_0402010204012403002", //"二取水4号泵累计流量" },
                           "_0402010204012503002", //"二取水5号泵累计流量" },
                    };
 
                    ZyModel.StatusScadaData pump1 = new ZyModel.StatusScadaData();
                    pump1.TagName = tagDict[0];
                    pump1.MonitorRecords = new List<ZyModel.RunStatusRecord>();
                    ZyModel.StatusScadaData pump2 = new ZyModel.StatusScadaData();
                    ZyModel.StatusScadaData pump3 = new ZyModel.StatusScadaData();
                    ZyModel.StatusScadaData pump4 = new ZyModel.StatusScadaData();
                    ZyModel.StatusScadaData pump5 = new ZyModel.StatusScadaData();
                     string path = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(),
                      "PumpRunParas", "二取水机泵开停状态.csv");
                    if (!File.Exists(path))
                        return null;
                    int totalLines = File.ReadLines(path, Encoding.GetEncoding("gb2312")).Count();//总行数
                    System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
                    System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.GetEncoding("gb2312"));
                    string tempText;
                     int last_status1 = 0;
                    for (int i = 0; i < totalLines - 1; i++)
                    {
                        tempText = sr.ReadLine();
                        string[] arr = tempText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                         var row = new ZyModel.MonitorRecord
                        {
                            Time = Convert.ToDateTime(arr[0]),
                            Value = Convert.ToDouble(arr[2])
                        };
                     }
                    fs.Close();
                     //   return new List<ZyModel.RealScadaData>() { pump1, pump2, pump3, pump4, pump5 };
                    return null;
                }*/
 
 
        /*        /// <summary>
                /// 获取电费价格
                /// </summary>
                private double getEleMoney(List<RunParasBlock> runParasBlocks)
                {
                    double totalPrice=0;
                    foreach (var item in runParasBlocks)
                    {
                        totalPrice+=GetMoney(item.StartTime, item.EndTime);
                    }
                    return totalPrice;
                }*/
 
        //获取电费
        private double GetMoney(DateTime str, DateTime end, int sort)
        {
            var list = IStation.Common.ElecPrice.GetAll();
            double Price = 0;
            var month = str.Month;
            var Starthour = str.Hour;
            var Endhour = end.Hour;
            foreach (var item in list[0].Settings.MonthList)
            {
                if (month >= item.StartMonth && month <= item.EndMonth)
                {
                    foreach (var Hour in item.HourList)
                    {
                        if (Starthour == Endhour) continue;
                        // Check if the hour range overlaps with the given start and end hours
                        if ((Starthour >= Hour.StartHour && Starthour <= Hour.EndHour) ||
                          (Endhour >= Hour.StartHour && Endhour <= Hour.EndHour) ||
                          (Starthour < Hour.StartHour && Endhour > Hour.EndHour)) // If the range spans across multiple hours
                        {
                            // Calculate the amount based on the portion of hours covered
                            double? start_flow_in = GetPumpEleIndexByStartTime(sort, sort, new DateTime(str.Year, str.Month, str.Day, Starthour, 0, 0));
                            double? end_flow = GetPumpEleIndexByStartTime(sort, sort, new DateTime(str.Year, str.Month, str.Day, Endhour, 0, 0));
                            double amount = 0;
                            if (Starthour >= Hour.StartHour && Endhour <= Hour.EndHour) // If start and end hours are within this hour range
                            {
                                amount = (double)(end_flow - start_flow_in);
                                Price += Hour.Price * amount; // Accumulate price
                                Starthour = Endhour; // Update StartHour to EndHour for next iteration
                            }
                            else if (Starthour < Hour.StartHour && Endhour <= Hour.EndHour) // If the range starts before this hour range
                            {
                                double? next_flow = GetPumpEleIndexByStartTime(sort, sort, new DateTime(str.Year, str.Month, str.Day, Hour.StartHour, 0, 0));
                                amount = (double)(next_flow - start_flow_in);
                                Price += Hour.Price * amount; // Accumulate price
                                Starthour = Hour.StartHour; // Update StartHour to Hour.StartHour for next iteration
                            }
                            else if (Starthour >= Hour.StartHour && Endhour > Hour.EndHour && Starthour < Hour.EndHour) // If the range ends after this hour range
                            {
                                double? prev_flow = GetPumpEleIndexByStartTime(sort, sort, new DateTime(str.Year, str.Month, str.Day, Hour.EndHour, 0, 0));
                                amount = (double)(prev_flow - start_flow_in);
                                Price += Hour.Price * amount; // Accumulate price
                                Starthour = Hour.EndHour; // Update StartHour to Hour.EndHour for next iteration
                            }
                            else // If the range spans across this hour range
                            {
                                double? next_flow = GetPumpEleIndexByStartTime(sort, sort, new DateTime(str.Year, str.Month, str.Day, Hour.StartHour, 0, 0));
                                double? prev_flow = GetPumpEleIndexByStartTime(sort, sort, new DateTime(str.Year, str.Month, str.Day, Hour.EndHour, 0, 0));
                                amount = (double)(next_flow - prev_flow);
                                Price += Hour.Price * amount; // Accumulate price
                                Starthour = Hour.EndHour; // Update StartHour to Hour.EndHour for next iteration
                            }
                        }
 
                    }
                }
            };
            return Price;
        }
 
 
        /// <summary>
        /// 获取总取水量
        /// </summary>
        /// <returns></returns>
        public List<ZyModel.RealScadaData> GetAllWaterData()
        {
            var tagDict = new List<string>
            {
                   "二取水1号主水泵.日累计",//"二取水1号泵累计流量" },
                   "二取水2号主水泵.日累计",//"二取水2号泵累计流量" },
                   "二取水3号主水泵.日累计",// "二取水3号泵累计流量" },
                   "二取水4号主水泵.日累计", //"二取水4号泵累计流量" },
                   "二取水5号主水泵.日累计", //"二取水5号泵累计流量" },
            };
            ZyModel.RealScadaData pump1 = new ZyModel.RealScadaData();
            pump1.TagName = tagDict[0];
            pump1.MonitorRecords = new List<ZyModel.MonitorRecord>();
            ZyModel.RealScadaData pump2 = new ZyModel.RealScadaData();
            pump2.TagName = tagDict[1];
            pump2.MonitorRecords = new List<ZyModel.MonitorRecord>();
            ZyModel.RealScadaData pump3 = new ZyModel.RealScadaData();
            pump3.TagName = tagDict[2];
            pump3.MonitorRecords = new List<ZyModel.MonitorRecord>();
            ZyModel.RealScadaData pump4 = new ZyModel.RealScadaData();
            pump4.TagName = tagDict[3];
            pump4.MonitorRecords = new List<ZyModel.MonitorRecord>();
            ZyModel.RealScadaData pump5 = new ZyModel.RealScadaData();
            pump5.TagName = tagDict[4];
            pump5.MonitorRecords = new List<ZyModel.MonitorRecord>();
            string path = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(),
              "PumpRunParas", "二取水机泵累计流量.csv");
            if (!File.Exists(path))
                return null;
            int totalLines = File.ReadLines(path, Encoding.GetEncoding("gb2312")).Count();//总行数
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.GetEncoding("gb2312"));
            string tempText;
 
            for (int i = 0; i < totalLines - 1; i++)
            {
                tempText = sr.ReadLine();
                string[] arr = tempText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var row = new ZyModel.MonitorRecord
                {
                    Time = Convert.ToDateTime(arr[0]),
                    Value = Convert.ToDouble(arr[2])
                };
                if (arr[1] == tagDict[0])
                {
                    pump1.MonitorRecords.Add(row);
                }
                if (arr[1] == tagDict[1])
                {
                    pump2.MonitorRecords.Add(row);
                }
                if (arr[1] == tagDict[2])
                {
                    pump3.MonitorRecords.Add(row);
                }
                if (arr[1] == tagDict[3])
                {
                    pump4.MonitorRecords.Add(row);
                }
                if (arr[1] == tagDict[4])
                {
                    pump5.MonitorRecords.Add(row);
                }
            }
            fs.Close();
            return new List<ZyModel.RealScadaData>() { pump1, pump2, pump3, pump4, pump5 };
        }
 
 
 
        /// <summary>
        /// 获取有功电能
        /// </summary>
        /// <returns></returns>
        public static List<ZyModel.RealScadaData> GetAllEleData()
        {
            var tagDict = new List<string>
            {
                   "_0402010204040193046",//"二取水1号泵累计流量" },
                   "_0402010204040193047",//"二取水2号泵累计流量" },
                   "_0402010204012308007",// "二取水3号泵累计流量" },
                   "_0402010204012408007", //"二取水4号泵累计流量" },
                   "_0402010204012508007", //"二取水5号泵累计流量" },
            };
            string path = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(),
              "PumpRunParas", "二取水机泵有功电能.csv");
            if (!File.Exists(path))
                return null;
            int totalLines = File.ReadLines(path, Encoding.GetEncoding("gb2312")).Count();//总行数
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.GetEncoding("gb2312"));
            ZyModel.RealScadaData pump1 = new ZyModel.RealScadaData();
            pump1.TagName = tagDict[0];
            pump1.MonitorRecords = new List<ZyModel.MonitorRecord>();
            ZyModel.RealScadaData pump2 = new ZyModel.RealScadaData();
            pump2.TagName = tagDict[1];
            pump2.MonitorRecords = new List<ZyModel.MonitorRecord>();
            ZyModel.RealScadaData pump3 = new ZyModel.RealScadaData();
            pump3.TagName = tagDict[2];
            pump3.MonitorRecords = new List<ZyModel.MonitorRecord>();
            ZyModel.RealScadaData pump4 = new ZyModel.RealScadaData();
            pump4.TagName = tagDict[3];
            pump4.MonitorRecords = new List<ZyModel.MonitorRecord>();
            ZyModel.RealScadaData pump5 = new ZyModel.RealScadaData();
            pump5.TagName = tagDict[4];
            pump5.MonitorRecords = new List<ZyModel.MonitorRecord>();
            string tempText;
            for (int i = 0; i < totalLines; i++)
            {
                tempText = sr.ReadLine();
                string[] arr = tempText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var row = new ZyModel.MonitorRecord
                {
                    Time = Convert.ToDateTime(arr[0]),
                    Value = Convert.ToDouble(arr[2])
                };
                if (arr[1] == tagDict[0])
                {
                    pump1.MonitorRecords.Add(row);
                }
                if (arr[1] == tagDict[1])
                {
                    pump2.MonitorRecords.Add(row);
                }
                if (arr[1] == tagDict[2])
                {
                    pump3.MonitorRecords.Add(row);
                }
                if (arr[1] == tagDict[3])
                {
                    pump4.MonitorRecords.Add(row);
                }
                if (arr[1] == tagDict[4])
                {
                    pump5.MonitorRecords.Add(row);
                }
            }
            fs.Close();
            return new List<ZyModel.RealScadaData>() { pump1, pump2, pump3, pump4, pump5 };
        }
 
 
 
        /// <summary>
        /// 获取具体时间段的开泵台数
        /// </summary>
        public List<(int, DateTime, DateTime)> getPumpIsOpen()
        {
            List<IsOpen> isOpenpump1 = new List<IsOpen>();
            List<IsOpen> isOpenpump2 = new List<IsOpen>();
            List<IsOpen> isOpenpump3 = new List<IsOpen>();
            List<IsOpen> isOpenpump4 = new List<IsOpen>();
            List<IsOpen> isOpenpump5 = new List<IsOpen>();
            var TestIsOpen = GetPumpRunParas();
            if (TestIsOpen == null)
                return null;
            var Pump1 = TestIsOpen.Where(x => x.Tag == "_0402010204012101001").ToList();
            foreach (var item in Pump1)
            {
                isOpenpump1.AddRange(item.Values);
            }
            var Pump2 = TestIsOpen.Where(x => x.Tag == "_0402010204012201001").ToList();
            foreach (var item in Pump2)
            {
                isOpenpump2.AddRange(item.Values);
            }
            var Pump3 = TestIsOpen.Where(x => x.Tag == "_0402010204012301001").ToList();
            foreach (var item in Pump3)
            {
                isOpenpump3.AddRange(item.Values);
            }
            var Pump4 = TestIsOpen.Where(x => x.Tag == "_0402010204012401001").ToList();
            foreach (var item in Pump4)
            {
                isOpenpump4.AddRange(item.Values);
            }
            var Pump5 = TestIsOpen.Where(x => x.Tag == "_0402010204012501001").ToList();
            foreach (var item in Pump5)
            {
                isOpenpump5.AddRange(item.Values);
            }
            //    List<string> startTime = new List<string>();
            //  List<string> endtime = new List<string>();
            var Run1 = GetData(isOpenpump1, 1);
            var Run2 = GetData(isOpenpump2, 2);
            var Run3 = GetData(isOpenpump3, 3);
            var Run4 = GetData(isOpenpump4, 4);
            var Run5 = GetData(isOpenpump5, 5);
            List<(int, DateTime, DateTime)> mergedList = MergeList(Run1, Run2, Run3, Run4, Run5);
            return (mergedList);
        }
 
        /// <summary>
        /// 获取泵的开机参数
        /// </summary>
        /// <returns></returns>
        public List<Model.PumpIsopen> GetPumpRunParas()
        {
            string path = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(),
               "PumpRunParas", "二取水机泵开停状态.csv");
            if (!File.Exists(path))
                return null;
            int totalLines = File.ReadLines(path, Encoding.GetEncoding("gb2312")).Count();//总行数
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.GetEncoding("gb2312"));
            string tempText;
            List<Model.PumpIsopen> pumpISopenlist = new List<Model.PumpIsopen>();
            for (int i = 0; i < totalLines; i++)
            {
                tempText = sr.ReadLine();
                string[] arr = tempText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var pumpISopen = new Model.PumpIsopen
                {
                    Tag = arr[1],
                    Values = new List<Model.IsOpen>
                    {
                        new Model.IsOpen
                        {
                            DateTime = Convert.ToDateTime(arr[0]),
                            Isopen = arr[2]
                        }
                     }
                };
                pumpISopenlist.Add(pumpISopen);
            }
            fs.Close();
            return pumpISopenlist;
        }
 
 
        //泵开机参数的辅助方法
        private static List<(int, DateTime, DateTime)> GetData(List<IsOpen> PumpList, int sort)
        {
            List<(int, DateTime, DateTime)> openTimeRanges = new List<(int, DateTime, DateTime)>();
 
            // 每天的数据条数
            int itemsPerDay = 288;
 
            // 遍历列表
            for (int i = 0; i < PumpList.Count - 1; i += itemsPerDay)
            {
                DateTime currentDate = PumpList[i].DateTime.Date;
                DateTime startTime = DateTime.MinValue;
                bool isOpen = false;
 
                // 遍历当天的数据
                for (int j = i; j < i + itemsPerDay && j < PumpList.Count - 1; j++)
                {
                    if (PumpList[j].Isopen == "1")
                    {
                        if (!isOpen)
                        {
                            startTime = PumpList[j].DateTime;
                            isOpen = true;
                        }
                    }
                    else
                    {
                        if (isOpen)
                        {
                            openTimeRanges.Add((sort, startTime, PumpList[j - 1].DateTime));
                            isOpen = false;
                        }
                    }
                }
 
                // 如果当天最后一条数据是打开状态,添加时间段
                if (isOpen)
                {
                    openTimeRanges.Add((sort, startTime, PumpList[i + itemsPerDay - 1].DateTime));
                }
            }
 
            return openTimeRanges;
        }
 
 
 
 
 
        /// <summary>
        /// 模糊合并时间段
        /// </summary>
        /// <param name="Date"></param>
        /// <returns></returns>
        static List<(DateTime, DateTime, int)> MergeTimeSlots(List<(DateTime, DateTime)> Date)
        {
            List<(DateTime, DateTime, int)> mergedSlots = new List<(DateTime, DateTime, int)>();
            Date.Sort((x, y) => x.Item1.CompareTo(y.Item1)); // 按照起始时间排序
 
            DateTime mergedStart = Date[0].Item1;
            DateTime mergedEnd = Date[0].Item2;
            int mergeCount = 1;
 
            // 从第二个时间段开始迭代
            for (int i = 1; i < Date.Count; i++)
            {
                DateTime start = Date[i].Item1;
                DateTime end = Date[i].Item2;
 
                // 如果当前时间段的起始时间在模糊时间范围内,则合并
                if (start <= mergedEnd.AddMinutes(40)) // 假设模糊时间范围为15分钟
                {
                    mergedEnd = DateTime.Compare(mergedEnd, end) < 0 ? end : mergedEnd;
                    mergeCount++;
                }
                else
                {
                    mergedSlots.Add((mergedStart, mergedEnd, mergeCount));
                    mergedStart = start;
                    mergedEnd = end;
                    mergeCount = 1;
                }
            }
 
            // 添加最后一个时间段
            mergedSlots.Add((mergedStart, mergedEnd, mergeCount));
 
            return mergedSlots;
        }
 
 
        /// <summary>
        /// 合并五台泵的运行时间
        /// </summary>
        /// <param name="lists"></param>
        /// <returns></returns>
        static List<(int, DateTime, DateTime)> MergeList(params List<(int, DateTime, DateTime)>[] lists)
        {
            List<(int, DateTime, DateTime)> mergedList = new List<(int, DateTime, DateTime)>();
            foreach (var list in lists)
            {
                mergedList.AddRange(list);
            }
            return mergedList;
        }
 
 
 
 
        /*  private static double GetWaterdata(DateTime Str, DateTime end)
          {
              List<Model.Water> water1 = new List<Model.Water>();
              List<Model.Water> water2 = new List<Model.Water>();
              List<Model.Water> water3 = new List<Model.Water>();
              List<Model.Water> water4 = new List<Model.Water>();
              List<Model.Water> water5 = new List<Model.Water>();
              var waterData = GetWaterData();
              var pump1 = waterData.Where(x => x.Tag == "二取水1号主水泵.日累计").ToList();
              foreach (var item in pump1)
              {
                  water1.AddRange(item.Values.Where(x => x.DateTime >= Str && x.DateTime <= end));
              }
              var pump2 = waterData.Where(x => x.Tag == "二取水2号主水泵.日累计").ToList();
              foreach (var item in pump2)
              {
                  water2.AddRange(item.Values.Where(x => x.DateTime >= Str && x.DateTime <= end));
              }
              var pump3 = waterData.Where(x => x.Tag == "二取水3号主水泵.日累计").ToList();
              foreach (var item in pump3)
              {
                  water3.AddRange(item.Values.Where(x => x.DateTime >= Str && x.DateTime <= end));
              }
              var pump4 = waterData.Where(x => x.Tag == "二取水4号主水泵.日累计").ToList();
              foreach (var item in pump4)
              {
                  water4.AddRange(item.Values.Where(x => x.DateTime >= Str && x.DateTime <= end));
              }
              var pump5 = waterData.Where(x => x.Tag == "二取水5号主水泵.日累计").ToList();
              foreach (var item in pump5)
              {
                  water5.AddRange(item.Values.Where(x => x.DateTime >= Str && x.DateTime <= end));
              }
              var Result1 = water1.Last().SingleWater - water1.First().SingleWater;
              var Result2 = water2.Last().SingleWater - water2.First().SingleWater;
              var Result3 = water3.Last().SingleWater - water3.First().SingleWater;
              var Result4 = water4.Last().SingleWater - water4.First().SingleWater;
              var Result5 = water5.Last().SingleWater - water5.First().SingleWater;
              return Result1 + Result2 + Result3 + Result4 + Result5;
          }
 
  */
 
        private static double GetElectrityData(DateTime Str, DateTime end)
        {
            /*   var Ele = GetEleData();
              List<Model.Ele> Ele1 = new List<Model.Ele>();
              List<Model.Ele> Ele2 = new List<Model.Ele>();
              List<Model.Ele> Ele3 = new List<Model.Ele>();
              List<Model.Ele> Ele4 = new List<Model.Ele>();
              List<Model.Ele> Ele5 = new List<Model.Ele>();*/
            /*         var pump1 = Ele.Where(x => x.Tag == "_0402010204040193046").ToList();
                     foreach (var item in pump1)
                     {
                         Ele1.AddRange(item.TotalEle.Where(x => x.DateTime >= Str && x.DateTime <= end));
                     }
                     var pump2 = Ele.Where(x => x.Tag == "_0402010204040193047").ToList();
                     foreach (var item in pump2)
                     {
                         Ele2.AddRange(item.TotalEle.Where(x => x.DateTime >= Str && x.DateTime <= end));
                     }
                     var pump3 = Ele.Where(x => x.Tag == "_0402010204012308007").ToList();
                     foreach (var item in pump3)
                     {
                         Ele3.AddRange(item.TotalEle.Where(x => x.DateTime >= Str && x.DateTime <= end));
                     }
                     var pump4 = Ele.Where(x => x.Tag == "_0402010204012408007").ToList();
                     foreach (var item in pump4)
                     {
                         Ele4.AddRange(item.TotalEle.Where(x => x.DateTime >= Str && x.DateTime <= end));
                     }
                     var pump5 = Ele.Where(x => x.Tag == "_0402010204012508007").ToList();
                     foreach (var item in pump5)
                     {
                         Ele5.AddRange(item.TotalEle.Where(x => x.DateTime >= Str && x.DateTime <= end));
                     }
                     var Result1 = Ele1.Last().Value - Ele1.First().Value;
                     var Result2 = Ele2.Last().Value - Ele2.First().Value;
                     var Result3 = Ele3.Last().Value - Ele3.First().Value;
                     var Result4 = Ele4.Last().Value - Ele4.First().Value;
                     var Result5 = Ele5.Last().Value - Ele5.First().Value;
                     return Result1 + Result2 + Result3 + Result4 + Result5;*/
            return 0;
        }
    }
}