duheng
2024-05-29 b4189107397fb4e2da2bd9343d0eb85d44918932
WebApi/Controllers/PumpRunController.cs
@@ -1,9 +1,16 @@
using IStation.Dto;
using IStation.CalcModel.Tide;
using IStation.Common;
using IStation.Dto;
using IStation.Model;
using IStation.ZyModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;
using System.Web.Http;
using static IStation.Common.PumpHistoryDataFileHelper;
using System.Web.Http.Results;
namespace IStation.WebApi.Controllers
{
@@ -13,56 +20,403 @@
    [RoutePrefix("PumpRun")]
    public class PumpRunController : ApiController
    {
        private bool isdebug = true;
         /// <summary>
        ///  获取某一天参数
        /// <summary>
        ///  获取泵实际运行参数(一天)
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetByOneDay")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetByOneDay(DateTime startday)
        public async Task<IStation.Dto.ApiResult> GetByOneDay(string startday)
        {
            Dto.PumpRunParasDays pumpRunParasDay = new Dto.PumpRunParasDays();
            var PumpState = await ZyConnectHelper.GetPumpIsopen(startday,startday);
            //    var totalWater = await ZyConnectHelper.GetTotalWaterByDayRange_In(startday, startday);
            //   pumpRunParasDay.TotalWater = totalWater;
         //   var PumpTotalElecty = await ZyConnectHelper.GetPumpTotalElectricity(startday, startday);
        //    pumpRunParasDay.Totalelectricity = PumpTotalElecty;
            pumpRunParasDay.PumpRunTotalTime1 = PumpState[0];
            pumpRunParasDay.PumpRunTotalTime2 = PumpState[0];
            pumpRunParasDay.PumpRunTotalTime3 = PumpState[0];
            pumpRunParasDay.PumpRunTotalTime4 = PumpState[0];
            pumpRunParasDay.PumpRunTotalTime5 = PumpState[0];
            if (startday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime d;
            if (!DateTime.TryParse(startday, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            if (d > DateTime.Now.AddDays(1))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "天数不正确" };
            }
            RunParasData result = null;
            PumpRunParasHelper pumpRunParasHelper = new PumpRunParasHelper();
            result = Common.PumpRunParasHelper.ReadPumpRunParas(d);
            string geterroer = null;
            if (result != null && isdebug)
            {
                result.Electric = Math.Round(result.Electric, 2);
                result.Money = Math.Round(result.Money, 2);
                result.FlowIn = Math.Round(result.FlowIn, 2);
                if (result.Pump1.Count != 0)
                {
                    foreach (var item in result.Pump1)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
                if (result.Pump2.Count != 0)
                {
                    foreach (var item in result.Pump2)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
                if (result.Pump3.Count != 0)
                {
                    foreach (var item in result.Pump3)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
                if (result.Pump4.Count != 0)
                {
                    foreach (var item in result.Pump4)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
                if (result.Pump5.Count != 0)
                {
                    foreach (var item in result.Pump5)
                    {
                        item.Electric = Math.Round(item.Electric, 2);
                        item.Money = Math.Round(item.Money, 2);
                        item.FlowIn = Math.Round(item.FlowIn, 2);
                    }
                }
            }
            else
            {
                var PumpTotalElecty = await ZyConnectHelper.GetPumpTotalElectricity(d, d);    //获取某段时间的用电量
                var PumpState = await ZyConnectHelper.GetPumpOpenStatusBySingleDay(d);  //获取泵的开机情况
                var TotalWater = await ZyConnectHelper.GetTotalWaterByDayTimeRange_In(d, d);    //取水量
            return new IStation.Dto.ApiResult<Dto.PumpRunParasDays>(pumpRunParasDay) { Code = ApiResultCode.Success, Message = "Success" };
                /*        PumpHistoryDataFileHelper pumpHistoryDataFileHelper=new PumpHistoryDataFileHelper();
                        var TotalWater = pumpHistoryDataFileHelper.GetAllWaterData();
                        var PumpState = pumpHistoryDataFileHelper.getpumpstate();
                        var PumpTotalElecty = pumpHistoryDataFileHelper.GetAllEleData();*/
                result = pumpRunParasHelper.GetRunParasDatas(PumpState, TotalWater, PumpTotalElecty, d, out geterroer);
            }
            if (result != null)
            {
                pumpRunParasHelper.Save(result, out string error);
                //  LogHelper.Info(error);
            }
            if (geterroer != null)
            {
                LogHelper.Info(geterroer);
            }
            return new IStation.Dto.ApiResult<Model.RunParasData>(result) { Code = ApiResultCode.Success, Message = "Success" };
        }
        /// <summary>
        /// 获取泵历史运行参数
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetByHistoryOneDay")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetByHistoryOneDay(DateTime day)
        public IStation.Dto.ApiResult GetByHistoryOneDay(string day)
        {
            if (day == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime d;
           /* if (!DateTime.TryParse(day, out d))
            if (!DateTime.TryParse(day, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }*/
            }
            var prj = Common.PumpHistoryDataFileHelper.GetHistoryData(day);
            return new IStation.Dto.ApiResult<List<Common.PumpHistoryDataFileHelper.RunParasData>>(prj) { Code = ApiResultCode.Success, Message = "Success" };
            var history = Common.PumpRunParasHelper.ReadPumpRunParas(d);
            if (history == null)
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "Data null" };
            history.Electric = Math.Round(history.Electric, 2);
            history.Money = Math.Round(history.Money, 2);
            history.FlowIn = Math.Round(history.FlowIn, 2);
            if (history.Pump1.Count != 0)
            {
                foreach (var item in history.Pump1)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            if (history.Pump2.Count != 0)
            {
                foreach (var item in history.Pump2)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            if (history.Pump3.Count != 0)
            {
                foreach (var item in history.Pump3)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            if (history.Pump4.Count != 0)
            {
                foreach (var item in history.Pump4)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            if (history.Pump5.Count != 0)
            {
                foreach (var item in history.Pump5)
                {
                    item.Electric = Math.Round(item.Electric, 2);
                    item.Money = Math.Round(item.Money, 2);
                    item.FlowIn = Math.Round(item.FlowIn, 2);
                }
            }
            return new IStation.Dto.ApiResult<Model.RunParasData>(history) { Code = ApiResultCode.Success, Message = "Success" };
        }
        /// <summary>
        /// 一天内的总取水量和用电量
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetOneDayData")]
        [HttpGet]
        public IStation.Dto.ApiResult GetOneDayPumpWaterData(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 格式不正确" };
            }
            var WaterParas = Common.PumpRunParasHelper.ReadPumpWaterParas(d);
            return new IStation.Dto.ApiResult<List<Model.PumpWater>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
        }
        /// <summary>
        /// 获取时间段内的单管道供水数据
        /// </summary>
        /// <param name="startday"></param>
        /// <param name="endday"></param>
        /// <param name="sign"></param>
        /// <returns></returns>
        [Route("GetPumpOutDayWater")]
        [HttpGet]
        public IStation.Dto.ApiResult GetPumpOutWater(string startday, string endday, int sign)
        {
            if (startday == null || endday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime Start;
            if (!DateTime.TryParse(startday, out Start))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            DateTime End;
            if (!DateTime.TryParse(endday, out End))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var WaterParas = Common.PumpRunParasHelper.ReadPumpOutWater(Start, End, sign);
            return new IStation.Dto.ApiResult<List<Model.OutList>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
        }
        /// <summary>
        /// 获取所有的月合计数据(单管道)
        /// </summary>
        /// <returns></returns>
        [Route("GetPumpOutMonthWater")]
        [HttpGet]
        public IStation.Dto.ApiResult GetPumpOutMonthWater()
        {
            var WaterParas = Common.PumpRunParasHelper.ReadPumpMonthOutWater();
            return new IStation.Dto.ApiResult<List<Model.PumpOutWater>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
        }
        /*       /// <summary>
               /// 获取天的合并供水瞬时数据(总管)
               /// </summary>
               /// <param name="startday"></param> 开始日期
               /// <param name="endday"></param> 结束日期
               /// <returns></returns>
               [Route("GetPumpTotalDayOutWater")]
               [HttpGet]
               public IStation.Dto.ApiResult GetPumpTotalDayOutWater(string startday, string endday)
               {
                   if (startday == null || endday == null)
                   {
                       return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
                   }
                   DateTime Start;
                   if (!DateTime.TryParse(startday, out Start))
                   {
                       return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
                   }
                   DateTime End;
                   if (!DateTime.TryParse(endday, out End))
                   {
                       return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
                   }
                   var WaterParas = Common.PumpRunParasHelper.ReadPumpAllDayOutWater(Start, End);
                   return new IStation.Dto.ApiResult<List<Model.OutTotalOneDayList>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
               }*/
        /// <summary>
        /// 获取天的合并供水累计数据(总管)
        /// </summary>
        /// <param name="startday"></param> 开始日期
        /// <param name="endday"></param> 结束日期
        /// <returns></returns>
        [Route("GetPumpTotalDayAccOutWater")]
        [HttpGet]
        public IStation.Dto.ApiResult GetPumpTotalDayAccOutWater(string startday, string endday)
        {
            if (startday == null || endday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime Start;
            if (!DateTime.TryParse(startday, out Start))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            DateTime End;
            if (!DateTime.TryParse(endday, out End))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var WaterParas = Common.PumpRunParasHelper.ReadPumpAllDayOutAccWater(Start, End);
            return new IStation.Dto.ApiResult<List<Model.OutTotalOneDayList>>(WaterParas) { Code = ApiResultCode.Success, Message = "Success" };
        }
        /// <summary>
        /// 获取给预测水位用的数据的历史取水量 (从众毅)
        /// </summary>
        /// <param name="startday"></param> 开始日期
        /// <param name="endday"></param> 结束日期
        /// <returns></returns>
        [Route("GetPumpHistoryData")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetPumpHistoryData(string startday, string endday)
        {
            if (startday == null || endday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime Start;
            if (!DateTime.TryParse(startday, out Start))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            DateTime d;
            if (!DateTime.TryParse(endday, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var TotalWater = await ZyConnectHelper.GetTotalWaterByTimeRange_Out(Start, d, "1h");    //取水量
            /*     var one = TotalWater
                   .SelectMany(otdl => otdl.MonitorRecords);
                 var two = one.GroupBy(totalDay => totalDay.Time);
                 var three = two.Select(group =>
                 {
                     var time = group.First().Time; // 从组中获取时间
                     var totalSumForTimePoint = group.Sum(totalDay => totalDay.Value); // 在时间点分组中计算总和
                     return new { Time = time, Total = totalSumForTimePoint };
                 });*/
            var totalSumByTimePoint = TotalWater
              .SelectMany(otdl => otdl.MonitorRecords)
              .GroupBy(totalDay => totalDay.Time) // 按照小时和分钟进行分组
              .Select(group =>
              {
                  var time = group.First().Time; // 从组中获取时间
                  var totalSumForTimePoint = group.Sum(totalDay => totalDay.Value); // 在时间点分组中计算总和
                  return new { Time = time, Total = totalSumForTimePoint };
              });
            /*  var totalSumByTimePoint = TotalWater
                .SelectMany(otdl => otdl.pumpOutWater)
                .GroupBy(totalDay => totalDay.DateTime.TimeOfDay) // 按照小时和分钟进行分组
                .Select(group =>
                {
                    var time = group.First().DateTime.TimeOfDay; // 从组中获取时间
                    var totalSumForTimePoint = group.Sum(totalDay => totalDay.Total); // 在时间点分组中计算总和
                    return new { Time = time, Total = totalSumForTimePoint };
                });*/
            List<SSAData> totalOneDays = new List<SSAData>();
            foreach (var item in totalSumByTimePoint)
            {
                totalOneDays.Add(new SSAData() { DateTime = item.Time, Total = (double)item.Total });
            }
            return new IStation.Dto.ApiResult<List<SSAData>>(totalOneDays) { Code = ApiResultCode.Success, Message = "Success" };
        }
        /// <summary>
        /// 获取供水开机状态
        /// </summary>
        /// <param name="startday"></param> 开始日期
        /// <param name="endday"></param> 结束日期
        /// <returns></returns>
        [Route("GetPumpOpenState")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetPumpOpenState(string startday, string endday)
        {
            if (startday == null || endday == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime Start;
            if (!DateTime.TryParse(startday, out Start))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            DateTime d;
            if (!DateTime.TryParse(endday, out d))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            var result = await ZyConnectHelper.GetPumpOpenStatusDay(Start, d, "1h");
            var totalSumByTimePoint = result
            .SelectMany(otdl => otdl.MonitorRecords)
            .GroupBy(totalDay => totalDay.Time) // 按照小时和分钟进行分组
            .Select(group =>
            {
                var time = group.First().Time; // 从组中获取时间
                var totalSumForTimePoint = group.Sum(totalDay => totalDay.Value); // 在时间点分组中计算总和
                return new { Time = time, Total = totalSumForTimePoint };
            });
            List<SSAData> totalOneDays = new List<SSAData>();
            foreach (var item in totalSumByTimePoint)
            {
                totalOneDays.Add(new SSAData() { DateTime = item.Time, Total = (double)item.Total });
            }
            return new IStation.Dto.ApiResult<List<SSAData>>(totalOneDays) { Code = ApiResultCode.Success, Message = "Success" };
        }
    }
}
}