CommonTool.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Npgsql;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.SqlClient;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using System.Configuration;
  11. namespace DataTransmission
  12. {
  13. public class CommonTool
  14. {
  15. public static string CenterId = string.Empty;
  16. public static string Paseword = string.Empty;
  17. public SqlConnection GetConSqlServer()
  18. {
  19. return new SqlConnection(ConfigurationManager.AppSettings["SQLServerDB"]);
  20. }
  21. public NpgsqlConnection GetConPgSql()
  22. {
  23. return new NpgsqlConnection(ConfigurationManager.AppSettings["PGSQLDB"]);
  24. }
  25. public DataTable GetDataTable(string sql, SqlConnection conn)
  26. {
  27. conn.Open();
  28. SqlCommand cmd = new SqlCommand(sql, conn);
  29. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  30. DataSet dataSet = new DataSet();
  31. sda.Fill(dataSet, "table");
  32. conn.Close();
  33. return dataSet.Tables[0];
  34. }
  35. public DataTable GetDataTable(string sql, NpgsqlConnection conn)
  36. {
  37. conn.Open();
  38. NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
  39. NpgsqlDataAdapter sda = new NpgsqlDataAdapter(cmd);
  40. DataSet dataSet = new DataSet();
  41. sda.Fill(dataSet, "table");
  42. conn.Close();
  43. return dataSet.Tables[0];
  44. }
  45. public int Execute(string sql, NpgsqlConnection conn)
  46. {
  47. conn.Open();
  48. var i = 0;
  49. try
  50. {
  51. NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
  52. i = cmd.ExecuteNonQuery();
  53. }
  54. catch (Exception e)
  55. {
  56. Console.WriteLine(e);
  57. SaveLog(e.ToString() + "/n SQL:" + sql, 1);
  58. }
  59. conn.Close();
  60. return i;
  61. }
  62. public string MaskPhoneNumber(string phoneNumber)
  63. {
  64. if (String.IsNullOrEmpty(phoneNumber)) return "";
  65. Regex regex = new Regex(@"(\d{3})(\d{4})(\d{4})");
  66. Match match = regex.Match(phoneNumber);
  67. if (!match.Success || match.Groups.Count != 4) return phoneNumber;
  68. string frontCode = match.Groups[1].Value;
  69. string areaCode = match.Groups[2].Value;
  70. string prefixAndSuffix = match.Groups[3].Value;
  71. string maskedAreaCode = String.Join("", Enumerable.Repeat('*', areaCode.Length));
  72. string maskedPrefixAndSuffix = String.Join("", Enumerable.Repeat('*', prefixAndSuffix.Length - 4).Concat(prefixAndSuffix.Substring(prefixAndSuffix.Length - 4)));
  73. return $"{frontCode}{maskedAreaCode}{maskedPrefixAndSuffix}";
  74. }
  75. public void SaveLog(string LogMessage, int Level, string PreLogDir = "")
  76. {
  77. int LogLevel = 0;
  78. int.TryParse(ConfigurationManager.AppSettings["LogLevel"], out LogLevel);
  79. if (Level <= LogLevel)
  80. {
  81. try
  82. {
  83. string dirpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + PreLogDir + "log";
  84. if (!Directory.Exists(dirpath))
  85. {
  86. Directory.CreateDirectory(dirpath);
  87. }
  88. if (File.Exists(dirpath + "\\log.txt") && new FileInfo(dirpath + "\\log.txt").Length > 512 * 1024)
  89. {
  90. File.Move(dirpath + "\\log.txt", dirpath + "\\log" + DateTime.Now.ToString("yyyy-MM-ddHHmmssfff") + ".txt");
  91. }
  92. foreach (string LogFile in Directory.GetFiles(dirpath, "*.txt"))
  93. {
  94. if (File.GetLastWriteTime(LogFile) < DateTime.Now.AddDays(-3))
  95. {
  96. File.Delete(LogFile);
  97. }
  98. }
  99. File.AppendAllText(dirpath + "\\log.txt", DateTime.Now.ToString("yyyy-MM-ddHHmmssfff") + "_" + LogMessage);
  100. File.AppendAllText(dirpath + "\\log.txt", "\r\n");
  101. }
  102. catch { }
  103. }
  104. }
  105. }
  106. }