PushHelper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Linq;
  3. using System.Data;
  4. using System.Threading;
  5. using Library;
  6. using LitJson;
  7. using MySystem.Models.Push;
  8. using System.Collections.Generic;
  9. namespace MySystem
  10. {
  11. public class PushHelper
  12. {
  13. public readonly static PushHelper Instance = new PushHelper();
  14. private PushHelper()
  15. {
  16. }
  17. public void Start()//启动
  18. {
  19. Thread thread = new Thread(threadStart);
  20. thread.IsBackground = true;
  21. thread.Start();
  22. }
  23. private void threadStart()
  24. {
  25. while (true)
  26. {
  27. DoSomeThing();
  28. Thread.Sleep(1000);
  29. }
  30. }
  31. //要执行的方法
  32. public void DoSomeThing()
  33. {
  34. WebCMSEntities db = new WebCMSEntities();
  35. //查找开通推送的商户
  36. var merchants = db.Merchant.Where(m => m.Status == 1).ToList();
  37. foreach(var merchant in merchants)
  38. {
  39. string RsaPubKey = merchant.RsaPubKey;
  40. string RsaPriKey = merchant.RsaPriKey;
  41. string AesSecret = merchant.AesSecret;
  42. string MerchantNo = merchant.MerchantNo;
  43. string MerchantName = merchant.MerchantName;
  44. //查找商户开通的推送项目
  45. var pushObj = db.PushObj.Where(m => m.Status == 1 && m.MerchantId == merchant.Id).ToList();
  46. foreach(var pushItem in pushObj)
  47. {
  48. string FieldList = pushItem.FieldList;
  49. string TableName = pushItem.TableName;
  50. int EncryptMode = pushItem.EncryptMode;
  51. string NoticeUrl = pushItem.NoticeUrl;
  52. string Title = pushItem.Title;
  53. int MerchantId = pushItem.MerchantId;
  54. //构造抓取数据sql
  55. string sql = "select " + FieldList.TrimEnd(',') + " from u_" + TableName + " where 1=1";
  56. var condiList = db.PushObjCondition.Where(m => m.PushObjId == pushItem.Id).ToList();
  57. foreach(var condi in condiList)
  58. {
  59. int QueryCondition = condi.QueryCondition;
  60. if(QueryCondition == 1)
  61. {
  62. sql += " and " + condi.QueryField + "='" + condi.QueryVal + "'";
  63. }
  64. else if(QueryCondition == 2)
  65. {
  66. sql += " and " + condi.QueryField + " like '%" + condi.QueryVal + "%'";
  67. }
  68. else if(QueryCondition == 3)
  69. {
  70. string[] QueryValList = condi.QueryVal.Split('|');
  71. sql += " and " + condi.QueryField + ">=" + QueryValList[0] + " and " + condi.QueryField + "<=" + QueryValList[1] + "";
  72. }
  73. else if(QueryCondition == 4)
  74. {
  75. string[] QueryValList = condi.QueryVal.Split('|');
  76. sql += " and " + condi.QueryField + ">='" + QueryValList[0] + "' and " + condi.QueryField + "<='" + QueryValList[1] + "'";
  77. }
  78. else if(QueryCondition == 5)
  79. {
  80. sql += " and " + condi.QueryField + " in (" + condi.QueryVal + ")";
  81. }
  82. else if(QueryCondition == 6)
  83. {
  84. sql += " and " + condi.QueryField + " in ('" + condi.QueryVal.Replace(",", "','") + "')";
  85. }
  86. else if(QueryCondition == 7)
  87. {
  88. string[] QueryValList = condi.QueryVal.Split(',');
  89. sql += " and (";
  90. int index = 0;
  91. foreach(string QueryVal in QueryValList)
  92. {
  93. index += 1;
  94. sql += condi.QueryField + "=" + QueryVal;
  95. if(index < QueryValList.Length)
  96. {
  97. sql += " or ";
  98. }
  99. }
  100. sql += ")";
  101. }
  102. else
  103. {
  104. sql += " and " + condi.QueryField + "=" + condi.QueryVal;
  105. }
  106. }
  107. //抓取数据开始post数据
  108. DataTable dt = CustomerSqlConn.dtable(sql, AppConfig.Base.SqlConnStr);
  109. foreach(DataRow dr in dt.Rows)
  110. {
  111. SortedList<string, string> obj = new SortedList<string, string>();
  112. foreach(DataColumn dc in dt.Columns)
  113. {
  114. obj.Add(dc.ColumnName, dr[dc.ColumnName].ToString());
  115. }
  116. if(EncryptMode == 1)
  117. {
  118. string content = EncryptHelper.Encrypt1(obj, AesSecret);
  119. obj = new SortedList<string, string>();
  120. obj.Add("type", Title);
  121. obj.Add("notice_id", Guid.NewGuid().ToString());
  122. obj.Add("timestamp", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8));
  123. obj.Add("content", content);
  124. string requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  125. string result = function.PostWebRequest(NoticeUrl, requestJson, "application/json");
  126. if(result.Contains("\"code\":\"200\""))
  127. {
  128. }
  129. }
  130. }
  131. }
  132. }
  133. db.Dispose();
  134. }
  135. }
  136. }