tangxu
2024-10-08 54d6c9937e34066e357c3212477914ecef1370b6
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 IStation.Common;
using IStation.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.UI.WebControls;
 
namespace IStation.WebApi.Controllers
{
    /// <summary>
    /// 供水总量
    /// </summary>
    [RoutePrefix("TotalWaterOut")]
    public class TotalWaterOutController : ApiController
    {
        /// <summary>
        /// 获取某日的供水总量
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetByDay")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetByDay(string day)
        {
            if (day == null)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" };
            }
            DateTime dayD;
            if (!DateTime.TryParse(day, out dayD))
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 格式不正确" };
            }
            if (dayD > DateTime.Today)
            {
                return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "不能查询今日的数据" };
            }
 
            double sum = -1;
            if (dayD < DateTime.Today)
            {
                sum = TotalWaterOutHelper.Read(dayD);
            }
            if (sum < 0)
            {
                sum = await  ZyConnectHelper.GetTotalWaterByDay_Out(DateTime.Parse(day));//.Result;//.GetAwaiter().GetResult();
                if (dayD < DateTime.Today)
                {
                    TotalWaterOutHelper.Save(dayD, sum);
                }
            }
             
            return new IStation.Dto.ApiResult<double>(sum / 10000);
        }
 
 
        /// <summary>
        /// 获取最近三天的供水总量
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        [Route("GetLastDay3")]
        [HttpGet]
        public async Task<IStation.Dto.ApiResult> GetLastDay3()
        {
            DateTime yest1 = DateTime.Today.AddDays(-1);
            var sum1 = TotalWaterOutHelper.Read(yest1);
            if (sum1 < 0)
            {
                sum1 = await ZyConnectHelper.GetTotalWaterByDay_Out(yest1);//.Result;//.GetAwaiter().GetResult();
 
                TotalWaterOutHelper.Save(yest1, sum1);
            }
 
            DateTime yest2 = DateTime.Today.AddDays(-2);
            var sum2 = TotalWaterOutHelper.Read(yest2);
            if (sum2 < 0)
            {
                sum2 = await ZyConnectHelper.GetTotalWaterByDay_Out(yest2);//.Result;//.GetAwaiter().GetResult();
 
                TotalWaterOutHelper.Save(yest2, sum2);
            }
 
            DateTime yest3 = DateTime.Today.AddDays(-3);
            var sum3 = TotalWaterOutHelper.Read(yest3);
            if (sum3 < 0)
            {
                sum3 = await ZyConnectHelper.GetTotalWaterByDay_Out(yest3);//.Result;//.GetAwaiter().GetResult();
 
                TotalWaterOutHelper.Save(yest3, sum3);
            }
 
            return new IStation.Dto.ApiResult<List<double>>(new List<double> { sum1 / 10000, sum2 / 10000, sum3 / 10000 });
        }
 
 
 
 
 
 
 
 
        /// <summary>
        /// 获取web客户端ip
        /// </summary>
        /// <returns></returns>
        public static string GetWebClientIp()
        {
            string userIP = "";
 
            try
            {
                if (System.Web.HttpContext.Current == null
                 || System.Web.HttpContext.Current.Request == null
                 || System.Web.HttpContext.Current.Request.ServerVariables == null)
                {
                    return "";
                }
 
                string CustomerIP = "";
 
                //CDN加速后取到的IP simone 090805
                CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                if (!string.IsNullOrEmpty(CustomerIP))
                {
                    return CustomerIP;
                }
 
                CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
 
                if (!String.IsNullOrEmpty(CustomerIP))
                {
                    return CustomerIP;
                }
 
                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
 
                    if (CustomerIP == null)
                    {
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    }
                }
                else
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
 
                if (string.Compare(CustomerIP, "unknown", true) == 0 || String.IsNullOrEmpty(CustomerIP))
                {
                    return System.Web.HttpContext.Current.Request.UserHostAddress;
                }
                return CustomerIP;
            }
            catch { }
 
            return userIP;
        }
 
 
 
 
    }
}