浏览代码

写完推送数据基础逻辑,待完善

lcl 1 年之前
父节点
当前提交
1f92c19308
共有 6 个文件被更改,包括 253 次插入19 次删除
  1. 1 0
      AppStart/StartHelper.cs
  2. 25 0
      Models/Push/PushObjCondition.cs
  3. 85 0
      Models/Push/WebCMSEntities.cs
  4. 37 0
      Util/EncryptHelper.cs
  5. 103 17
      Util/Queue/PushHelper.cs
  6. 2 2
      Util/Queue/SourceHelper.cs

+ 1 - 0
AppStart/StartHelper.cs

@@ -13,6 +13,7 @@
             if(Environment == 2)
             {
                 SourceHelper.Instance.Start(); //获取原始数据队列
+                PushHelper.Instance.Start(); //推送队列
 
                 // DefaultHelper.Instance.Start(); //队列程序
                 // ......其他队列程序

+ 25 - 0
Models/Push/PushObjCondition.cs

@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+
+namespace MySystem.Models.Push
+{
+    public partial class PushObjCondition
+    {
+        public int Id { get; set; }
+        public int Sort { get; set; }
+        public int QueryCount { get; set; }
+        public int Status { get; set; }
+        public int Version { get; set; }
+        public DateTime? CreateDate { get; set; }
+        public DateTime? UpdateDate { get; set; }
+        public string CreateMan { get; set; }
+        public string UpdateMan { get; set; }
+        public string SeoTitle { get; set; }
+        public string SeoKeyword { get; set; }
+        public string SeoDescription { get; set; }
+        public int QueryCondition { get; set; }
+        public string QueryVal { get; set; }
+        public string QueryField { get; set; }
+        public int PushObjId { get; set; }
+    }
+}

+ 85 - 0
Models/Push/WebCMSEntities.cs

@@ -17,6 +17,7 @@ namespace MySystem.Models.Push
 
         public virtual DbSet<Merchant> Merchant { get; set; }
         public virtual DbSet<PushObj> PushObj { get; set; }
+        public virtual DbSet<PushObjCondition> PushObjCondition { get; set; }
         public virtual DbSet<PushRecord> PushRecord { get; set; }
         public virtual DbSet<RePushQueue> RePushQueue { get; set; }
         public virtual DbSet<SourceCondition> SourceCondition { get; set; }
@@ -178,6 +179,90 @@ namespace MySystem.Models.Push
                     .HasComment("版本号");
             });
 
+            modelBuilder.Entity<PushObjCondition>(entity =>
+            {
+                entity.ToTable("push_obj_condition");
+
+                entity.HasComment("推送数据抓取条件");
+
+                entity.Property(e => e.Id).HasColumnType("int(11)");
+
+                entity.Property(e => e.CreateDate)
+                    .HasColumnType("datetime")
+                    .HasComment("创建时间");
+
+                entity.Property(e => e.CreateMan)
+                    .HasColumnType("varchar(50)")
+                    .HasComment("创建人")
+                    .HasCharSet("utf8")
+                    .HasCollation("utf8_general_ci");
+
+                entity.Property(e => e.PushObjId)
+                    .HasColumnName("push_obj_id")
+                    .HasColumnType("int(11)")
+                    .HasComment("推送项目");
+
+                entity.Property(e => e.QueryCondition)
+                    .HasColumnName("query_condition")
+                    .HasColumnType("int(11)")
+                    .HasComment("查询条件");
+
+                entity.Property(e => e.QueryCount)
+                    .HasColumnType("int(11)")
+                    .HasComment("点击量");
+
+                entity.Property(e => e.QueryField)
+                    .HasColumnName("query_field")
+                    .HasColumnType("varchar(50)")
+                    .HasComment("查询字段")
+                    .HasCharSet("utf8")
+                    .HasCollation("utf8_general_ci");
+
+                entity.Property(e => e.QueryVal)
+                    .HasColumnName("query_val")
+                    .HasColumnType("varchar(50)")
+                    .HasComment("查询值")
+                    .HasCharSet("utf8")
+                    .HasCollation("utf8_general_ci");
+
+                entity.Property(e => e.SeoDescription)
+                    .HasColumnType("varchar(500)")
+                    .HasCharSet("utf8")
+                    .HasCollation("utf8_general_ci");
+
+                entity.Property(e => e.SeoKeyword)
+                    .HasColumnType("varchar(200)")
+                    .HasCharSet("utf8")
+                    .HasCollation("utf8_general_ci");
+
+                entity.Property(e => e.SeoTitle)
+                    .HasColumnType("varchar(100)")
+                    .HasCharSet("utf8")
+                    .HasCollation("utf8_general_ci");
+
+                entity.Property(e => e.Sort)
+                    .HasColumnType("int(11)")
+                    .HasComment("排序序号");
+
+                entity.Property(e => e.Status)
+                    .HasColumnType("int(11)")
+                    .HasComment("状态");
+
+                entity.Property(e => e.UpdateDate)
+                    .HasColumnType("datetime")
+                    .HasComment("更新时间");
+
+                entity.Property(e => e.UpdateMan)
+                    .HasColumnType("varchar(50)")
+                    .HasComment("更新人")
+                    .HasCharSet("utf8")
+                    .HasCollation("utf8_general_ci");
+
+                entity.Property(e => e.Version)
+                    .HasColumnType("int(11)")
+                    .HasComment("版本号");
+            });
+
             modelBuilder.Entity<PushRecord>(entity =>
             {
                 entity.ToTable("push_record");

+ 37 - 0
Util/EncryptHelper.cs

@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using Library;
+using System.Text;
+
+namespace MySystem
+{
+    public class EncryptHelper
+    {
+
+        public static string Encrypt1(SortedList<string, string> obj, string key)
+        {
+            string signstr = function.BuildQueryString(obj) + key;
+            string sign = function.MD532(signstr).ToUpper();
+            obj.Add("sign", sign);
+            return AesEncrypt(Newtonsoft.Json.JsonConvert.SerializeObject(obj), key);
+        }
+
+        public static string AesEncrypt(string str, string key)
+        {
+            if (string.IsNullOrEmpty(str)) return null;
+            Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
+
+            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
+            {
+                Key = Encoding.UTF8.GetBytes(key),
+                Mode = System.Security.Cryptography.CipherMode.ECB,
+                Padding = System.Security.Cryptography.PaddingMode.PKCS7
+            };
+            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
+            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
+            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
+        }
+
+        
+    }
+}

+ 103 - 17
Util/Queue/PushHelper.cs

@@ -1,9 +1,11 @@
 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
 {   
@@ -34,25 +36,109 @@ namespace MySystem
         public void DoSomeThing()
         {
             WebCMSEntities db = new WebCMSEntities();
-            
-            db.Dispose();
-        }
-        
-        //创建表
-        public void CreateTable(string Title, string tableName, string fieldList)
-        {
-            string sql = "CREATE TABLE `" + tableName + "` (\n";
-            sql += "`id` int(11) NOT NULL AUTO_INCREMENT,\n";
-            sql += "`status` int(11) NOT NULL,\n";
-            JsonData fieldJson = JsonMapper.ToObject(fieldList);
-            for (int i = 0; i < fieldJson.Count; i++)
+            //查找开通推送的商户
+            var merchants = db.Merchant.Where(m => m.Status == 1).ToList();
+            foreach(var merchant in merchants)
             {
-                JsonData item = fieldJson[i];
-                sql += "`" + item["name"].ToString() + "` varchar(" + item["len"].ToString() + ") DEFAULT NULL COMMENT '" + item["title"].ToString() + "',\n";
+                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<string, string> obj = new SortedList<string, string>();
+                        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<string, string>();
+                            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\""))
+                            {
+                            }
+                        }
+                    }
+                }
             }
-            sql += "PRIMARY KEY (`id`)\n";
-            sql += ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='" + Title + "';";
-            CustomerSqlConn.op(sql, AppConfig.Base.SqlConnStr);
+            db.Dispose();
         }
+        
     }
 }

+ 2 - 2
Util/Queue/SourceHelper.cs

@@ -120,7 +120,7 @@ namespace MySystem
                     JsonData item = TargetFieldJson[i];
                     TargetQueryField += item["name"].ToString() + ",";
                 }
-                string insertSql = "insert into " + TableName + " (" + TargetQueryField + ")" + sql;
+                string insertSql = "insert into u_" + TableName + " (" + TargetQueryField + ")" + sql;
                 CustomerSqlConn.op(insertSql, AppConfig.Base.SqlConnStr);
             }
             db.Dispose();
@@ -129,7 +129,7 @@ namespace MySystem
         //创建表
         public void CreateTable(string Title, string tableName, string fieldList)
         {
-            string sql = "CREATE TABLE `" + tableName + "` (\n";
+            string sql = "CREATE TABLE `u_" + tableName + "` (\n";
             sql += "`id` int(11) NOT NULL AUTO_INCREMENT,\n";
             sql += "`status` int(11) NOT NULL,\n";
             JsonData fieldJson = JsonMapper.ToObject(fieldList);