PushHelper.cs 11 KB

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