using System;
using System.Collections.Generic;
using FCS.Common;
using FCS.Interface;
using FCS.Models;
using HtmlAgilityPack;
using Quartz;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
namespace FCS.Crawler.ZCMatchRankingList
{
///
/// 上下盘球路
///
public class MakeBallListJob : CommonJob, IJob
{
private static List TeamList = new List();
private static List RQPLList = new List();
public MakeBallListJob()
{
log = new LogHelper();
services = IOC.Resolve();
}
public void Execute(IJobExecutionContext context)
{
Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
GetAll();
}
#region 更新数据
///
/// 获取所有数据
///
public void GetAll()
{
GetSqlString(GetAllEventUrl,"");
}
///
/// 更新数据
///
///
public void Click(string Year = "")
{
if (Year == "")
{
Year = DateTime.Now.Year.ToString();
}
var sql = string.Format(GetNewEventUrl, Year);
GetSqlString(sql, Year);
}
#endregion
///
/// 解析数据
///
///
private void GetSqlString(string sqlstring,string Year)
{
ThreadPool.SetMinThreads(10, 10);
ThreadPool.SetMaxThreads(500, 500);
TeamList = services.GetTeamList();
var ds = SqlHelper.ExecuteDataset(CommandType.Text, sqlstring);
if (ds != null && ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
var url = ds.Tables[0].Rows[i]["Remark"].ToString();
var eventId = ds.Tables[0].Rows[i]["EventId"].ToString();
var season = ds.Tables[0].Rows[i]["Season"].ToString();
//获取到总进球单双的链接
Task.Run(() =>
{
HtmlDocument doc = CommonHelper.GetHtml(url, new Dictionary(), "", "", 10000, 100);
var TJList = doc.DocumentNode.SelectNodes("//*[@class='tongji_list']");
if (TJList != null && TJList.Count() != 0 && TJList[0].InnerText.Contains("赛事统计"))
{
HtmlDocument doc1 = new HtmlDocument();
doc1.LoadHtml(TJList[0].InnerHtml);
var liList = doc1.DocumentNode.SelectNodes("//ul/li/a").ToList();
var Glod = liList.Where(o => o.InnerText.Contains("上下盘路")).First();
if (Glod != null)
{
var sxplurl = Glod.Attributes.SingleOrDefault(a => a.Name.Equals("href")).Value.ToString();
Task.Run(() =>
{
GetList(sxplurl, eventId, season);
});
}
}
});
}
}
int maxWorkerThreads, workerThreads;
int maxportThreads, portThreads;
while (true)
{
/*
GetAvailableThreads():检索由 GetMaxThreads 返回的线程池线程的最大数目和当前活动数目之间的差值。
而GetMaxThreads 检索可以同时处于活动状态的线程池请求的数目。
通过最大数目减可用数目就可以得到当前活动线程的数目,如果为零,那就说明没有活动线程,说明所有线程运行完毕。
*/
ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxportThreads);
ThreadPool.GetAvailableThreads(out workerThreads, out portThreads);
Thread.Sleep(1000);
Trace.WriteLine("正在执行任务的线程数" + (maxWorkerThreads - workerThreads));
if (maxWorkerThreads - workerThreads == 0)
{
Console.WriteLine("Thread Finished!");
break;
}
}
try
{
if (Year != "")
{
services.Delete(" and Season like '%" + Year + "%'");
}
services.SqlBulkCopyAdd(RQPLList);
Trace.WriteLine("F_MakeBallList更新完毕");
}
catch (Exception ex)
{
throw;
}
}
///
/// 读取上下盘路榜信息
///
///
///
///
public void GetList(string url, string eventId, string season)
{
if (url.Contains("wwaattssuunn"))
{
return;
}
HtmlDocument doc = CommonHelper.GetHtml(url, new Dictionary(), "", "", 10000, 100);
//获取上下盘路列表
var TongJiListData = doc.DocumentNode.SelectNodes("//*[@class='zstab']/tbody/tr");
if (TongJiListData == null || TongJiListData.Count() == 0) return;
//上下盘路类别
var dataItem = services.GetDataItem(DataItemDetailEnum.FootBallMakeBallType);
foreach (var tritem in TongJiListData)
{
try
{
HtmlDocument doc11 = new HtmlDocument();
doc11.LoadHtml(tritem.InnerHtml);
var tdList = doc11.DocumentNode.SelectNodes("//td");
if (tdList == null || tdList.Count == 0) continue;
#region 区分总榜,主场,客场
var choosetd = doc11.DocumentNode.SelectNodes("//*[@class='dataChoose']/span");
if (choosetd.Count < 30) continue;
for (int i = 0; i < choosetd.Count / 10; i++)
{
F_MakeBallList f_MakeBallList = new F_MakeBallList();
f_MakeBallList.Id = Guid.NewGuid().ToString();
f_MakeBallList.EventId = eventId;
f_MakeBallList.Season = season;
f_MakeBallList.Sort = tdList[0].InnerText.Trim() == null ? 0 : int.Parse(tdList[0].InnerText.Trim().ToString());
var TeamName = tdList[1].InnerText.Trim().ToString();
var Team = TeamList.Where(o => o.Name == TeamName);
f_MakeBallList.TeamId = Team.Count() > 0 ? Team.FirstOrDefault().Id : "";
if (Team.Count() == 0)
{
f_MakeBallList.Remark = TeamName;
}
switch (i)
{
case 0:
//总榜
f_MakeBallList.MakeBallType = dataItem.Where(o => o.ItemValue == "1").First().Id.ToString();
break;
case 1:
//主场
f_MakeBallList.MakeBallType = dataItem.Where(o => o.ItemValue == "2").First().Id.ToString();
break;
case 2:
//客场
f_MakeBallList.MakeBallType = dataItem.Where(o => o.ItemValue == "3").First().Id.ToString();
break;
}
f_MakeBallList.UpDisc = choosetd[0 + i].InnerText == null || choosetd[0 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[0 + i].InnerText.Trim().ToString());
f_MakeBallList.FlatDisc = choosetd[3 + i].InnerText == null || choosetd[3 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[3 + i].InnerText.Trim().ToString());
f_MakeBallList.DownDisc = choosetd[6 + i].InnerText == null || choosetd[6 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[6 + i].InnerText.Trim().ToString());
f_MakeBallList.WinDiscNum = choosetd[9 + i].InnerText == null || choosetd[9 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[9 + i].InnerText.Trim().ToString());
f_MakeBallList.GoDiscNum = choosetd[12 + i].InnerText == null || choosetd[12 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[12 + i].InnerText.Trim().ToString());
f_MakeBallList.TransportDiscNum = choosetd[15 + i].InnerText == null || choosetd[15 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[15 + i].InnerText.Trim().ToString());
f_MakeBallList.NetDiscNum = choosetd[18 + i].InnerText == null || choosetd[18 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[18 + i].InnerText.Trim().ToString());
f_MakeBallList.WinPercentage = choosetd[21 + i].InnerText == null || choosetd[21 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[21 + i].InnerText.Trim().ToString());
f_MakeBallList.GoPercentage = choosetd[24 + i].InnerText == null || choosetd[24 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[24 + i].InnerText.Trim().ToString());
f_MakeBallList.NegativePercentage = choosetd[27 + i].InnerText == null || choosetd[27 + i].InnerText.Trim().ToString() == "" ? 0 : int.Parse(choosetd[27 + i].InnerText.Trim().ToString());
f_MakeBallList.SixRounds = tdList[12].InnerText.Replace("\r", ",").Replace("\n", "").Replace("\t", "").Replace(" ", "").Trim();
RQPLList.Add(f_MakeBallList);
}
#endregion
}
catch (Exception ex)
{
throw;
}
}
}
#region SQL语句
///
/// 获取所有的赛事
///
private static string GetAllEventUrl = @"select distinct EventId,Season,Remark from F_Grouping where Remark is not null";
///
/// 获取最新两年的数据赛事
///
private static string GetNewEventUrl = @"select distinct EventId,Season,Remark from F_Grouping where Remark is not null and Season like '%{0}%'";
#endregion
}
}