tangxu
2024-10-08 54d6c9937e34066e357c3212477914ecef1370b6
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
using IStation.CalcModel.Tide;
using IStation.Common;
using IStation.Dto;
using IStation.Model;
using IStation.ZyModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;
 
namespace IStation.WebApi.Controllers
{
    /// <summary>
    /// 泵运行时间
    /// </summary>
    [RoutePrefix("PumpRun")]
    public class PumpRunController : ApiController
    {
        /// <summary>
        ///  获取泵实际运行参数(一天)
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetByOneDay")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetByOneDay(string startday)
        {
            if (startday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime d;
            if (!DateTime.TryParse(startday, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            if (d > DateTime.Now.AddDays(1))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "天数不正确" };
            }
            RunParasData result = null;
            PumpRunParasHelper pumpRunParasHelper = new PumpRunParasHelper();
            result = Common.PumpRunParasHelper.ReadPumpRunParas(d);
            string geterroer = null;
            if (result != null)
            {
                result.Electric = Math.Round(result.Electric, 2);
                result.Money = Math.Round(result.Money, 2);
                result.FlowIn = Math.Round(result.FlowIn, 2);
                if (result.Pump1.Count != 0)
                {
                    foreach (var item in result.Pump1)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
                if (result.Pump2.Count != 0)
                {
                    foreach (var item in result.Pump2)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
                if (result.Pump3.Count != 0)
                {
                    foreach (var item in result.Pump3)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
                if (result.Pump4.Count != 0)
                {
                    foreach (var item in result.Pump4)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
                if (result.Pump5.Count != 0)
                {
                    foreach (var item in result.Pump5)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
            }
            else
            {
                var PumpTotalElecty = await ZyConnectHelper.GetPumpTotalElectricity(d, d);    //获取某段时间的用电量
                var PumpState = await ZyConnectHelper.GetPumpOpenStatusBySingleDay(d);  //获取泵的开机情况
                var TotalWater = await ZyConnectHelper.GetTotalWaterByDayTimeRange_In(d.AddDays(-1), d);    //取水量
 
                /*        PumpHistoryDataFileHelper pumpHistoryDataFileHelper=new PumpHistoryDataFileHelper();
                        var TotalWater = pumpHistoryDataFileHelper.GetAllWaterData();
                        var PumpState = pumpHistoryDataFileHelper.getpumpstate();
                        var PumpTotalElecty = pumpHistoryDataFileHelper.GetAllEleData();*/
                result = pumpRunParasHelper.GetRunParasDatas(PumpState, TotalWater, PumpTotalElecty, d, out geterroer);
            }
            if (result != null)
            {
                pumpRunParasHelper.Save(result, out string error);
                //  LogHelper.Info(error);
            }
            if (geterroer != null)
            {
                LogHelper.Info(geterroer);
            }
            return new IStation.Dto.ApiResult<Model.RunParasData>(result) { Code = ApiResultCode.Success, Message = "Success" };
        }
 
        /// <summary>
        /// 获取泵历史运行参数
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetByHistoryOneDay")]
        [HttpGet]
        public IStation.Dto.ApiResult GetByHistoryOneDay(string day)
        {
            if (day == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime d;
            if (!DateTime.TryParse(day, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
 
            var history = Common.PumpRunParasHelper.ReadPumpRunParas(d);
            if (history == null)
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "Data null" };
            history.Electric = Math.Round(history.Electric, 2);
            history.Money = Math.Round(history.Money, 2);
            history.FlowIn = Math.Round(history.FlowIn, 2);
            if (history.Pump1.Count != 0)
            {
                foreach (var item in history.Pump1)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            if (history.Pump2.Count != 0)
            {
                foreach (var item in history.Pump2)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            if (history.Pump3.Count != 0)
            {
                foreach (var item in history.Pump3)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            if (history.Pump4.Count != 0)
            {
                foreach (var item in history.Pump4)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            if (history.Pump5.Count != 0)
            {
                foreach (var item in history.Pump5)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            return new IStation.Dto.ApiResult<Model.RunParasData>(history) { Code = ApiResultCode.Success, Message = "Success" };
        }
 
        /// <summary>
        /// 一天内的总取水量和用电量
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetOneDayData")]
        [HttpGet]
        public IStation.Dto.ApiResult GetOneDayPumpWaterData(string day)
        {
            if (day == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime d;
            if (!DateTime.TryParse(day, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var WaterParas = Common.PumpRunParasHelper.ReadPumpWaterParas(d);
 
            return new IStation.Dto.ApiResult<List<Model.PumpWater>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
        }
 
        /// <summary>
        /// 获取时间段内的单管道供水数据
        /// </summary>
        /// <param name="startday"></param>
        /// <param name="endday"></param>
        /// <param name="sign"></param>
        /// <returns></returns>
        [Route("GetPumpOutDayWater")]
        [HttpGet]
        public IStation.Dto.ApiResult GetPumpOutWater(string startday, string endday, int sign)
        {
            if (startday == null || endday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime Start;
            if (!DateTime.TryParse(startday, out Start))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            DateTime End;
            if (!DateTime.TryParse(endday, out End))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var WaterParas = Common.PumpRunParasHelper.ReadPumpOutWater(Start, End, sign);
 
            return new IStation.Dto.ApiResult<List<Model.OutList>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
        }
 
        /// <summary>
        /// 获取所有的月合计数据(单管道)
        /// </summary>
        /// <returns></returns>
        [Route("GetPumpOutMonthWater")]
        [HttpGet]
        public IStation.Dto.ApiResult GetPumpOutMonthWater()
        {
            var WaterParas = Common.PumpRunParasHelper.ReadPumpMonthOutWater();
            return new IStation.Dto.ApiResult<List<Model.PumpOutWater>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
        }
 
        /*       /// <summary>
               /// 获取天的合并供水瞬时数据(总管)
               /// </summary>
               /// <param name="startday"></param> 开始日期
               /// <param name="endday"></param> 结束日期
               /// <returns></returns>
               [Route("GetPumpTotalDayOutWater")]
               [HttpGet]
               public IStation.Dto.ApiResult GetPumpTotalDayOutWater(string startday, string endday)
               {
                   if (startday == null || endday == null)
                   {
                       return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
                   }
                   DateTime Start;
                   if (!DateTime.TryParse(startday, out Start))
                   {
                       return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
                   }
                   DateTime End;
                   if (!DateTime.TryParse(endday, out End))
                   {
                       return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
                   }
                   var WaterParas = Common.PumpRunParasHelper.ReadPumpAllDayOutWater(Start, End);
                   return new IStation.Dto.ApiResult<List<Model.OutTotalOneDayList>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
               }*/
 
        /// <summary>
        /// 获取天的合并供水累计数据(总管)
        /// </summary>
        /// <param name="startday"></param> 开始日期
        /// <param name="endday"></param> 结束日期
        /// <returns></returns>
        [Route("GetPumpTotalDayAccOutWater")]
        [HttpGet]
        public IStation.Dto.ApiResult GetPumpTotalDayAccOutWater(string startday, string endday)
        {
            if (startday == null || endday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime Start;
            if (!DateTime.TryParse(startday, out Start))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            DateTime End;
            if (!DateTime.TryParse(endday, out End))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var WaterParas = Common.PumpRunParasHelper.ReadPumpAllDayOutAccWater(Start, End);
            return new IStation.Dto.ApiResult<List<Model.OutTotalOneDayList>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
        }
 
        /// <summary>
        /// 获取给预测水位用的数据的历史取水量 (从众毅)
        /// </summary>
        /// <param name="startday"></param> 开始日期
        /// <param name="endday"></param> 结束日期
        /// <returns></returns>
        [Route("GetPumpHistoryData")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetPumpHistoryData(string startday, string endday, string resolution)
        {
            if (startday == null || endday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime Start;
            if (!DateTime.TryParse(startday, out Start))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            DateTime d;
            if (!DateTime.TryParse(endday, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var TotalWater = await ZyConnectHelper.GetTotalWaterByTimeRange_Out(Start, d, resolution);    //取水量
            /*     var one = TotalWater
                   .SelectMany(otdl => otdl.MonitorRecords);
                 var two = one.GroupBy(totalDay => totalDay.Time);
                 var three = two.Select(group =>
                 {
                     var time = group.First().Time; // 从组中获取时间
                     var totalSumForTimePoint = group.Sum(totalDay => totalDay.Value); // 在时间点分组中计算总和
                     return new { Time = time, Total = totalSumForTimePoint };
                 });*/
            var totalSumByTimePoint = TotalWater
              .SelectMany(otdl => otdl.MonitorRecords)
              .GroupBy(totalDay => totalDay.Time) // 按照小时和分钟进行分组
              .Select(group =>
              {
                  var time = group.First().Time; // 从组中获取时间
                  var totalSumForTimePoint = group.Sum(totalDay => totalDay.Value); // 在时间点分组中计算总和
                  return new { Time = time, Total = totalSumForTimePoint };
              });
            /*  var totalSumByTimePoint = TotalWater
                .SelectMany(otdl => otdl.pumpOutWater)
                .GroupBy(totalDay => totalDay.DateTime.TimeOfDay) // 按照小时和分钟进行分组
                .Select(group =>
                {
                    var time = group.First().DateTime.TimeOfDay; // 从组中获取时间
                    var totalSumForTimePoint = group.Sum(totalDay => totalDay.Total); // 在时间点分组中计算总和
                    return new { Time = time, Total = totalSumForTimePoint };
                });*/
            List<SSAData> totalOneDays = new List<SSAData>();
            foreach (var item in totalSumByTimePoint)
            {
                totalOneDays.Add(new SSAData() { DateTime = item.Time, Total = (double)item.Total });
            }
            return new IStation.Dto.ApiResult<List<SSAData>>(totalOneDays) { Code = ApiResultCode.Success, Message = "Success" };
        }
 
        /// <summary>
        /// 获取供水开机状态
        /// </summary>
        /// <param name="startday"></param> 开始日期
        /// <param name="endday"></param> 结束日期
        /// <returns></returns>
        [Route("GetPumpOpenState")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetPumpOpenState(string startday, string endday)
        {
            if (startday == null || endday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime Start;
            if (!DateTime.TryParse(startday, out Start))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            DateTime d;
            if (!DateTime.TryParse(endday, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var result = await ZyConnectHelper.GetPumpOpenStatusDay(Start, d, "1h");
            var totalSumByTimePoint = result
            .SelectMany(otdl => otdl.MonitorRecords)
            .GroupBy(totalDay => totalDay.Time) // 按照小时和分钟进行分组
            .Select(group =>
            {
                var time = group.First().Time; // 从组中获取时间
                var totalSumForTimePoint = group.Sum(totalDay => totalDay.Value); // 在时间点分组中计算总和
                return new { Time = time, Total = totalSumForTimePoint };
            });
            List<SSAData> totalOneDays = new List<SSAData>();
            foreach (var item in totalSumByTimePoint)
            {
                totalOneDays.Add(new SSAData() { DateTime = item.Time, Total = (double)item.Total });
            }
            return new IStation.Dto.ApiResult<List<SSAData>>(totalOneDays) { Code = ApiResultCode.Success, Message = "Success" };
        }
 
 
 
        [Route("GetPumpRealData")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetPumpRealData(string startday, string endday, string resolution)
        {
            if (startday == null || endday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime Start;
            if (!DateTime.TryParse(startday, out Start))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            DateTime d;
            if (!DateTime.TryParse(endday, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var TotalWater = await ZyConnectHelper.GetTotalWaterByTimeRange_Out(Start, d, resolution);    //取水量
            return new IStation.Dto.ApiResult<List<RealScadaData>>(TotalWater) { Code = ApiResultCode.Success, Message = "Success" };
        }
    }
}