tangxu
2024-06-11 6954aa5b4a32aeba903b9532d5ff8e75c1b9548b
WebApi/Controllers/AnaPrjController.cs
@@ -1,6 +1,7 @@
using IStation.CalcModel;
using IStation.Common;
using IStation.Dto;
using IStation.ZyDto;
using System;
using System.Collections.Generic;
using System.IO;
@@ -11,9 +12,7 @@
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Http;
namespace IStation.WebApi.Controllers
{
@@ -76,8 +75,202 @@
            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);
            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.MaxReservoirLevel = 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.MaxReservoirLevelTime = request.MaxReservoirLevelTime;
            //开始时的开机状态
            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);
            }
            #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>
@@ -127,8 +320,10 @@
        public IStation.Dto.ApiResult GetLastTimeOpenPumpStatus(DateTime day)
        {
            var  status = HistoryAnaPrjFileHelper.GetLastTime_OpenPumpStatus(day);
            return new IStation.Dto.ApiResult<List<int>>(status);
            if(status == null)
                return new IStation.Dto.ApiResult<string >("");
            else
            return new IStation.Dto.ApiResult<string >(string.Join(",",status));
        }
        public class SavePrjRequest