tangxu
2024-04-30 26d8996e55c33186800031f7c305688035bb3182
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static IStation.Service.SSAPredictHelper;
 
namespace IStation.Application
{
    public class ConnectHelper
    {
 
        public class OutTotalDayList
        {
            public DateTime DateTime { get; set; }
            public List<TotalOneDay> pumpOutWater { get; set; }
        }
 
        public class TotalOneDay
        {
            public DateTime DateTime { get; set; }
            public double Total { get; set; }
        }
 
 
 
        //获取总管天参数(瞬时总计)
        public static List<OutTotalDayList> ReadPumpAllDayOutWater(DateTime StartDay, DateTime EndDay)
        {
          var root_folder = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(), "供水参数");
         // var root_folder = "D:\\IStation\\ChErWebApi\\Data\\供水参数";
            if (!System.IO.Directory.Exists(root_folder))
            {
                return null;
            }
            var month_folder = System.IO.Path.Combine(root_folder, "瞬时总计");
            //  day.ToString("yyyy-MM")
            List<OutTotalDayList> outTotalDayLists = new List<OutTotalDayList>();
 
            for (DateTime date = StartDay; date < EndDay; date = date.AddDays(1))
            {
                string Path = System.IO.Path.Combine(month_folder,
                string.Format("{0}", date.ToString("yyyy-MM")));
                string filepath = System.IO.Path.Combine(Path,
                string.Format("{0}.json", date.ToString("yyyy-MM-dd")));
                if (!File.Exists(filepath))
                {
                    return null;
                }
                string Text = File.ReadAllText(filepath);
                var outwater = Yw.JsonHelper.Json2Object<List<TotalOneDay>>(Text);
                outTotalDayLists.Add(new OutTotalDayList() { pumpOutWater = outwater, DateTime = outwater[0].DateTime });
            }
            return outTotalDayLists;
        }
 
 
 
        //获取总管天参数(累计总计)
        public static List<OutTotalDayList> ReadPumpAllDayAccOutWater(DateTime StartDay, DateTime EndDay)
        {
            var root_folder = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(), "供水参数");
            // var root_folder = "D:\\IStation\\ChErWebApi\\Data\\供水参数";
            if (!System.IO.Directory.Exists(root_folder))
            {
                return null;
            }
            var month_folder = System.IO.Path.Combine(root_folder, "累计总计");
            //  day.ToString("yyyy-MM")
            List<OutTotalDayList> outTotalDayLists = new List<OutTotalDayList>();
            if(StartDay<new DateTime(2022, 11, 2))
            {
                return null;
            }
            for (DateTime date = StartDay; date < EndDay; date = date.AddDays(1))
            {
                string Path = System.IO.Path.Combine(month_folder,
                string.Format("{0}", date.ToString("yyyy-MM")));
                string filepath = System.IO.Path.Combine(Path,
                string.Format("{0}.json", date.ToString("yyyy-MM-dd")));
                 if (!File.Exists(filepath))
                {
                    var  reuslt=  GetByPumpOneDayWaterData(date, date);
                    outTotalDayLists.Add(new OutTotalDayList() { pumpOutWater = reuslt, DateTime = reuslt[0].DateTime });
                 }
                else
                {
                    string Text = File.ReadAllText(filepath);
                    var outwater = Yw.JsonHelper.Json2Object<List<TotalOneDay>>(Text);
                    outTotalDayLists.Add(new OutTotalDayList() { pumpOutWater = outwater, DateTime = outwater[0].DateTime });
                  }
              }
            return outTotalDayLists;
        }
 
 
        public List<TotalOneDay> GetMeanValue(List<OutTotalDayList> realHistoryData)
        {
            List<TotalOneDay> listoutwater = new List<TotalOneDay>();
            List<OutTotalDayList> currentlist = new List<OutTotalDayList>(realHistoryData); // 创建一个新的列表,并将 realHistoryData 的内容复制过来
 
            for (int i = 0; i < 2; i++)
            {
                var totalSumByTimePoint = currentlist
                    .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<TotalOneDay> demo = new List<TotalOneDay>();
                foreach (var item in totalSumByTimePoint)
                {
                    listoutwater.Add(new TotalOneDay() { DateTime = currentlist.Last().DateTime.AddDays(1) + item.Time, Total = Math.Round(item.Total / currentlist.Count, 2) });
                    demo.Add(new TotalOneDay() { DateTime = currentlist.Last().DateTime.AddDays(1) + item.Time, Total = Math.Round(item.Total / currentlist.Count, 2) });
                }
 
                currentlist.Add(new OutTotalDayList() { DateTime = currentlist.Last().DateTime.AddDays(1), pumpOutWater = demo });
                currentlist.RemoveAt(0);
                demo = new List<TotalOneDay>();
            }
            return listoutwater;
        }
 
 
 
 
 
        public  static  List<TotalOneDay> GetByPumpOneDayWaterData(DateTime starttime,DateTime endtime)
        {
            // string apiServiceUrl = System.Configuration.ConfigurationManager.AppSettings["ApiServiceUrl"];
            string apiServiceUrl = "http://47.100.245.85:86/";
 
            string url = string.Format(@"{0}/PumpRun/GetPumpHistoryData?startday={1}&endday={2}", apiServiceUrl,starttime,endtime);
            string Accept = "application/json";
            //创建Web访问对象
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = "GET";
            //myRequest.Accept = "application/json";
            // myRequest.ContentType = "application/json";  // //Content-Type: application/x-www-form-urlencoded 
            myRequest.AutomaticDecompression = DecompressionMethods.GZip;
            myRequest.Accept = Accept;
            //myRequest.ContentType = ContentType;
            myRequest.ContentType = "application/json; charset=UTF-8";
            //myRequest.ContentLength = buf.Length;
            myRequest.MaximumAutomaticRedirections = 1;
            myRequest.AllowAutoRedirect = true;
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
            string returnData = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
            if (string.IsNullOrEmpty(returnData))
            {
                myResponse.Close();
         //       error = "利用StreamReader就可以从响应内容从头读到尾";
                return null;
            }
            var ret = Yw.JsonHelper.Json2Object<IStation.Dto.ApiResult<List<TotalOneDay>>>(returnData);
            reader.Close();
            myResponse.Close();
         //   error = null;
            return ret.Data;
        }
    }
}