MakeBallListJob.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Collections.Generic;
  3. using FCS.Common;
  4. using FCS.Interface;
  5. using FCS.Models;
  6. using HtmlAgilityPack;
  7. using Quartz;
  8. using System.Data;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using System.Threading;
  12. using System.Diagnostics;
  13. namespace FCS.Crawler.ZCMatchRankingList
  14. {
  15. /// <summary>
  16. /// 上下盘球路
  17. /// </summary>
  18. public class MakeBallListJob : CommonJob, IJob
  19. {
  20. private List<F_Team> TeamList = new List<F_Team>();
  21. private List<F_MakeBallList> RQPLList = new List<F_MakeBallList>();
  22. F_MakeBallList g;
  23. public MakeBallListJob()
  24. {
  25. log = new LogHelper();
  26. services = IOC.Resolve<IDTOpenCode>();
  27. g = new F_MakeBallList();
  28. }
  29. public void Execute(IJobExecutionContext context)
  30. {
  31. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  32. GetAll();
  33. }
  34. #region 更新数据
  35. /// <summary>
  36. /// 获取所有数据
  37. /// </summary>
  38. public void GetAll()
  39. {
  40. GetSqlString(GetAllEventUrl, "");
  41. }
  42. /// <summary>
  43. /// 更新数据
  44. /// </summary>
  45. /// <param name="Year"></param>
  46. public void Click(string Year = "")
  47. {
  48. CommonHelper.LogBD(typeof(MakeBallListJob), () =>
  49. {
  50. if (Year == "")
  51. {
  52. Year = DateTime.Now.Year.ToString();
  53. }
  54. var sql = string.Format(GetNewEventUrl, Year);
  55. GetSqlString(sql, Year);
  56. });
  57. }
  58. #endregion
  59. /// <summary>
  60. /// 解析数据
  61. /// </summary>
  62. /// <param name="sqlstring"></param>
  63. private void GetSqlString(string sqlstring, string Year)
  64. {
  65. TeamList = services.GetTeamList();
  66. var searchsql = "and Remark is not null ";
  67. if (Year != "")
  68. {
  69. searchsql += "and Season like '%" + Year + "%'";
  70. }
  71. var ds = services.Query<F_Grouping>(searchsql, "", "distinct EventId,Season,Remark").ToList();
  72. if (ds != null && ds.Count > 0)
  73. {
  74. int max = ds.Count;
  75. int num = 0;
  76. ds.ForEach(async p =>
  77. {
  78. var url = p.Remark.ToString();
  79. var eventId = p.EventId.ToString();
  80. var season = p.Season.ToString();
  81. //获取到总进球单双的链接
  82. await Task.Run(() =>
  83. {
  84. HtmlDocument doc = CommonHelper.GetHtml(url, new Dictionary<string, string>(), "", "", 10000, 100);
  85. var TJList = doc.DocumentNode.SelectNodes("//*[@class='tongji_list']");
  86. if (TJList != null && TJList.Count() != 0 && TJList[0].InnerText.Contains("赛事统计"))
  87. {
  88. HtmlDocument doc1 = new HtmlDocument();
  89. doc1.LoadHtml(TJList[0].InnerHtml);
  90. var liList = doc1.DocumentNode.SelectNodes("//ul/li/a").ToList();
  91. var Glod = liList.Where(o => o.InnerText.Contains("上下盘路")).First();
  92. if (Glod != null)
  93. {
  94. var sxplurl = Glod.Attributes.SingleOrDefault(a => a.Name.Equals("href")).Value.ToString();
  95. Task.Run(() =>
  96. {
  97. GetList(sxplurl, eventId, season);
  98. });
  99. }
  100. }
  101. });
  102. lock (g)
  103. {
  104. num++;
  105. Monitor.Pulse(g); //完成,通知等待队列,告知已完,执行下一个。
  106. }
  107. });
  108. lock (g)
  109. {
  110. while (num < max)
  111. {
  112. Monitor.Wait(g);//等待
  113. }
  114. }
  115. Trace.WriteLine("获取MakeBallListJob 完结");
  116. try
  117. {
  118. if (Year != "")
  119. {
  120. services.Delete<F_MakeBallList>(" and Season like '%" + Year + "%'");
  121. }
  122. services.SqlBulkCopyAdd<F_MakeBallList>(RQPLList);
  123. Trace.WriteLine("F_MakeBallList更新完毕");
  124. }
  125. catch (Exception ex)
  126. {
  127. throw;
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// 读取上下盘路榜信息
  133. /// </summary>
  134. /// <param name="sxplurl"></param>
  135. /// <param name="eventId"></param>
  136. /// <param name="season"></param>
  137. public void GetList(string url, string eventId, string season)
  138. {
  139. if (url.Contains("wwaattssuunn"))
  140. {
  141. return;
  142. }
  143. HtmlDocument doc = CommonHelper.GetHtml(url, new Dictionary<string, string>(), "", "", 10000, 100);
  144. //获取上下盘路列表
  145. var TongJiListData = doc.DocumentNode.SelectNodes("//*[@class='zstab']/tbody/tr");
  146. if (TongJiListData == null || TongJiListData.Count() == 0) return;
  147. //上下盘路类别
  148. var dataItem = services.GetDataItem(DataItemDetailEnum.FootBallMakeBallType);
  149. foreach (var tritem in TongJiListData)
  150. {
  151. try
  152. {
  153. HtmlDocument doc11 = new HtmlDocument();
  154. doc11.LoadHtml(tritem.InnerHtml);
  155. var tdList = doc11.DocumentNode.SelectNodes("//td");
  156. if (tdList == null || tdList.Count == 0) continue;
  157. #region 区分总榜,主场,客场
  158. var choosetd = doc11.DocumentNode.SelectNodes("//*[@class='dataChoose']/span");
  159. if (choosetd.Count < 30) continue;
  160. for (int i = 0; i < choosetd.Count / 10; i++)
  161. {
  162. F_MakeBallList f_MakeBallList = new F_MakeBallList();
  163. f_MakeBallList.Id = Guid.NewGuid().ToString();
  164. f_MakeBallList.EventId = eventId;
  165. f_MakeBallList.Season = season;
  166. f_MakeBallList.Sort = tdList[0].InnerText.Trim() == null ? 0 : int.Parse(tdList[0].InnerText.Trim().ToString());
  167. var TeamName = tdList[1].InnerText.Trim().ToString();
  168. var Team = TeamList.Where(o => o.Name == TeamName);
  169. f_MakeBallList.TeamId = Team.Count() > 0 ? Team.FirstOrDefault().Id : "";
  170. if (Team.Count() == 0)
  171. {
  172. f_MakeBallList.Remark = TeamName;
  173. }
  174. switch (i)
  175. {
  176. case 0:
  177. //总榜
  178. f_MakeBallList.MakeBallType = dataItem.Where(o => o.ItemValue == "1").First().Id.ToString();
  179. break;
  180. case 1:
  181. //主场
  182. f_MakeBallList.MakeBallType = dataItem.Where(o => o.ItemValue == "2").First().Id.ToString();
  183. break;
  184. case 2:
  185. //客场
  186. f_MakeBallList.MakeBallType = dataItem.Where(o => o.ItemValue == "3").First().Id.ToString();
  187. break;
  188. }
  189. f_MakeBallList.UpDisc = choosetd[0 + i].InnerText == null || choosetd[0 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[0 + i].InnerText.Trim().ToString());
  190. f_MakeBallList.FlatDisc = choosetd[3 + i].InnerText == null || choosetd[3 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[3 + i].InnerText.Trim().ToString());
  191. f_MakeBallList.DownDisc = choosetd[6 + i].InnerText == null || choosetd[6 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[6 + i].InnerText.Trim().ToString());
  192. f_MakeBallList.WinDiscNum = choosetd[9 + i].InnerText == null || choosetd[9 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[9 + i].InnerText.Trim().ToString());
  193. f_MakeBallList.GoDiscNum = choosetd[12 + i].InnerText == null || choosetd[12 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[12 + i].InnerText.Trim().ToString());
  194. f_MakeBallList.TransportDiscNum = choosetd[15 + i].InnerText == null || choosetd[15 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[15 + i].InnerText.Trim().ToString());
  195. f_MakeBallList.NetDiscNum = choosetd[18 + i].InnerText == null || choosetd[18 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[18 + i].InnerText.Trim().ToString());
  196. f_MakeBallList.WinPercentage = choosetd[21 + i].InnerText == null || choosetd[21 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[21 + i].InnerText.Trim().ToString());
  197. f_MakeBallList.GoPercentage = choosetd[24 + i].InnerText == null || choosetd[24 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[24 + i].InnerText.Trim().ToString());
  198. f_MakeBallList.NegativePercentage = choosetd[27 + i].InnerText == null || choosetd[27 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[27 + i].InnerText.Trim().ToString());
  199. f_MakeBallList.SixRounds = tdList[12].InnerText.Replace("\r", ",").Replace("\n", "").Replace("\t", "").Replace(" ", "").Trim();
  200. RQPLList.Add(f_MakeBallList);
  201. }
  202. #endregion
  203. }
  204. catch (Exception ex)
  205. {
  206. throw;
  207. }
  208. }
  209. }
  210. #region SQL语句
  211. /// <summary>
  212. /// 获取所有的赛事
  213. /// </summary>
  214. private static string GetAllEventUrl = @"select distinct EventId,Season,Remark from F_Grouping where Remark is not null";
  215. /// <summary>
  216. /// 获取最新两年的数据赛事
  217. /// </summary>
  218. private static string GetNewEventUrl = @"select distinct EventId,Season,Remark from F_Grouping where Remark is not null and Season like '%{0}%'";
  219. #endregion
  220. }
  221. }