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
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
using IStation.CalcModel;
using IStation.Common;
using IStation.Dto;
using IStation.ZyDto;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
 
namespace IStation.WebApi.Controllers
{
    /// <summary>
    /// 计算配置
    /// </summary>
    [RoutePrefix("AnaPrj")]
    public class AnaPrjController : ApiController
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("Debug")]
        [HttpGet]
        public IStation.Dto.ApiResult Debug(string info)
        {
            IStation.LogHelper.Info("debug:" + info);
 
            //IStation.CalcModel.AnaPrj entity = new IStation.CalcModel.AnaPrj();
            //entity.ID = "123";
            //entity.StartTime = DateTime.Now;
            //entity.EndTime = DateTime.Now;
            //entity.Name = "232";
 
            //IStation.CalcModel.AnaRequest ana_request = new AnaRequest();
            //ana_request.TotalFlow取水总量 = 102;
 
            //var dal = new IStation.DAL.AnaProject();
            //dal.Insert(entity, ana_request);
 
            var dal = new IStation.DAL.AnaProject();
            var aa = dal.GetLastRecord();
            return new IStation.Dto.ApiResult<string>("ok");
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetDefaultPrjByStartDay")]
        [HttpGet]
        public IStation.Dto.ApiResult GetDefaultPrjByStartDay(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 格式不正确" };
            }
            IStation.CalcModel.AnaRequestBase requestBase = null;
            var prj = Common.HistoryAnaPrjFileHelper.GetByStartDay(d, out requestBase);
            return new IStation.Dto.ApiResult<IStation.CalcModel.AnaPrj>(prj);
        }
 
        /// <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长江 = ZyPrjController.GetTide3Day(Model.eTideSouce.Book, 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 ;//计算中用吨
                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(TotalFlowOut / count);
                }
                anaRequest.PerHourFlowOut = list;
            }
 
            #endregion 供水量
 
            //开始时的开机状态
            List<int> StartOpenPumpIndexArray = new List<int>();
            if (!string.IsNullOrEmpty(request.StartOpenPumpIndexArray))
            {
                var sss = request.StartOpenPumpIndexArray.Split(',');
                foreach (var item in sss)
                {
                    if (int.TryParse(item, out int index))
                    {
                        StartOpenPumpIndexArray.Add(index);
                    }
                }
            }
            anaRequest.IsKnownStartOpenPump = true;
            anaRequest.StartOpenPumpIndexArray = 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);
            //}
 
            #region 推送
 
            //if (!string.IsNullOrEmpty(request.IsSubmit) && Convert.ToBoolean(request.IsSubmit))
            //{
            //    Task.Run(() =>
            //    {
            //        _ = ZyProjectHelper.SubmitPrj(defaultPrj);
            //    });
            //}
 
            #endregion 推送
 
            return new IStation.Dto.ApiResult<AnaPrj>(defaultPrj) { 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) };
                }
 
                DateTime calcTime = DateTime.Now;//new DateTime(2024, 6, 11, 17, 10, 10);//
 
                IStation.CalcModel.ZyStartInfo zyStartInfo = new IStation.CalcModel.ZyStartInfo();
                if (prj.EndTimeOpenPumpStatus != null && prj.EndTimeOpenPumpStatus.Count > 0)
                {
                    zyStartInfo.OpenPumpStatus = string.Join(",", prj.EndTimeOpenPumpStatus);
                }
 
                var near_pt = prj.NearPointTime(calcTime);
                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) };
            }
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetExistStatus")]
        [HttpGet]
        public IStation.Dto.ApiResult GetExistStatus(int year, int month)
        {
            var list = HistoryAnaPrjFileHelper.GetExistStatus(year, month);
            if (list == null)
            {
                list = new List<string>();
            }
 
            return new IStation.Dto.ApiResult<List<string>>(list);
        }
 
        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="day"></param>
        ///// <returns></returns>
        //[Route("GetLastTimeOpenPumpCount")]
        //[HttpGet]
        //public IStation.Dto.ApiResult GetLastTimeOpenPumpCount(DateTime day)
        //{
        //    var  status = HistoryAnaPrjFileHelper.GetLastTime_OpenPumpStatus (day);
        //    if(status == null)
        //    {
        //        return new IStation.Dto.ApiResult<int>(-1);
        //    }
        //  else
        //    {
        //        return new IStation.Dto.ApiResult<int>(status.Count);
        //    }
        //}
 
        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="day"></param>
        ///// <returns></returns>
        //[Route("GetLastTimeOpenPumpStatus")]
        //[HttpGet]
        //public IStation.Dto.ApiResult GetLastTimeOpenPumpStatus(DateTime day)
        //{
        //    var  status = HistoryAnaPrjFileHelper.GetLastTime_OpenPumpStatus(day);
        //    if(status == null)
        //        return new IStation.Dto.ApiResult<string >("");
        //    else
        //    return new IStation.Dto.ApiResult<string >(string.Join(",",status));
        //}
 
        public class SavePrjRequest
        {
            public IStation.CalcModel.AnaSetting settting { get; set; }
            public IStation.CalcModel.AnaRequestBase requestBase { get; set; }
            public IStation.CalcModel.AnaPrj prj { get; set; }
            public bool isSubmit { get; set; }
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("SavePrj")]
        [HttpPost]
        public async Task<IStation.Dto.ApiResult> SavePrj([FromBody] SavePrjRequest request)
        {
            if (request == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            if (request.prj == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "prj null" };
            }
 
            var ret = Common.HistoryAnaPrjFileHelper.SavePrj(request.settting, request.requestBase, request.prj);
 
            //插入到数据库
            //var dal = new IStation.DAL.AnaProject();
            //dal.Insert(request.prj, request.requestBase);
 
            if (request.isSubmit)
            {
                string error = await ZyProjectHelper.SubmitPrj(request.prj);//, (id) => { IStation.LogHelper.Info("SavePrj success id=" + id); }, (info) => { IStation.LogHelper.Info("SavePrj failse info:" + info); });//.Result;
 
                if (!string.IsNullOrEmpty(error))
                {
                    IStation.LogHelper.Info("Submit prj finish (fail) day:" + request.requestBase.StartTime + ",error:" + error);
                    return new IStation.Dto.ApiResult<string>(error) { Code = ApiResultCode.Error };
                }
                else
                {
                    IStation.LogHelper.Info("Submit prj finish (success) day:" + request.requestBase.StartTime);
                }
            }
 
            return new IStation.Dto.ApiResult<bool>(ret) { Code = ApiResultCode.Success };
        }
    }
}