wzp
2022-07-12 93dbdc305bd736f5745d217e33e2ba79cc7782fe
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using Dao;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Reflection;
 
public partial class ExportDocument : PageBase<SysUser>, IRequiresSessionState
{
    
 
    protected void Page_Load(object sender, EventArgs e)
    {
        switch (this.AppContext.GetString("action"))
        {
            case "exPortGwSp":
                this.ExPortGwSp();
                break;
            case "exPortGwSm":
                this.ExportGwSmData();
                break;
            case "exportGwStatisV3"://导出统计报表
                this.ExprortGwStatisV3();
                break;
        }
    }
 
    #region 统计报表导出
    /// <summary>
    /// 统计报表导出
    /// </summary>
    private void ExprortGwStatisV3()
    {
        string apMid = this.AppContext.GetString("ApMID");
        string opid = this.AppContext.GetString("OPID");
        string spid = this.AppContext.GetString("SpID");
        string clientId = this.AppContext.GetString("ClientID");
        string beginTime = this.AppContext.GetString("BeginTime");
        if (string.IsNullOrEmpty(beginTime))
            beginTime = DateTime.Now.AddHours(-1.0).Day == DateTime.Now.Day ? DateTime.Now.AddHours(-1.0).ToString("HH:mm:ss") : "00:00:00";
        string endTime = this.AppContext.GetString("EndTime");
        string statistype = this.AppContext.GetString("Statistype");
        StatisOption statisoption = (StatisOption)this.AppContext.GetInt("Statisoption");
 
        List<GwOp> opList;
 
        using (GwOpDao dao = new GwOpDao())
        {
            opList = dao.LoadInfoList();
        }
 
        List<GwClient> clientList;
 
        using (GwClientDao dao = new GwClientDao())
        {
            clientList = dao.LoadInfoList("", "", "", "");
        }
 
        List<GwAp> apList;
 
        using (GwApDao dao = new GwApDao())
        {
            int recordCount = 0;
 
            apList = dao.LoadInfoList("", "", "", out recordCount, 99999, 1);
        }
 
        GwStatisV3Dao _Dao = new GwStatisV3Dao();
        var list = _Dao.Query(Convert.ToDateTime(beginTime), Convert.ToDateTime(endTime), clientId, spid, apMid, opid, statistype, (StatisOption)statisoption);
 
        List<Model.GwStatisV3> statisList = new List<Model.GwStatisV3>();
        foreach (GwStatisV3Item item in list)
        {
            Model.GwStatisV3 ss = new Model.GwStatisV3();
 
            if ((statisoption & StatisOption.APID) == StatisOption.APID)
            {
                ss.ApName = GetApName(apList, item.APID);
            }
            if ((statisoption & StatisOption.OPID) == StatisOption.OPID)
            {
                ss.OpName = GetOpName(opList, item.OPID);
            }
            if ((statisoption & StatisOption.SpID) == StatisOption.SpID)
            {
                ss.SpId = item.SpID;
            }
            if ((statisoption & StatisOption.ClientID) == StatisOption.ClientID)
            {
                ss.ClientId = GetClientName(clientList, item.ClientID);
            }
 
            int opTotal = item.OPStatus0 + item.OPStatus1 + item.OPStatus2 + item.OPStatus3 + item.OPStatus4 + item.OPStatus5;
            int apTotal = item.APStatus0 + item.APStatus1 + item.APStatus2 + item.APStatus3 + item.APStatus4 + item.APStatus5;
 
            ss.ApReciveTotal = apTotal.ToString();
            ss.OpSubTotal = opTotal.ToString();
 
            ss.ApSubFailed = item.APStatus1.ToString();
            ss.ApNotForwarded = item.APStatus0.ToString();
 
            ss.ApFailed = (item.APStatus3 + item.APStatus5).ToString();
            ss.ApUnkown = item.APStatus3.ToString();
            ss.ApNeedReturn = (item.APStatus3 + item.APStatus2 + item.APStatus5 + item.APStatus0).ToString();
 
            ss.OpFailed = item.OPStatus5.ToString();
            ss.OpUnkown = item.OPStatus2.ToString();
            ss.OpSuccess = item.OPStatus4.ToString();
 
            ss.OpSuccessRate = Divide(item.OPStatus4 * 100, opTotal).ToString()+"%";
 
             ss.OpReportRate = (100 - Divide(item.OPStatus2 * 100, opTotal)).ToString() + "%";
             ss.Date = item.Title;
 
            statisList.Add(ss);
        }
 
        if (statisList.Count > 0)
        {
            DataTable dt = ToDataTable<Model.GwStatisV3>(statisList);
            WebTool webtool = new WebTool();
            Dictionary<string, string> dicTitle = new Dictionary<string, string>();
            dicTitle["日期"] = "Date";
            dicTitle["接入点"] = "ApName";
            dicTitle["通道"] = "OpName";
            dicTitle["账号"] = "SpId";
            dicTitle["客户"] = "ClientId";
            dicTitle["AP接收总量"] = "ApReciveTotal";
            dicTitle["AP提交失败"] = "ApSubFailed";
            dicTitle["AP未转发"] = "ApNotForwarded";
            dicTitle["AP失败"] = "ApFailed";
            dicTitle["AP未知"] = "ApUnkown";
            dicTitle["AP需返还"] = "ApNeedReturn";
            dicTitle["OP提交总量"] = "OpSubTotal";
            dicTitle["OP失败"] = "OpFailed";
            dicTitle["OP未知"] = "OpUnkown";
            dicTitle["OP成功"] = "OpSuccess";
            dicTitle["OP成功率"] = "OpSuccessRate";
            dicTitle["OP状态率"] = "OpReportRate";
            webtool.Export(dt,dicTitle,"统计报表",this.Context.Response);
        }
 
    }
 
 
    public decimal Divide(decimal v, decimal total)
    {
        if (total == 0)
        {
            return 0;
        }
 
        return Math.Round(v / total, 2);
    }
 
    private string GetApName(List<GwAp> aplist, int apID)
    {
        if (aplist == null)
            return string.Empty;
 
        Model.GwAp x = aplist.Find(delegate(Model.GwAp ap) { return ap.ApID == apID; });
 
        return x == null ? string.Empty : string.Format("{0}-{1}", x.ApID, x.ApName);
    }
 
    private string GetOpName(List<GwOp> oplist, int opID)
    {
        if (oplist == null)
            return string.Empty;
 
        Model.GwOp x = oplist.Find(delegate(Model.GwOp op) { return op.OpID == opID; });
 
        return x == null ? string.Empty : string.Format("{0}-{1}", x.OpID, x.OpName);
    }
 
    private string GetClientName(List<GwClient> clientlist, string clientID)
    {
        if (clientlist == null)
            return string.Empty;
 
        Model.GwClient x = clientlist.Find(delegate(Model.GwClient client) { return client.ClientID == clientID; });
 
        return x == null ? string.Format("{0}", clientID) : string.Format("{0}-{1}", x.ClientID, x.ClientName);
    }
 
 
 
    //=================================================
    /// <summary>
    /// Convert a List{T} to a DataTable.
    /// </summary>
    private DataTable ToDataTable<T>(List<T> items)
    {
        var tb = new DataTable(typeof(T).Name);
 
        PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
 
        foreach (PropertyInfo prop in props)
        {
            Type t = GetCoreType(prop.PropertyType);
            tb.Columns.Add(prop.Name, t);
        }
 
        foreach (T item in items)
        {
            var values = new object[props.Length];
 
            for (int i = 0; i < props.Length; i++)
            {
                values[i] = props[i].GetValue(item, null);
            }
 
            tb.Rows.Add(values);
        }
 
        return tb;
    }
 
    /// <summary>
    /// Determine of specified type is nullable
    /// </summary>
    public static bool IsNullable(Type t)
    {
        return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
    }
 
    /// <summary>
    /// Return underlying type if type is Nullable otherwise return the type
    /// </summary>
    public static Type GetCoreType(Type t)
    {
        if (t != null && IsNullable(t))
        {
            if (!t.IsValueType)
            {
                return t;
            }
            else
            {
                return Nullable.GetUnderlyingType(t);
            }
        }
        else
        {
            return t;
        }
    }
 
    //=============================end===================================
    #endregion
 
 
    private void ExPortGwSp()
    {
        string string1 = this.AppContext.GetString("SpID");
        string string2 = this.AppContext.GetString("ApID");
        string string3 = this.AppContext.GetString("ClientID");
        using (GwSpDao gwSpDao = new GwSpDao())
        {
            DataTable dataTable = gwSpDao.LoadSpDataTable(string1, string2, string3);
            WebTool webTool = new WebTool();
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary["客户编码"] = "CLIENT_ID";
            dictionary["账号"] = "SP_ID";
            dictionary["余额"] = "BALANCE";
            dictionary["单价"] = "PRICE";
            dictionary["付款方式"] = "CHARGE_TYPE";
            dictionary["鉴权地址"] = "CLIENT_IP";
            dictionary["门限"] = "THRESHOLD";
            dictionary["密码"] = "PASSWORD";
            webTool.Export(dataTable, dictionary, "SP_" + (object)DateTime.Now.Ticks, this.Context.Response);
        }
    }
 
    private void ExportGwSmData()
    {
        string apMid = this.AppContext.GetString("ApMID");
        string sDate = this.AppContext.GetString("QueryDate");
        if (string.IsNullOrEmpty(sDate))
            sDate = DateTime.Today.ToString("yyyy-MM-dd");
        string content = this.AppContext.GetString("Content");
        string opstat = this.AppContext.GetString("OpStat");
        string opName = this.AppContext.GetString("OPName");
        string mobile = this.AppContext.GetString("DestnationID");
        string spid = this.AppContext.GetString("SpID");
        string clientId = this.AppContext.GetString("ClientID");
        string beginTime = this.AppContext.GetString("BeginTime");
        if (string.IsNullOrEmpty(beginTime))
            beginTime = DateTime.Now.AddHours(-1.0).Day == DateTime.Now.Day ? DateTime.Now.AddHours(-1.0).ToString("HH:mm:ss") : "00:00:00";
        string endTime = this.AppContext.GetString("EndTime");
        if (string.IsNullOrEmpty(endTime))
            endTime = DateTime.Now.Date.AddSeconds(-1.0).ToString("HH:mm:ss");
        int @int = this.AppContext.GetInt("TStatus", -1);
        using (GwSmDao gwSmDao = new GwSmDao())
        {
            int recordcount = 0;
            if (gwSmDao.LoadInfoListcount(apMid, opName, clientId, spid, mobile, sDate, beginTime, endTime, content, opstat, @int, out recordcount).Count > 1000000)
            {
                this.Response.Write("<script type='text/javascript'>alert('导出数据不能大于100万条!');location.href='GwSm.aspx';</script>");
                this.Response.End();
            }
            DataTable dataTable = gwSmDao.LoadSpDataTable(apMid, opName, clientId, spid, mobile, sDate, beginTime, endTime, content, @int, opstat);
            WebTool webTool = new WebTool();
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary["AP消息ID"] = "AP_MID";
            dictionary["CLTID"] = "CLIENT_ID";
            dictionary["账号"] = "SP_ID";
            dictionary["APID"] = "AP_ID";
            dictionary["OPID"] = "OP_ID";
            dictionary["码号"] = "ACCESS_CODE";
            dictionary["接收号码"] = "MOBILE";
            dictionary["PKN "] = "PK_NUMBER";
            dictionary["PKT "] = "PK_NUMBER";
            dictionary["AP计费"] = "AP_FEE_COUNT";
            dictionary["OP计费 "] = "OP_FEE_COUNT";
            dictionary["UDH"] = "TPUDHI";
            dictionary["优先级"] = "PRIORITY";
            dictionary["FMT"] = "MSG_FORMAT";
            dictionary["长度"] = "MSG_LENGTH";
            dictionary["AP结果 "] = "AP_RESULT";
            dictionary["OP消息ID "] = "OP_MID";
            dictionary["OP结果 "] = "OP_RESULT";
            dictionary["客户提交时间"] = "AP_SUBMIT_TIME";
            dictionary["对外转发时间"] = "OP_SUBMIT_TIME";
            dictionary["状态报告回执时间"] = "OP_DELIVER_TIME";
            dictionary["内容"] = "MSG_CONTENT";
            dictionary["状态"] = "STAT";
            webTool.Export(dataTable, dictionary, "日志_" + sDate, this.Context.Response);
        }
    }
}