tangxu
2024-06-07 18a1a6faf82b1ad00d27f6fc7f5734c34aea32df
WebApi/Controllers/ZyPrjController.cs
@@ -29,8 +29,8 @@
        public string StartTime { get; set; }
        public string EndTime { get; set; }
        public int? StartOpenCount { get; set; } = -1;
        public double? TotalFlowIn { get; set; }//取水总量 (单位 吨)
        public string TotalFlowIn { get; set; }//取水总量 (单位 吨)
        public string StartReservoirLevel { get; set; }//开始水库水位(可空):单位m
        public string MaxReservoirLevel { get; set; }//最高水库水位限制(可空):单位m
        public string MaxReservoirLevelTime { get; set; }//最高水库水位限制时间(可空) 时间格式
@@ -56,6 +56,7 @@
        [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" };
@@ -91,17 +92,6 @@
                IStation.LogHelper.Error(string.Format("ZyPrjRequest Time:{0}-{1} 时间范围错误,不能超过24小时", start_timme, end_timme));
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "时间范围错误,不能超过24小时" };
            }
            if (request.TotalFlowIn == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "取水总量错误" };
            }
            if (request.TotalFlowIn < 10000)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "注意取水总量单位是吨,当前参数参数过小" };
            }
            IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,StartOpenCount:{2}, TotalFlowIn:{3}",
                start_timme, end_timme, request.StartOpenCount, request.TotalFlowIn));
            IStation.CalcModel.AnaRequest anaRequest = new AnaRequest();
            anaRequest.CalcOptType = (eCalcOptType)IStation.AnaGlobalParas.Setting.OptSortType;
@@ -109,8 +99,73 @@
            anaRequest.EndTime = end_timme;
            anaRequest.WaterLevels长江 = GetTide3Day(start_timme);
            anaRequest.TotalFlow取水总量 = request.TotalFlowIn;
            if (string.IsNullOrEmpty(request.TotalFlowIn))
            {
                if (string.IsNullOrEmpty(request.MaxReservoirLevel))
                {
                    IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  请输入取水总量或水位要求",
                start_timme, end_timme));
                    return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "请输入取水总量或水位要求" };
                }
            }
            else
            {
                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;
            }
            if (!string.IsNullOrEmpty(request.MaxReservoirLevel))
            {
                anaRequest.MaxLimitWaterBoxLevel = Convert.ToDouble(request.MaxReservoirLevel);
                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 = "预测数据获取失败,数据条无法匹配, 检查是否预测服务中断" };
                }
                anaRequest.SpaceFlowOut = new List<double>();
                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.SpaceFlowOut.Add(r.LastPredictValue / 10000);
                }
            }
            if (!string.IsNullOrEmpty(request.MaxReservoirLevelTime))
                anaRequest.MaxLimitWaterBoxTime = request.MaxReservoirLevelTime;
            //
            if (request.StartOpenCount == null)
            {
                anaRequest.StartOpenCount = -1;
@@ -120,38 +175,29 @@
                anaRequest.StartOpenCount = request.StartOpenCount.Value;
            }
            #region 陈行水库水位
            //陈行水库水位
            if (!string.IsNullOrEmpty(request.StartReservoirLevel))
            {
                anaRequest.CurrentWaterBoxLevel = Convert.ToDouble(request.StartReservoirLevel);
            }
            else
            {
                /*      var tagDict = new Dictionary<string, string> { { "_0402010201030103006", "陈行水库水位" } };
                     var input = new { search = "real", tablename = "陈行智能调度" };
                     var tags = tagDict.Keys.ToList();
                     var realScadaRecordList = ZyConnectHelper.GetRealScadaResultAsync(tags, input).GetAwaiter().GetResult();
                      if (realScadaRecordList != null && realScadaRecordList.Count() > 0)
                     {
                         anaRequest.CurrentWaterBoxLevel = realScadaRecordList.Last().Value;
                     }*/
                anaRequest.CurrentWaterBoxLevel = ZyConnectHelper.GetRealReservoirWaterLevel();
            }
            #endregion 陈行水库水位
            if (!string.IsNullOrEmpty(request.MaxReservoirLevel))
                anaRequest.MaxLimitWaterBoxLevel = Convert.ToDouble(request.MaxReservoirLevel);
            if (!string.IsNullOrEmpty(request.MaxReservoirLevelTime))
                anaRequest.MaxLimitWaterBoxTime = request.MaxReservoirLevelTime;
            var calcHelper = new IStation.CalcPrjHelper();
            AnaPrj defaultPrj = null;
            string error_info = calcHelper.Calc(anaRequest, out defaultPrj);
            LogHelper.Info(error_info);
            if (!string.IsNullOrEmpty(error_info) || defaultPrj == null)
            {
                IStation.LogHelper.Info(string.Format("Zy Prj  Time:{0}-{1} ,  计算错误, 原因是:{2} ,Request:::  ",
                start_timme, end_timme, error_info) + JsonHelper.Object2Json(anaRequest));
                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));
            }
            //保存方案
@@ -342,7 +388,7 @@
                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 = "最少开泵时间 设置过少" };
@@ -372,13 +418,18 @@
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "最大切泵次数 设置过长" };
            }
            model_setting.MaxPumpSwitchCountQ = 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 = "不许切泵时间 时间范围设置不合理" };
@@ -460,7 +511,7 @@
                }
            }
            if (dto.OpenPumpIndexs != null && dto.OpenPumpIndexs.Count() == 5)
            if (dto.OpenPumpIndexs != null)
            {
                List<int> penPumpIndexs = new List<int>();
                foreach (var ind in dto.OpenPumpIndexs)
@@ -469,6 +520,10 @@
                    {
                        return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "机泵开泵顺序设置参数不合理" };
                    }
                    else
                    {
                        penPumpIndexs.Add(ind);
                    }
                }
                if (penPumpIndexs.Distinct().Count() != 5)
                {