PushHelper.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. string QueryField = "";
  55. JsonData fieldJson = JsonMapper.ToObject(FieldList);
  56. for (int i = 0; i < fieldJson.Count; i++)
  57. {
  58. JsonData item = fieldJson[i];
  59. if(!string.IsNullOrEmpty(item["name"].ToString())) QueryField += item["name"].ToString() + ",";
  60. }
  61. //构造抓取数据sql
  62. string sql = "select " + QueryField.TrimEnd(',') + " from u_" + TableName + " where 1=1";
  63. var condiList = db.PushObjCondition.Where(m => m.PushObjId == pushItem.Id).ToList();
  64. foreach(var condi in condiList)
  65. {
  66. int QueryCondition = condi.QueryCondition;
  67. if(QueryCondition == 1)
  68. {
  69. sql += " and " + condi.QueryField + "='" + condi.QueryVal + "'";
  70. }
  71. else if(QueryCondition == 2)
  72. {
  73. sql += " and " + condi.QueryField + " like '%" + condi.QueryVal + "%'";
  74. }
  75. else if(QueryCondition == 3)
  76. {
  77. sql += " and " + condi.QueryField + ">" + condi.QueryVal + "";
  78. }
  79. else if(QueryCondition == 4)
  80. {
  81. sql += " and " + condi.QueryField + "<" + condi.QueryVal + "";
  82. }
  83. else if(QueryCondition == 5)
  84. {
  85. string[] QueryValList = condi.QueryVal.Split('|');
  86. sql += " and " + condi.QueryField + ">=" + QueryValList[0] + " and " + condi.QueryField + "<=" + QueryValList[1] + "";
  87. }
  88. else if(QueryCondition == 6)
  89. {
  90. string[] QueryValList = condi.QueryVal.Split('|');
  91. sql += " and " + condi.QueryField + ">='" + QueryValList[0] + "' and " + condi.QueryField + "<='" + QueryValList[1] + "'";
  92. }
  93. else if(QueryCondition == 7)
  94. {
  95. sql += " and " + condi.QueryField + " in (" + condi.QueryVal + ")";
  96. }
  97. else if(QueryCondition == 8)
  98. {
  99. sql += " and " + condi.QueryField + " in ('" + condi.QueryVal.Replace(",", "','") + "')";
  100. }
  101. else if(QueryCondition == 9)
  102. {
  103. string[] QueryValList = condi.QueryVal.Split(',');
  104. sql += " and (";
  105. int index = 0;
  106. foreach(string QueryVal in QueryValList)
  107. {
  108. index += 1;
  109. sql += condi.QueryField + "=" + QueryVal;
  110. if(index < QueryValList.Length)
  111. {
  112. sql += " or ";
  113. }
  114. }
  115. sql += ")";
  116. }
  117. else
  118. {
  119. sql += " and " + condi.QueryField + "='" + condi.QueryVal + "'";
  120. }
  121. }
  122. //抓取数据开始post数据
  123. int StartId = pushItem.QueryId;
  124. sql = sql.Replace("${QueryId}$", StartId.ToString());
  125. sql += " order by id limit 10";
  126. DataTable dt = CustomerSqlConn.dtable(sql, AppConfig.Base.SqlConnStr);
  127. foreach(DataRow dr in dt.Rows)
  128. {
  129. SortedList<string, string> obj = new SortedList<string, string>();
  130. foreach(DataColumn dc in dt.Columns)
  131. {
  132. obj.Add(dc.ColumnName, dr[dc.ColumnName].ToString());
  133. }
  134. int Status = 0;
  135. string PushData = "";
  136. string PushDataEncrypt = "";
  137. if(EncryptMode == 1)
  138. {
  139. PushData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  140. string content = EncryptHelper.Encrypt1(obj, AesSecret);
  141. obj = new SortedList<string, string>();
  142. obj.Add("type", Title);
  143. obj.Add("notice_id", Guid.NewGuid().ToString());
  144. obj.Add("timestamp", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8));
  145. obj.Add("content", content);
  146. string requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  147. PushDataEncrypt = requestJson;
  148. string result = function.PostWebRequest(NoticeUrl, requestJson, "application/json");
  149. if(result.Contains("\"code\":\"200\""))
  150. {
  151. Status = 1;
  152. }
  153. }
  154. PushRecord rec = db.PushRecord.Add(new PushRecord()
  155. {
  156. CreateDate = DateTime.Now,
  157. UpdateDate = DateTime.Now,
  158. Status = Status,
  159. PushData = PushData,
  160. PushDataEncrypt = PushDataEncrypt,
  161. PushObjId = pushItem.Id,
  162. MerchantId = merchant.Id
  163. }).Entity;
  164. db.SaveChanges();
  165. if(Status != 1)
  166. {
  167. db.RePushQueue.Add(new RePushQueue()
  168. {
  169. CreateDate = DateTime.Now,
  170. RePushUrl = NoticeUrl,
  171. RePushDate = RePushHelper.Instance.GetNextTime(1),
  172. Times = 1,
  173. PushData = PushData,
  174. PushDataEncrypt = PushDataEncrypt,
  175. PushObjId = pushItem.Id,
  176. MerchantId = merchant.Id,
  177. PushRecordId = rec.Id,
  178. });
  179. }
  180. db.SaveChanges();
  181. StartId = int.Parse(dr["id"].ToString());
  182. }
  183. var edit = db.PushObj.FirstOrDefault(m => m.Id == pushItem.Id);
  184. if(edit != null)
  185. {
  186. edit.QueryId = StartId;
  187. db.SaveChanges();
  188. }
  189. }
  190. }
  191. db.Dispose();
  192. }
  193. }
  194. }