B_TeamStatisticsJob.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using FCS.Common;
  3. using FCS.Interface;
  4. using FCS.Models;
  5. using FCS.Models.Entity;
  6. using HtmlAgilityPack;
  7. using Quartz;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using FCS.Models.DTO;
  16. namespace FCS.Crawler.Basketball
  17. {
  18. /// <summary>
  19. /// 篮球球队统计
  20. /// </summary>
  21. public class B_TeamStatisticsJob : CommonJob, IJob
  22. {
  23. private List<B_TeamStatistics> TeamStatisticsList = new List<B_TeamStatistics>();
  24. private List<DataItemDetail> DateItem = new List<DataItemDetail>();
  25. private List<B_Team> TeamList = new List<B_Team>();
  26. B_TeamStatistics g;
  27. public B_TeamStatisticsJob()
  28. {
  29. g = new B_TeamStatistics();
  30. }
  31. public void Execute(IJobExecutionContext context)
  32. {
  33. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  34. }
  35. #region 更新数据
  36. /// <summary>
  37. /// 获取所有数据
  38. /// </summary>
  39. public void GetAll()
  40. {
  41. GetSqlString("");
  42. }
  43. /// <summary>
  44. /// 更新数据
  45. /// </summary>
  46. /// <param name="Year"></param>
  47. public void Click(string Year = "")
  48. {
  49. if (Year == "")
  50. {
  51. Year = DateTime.Now.Year.ToString().Substring(2, 2); ;
  52. }
  53. GetSqlString(Year);
  54. }
  55. #endregion
  56. /// <summary>
  57. /// 解析数据
  58. /// </summary>
  59. /// <param name="sqlstring"></param>
  60. private void GetSqlString(string Year)
  61. {
  62. TeamList = services.Query<B_Team>().ToList();
  63. var dataItem = services.GetDataItem(DataItemDetailEnum.BasketballGameType);
  64. var ds = services.Query<B_Grouping>("and Remark is not null", "", "distinct EventId,Season,Remark").ToList();
  65. if (Year != "")
  66. {
  67. ds = services.Query<B_Grouping>("and Remark is not null and Season like '%" + Year + "%'", "", "distinct EventId,Season,Remark").ToList();
  68. }
  69. int max = ds.Count;
  70. int num = 0;
  71. ds.ForEach(async p =>
  72. {
  73. await Task.Run(() =>
  74. {
  75. var eventId = p.EventId;
  76. var season = p.Season;
  77. var url = p.Remark.ToString().Replace("saiAll.do", "1/teamStatistics.do");
  78. GetData(url, eventId, season, dataItem.Where(o => o.ItemValue == "1").First().Id.ToString());
  79. url = p.Remark.ToString().Replace("saiAll.do", "2/teamStatistics.do");
  80. GetData(url, eventId, season, dataItem.Where(o => o.ItemValue == "2").First().Id.ToString());
  81. url = p.Remark.ToString().Replace("saiAll.do", "3/teamStatistics.do");
  82. GetData(url, eventId, season, dataItem.Where(o => o.ItemValue == "3").First().Id.ToString());
  83. });
  84. lock (g)
  85. {
  86. num++;
  87. Monitor.Pulse(g); //完成,通知等待队列,告知已完,执行下一个。
  88. }
  89. });
  90. lock (g)
  91. {
  92. while (num < max)
  93. {
  94. Monitor.Wait(g);//等待
  95. }
  96. }
  97. Trace.WriteLine("获取GoalSingleDbListJob 完结");
  98. try
  99. {
  100. if (Year != "")
  101. {
  102. services.Delete<B_TeamStatistics>(" and Season like '%" + Year + "%'");
  103. }
  104. services.SqlBulkCopyAdd<B_TeamStatistics>(TeamStatisticsList);
  105. Trace.WriteLine("B_TeamStatistics更新完毕");
  106. }
  107. catch (Exception ex)
  108. {
  109. throw;
  110. }
  111. }
  112. /// <summary>
  113. /// 解析排行榜数据
  114. /// </summary>
  115. /// <param name="url"></param>
  116. /// <param name="eventId"></param>
  117. /// <param name="season"></param>
  118. private void GetData(string url, string eventId, string season, string gameType)
  119. {
  120. HtmlParameterDTO dtomodel = new HtmlParameterDTO();
  121. dtomodel.Url = url;
  122. HtmlDocument doc = CommonHelper.GetHtmlHtmlDocument(dtomodel);
  123. if (doc.DocumentNode.InnerHtml == "Termination" || doc.DocumentNode.InnerHtml == "" || doc.DocumentNode.SelectNodes("//*[@class='paiming']") == null)
  124. {
  125. return;
  126. }
  127. var team = doc.DocumentNode.SelectNodes("//*[@class='paiming']").First();
  128. HtmlDocument itemdoc = new HtmlDocument();
  129. itemdoc.LoadHtml(team.InnerHtml);
  130. var trList = itemdoc.DocumentNode.SelectNodes("//table/tr").ToList();
  131. trList.ForEach(p =>
  132. {
  133. if (!p.InnerHtml.Contains("<th"))
  134. {
  135. HtmlDocument trdoc = new HtmlDocument();
  136. trdoc.LoadHtml(p.InnerHtml);
  137. var tdList = trdoc.DocumentNode.InnerHtml.Contains("td") ? trdoc.DocumentNode.SelectNodes("//td").ToList() : null;
  138. if (tdList != null && tdList.Count() > 1)
  139. {
  140. B_TeamStatistics teamStatistics = new B_TeamStatistics();
  141. teamStatistics.Id = Guid.NewGuid().ToString();
  142. teamStatistics.TeamId = TeamList.Where(o => o.Name == tdList[0].InnerText.Trim().ToString()).Count() > 0 ? TeamList.Where(o => o.Name == tdList[0].InnerText.Trim().ToString()).FirstOrDefault().Id : "";
  143. teamStatistics.EventId = eventId;
  144. teamStatistics.Season = season;
  145. teamStatistics.GameType = gameType;
  146. teamStatistics.Sum = int.Parse(tdList[1].InnerText.Trim().ToString());
  147. teamStatistics.ScoreAvg = tdList[2].InnerText.Trim().ToString();
  148. teamStatistics.LossAvg = tdList[3].InnerText.Trim().ToString();
  149. teamStatistics.ShootingHitRate = tdList[4].InnerText.Trim().ToString();
  150. teamStatistics.TrisectionHitRate = tdList[5].InnerText.Trim().ToString();
  151. teamStatistics.PenaltyKickHitRate = tdList[6].InnerText.Trim().ToString();
  152. teamStatistics.AvgBasketball = tdList[7].InnerText.Trim().ToString();
  153. teamStatistics.AvgAssists = tdList[8].InnerText.Trim().ToString();
  154. teamStatistics.AvgBlocks = tdList[9].InnerText.Trim().ToString();
  155. teamStatistics.AvgSteals = tdList[10].InnerText.Trim().ToString();
  156. teamStatistics.AvgMiss = tdList[11].InnerText.Trim().ToString();
  157. teamStatistics.AvgFoul = tdList[12].InnerText.Trim().ToString();
  158. TeamStatisticsList.Add(teamStatistics);
  159. }
  160. }
  161. });
  162. }
  163. }
  164. }