PushHelper.cs 11 KB

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