using System; using System.Linq; using System.Data; using System.Threading; using Library; using LitJson; using MySystem.Models.Push; using System.Collections.Generic; namespace MySystem { public class PushHelper { public readonly static PushHelper Instance = new PushHelper(); private PushHelper() { } public void Start()//启动 { Thread thread = new Thread(threadStart); thread.IsBackground = true; thread.Start(); } private void threadStart() { while (true) { DoSomeThing(); Thread.Sleep(1000); } } //要执行的方法 public void DoSomeThing() { WebCMSEntities db = new WebCMSEntities(); //查找开通推送的商户 var merchants = db.Merchant.Where(m => m.Status == 1).ToList(); foreach(var merchant in merchants) { string RsaPubKey = merchant.RsaPubKey; string RsaPriKey = merchant.RsaPriKey; string AesSecret = merchant.AesSecret; string MerchantNo = merchant.MerchantNo; string MerchantName = merchant.MerchantName; //查找商户开通的推送项目 var pushObj = db.PushObj.Where(m => m.Status == 1 && m.MerchantId == merchant.Id).ToList(); foreach(var pushItem in pushObj) { string FieldList = pushItem.FieldList; string TableName = pushItem.TableName; int EncryptMode = pushItem.EncryptMode; string NoticeUrl = pushItem.NoticeUrl; string Title = pushItem.Title; int MerchantId = pushItem.MerchantId; //构造抓取数据sql string sql = "select " + FieldList.TrimEnd(',') + " from u_" + TableName + " where 1=1"; var condiList = db.PushObjCondition.Where(m => m.PushObjId == pushItem.Id).ToList(); foreach(var condi in condiList) { int QueryCondition = condi.QueryCondition; if(QueryCondition == 1) { sql += " and " + condi.QueryField + "='" + condi.QueryVal + "'"; } else if(QueryCondition == 2) { sql += " and " + condi.QueryField + " like '%" + condi.QueryVal + "%'"; } else if(QueryCondition == 3) { string[] QueryValList = condi.QueryVal.Split('|'); sql += " and " + condi.QueryField + ">=" + QueryValList[0] + " and " + condi.QueryField + "<=" + QueryValList[1] + ""; } else if(QueryCondition == 4) { string[] QueryValList = condi.QueryVal.Split('|'); sql += " and " + condi.QueryField + ">='" + QueryValList[0] + "' and " + condi.QueryField + "<='" + QueryValList[1] + "'"; } else if(QueryCondition == 5) { sql += " and " + condi.QueryField + " in (" + condi.QueryVal + ")"; } else if(QueryCondition == 6) { sql += " and " + condi.QueryField + " in ('" + condi.QueryVal.Replace(",", "','") + "')"; } else if(QueryCondition == 7) { string[] QueryValList = condi.QueryVal.Split(','); sql += " and ("; int index = 0; foreach(string QueryVal in QueryValList) { index += 1; sql += condi.QueryField + "=" + QueryVal; if(index < QueryValList.Length) { sql += " or "; } } sql += ")"; } else { sql += " and " + condi.QueryField + "=" + condi.QueryVal; } } //抓取数据开始post数据 DataTable dt = CustomerSqlConn.dtable(sql, AppConfig.Base.SqlConnStr); foreach(DataRow dr in dt.Rows) { SortedList obj = new SortedList(); foreach(DataColumn dc in dt.Columns) { obj.Add(dc.ColumnName, dr[dc.ColumnName].ToString()); } if(EncryptMode == 1) { string content = EncryptHelper.Encrypt1(obj, AesSecret); obj = new SortedList(); obj.Add("type", Title); obj.Add("notice_id", Guid.NewGuid().ToString()); obj.Add("timestamp", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8)); obj.Add("content", content); string requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj); string result = function.PostWebRequest(NoticeUrl, requestJson, "application/json"); if(result.Contains("\"code\":\"200\"")) { } } } } } db.Dispose(); } } }