PushHelper.cs 12 KB

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