tangxu
2024-06-12 e3683a825a39950c35008b0320717f8e567594af
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
using Antlr.Runtime.Tree;
using IStation.CalcModel;
using IStation.Common;
using IStation.Dto;
using IStation.Model;
using IStation.ZyDto;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http; 
 
namespace IStation.ZyDto
{
    public class ZyPrjRequest
    {
        public ZyPrjRequest()
        { }
 
        public string StartTime { get; set; }
        public string EndTime { get; set; }
        //public int? StartOpenCount { get; set; } = -1;//已作废
 
        public string StartReservoirLevel { get; set; }//开始水库水位(可空):单位m
        public List<int> StartOpenPumpIndexArray { get; set; }//开始机泵开机状态(新)
 
        public string TotalFlowIn { get; set; }//取水总量 (单位 吨)
        public string TotalFlowOut { get; set; }//供水总量 (单位 吨) 不输入就用预测值
       
        public string MaxReservoirLevel { get; set; }//最高水库水位限制(可空):单位m
        public string MaxReservoirLevelTime { get; set; }//最高水库水位限制时间(可空) 时间格式
 
        public string IsSubmit { get; set; }//计算完,是否推送
        public string IsTemporary { get; set; }//是否是临时算一下(新)
    }
}
 
namespace IStation.WebApi.Controllers
{
    /// <summary>
    /// 计算配置
    /// </summary>
    [RoutePrefix("ZyPrj")]
    public class ZyPrjController : ApiController
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("Calc")]
        [HttpPost]
        public IStation.Dto.ApiResult Calc([FromBody] ZyPrjRequest request)
        {
            LogHelper.Info(JsonHelper.Object2Json(request));
            if (request == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "request is null" };
            }
            if (string.IsNullOrEmpty(request.StartTime))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "StartTime is null" };
            }
            if (string.IsNullOrEmpty(request.EndTime))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "EndTime is null" };
            }
 
            DateTime start_timme = DateTime.Now;
            DateTime end_timme = DateTime.Now;
            if (!DateTime.TryParse(request.StartTime, out start_timme))
            {
                IStation.LogHelper.Error(string.Format("ZyPrjRequest Time:{0}-{1} StartTime is error format", start_timme, end_timme));
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "StartTime is error format" };
            }
            if (!DateTime.TryParse(request.EndTime, out end_timme))
            {
                IStation.LogHelper.Error(string.Format("ZyPrjRequest Time:{0}-{1} EndTime is error format", start_timme, end_timme));
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "EndTime is error format" };
            }
            if (end_timme < start_timme.AddMinutes(60))
            {
                IStation.LogHelper.Error(string.Format("ZyPrjRequest Time:{0}-{1} 时间范围错误,间隔过短", start_timme, end_timme));
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "时间范围错误,间隔过短" };
            }
            if (end_timme > start_timme.AddDays(1))
            {
                IStation.LogHelper.Error(string.Format("ZyPrjRequest Time:{0}-{1} 时间范围错误,不能超过24小时", start_timme, end_timme));
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "时间范围错误,不能超过24小时" };
            }
 
            IStation.CalcModel.AnaRequest anaRequest = new AnaRequest();
            anaRequest.CalcOptType = (eCalcOptType)IStation.AnaGlobalParas.Setting.OptSortType;
            anaRequest.StartTime = start_timme;
            anaRequest.EndTime = end_timme;
            anaRequest.WaterLevels长江 = GetTide3Day(_tideSouce, start_timme);
 
            #region 取水总量
            if (!string.IsNullOrEmpty(request.TotalFlowIn))
            {
                double TotalFlowIn = 0;
                if (!double.TryParse(request.TotalFlowIn, out TotalFlowIn))
                {
                    IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  注意取水总量单位是吨,当前参数参数过小",
                start_timme, end_timme));
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "注意取水总量单位是吨,当前参数参数过小" };
                }
                if (TotalFlowIn < 10000)
                {
                    IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  注意取水总量单位是吨,当前参数参数过小",
                start_timme, end_timme));
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "注意取水总量单位是吨,当前参数参数过小" };
                }
 
                anaRequest.TotalFlowIn = TotalFlowIn;
            } 
            #endregion
 
            #region  水位要求
            if (!string.IsNullOrEmpty(request.MaxReservoirLevelTime))
            {
                anaRequest.MaxReservoirLevelTime = request.MaxReservoirLevelTime;
            }
 
            if (!string.IsNullOrEmpty(request.MaxReservoirLevel))
            {
                double MaxReservoirLevel = 0;
                if (!double.TryParse(request.MaxReservoirLevel, out MaxReservoirLevel))
                {
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "请输入合理的水位要求值" };
                }
                if (MaxReservoirLevel < 1 || MaxReservoirLevel > 10)
                {
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "请输入合理的水位要求值" };
                }
                anaRequest.MaxReservoirLevel = MaxReservoirLevel;
            }
            else
            {
                if (string.IsNullOrEmpty(request.TotalFlowIn))
                { 
                    IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  请输入取水总量或水位要求",
                start_timme, end_timme));
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "请输入取水总量或水位要求" };
                }
            } 
            #endregion
 
            #region 供水量
            if (string.IsNullOrEmpty(request.TotalFlowOut))
            {//用预测值
                #region 用预测值
                anaRequest.PerHourFlowOut = new List<double>();
                IStation.DAL.WaterPredictRecord dal = new DAL.WaterPredictRecord();
                var water_records = dal.GetByHourRangle(start_timme, end_timme);
                //LogHelper.Info(water_records.Count.ToString());
                if (water_records == null)
                {
                    IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  水位预测数据为空,请先保持水位预测服务正常运行",
                start_timme, end_timme));
                    return new IStation.Dto.ApiResult<string>() { Code = ApiResultCode.Error, Data = "水位预测数据为空,请先保持水位预测服务正常运行" };
                }
                var count = (end_timme - start_timme).TotalHours;
                if (water_records.Count != count)
                {
                    IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  预测数据获取失败,数据条无法匹配, 检查是否预测服务中断",
                start_timme, end_timme));
                    return new IStation.Dto.ApiResult<string>() { Code = ApiResultCode.Error, Data = "预测数据获取失败,数据条无法匹配, 检查是否预测服务中断" };
                }
 
                foreach (var r in water_records)
                {
                    if (r.States == 0)
                    {
                        var error_info11 = string.Format("预测数据异常,无法进行水位计算:{0} {1},{2}", r.DayHour, r.LastPredictValue, r.Description);
                        IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} , {2}}",
                start_timme, end_timme, error_info11));
                        return new IStation.Dto.ApiResult<string>()
                        {
                            Code = ApiResultCode.Error,
                            Data = error_info11
                        };
                    }
                    anaRequest.PerHourFlowOut.Add(r.LastPredictValue );//计算中用吨
                }
                #endregion
            }
            else
            {
                double TotalFlowOut = 0;
                if (!double.TryParse(request.TotalFlowOut, out TotalFlowOut))
                {
                    IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  注意取水总量单位是吨,当前参数参数过小",
                start_timme, end_timme));
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "注意供水总量单位是吨,当前参数参数过小" };
                }
                if (TotalFlowOut < 10000)
                {
                    IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  注意取水总量单位是吨,当前参数参数过小",
                start_timme, end_timme));
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "注意供水总量单位是吨,当前参数参数过小" };
                }
                //TotalFlowOut = TotalFlowOut / 10000;//计算中用吨
                List<double> list = new List<double>();
                var count = (end_timme - start_timme).TotalHours;
                for (var d = start_timme; d <= end_timme; d = d.AddHours(1))
                {
                    list.Add(Math.Round( TotalFlowOut / count,1));
                }
                anaRequest.PerHourFlowOut = list;
            } 
            #endregion
 
 
            //开始时的开机状态
            if (request.StartOpenPumpIndexArray == null)
            {
                IStation.LogHelper.Info(" 未设置开始时机泵开停状态 (Error: StartOpenPumpIndexArray is null)");
                return new IStation.Dto.ApiResult<string>() { Code = ApiResultCode.Error, Data = "未设置开始时机泵开停状态 (Error: StartOpenPumpIndexArray is null)" };
            }
            anaRequest.IsKnownStartOpenPump = true;
            anaRequest.StartOpenPumpIndexArray = request.StartOpenPumpIndexArray;
 
 
            //陈行水库水位
            if (string.IsNullOrEmpty(request.StartReservoirLevel))
            {
                IStation.LogHelper.Info(" 未设置开始时水库水位 (Error: StartReservoirLevel is null)");
                return new IStation.Dto.ApiResult<string>() { Code = ApiResultCode.Error, Data = "未设置开始时水库水位 (Error: StartReservoirLevel is null) " };
            }
            anaRequest.StartReservoirLevel = Convert.ToDouble(request.StartReservoirLevel);
            if (anaRequest.StartReservoirLevel < 2)
            {
                IStation.LogHelper.Info(string.Format("  设置开始时水库水位的数据过小 (Error: StartReservoirLevel is {0}})",
                    anaRequest.StartReservoirLevel));
                return new IStation.Dto.ApiResult<string>()
                {
                    Code = ApiResultCode.Error,
                    Data = string.Format("  设置开始时水库水位的数据过小 (Error: StartReservoirLevel is {0}})",
                    anaRequest.StartReservoirLevel)
                };
            }
            bool isTemp = false;
            if (!string.IsNullOrEmpty(request.IsTemporary))
            {
                isTemp = Convert.ToBoolean(request.IsTemporary);
            }
 
 
            var calcHelper = new IStation.CalcPrjHelper();
            AnaPrj defaultPrj = null;
            string error_info = calcHelper.Calc(anaRequest, out defaultPrj);
           
            if (!string.IsNullOrEmpty(error_info) || defaultPrj == null)
            {
                var er = string.Format("Zy Prj  Time:{0}-{1} ,  计算错误, 原因是:{2} ,Request:::  {3}",
                start_timme, end_timme, error_info, JsonHelper.Object2Json(anaRequest));
                
                IStation.LogHelper.Info(er);
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "计算错误, 原因是:" + error_info };
            }
            else
            {
                IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  计算成功", start_timme, end_timme));
            }
 
            //保存方案
            if (!isTemp)
            {
                var ret = Common.HistoryAnaPrjFileHelper.SavePrj(IStation.AnaGlobalParas.Setting, anaRequest, defaultPrj);
            }
     
 
            var dto_prj = ToPrjDto(defaultPrj);
 
            #region 推送
 
            if (!string.IsNullOrEmpty(request.IsSubmit) && Convert.ToBoolean(request.IsSubmit))
            {
                Task.Run(() =>
                {
                    _ = ZyProjectHelper.SubmitPrj(defaultPrj);
                });
            }
 
            #endregion 推送
 
            return new IStation.Dto.ApiResult<IStation.ZyDto.SubmitPrj>(dto_prj) { Code = ApiResultCode.Success };
        }
 
        /// <summary>
        /// 获取初始水位,和当时的开机状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("GetAnaStartPara")]
        [HttpGet]
        public IStation.Dto.ApiResult GetAnaStartPara(string AnaStartTime)
        {
            if (string.IsNullOrEmpty(AnaStartTime)) { return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" }; }
 
            try
            {
                DateTime last_end_time;
                if (!DateTime.TryParse(AnaStartTime, out last_end_time))
                {
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
                }
 
 
 
                IStation.CalcModel.AnaRequestBase requestBase;
                var prj = Common.HistoryAnaPrjFileHelper.GetByEndTime(last_end_time, out requestBase);
                if (prj == null)
                {
                    IStation.LogHelper.Info(string.Format("GetAnaStartPara 未找到{0}结束的分析项目", AnaStartTime));
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = string.Format("未找到{0}结束的分析项目", AnaStartTime) };
                }
 
 
 
                IStation.CalcModel.ZyStartInfo zyStartInfo = new IStation.CalcModel.ZyStartInfo();
                //zyStartInfo.OpenPumpStatus = prj.EndTimeOpenPumpStatus;
                if (prj.EndTimeOpenPumpStatus != null && prj.EndTimeOpenPumpStatus.Count > 0)
                {
                    zyStartInfo.OpenPumpStatus = string.Join(",", prj.EndTimeOpenPumpStatus);
                }
 
 
                var near_pt = prj.NearPointTime(DateTime.Now);
                if (near_pt == null)
                {
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = string.Format("在项目中, 未找到{0}的水库水位数据", DateTime.Now) };
                }
 
                var new_yuce = near_pt.WaterLevelH;//昨天预测水位
                double now_level = ZyConnectHelper.GetRealReservoirWaterLevel();//实时水位
                if (prj.BlockTimes == null || prj.BlockTimes.Count() == 0)
                {
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "在项目中, BlockTimes值为空" };
                }
                if (prj.BlockTimes.Last().PointTimes == null || prj.BlockTimes.Last().PointTimes.Count() == 0)
                {
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "在项目中, 最后一个 BlockTime的 PointTimes 属性值为空" };
                }
                var lz = prj.BlockTimes.Last().PointTimes.Last().WaterLevelH;
                double start_yuce = lz + now_level - new_yuce;
 
 
               // IStation.LogHelper.Info(string.Format("GetAnaStartPara  {0},{1},{2},{3} ", lz , now_level , new_yuce, start_yuce));
 
                zyStartInfo.ReservoirWaterLevel = start_yuce;
                return new IStation.Dto.ApiResult<IStation.CalcModel.ZyStartInfo>(zyStartInfo) { Code = ApiResultCode.Success };
            }
            catch (Exception ex)
            {
                IStation.LogHelper.Error("GetAnaStartPara line 277", ex);
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = string.Format("在项目中, 接口报错: {0} ", ex.Message) };
            }
        }
 
        #region 换算成DTO
 
        private IStation.ZyDto.SubmitPrj ToPrjDto(AnaPrj defaultPrj)
        {
            IStation.ZyDto.SubmitPrj dto_prj = new ZyDto.SubmitPrj();
 
            if (string.IsNullOrEmpty(defaultPrj.ID))
            {
                var time = DateTime.Now;
                defaultPrj.ID = $"{time.Year}{time.Month}{time.Day}{time.Hour}{time.Minute}{time.Second}";
            }
            dto_prj.schemeID = defaultPrj.ID;
            dto_prj.Day = defaultPrj.StartTime.ToString("yyyy-MM-dd");
            dto_prj.StartTime = defaultPrj.StartTime.ToString("yyyy-MM-dd HH:mm:ss");
            dto_prj.EndTime = defaultPrj.EndTime.ToString("yyyy-MM-dd HH:mm:ss");
 
            dto_prj.FlowTotal = Math.Round(defaultPrj.SumFlow, 0);
            dto_prj.PowerTotal = Math.Round(defaultPrj.SumPower, 0);
            dto_prj.MoneyTotal = Math.Round(defaultPrj.SumMoney, 0);
 
            dto_prj.BlockTimes = new List<ZyDto.SubmitPrjBlockTime>();
            foreach (var bt in defaultPrj.BlockTimes)
            {
                if (bt.OpenPumpIndexs  == null)
                    continue;
                ZyDto.SubmitPrjBlockTime dto_item = new ZyDto.SubmitPrjBlockTime();
                dto_item.StartTime = bt.StartTime;
                dto_item.EndTime = bt.EndTime;
                dto_item.OpenPumpCount = bt.OpenPumpIndexs.Count;
                dto_item.OpenPumpIndexs = bt.OpenPumpIndexs;
                dto_item.StartSwitchGroupID = bt.StartSwitchGroupID;
             
                if (bt.PointTimes != null)
                {
                    dto_item.Records = new List<SubmitPrjTime>();
                    foreach (var bt2 in bt.PointTimes)
                    {
                        dto_item.Records.Add(new SubmitPrjTime(bt2));
                    }
                }
 
                dto_prj.BlockTimes.Add(dto_item);
            }
 
            dto_prj.Switchs = new List<SubmitPrjSwitchInfo>();
            if(defaultPrj.PumpSwitchs != null)
            {
                foreach (var bt in defaultPrj.PumpSwitchs)
                { 
                    ZyDto.SubmitPrjSwitchInfo dto_item = new ZyDto.SubmitPrjSwitchInfo();
                    dto_item.GroupID = bt.GroupID;//分组ID
                    dto_item.PumpIndex = bt.PumpIndex;//泵角标
                    dto_item.Time = bt.Time.ToString("yyyy-MM-dd HH:mm:ss");//时间
                    dto_item.SwitchType = bt.SwitchType;///1 开机 0 关机  
 
                    dto_prj.Switchs.Add(dto_item);
                }
            }
 
 
            return dto_prj;
        }
 
        #endregion 换算成DTO
 
 
        /// <summary>
        ///  获取某一天,最近一条
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetHistoryByDay")]
        [HttpGet]
        public IStation.Dto.ApiResult GetHistoryByDay(string day)
        {
            if (string.IsNullOrEmpty(day))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "入参day 为空" };
            }
            DateTime d;
            if (!DateTime.TryParse(day, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            IStation.CalcModel.AnaRequestBase requestBase = null;
            var prj = Common.HistoryAnaPrjFileHelper.GetByStartDay(d, out requestBase);
 
            if (prj != null)
            {
                var dto_prj = ToPrjDto(prj);
                return new IStation.Dto.ApiResult<IStation.ZyDto.SubmitPrj>(dto_prj) { Code = ApiResultCode.Success };
            }
            else
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Alert, Message = "未找到项目" };
            }
        }
 
 
        /// <summary>
        ///  获取最近一条
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetLastHistoryRecord")]
        [HttpGet]
        public IStation.Dto.ApiResult GetLastHistoryRecord()
        {
            var dal = new IStation.DAL.AnaProject();
            var record = dal.GetLastRecord();
            if (record == null)
                return null;
 
            IStation.CalcModel.AnaRequestBase requestBase = null;
            var prj = Common.HistoryAnaPrjFileHelper.GetByStartTime(record.StartTime, out requestBase);
            if (prj != null)
            {
                var dto_prj = ToPrjDto(prj);
                return new IStation.Dto.ApiResult<IStation.ZyDto.SubmitPrj>(dto_prj) { Code = ApiResultCode.Success };
            }
            else
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Alert, Message = "未找到项目" };
            }
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        [Route("GetSetting")]
        [HttpGet]
        public IStation.Dto.ApiResult GetSetting()
        {
            var model_setting = IStation.AnaGlobalParas.Setting;
            IStation.Dto.ZyPrjAnaSetting dto_setting = new ZyPrjAnaSetting();
            dto_setting.MinOpenTimeMinute = model_setting.MinOpenTimeMinute;
            dto_setting.MinSwitchTimeMinute = model_setting.MinSwitchTimeMinute;
            dto_setting.MaxPumpSwitchCount = model_setting.MaxPumpSwitchCount;
            dto_setting.OptSortType = model_setting.OptSortType;
            dto_setting.OpenPumpIndexSequence = model_setting.OpenPumpIndexSequence;
            dto_setting.ClosePumpIndexSequence = model_setting.ClosePumpIndexSequence;
            dto_setting.SwitchPumpMinuteSpace = model_setting.SwitchPumpMinuteSpace;
            dto_setting.OptimalPumpIndexSequence = model_setting.OptimalPumpIndexSequence;
 
            if (model_setting.SwitchPumpIgnoreTimes != null)
            {
                dto_setting.SwitchPumpIgnoreTimes = new List<ZyPrjAnaSetting.TimeRangeZy>();
                foreach (var m in model_setting.SwitchPumpIgnoreTimes)
                {
                    dto_setting.SwitchPumpIgnoreTimes.Add(new ZyPrjAnaSetting.TimeRangeZy()
                    {
                        IsUse = m.IsUse,
                        StartHour = m.StartHour,
                        StartMinute = m.StartMinute,
                        EndHour = m.EndHour,
                        EndMinute = m.EndMinute,
                    });
                }
            }
            if (model_setting.OpenPumpTimes != null)
            {
                dto_setting.OpenPumpTimes = new List<ZyPrjAnaSetting.TimeRangeZy>();
                foreach (var m in model_setting.OpenPumpTimes)
                {
                    dto_setting.OpenPumpTimes.Add(new ZyPrjAnaSetting.TimeRangeZy()
                    {
                        IsUse = m.IsUse,
                        StartHour = m.StartHour,
                        StartMinute = m.StartMinute,
                        EndHour = m.EndHour,
                        EndMinute = m.EndMinute,
                    });
                }
            }
 
            return new IStation.Dto.ApiResult<IStation.Dto.ZyPrjAnaSetting>(dto_setting);
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="ana"></param>
        /// <returns></returns>
        [Route("SaveSetting")]
        [HttpPost]
        public IStation.Dto.ApiResult SaveSetting([FromBody] IStation.Dto.ZyPrjAnaSetting dto)
        {
            if (dto == null)
            {
                return new IStation.Dto.ApiResult<bool>(false);
            }
            var model_setting = IStation.AnaGlobalParas.Setting;
            IStation.LogHelper.Info("ZyPrj SaveSetting");
            if (dto.MinOpenTimeMinute < 5)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "最少开泵时间 设置过少" };
            }
            if (dto.MinOpenTimeMinute > 120)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "最少开泵时间 设置过长" };
            }
            model_setting.MinOpenTimeMinute = dto.MinOpenTimeMinute;
 
            if (dto.MinSwitchTimeMinute < 5)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "最少切泵时间 设置过少" };
            }
            if (dto.MinSwitchTimeMinute > 120)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "最少切泵时间 设置过长" };
            }
            model_setting.MinSwitchTimeMinute = dto.MinSwitchTimeMinute;
 
            if (dto.MaxPumpSwitchCount < 1)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "最大切泵次数 设置过少" };
            }
            if (dto.MaxPumpSwitchCount > 10)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "最大切泵次数 设置过长" };
            }
            model_setting.MaxPumpSwitchCount = dto.MaxPumpSwitchCount;
            if (dto.OptSortType >= 2)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "OptSortType 只能设置 0 或者 1,  0代表 用电量最少  1代表 电费最少" };
            }
            model_setting.OptSortType = dto.OptSortType;
            if (dto.SwitchPumpIgnoreTimes != null)
            {
                model_setting.SwitchPumpIgnoreTimes = new List<CalcModel.AnaSetting.TimeRange>();
                foreach (var m in dto.SwitchPumpIgnoreTimes)
                {
                    if (m.StartHour == 0 && m.EndHour == 0 && m.StartMinute == 0 && m.EndMinute == 0)
                        continue;
                    if (m.StartHour < 0 || m.StartHour > 24)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "不许切泵时间 时间范围设置不合理" };
                    }
                    if (m.EndHour < 0 || m.EndHour > 24)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "不许切泵时间 时间范围设置不合理" };
                    }
                    if (m.StartMinute < 0 || m.StartMinute > 60)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "不许切泵时间 时间范围设置不合理" };
                    }
                    if (m.EndMinute < 0 || m.EndMinute > 60)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "不许切泵时间 时间范围设置不合理" };
                    }
                    if (m.StartHour > m.EndHour)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "不许切泵时间 时间范围设置不合理" };
                    }
                    if (m.StartHour == m.EndHour)
                    {
                        if (m.StartMinute > m.EndMinute)
                        {
                            return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "不许切泵时间 时间范围设置不合理" };
                        }
                    }
                    model_setting.SwitchPumpIgnoreTimes.Add(new CalcModel.AnaSetting.TimeRange()
                    {
                        IsUse = m.IsUse,
                        StartHour = m.StartHour,
                        StartMinute = m.StartMinute,
                        EndHour = m.EndHour,
                        EndMinute = m.EndMinute,
                    });
                }
            }
 
            if (dto.OpenPumpTimes != null)
            {
                model_setting.OpenPumpTimes = new List<CalcModel.AnaSetting.TimeRange>();
                foreach (var m in dto.OpenPumpTimes)
                {
                    if (m.StartHour < 0 || m.StartHour > 24)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "开泵时间 时间范围设置不合理" };
                    }
                    if (m.EndHour < 0 || m.EndHour > 24)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "开泵时间 时间范围设置不合理" };
                    }
                    if (m.StartMinute < 0 || m.StartMinute > 60)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "开泵时间 时间范围设置不合理" };
                    }
                    if (m.EndMinute < 0 || m.EndMinute > 60)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "开泵时间 时间范围设置不合理" };
                    }
                    if (m.StartHour > m.EndHour)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "开泵时间 时间范围设置不合理" };
                    }
                    if (m.StartHour == m.EndHour)
                    {
                        if (m.StartMinute > m.EndMinute)
                        {
                            return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "开泵时间 时间范围设置不合理" };
                        }
                    }
                    model_setting.OpenPumpTimes.Add(new CalcModel.AnaSetting.TimeRange()
                    {
                        IsUse = m.IsUse,
                        StartHour = m.StartHour,
                        StartMinute = m.StartMinute,
                        EndHour = m.EndHour,
                        EndMinute = m.EndMinute,
                    });
                }
            }
 
            if (dto.OpenPumpIndexSequence != null)
            {
                List<int> penPumpIndexs = new List<int>();
                foreach (var ind in dto.OpenPumpIndexSequence)
                {
                    if (ind < 0 || ind > 5)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "开泵顺序设置参数不合理" };
                    }
                    else
                    {
                        penPumpIndexs.Add(ind);
                    }
                }
                if (penPumpIndexs.Distinct().Count() != 5)
                {
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "开泵顺序设置参数不合理" };
                }
                model_setting.OpenPumpIndexSequence = penPumpIndexs.ToArray();
            }
 
            if (dto.ClosePumpIndexSequence != null)
            {
                List<int> penPumpIndexs = new List<int>();
                foreach (var ind in dto.ClosePumpIndexSequence)
                {
                    if (ind < 0 || ind > 5)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "关泵顺序设置参数不合理" };
                    }
                    else
                    {
                        penPumpIndexs.Add(ind);
                    }
                }
                if (penPumpIndexs.Distinct().Count() != 5)
                {
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "关泵顺序设置参数不合理" };
                }
                model_setting.ClosePumpIndexSequence = penPumpIndexs.ToArray();
            }
 
            if (dto.OptimalPumpIndexSequence != null)
            {
                List<int> penPumpIndexs = new List<int>();
                foreach (var ind in dto.OptimalPumpIndexSequence)
                {
                    if (ind < 0 || ind > 5)
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "调度泵优先度顺序设置参数不合理" };
                    }
                    else
                    {
                        penPumpIndexs.Add(ind);
                    }
                }
                if (penPumpIndexs.Distinct().Count() != 5)
                {
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "调度泵优先度顺序设置参数不合理" };
                }
                model_setting.OptimalPumpIndexSequence = penPumpIndexs.ToArray();
            }
 
            return new IStation.Dto.ApiResult<bool>(IStation.Common.AnaSetting.SaveSetting(model_setting));
        }
 
        private Model.eTideSouce _tideSouce = Model.eTideSouce.Book;
 
        public static List<Model.TimeWaterLevel> GetTide3Day(Model.eTideSouce  tideSouce, DateTime calc_day)
        {
            DateTime _rangDay1, _rangDay2, _rangDay3;
            string error1, error2, error3;
            List<Model.TimeWaterLevel> waterLevels长江1, waterLevels长江2, waterLevels长江3;
            if (calc_day == DateTime.Today)
            {
                _rangDay1 = calc_day.AddDays(-1);
                _rangDay2 = calc_day;
                _rangDay3 = calc_day.AddDays(1);
            }
            else if (calc_day == DateTime.Today.AddDays(1))
            {
                _rangDay1 = calc_day.AddDays(-2);
                _rangDay2 = calc_day.AddDays(-1);
                _rangDay3 = calc_day;
            }
            else
            {
                _rangDay1 = calc_day.AddDays(-1);
                _rangDay2 = calc_day;
                _rangDay3 = calc_day.AddDays(1);
            }
            waterLevels长江1 = TideHelper.GetByDay(tideSouce, _rangDay1, out error1);
            waterLevels长江2 = TideHelper.GetByDay(tideSouce, _rangDay2, out error2);
            waterLevels长江3 = TideHelper.GetByDay(tideSouce, _rangDay3, out error3);
 
            List<Model.TimeWaterLevel> timeValues = new List<Model.TimeWaterLevel>();
            if (waterLevels长江1 != null)
            {
                timeValues.AddRange(waterLevels长江1);
            }
 
            if (waterLevels长江2 != null)
            {
                timeValues.AddRange(waterLevels长江2);
            }
 
            if (waterLevels长江3 != null)
            {
                timeValues.AddRange(waterLevels长江3);
            }
 
            return timeValues;
        }
    }
}