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
{
///
/// 计算配置
///
[RoutePrefix("AnaPrj")]
public class AnaPrjController : ApiController
{
///
///
///
///
///
[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("ok");
}
///
///
///
///
///
[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(prj);
}
///
/// 远程计算, 用于调试众毅
///
///
///
[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();
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() { 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() { 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()
{
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 list = new List();
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 StartOpenPumpIndexArray = new List();
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() { 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()
{
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(defaultPrj) { Code = ApiResultCode.Success };
}
///
/// 获取初始水位,和当时的开机状态, 用于调试众毅
///
///
///
[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(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) };
}
}
///
///
///
///
///
[Route("GetExistStatus")]
[HttpGet]
public IStation.Dto.ApiResult GetExistStatus(int year, int month)
{
var list = HistoryAnaPrjFileHelper.GetExistStatus(year, month);
if (list == null)
{
list = new List();
}
return new IStation.Dto.ApiResult>(list);
}
/////
/////
/////
/////
/////
//[Route("GetLastTimeOpenPumpCount")]
//[HttpGet]
//public IStation.Dto.ApiResult GetLastTimeOpenPumpCount(DateTime day)
//{
// var status = HistoryAnaPrjFileHelper.GetLastTime_OpenPumpStatus (day);
// if(status == null)
// {
// return new IStation.Dto.ApiResult(-1);
// }
// else
// {
// return new IStation.Dto.ApiResult(status.Count);
// }
//}
/////
/////
/////
/////
/////
//[Route("GetLastTimeOpenPumpStatus")]
//[HttpGet]
//public IStation.Dto.ApiResult GetLastTimeOpenPumpStatus(DateTime day)
//{
// var status = HistoryAnaPrjFileHelper.GetLastTime_OpenPumpStatus(day);
// if(status == null)
// return new IStation.Dto.ApiResult("");
// else
// return new IStation.Dto.ApiResult(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; }
}
///
///
///
///
///
[Route("SavePrj")]
[HttpPost]
public async Task 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(error) { Code = ApiResultCode.Error };
}
else
{
IStation.LogHelper.Info("Submit prj finish (success) day:" + request.requestBase.StartTime);
}
}
return new IStation.Dto.ApiResult(ret) { Code = ApiResultCode.Success };
}
}
}