1
wangsheng
2022-03-22 7bd27b4c5967ffaa87e8a813687da7edb909148c
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<%@ WebHandler Language="C#" Class="GwBlackMobileHandler" %>
 
using com.softwee.smgw.common;
using Dao;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
 
public class GwBlackMobileHandler : PageHandler<SysUser>
{
  private HashSet<long> Map = new HashSet<long>();
  private GwBlackPoolDao _GwBlackPoolDao = new GwBlackPoolDao();
 
  public override JsonPageResult ProcessRequestInternal(PageContext<SysUser> context)
  {
    string @string = context.GetString("action");
    switch (@string)
    {
      case "saveMobileSegMent":
        return this.SaveMobileSegMent(context);
      case "getMobileSegment":
        return this.GetMobileSegment(context);
      case "loadGwBlackPool":
        return this.LoadGwBlackPool(context);
      case "getGwBlackPool":
        return this.GetGwBlackPool(context);
      case "save":
        return this.Save(context);
            
   //清除黑名单数据  
                  
      case "truntateBlackMobile":
        return this.truntateBlackMobile(context);
   //清除黑名单数据END
                
      case "update":
        return this.Update(context);
      case "deletePool":
        return this.DeletePool(context);
      case "loadGwBlackMobile":
        return this.LoadGwBlackMobile(context);
      case "addGwBlackMobile":
        return this.AddGwBlackMobile(context);
      case "deleteMobile":
        return this.DeleteMobile(context);
      case "txtimportBlackMobile":
        return this.ZipImportBlackMobile(context);
      default:
        throw new Exception("Invalid Action=" + @string);
    }
  }
 
  private JsonPageResult SaveMobileSegMent(PageContext<SysUser> context)
  {
    int @int = context.GetInt("pid");
    string @string = context.GetString("mobileData");
    if (@int < 0)
      throw new ArgumentException("参数PoolID获取失败");
    if (string.IsNullOrEmpty(@string))
      throw new ArgumentException("请填写号段");
    if (new GwBlackPoolDao().UpMobileSegMentdate(new GwBlackPool()
    {
      PoolID = @int,
      MobileSegMent = SegmentHelper.Filrate(@string)
    }))
      return new JsonPageResult(true, (object) "号段修改成功");
    return new JsonPageResult(false, (object) "添加号段失败");
  }
 
  private JsonPageResult GetMobileSegment(PageContext<SysUser> context)
  {
    return new JsonPageResult(true, (object) new GwBlackPoolDao().GetGwBlackPool(context.GetInt("pid")));
  }
 
  private JsonPageResult LoadGwBlackPool(PageContext<SysUser> context)
  {
    string @string = context.GetString("poolName");
    int recordcount = 0;
    int int1 = context.GetInt("pageIndex", 1);
    int int2 = context.GetInt("pageSize", 20);
    string str = "";
    using (GwBlackPoolDao gwBlackPoolDao = new GwBlackPoolDao())
    {
      List<GwBlackPool> list = gwBlackPoolDao.LoadInfoList(@string, out recordcount, int1, int2);
      if (list != null && list.Count > 0)
      {
        foreach (GwBlackPool gwBlackPool in list)
        {
          str = str + (object) "<tr ><td>" + gwBlackPool.PoolName + "</td><td>" + (object) gwBlackPool.BlackPoolID + "</td>";
          str += "<td>";
          str += string.Format("<a class=\"btn btn-xs btn-primary btn-poolmobile\" href=\"javascript:;\" data-id=\"{0}\">", (object) gwBlackPool.PoolID);
          str += "&nbsp;查询</a>&nbsp;";
          str += string.Format("<a class=\"btn btn-xs btn-primary btn-mobile-segment\" href=\"javascript:;\" data-id=\"{0}\">", (object) gwBlackPool.PoolID);
          str += "<i class=\"fa fa-edit\"></i>&nbsp;黑名单号段</a>&nbsp;";
          str += string.Format("<a class=\"action-txtimportBlack btn btn-info btn-xs \" href=\"javascript:;\" data-id=\"{0}\" data-name=\"{1}\">", (object) gwBlackPool.PoolID, (object) gwBlackPool.PoolName);
          str += "&nbsp;黑名单导入</a>&nbsp;";
          if (gwBlackPool.PoolID > 0)
          {
            str += string.Format("<a class=\"action-modal-edit btn btn-success btn-xs \" href=\"javascript:;\" data-id=\"{0}\">", (object) gwBlackPool.PoolID);
            str += "<i class='fa fa-edit'></i>&nbsp;编辑";
            str += "</a>&nbsp;";
            str += string.Format("<a class=\"action-delete btnbtn btn btn-default btn-xs\" href=\"javascript:;\" data-id=\"{0}\">", (object) gwBlackPool.PoolID);
            str += "<i class='fa fa-trash'></i>&nbsp;删除";
            str += "</a>&nbsp;";
          }
          str += "</td>";
          str += "</tr>";
        }
      }
      else
        str += "<tr><td colspan=\"3\" style=\"text-align: center;\">暂无信息</td></tr>";
    }
    return new JsonPageResult(true, (object) new
    {
      Table = str.ToString(),
      TotalCount = recordcount
    });
  }
 
  private JsonPageResult Save(PageContext<SysUser> context)
  {
    string @string = context.GetString("poolName");
    if (string.IsNullOrEmpty(@string))
      throw new ArgumentException("黑名单池名称不能为空!");
    if (this._GwBlackPoolDao.IsExitsPoolName(@string))
      throw new ArgumentException("黑名单池名称已存在!");
    this._GwBlackPoolDao.Add(new GwBlackPool()
    {
      PoolName = @string
    });
    return new JsonPageResult(true, (object) "创建黑名单池成功!");
  }
 
  private JsonPageResult Update(PageContext<SysUser> context)
  {
    int @int = context.GetInt("poolID");
    string @string = context.GetString("poolName");
    if (string.IsNullOrEmpty(@string))
      throw new ArgumentException("黑名单池名称不能为空!");
    if (this._GwBlackPoolDao.IsExitsPoolName(@string, @int))
      throw new ArgumentException("黑名单池名称已存在!");
    this._GwBlackPoolDao.Update(new GwBlackPool()
    {
      PoolID = @int,
      PoolName = @string
    });
    return new JsonPageResult(true, (object) "修改黑名单池成功!");
  }
 
  private JsonPageResult DeletePool(PageContext<SysUser> context)
  {
    int @int = context.GetInt("poolID");
    if (this._GwBlackPoolDao.IsGwOpPortExits(@int))
      throw new Exception("通道信息配置中正在使用该黑名单池,不能删除!");
    this._GwBlackPoolDao.DeleteMobile(@int);
    return new JsonPageResult(true, (object) "删除黑名单池成功!");
  }
 
 
  // 清除黑名单数据成功
 
  private JsonPageResult truntateBlackMobile(PageContext<SysUser> context)
  {
      this._GwBlackPoolDao.truntateBlackMobile();
      
      return new JsonPageResult(true,"清除全部黑名单号码成功!");
  }
  // 清除黑名单数据成功END
    
    
  private JsonPageResult GetGwBlackPool(PageContext<SysUser> context)
  {
    return new JsonPageResult(true, (object) this._GwBlackPoolDao.GetGwBlackPool(context.GetInt("poolID")));
  }
 
  private JsonPageResult LoadGwBlackMobile(PageContext<SysUser> context)
  {
    string string1 = context.GetString("blackpool");
    string string2 = context.GetString("mobile");
    if (string.IsNullOrEmpty(string2))
      throw new ArgumentException("手机号码不能为空!");
    string str = "";
    using (GwBlackPoolDao gwBlackPoolDao = new GwBlackPoolDao())
    {
      List<GwBlackPool> list = gwBlackPoolDao.LoadInfoList(string1, string2);
      if (list != null && list.Count > 0)
      {
        foreach (GwBlackPool gwBlackPool in list)
        {
          str = str + (object) "<tr ><td>" + gwBlackPool.Mobile + "</td><td>" + gwBlackPool.PoolName + "</td><td>" + (object) gwBlackPool.ModifyTime + "</td>";
          str += "<td>";
          str += string.Format("<a class=\"action-mobile-delete btnbtn btn btn-default btn-xs\" href=\"javascript:;\" data-id=\"{0}\" data-moblie=\"{1}\">", (object) gwBlackPool.BlackPoolID, (object) gwBlackPool.Mobile);
          str += "<i class='fa fa-trash'></i>&nbsp;删除";
          str += "</a>";
          str += "</td>";
          str += "</tr>";
        }
      }
      else
        str += "<tr><td colspan=\"4\" style=\"text-align: center;\">暂无信息</td></tr>";
    }
    return new JsonPageResult(true, (object) new
    {
      Table = str.ToString()
    });
  }
 
  private JsonPageResult AddGwBlackMobile(PageContext<SysUser> context)
  {
    string string1 = context.GetString("blackpool");
    string string2 = context.GetString("mobile");
    if (string.IsNullOrEmpty(string1))
      throw new ArgumentException("黑名单池不能为空!");
    if (string.IsNullOrEmpty(string2))
      throw new ArgumentException("手机号码不能为空!");
    if (!this._GwBlackPoolDao.IsPortExits(Convert.ToInt32(string1), string2))
      throw new Exception("黑名单号码已存在!");
    this._GwBlackPoolDao.AddMobile(Convert.ToInt32(string1), string2);
    return new JsonPageResult(true, (object) "新增黑名单号码成功!");
  }
 
  private JsonPageResult DeleteMobile(PageContext<SysUser> context)
  {
    this._GwBlackPoolDao.DeleteMobile(context.GetInt("poolID"), context.GetString("mobile"));
    return new JsonPageResult(true, (object) "删除黑名单号码成功!");
  }
 
 
    
    
    
  private JsonPageResult ZipImportBlackMobile(PageContext<SysUser> context)
  {
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    HttpPostedFile httpPostedFile = context.Files["certPath"];
    if (httpPostedFile == null || httpPostedFile.FileName == "")
      throw new Exception("请选择要导入的zip文件,并检查文件大小!");
    string extension = Path.GetExtension(httpPostedFile.FileName);
    if (!extension.Equals(".zip", StringComparison.OrdinalIgnoreCase) && !extension.Equals(".txt", StringComparison.OrdinalIgnoreCase))
      throw new Exception("导入文件格式不正确,只允许导入zip或txt文件!");
    if (httpPostedFile.ContentLength > 104857600)
      throw new Exception("文件上传不能超过100M,请检查文件大小,是否有写权限!");
    string @string = context.GetString("ablackpool");
    if (string.IsNullOrEmpty(@string))
      throw new Exception("请选择黑名单池!");
    List<Stream> outputStreamList = new List<Stream>();
    if (extension.Equals(".txt", StringComparison.OrdinalIgnoreCase))
      outputStreamList.Add(httpPostedFile.InputStream);
    else if (extension.Equals(".zip", StringComparison.OrdinalIgnoreCase))
      ZipUtils.Decompression(httpPostedFile.InputStream, ref outputStreamList, true);
    HashSet<string> blackPoolMobile = this._GwBlackPoolDao.GetBlackPoolMobile(Convert.ToInt32(@string.ToString()));
    HashSet<string> tempSet = new HashSet<string>();
    DataTable dt = new DataTable("GW_BLACKPOOL_MOBILE");
    dt.Columns.Add("MOBILE", typeof (string));
    DateTime now = DateTime.Now;
    DataColumn column1 = new DataColumn("BLACKPOOL_ID", typeof (int));
    DataColumn column2 = new DataColumn("MODIFY_TIME", typeof (DateTime));
    DataColumn column3 = new DataColumn("IS_DELETED", typeof (int));
    column1.DefaultValue = (object) @string;
    column2.DefaultValue = (object) now;
    column3.DefaultValue = (object) 0;
    dt.Columns.Add(column1);
    dt.Columns.Add(column2);
    dt.Columns.Add(column3);
    StringBuilder errorBuilder = new StringBuilder();
    int count = 0;
    foreach (Stream stream in outputStreamList)
    {
      stream.Seek(0L, SeekOrigin.Begin);
      count += this.ProcessStream(blackPoolMobile, tempSet, dt, errorBuilder, stream);
    }
    int num = this.SaveDataTable(dt, now, count);
    stopwatch.Stop();
    string str = "数量:" + (object) num + "个,耗时:" + (object) stopwatch.ElapsedMilliseconds + "ms";
    if (errorBuilder.Length > 0)
      str = str + ",错误号码包含:" + errorBuilder.ToString();
    return new JsonPageResult(true, (object) new
    {
      Message = "全局黑名单导入成功!",
      Msg = str
    });
  }
 
  private int SaveDataTable(DataTable dt, DateTime dtime, int count)
  {
    if (dt != null)
    {
      if (dt.Rows.Count > 0)
      {
        try
        {
          this._GwBlackPoolDao.Import(dt);
          this._GwBlackPoolDao.Add_EventLog(dtime);
          count += dt.Rows.Count;
        }
        catch (Exception ex)
        {
          throw new Exception("写入数据出错:" + ex.Message);
        }
      }
    }
    return count;
  }
 
  private int ProcessStream(HashSet<string> existsSet, HashSet<string> tempSet, DataTable dt, StringBuilder errorBuilder, Stream stream)
  {
    int count = 0;
    using (StreamReader streamReader = new StreamReader(stream, Encoding.Default))
    {
      Regex regex = new Regex("^1[34578]\\d{9}$");
      int num = 0;
      while (!streamReader.EndOfStream)
      {
        string str1;
        try
        {
          str1 = streamReader.ReadLine();
        }
        catch (Exception ex)
        {
          throw new Exception("读取文件出错,手机号码请换行!");
        }
        if (!string.IsNullOrEmpty(str1))
        {
          string[] strArray;
          try
          {
            strArray = str1.Split(new char[8]
            {
              '\r',
              '\n',
              ',',
              ';',
              ' ',
              '|',
              ';',
              ','
            }, StringSplitOptions.RemoveEmptyEntries);
          }
          catch (Exception ex)
          {
            throw new Exception("分割手机号码出错!");
          }
          foreach (string str2 in strArray)
          {
            string input = str2.Trim();
            if (!regex.IsMatch(input))
              errorBuilder.AppendFormat("{0};", (object) input);
            else if (!existsSet.Contains(input) && !tempSet.Contains(input))
            {
              ++num;
              DataRow row = dt.NewRow();
              row["MOBILE"] = (object) input;
              dt.Rows.Add(row);
              tempSet.Add(input);
            }
            if (num == 1000000)
            {
              count = this.SaveDataTable(dt, DateTime.Now, count);
              num = 0;
              dt.Clear();
            }
          }
        }
      }
    }
    return count;
  }
 
  private JsonPageResult TxtImportBlackMobile(PageContext<SysUser> context)
  {
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    HttpPostedFile httpPostedFile = context.Files["certPath"];
    if (httpPostedFile == null)
      throw new Exception("未选择文件或上传文件超过10M!");
    if (httpPostedFile.FileName == "")
      throw new Exception("请选择要导入的txt文件!");
    string extension = Path.GetExtension(httpPostedFile.FileName);
    if (!extension.Equals(".txt") && !extension.Equals(".TXT"))
      throw new Exception("导入文件格式不正确,只允许导入txt文件!");
    if (httpPostedFile.ContentLength > 10485760)
      throw new Exception("文件上传不能超过10M,请检查文件大小,是否有写权限!");
    string @string = context.GetString("ablackpool");
    if (string.IsNullOrEmpty(@string))
      throw new Exception("请选择黑名单池!");
    HashSet<string> blackPoolMobile = this._GwBlackPoolDao.GetBlackPoolMobile(Convert.ToInt32(@string.ToString()));
    HashSet<string> hashSet = new HashSet<string>();
    DataTable dt = new DataTable("GW_BLACKPOOL_MOBILE");
    dt.Columns.Add("MOBILE", typeof (string));
    DateTime dtime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    DataColumn column1 = new DataColumn("BLACKPOOL_ID", typeof (int));
    DataColumn column2 = new DataColumn("MODIFY_TIME", typeof (DateTime));
    DataColumn column3 = new DataColumn("IS_DELETED", typeof (int));
    column1.DefaultValue = (object) @string;
    column2.DefaultValue = (object) dtime;
    column3.DefaultValue = (object) 0;
    dt.Columns.Add(column1);
    dt.Columns.Add(column2);
    dt.Columns.Add(column3);
    StringBuilder stringBuilder = new StringBuilder();
    using (StreamReader streamReader = new StreamReader(httpPostedFile.InputStream, Encoding.Default))
    {
      Regex regex = new Regex("^1[34578]\\d{9}$");
      string str1 = streamReader.ReadToEnd();
      if (!string.IsNullOrEmpty(str1))
      {
        string str2 = str1;
        char[] separator = new char[6]
        {
          '\r',
          '\n',
          ',',
          ';',
          ' ',
          '|'
        };
        int num = 1;
        foreach (string str3 in str2.Split(separator, (StringSplitOptions) num))
        {
          string input = str3.Trim();
          if (!regex.IsMatch(input))
            stringBuilder.AppendFormat("{0};", (object) input);
          else if (!blackPoolMobile.Contains(input) && !hashSet.Contains(input))
          {
            DataRow row = dt.NewRow();
            row["MOBILE"] = (object) input;
            dt.Rows.Add(row);
            hashSet.Add(input);
          }
        }
      }
    }
    int num1 = 0;
    if (dt != null)
    {
      if (dt.Rows.Count > 0)
      {
        try
        {
          num1 = this._GwBlackPoolDao.Import(dt, dtime);
        }
        catch (Exception ex)
        {
          throw new Exception("写入数据出错:" + ex.Message);
        }
      }
    }
    stopwatch.Stop();
    string str = "数量:" + (object) num1 + "个,耗时:" + (object) stopwatch.ElapsedMilliseconds + "ms";
    if (stringBuilder.Length > 0)
      str = str + ",错误号码包含:" + stringBuilder.ToString();
    "".TrimEnd(',');
    return new JsonPageResult(true, (object) new
    {
      Message = "全局黑名单导入成功!",
      Msg = str
    });
  }
}