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("GetPrjByDay")]
|
[HttpGet]
|
public IStation.Dto.ApiResult GetPrjByDay(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.GetByDay(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);
|
|
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>
|
/// <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 };
|
}
|
|
|
|
|
|
|
}
|
}
|