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
using IStation.Model;
using IStation.ZyModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Common { 
 
    public class PumpReadFileHelper
    {
        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;
        }
 
 
 
 
 
        public void GetHistoryData(List< ZyModel.RealScadaData>  allFolws, List<ZyModel.RealScadaData> allEle, List<ZyModel.RealScadaData> allRunstatus)
        {
            List<RunParasData> runParasDatasList = new List<RunParasData>();
 
            DateTime startDay = new DateTime(2022, 11, 1);
            DateTime endDay = new DateTime(2023, 11, 14);
 
            allFlows = allFolws;  //取水量
            allEles = allEle;           //有功电能
             var allRunStaus = GetAllRunStatusData();
            //运行参数
            var pumprun1 = allRunstatus.Where(x => x.TagName == "1").ToList();
            var pumprun2 = allRunstatus.Where(x => x.TagName == "2").ToList();
            var pumprun3 = allRunstatus.Where(x => x.TagName == "3").ToList();
            var pumprun4 = allRunstatus.Where(x => x.TagName == "4").ToList();
            var pumprun5 = allRunstatus.Where(x => x.TagName == "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);  //用电量
                        }
                        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>
        public List<(int, DateTime, DateTime)> getPumpIsOpen(List<RealScadaData> TestIsOpen)
        {
            List<RealScadaData> isOpenpump1 = new List<RealScadaData>();
            List<RealScadaData> isOpenpump2 = new List<RealScadaData>();
            List<RealScadaData> isOpenpump3 = new List<RealScadaData>();
            List<RealScadaData> isOpenpump4 = new List<RealScadaData>();
            List<RealScadaData> isOpenpump5 = new List<RealScadaData>();
            //var TestIsOpen = GetPumpRunParas();
            if (TestIsOpen == null)
                return null;
            var Pump1 = TestIsOpen.Where(x => x.TagName == "_0402010204012101001").ToList();
            foreach (var item in Pump1)
            {
                isOpenpump1.AddRange(item.MonitorRecords);
            }
            var Pump2 = TestIsOpen.Where(x => x.TagName == "_0402010204012201001").ToList();
            foreach (var item in Pump2)
            {
                isOpenpump2.AddRange(item.Values);
            }
            var Pump3 = TestIsOpen.Where(x => x.TagName == "_0402010204012301001").ToList();
            foreach (var item in Pump3)
            {
                isOpenpump3.AddRange(item.Values);
            }
            var Pump4 = TestIsOpen.Where(x => x.TagName == "_0402010204012401001").ToList();
            foreach (var item in Pump4)
            {
                isOpenpump4.AddRange(item.Values);
            }
            var Pump5 = TestIsOpen.Where(x => x.TagName == "_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);
        }
 
 
 
 
        //泵开机参数的辅助方法
        private static List<(int, DateTime, DateTime)> GetData(List<RealScadaData> 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="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;
        }
 
    }
}