SourceHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4. using System.Threading;
  5. using Library;
  6. using LitJson;
  7. using MySystem.Models.Push;
  8. namespace MySystem
  9. {
  10. public class SourceHelper
  11. {
  12. public readonly static SourceHelper Instance = new SourceHelper();
  13. private SourceHelper()
  14. {
  15. }
  16. public void Start()//启动
  17. {
  18. Thread thread = new Thread(threadStart);
  19. thread.IsBackground = true;
  20. thread.Start();
  21. }
  22. private void threadStart()
  23. {
  24. while (true)
  25. {
  26. DoSomeThing();
  27. Thread.Sleep(1000);
  28. }
  29. }
  30. //要执行的方法
  31. public void DoSomeThing()
  32. {
  33. WebCMSEntities db = new WebCMSEntities();
  34. var sources = db.SourceData.Where(m => m.Status == 1).ToList();
  35. foreach(var source in sources)
  36. {
  37. string Title = source.Title; //说明
  38. string TableName = source.TableName; //数据表
  39. string FieldList = source.FieldList; //原始数据字段
  40. string FlagField = source.TargetFieldList; //原始标记字段
  41. string QueryField = "";
  42. JsonData fieldJson = JsonMapper.ToObject(FieldList);
  43. for (int i = 0; i < fieldJson.Count; i++)
  44. {
  45. JsonData item = fieldJson[i];
  46. string source_name = item["source_name"].ToString();
  47. if(source_name.StartsWith("v("))
  48. {
  49. QueryField += source_name.Replace("v(", "").Replace(")", "") + ",";
  50. }
  51. else
  52. {
  53. QueryField += source_name + ",";
  54. }
  55. }
  56. //构造抓取数据
  57. int StartId = source.QueryId;
  58. string sql = "select " + QueryField.TrimEnd(',') + " from " + TableName + " where 1=1";
  59. var condiList = db.SourceCondition.Where(m => m.SourceId == source.Id).ToList();
  60. foreach(var condi in condiList)
  61. {
  62. int QueryCondition = condi.QueryCondition;
  63. if(QueryCondition == 1)
  64. {
  65. sql += " and " + condi.QueryField + "='" + condi.QueryVal + "'";
  66. }
  67. else if(QueryCondition == 2)
  68. {
  69. sql += " and " + condi.QueryField + " like '%" + condi.QueryVal + "%'";
  70. }
  71. else if(QueryCondition == 3)
  72. {
  73. sql += " and " + condi.QueryField + ">" + condi.QueryVal + "";
  74. }
  75. else if(QueryCondition == 4)
  76. {
  77. sql += " and " + condi.QueryField + "<" + condi.QueryVal + "";
  78. }
  79. else if(QueryCondition == 5)
  80. {
  81. string[] QueryValList = condi.QueryVal.Split('|');
  82. sql += " and " + condi.QueryField + ">=" + QueryValList[0] + " and " + condi.QueryField + "<=" + QueryValList[1] + "";
  83. }
  84. else if(QueryCondition == 6)
  85. {
  86. string[] QueryValList = condi.QueryVal.Split('|');
  87. sql += " and " + condi.QueryField + ">='" + QueryValList[0] + "' and " + condi.QueryField + "<='" + QueryValList[1] + "'";
  88. }
  89. else if(QueryCondition == 7)
  90. {
  91. sql += " and " + condi.QueryField + " in (" + condi.QueryVal + ")";
  92. }
  93. else if(QueryCondition == 8)
  94. {
  95. sql += " and " + condi.QueryField + " in ('" + condi.QueryVal.Replace(",", "','") + "')";
  96. }
  97. else if(QueryCondition == 9)
  98. {
  99. string[] QueryValList = condi.QueryVal.Split(',');
  100. sql += " and (";
  101. int index = 0;
  102. foreach(string QueryVal in QueryValList)
  103. {
  104. index += 1;
  105. sql += condi.QueryField + "=" + QueryVal;
  106. if(index < QueryValList.Length)
  107. {
  108. sql += " or ";
  109. }
  110. }
  111. sql += ")";
  112. }
  113. else
  114. {
  115. sql += " and " + condi.QueryField + "='" + condi.QueryVal + "'";
  116. }
  117. }
  118. //创建原始数据表
  119. var sourceEdit = db.SourceData.FirstOrDefault(m => m.Id == source.Id && m.Version == 0);
  120. if(sourceEdit != null)
  121. {
  122. CreateTable(Title, TableName, FieldList);
  123. sourceEdit.Version = 1;
  124. db.SaveChanges();
  125. }
  126. //插入数据
  127. string TargetQueryField = "";
  128. JsonData TargetFieldJson = JsonMapper.ToObject(FieldList);
  129. for (int i = 0; i < TargetFieldJson.Count; i++)
  130. {
  131. JsonData item = TargetFieldJson[i];
  132. TargetQueryField += item["name"].ToString() + ",";
  133. }
  134. string insertSql = "";
  135. sql = sql.Replace("${QueryId}$", StartId.ToString());
  136. sql += " order by Id limit 10";
  137. DataTable dt = CustomerSqlConn.dtable(sql, AppConfig.Base.SourceSqlConnStr);
  138. foreach(DataRow dr in dt.Rows)
  139. {
  140. string values = "";
  141. for (int i = 0; i < fieldJson.Count; i++)
  142. {
  143. JsonData item = fieldJson[i];
  144. string source_name = item["source_name"].ToString();
  145. string field_type = item["field_type"].ToString();
  146. if(!string.IsNullOrEmpty(source_name))
  147. {
  148. if(source_name.StartsWith("v("))
  149. {
  150. values += source_name.Replace("v(", "").Replace(")", "") + ",";
  151. }
  152. else
  153. {
  154. if(field_type == "int" || field_type == "numeric")
  155. {
  156. values += "" + dr[source_name].ToString() + ",";
  157. }
  158. else if(field_type == "datetime")
  159. {
  160. values += "'" + DateTime.Parse(dr[source_name].ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "',";
  161. }
  162. else
  163. {
  164. values += "'" + dr[source_name].ToString() + "',";
  165. }
  166. }
  167. }
  168. }
  169. insertSql += "insert into u_" + TableName + " (" + TargetQueryField.TrimEnd(',') + ") values (" + values.TrimEnd(',') + ");";
  170. StartId = int.Parse(dr["Id"].ToString());
  171. CustomerSqlConn.op("update " + TableName + " set " + FlagField + "=1 where Id=" + StartId, AppConfig.Base.RmSourceSqlConnStr);
  172. }
  173. var edit = db.SourceData.FirstOrDefault(m => m.Id == source.Id);
  174. if(edit != null)
  175. {
  176. edit.QueryId = StartId;
  177. db.SaveChanges();
  178. }
  179. if(!string.IsNullOrEmpty(insertSql))
  180. {
  181. CustomerSqlConn.op(insertSql, AppConfig.Base.SqlConnStr);
  182. }
  183. }
  184. db.Dispose();
  185. }
  186. //创建表
  187. public void CreateTable(string Title, string tableName, string fieldList)
  188. {
  189. string sql = "CREATE TABLE `u_" + tableName + "` (\n";
  190. JsonData fieldJson = JsonMapper.ToObject(fieldList);
  191. string prikey = "";
  192. for (int i = 0; i < fieldJson.Count; i++)
  193. {
  194. JsonData item = fieldJson[i];
  195. string nullCondition = "";
  196. string fieldType = item["field_type"].ToString();
  197. string defaultValue = item["default"].ToString();
  198. if(!string.IsNullOrEmpty(defaultValue))
  199. {
  200. nullCondition = " NOT NULL DEFAULT " + defaultValue;
  201. }
  202. if(item["pri"].ToString() == "1" && item["name"].ToString() == "id")
  203. {
  204. sql += "`" + item["name"].ToString() + "` int NOT NULL AUTO_INCREMENT COMMENT '" + item["title"].ToString() + "',\n";
  205. }
  206. else
  207. {
  208. if(fieldType == "int")
  209. {
  210. sql += "`" + item["name"].ToString() + "` int NOT NULL DEFAULT " + defaultValue + " COMMENT '" + item["title"].ToString() + "',\n";
  211. }
  212. else if(fieldType == "numeric")
  213. {
  214. sql += "`" + item["name"].ToString() + "` numeric(18,2) NOT NULL DEFAULT " + defaultValue + " COMMENT '" + item["title"].ToString() + "',\n";
  215. }
  216. else if(fieldType == "datetime")
  217. {
  218. sql += "`" + item["name"].ToString() + "` datetime COMMENT '" + item["title"].ToString() + "',\n";
  219. }
  220. else
  221. {
  222. sql += "`" + item["name"].ToString() + "` " + fieldType + "(" + item["len"].ToString() + ")" + nullCondition + " COMMENT '" + item["title"].ToString() + "',\n";
  223. }
  224. }
  225. if(item["pri"].ToString() == "1")
  226. {
  227. prikey = item["name"].ToString();
  228. }
  229. }
  230. if(!string.IsNullOrEmpty(prikey))
  231. {
  232. sql += "PRIMARY KEY (`" + prikey + "`)\n";
  233. }
  234. sql += ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='" + Title + "';";
  235. CustomerSqlConn.op(sql, AppConfig.Base.SqlConnStr);
  236. }
  237. }
  238. }