123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using Cache;
- using Common;
- using HtmlAgilityPack;
- using Logger;
- using Main.Base;
- using Main.Lottery;
- using Models;
- using Quartz;
- namespace Main.Job
- {
- /// <summary>
- /// 自动生成M.55128.CN首页
- /// </summary>
- public class AutoGenerateM55128CNIndexPage : BaseJob, IJob
- {
- #region Attribute
- /// <summary>
- /// 当前作业配置信息
- /// </summary>
- private JobConfigEntity _config = null;
- /// <summary>
- /// 日志
- /// </summary>
- private static LogHelper _logHelper = new LogHelper(typeof(ThematicArticleSpiderJob));
- /// <summary>
- /// 资讯类别
- /// </summary>
- private static string[] _zxtypes = { "FC3D", "SSQ", "PL3", "QT" };
- /// <summary>
- /// 需要展示的开奖号
- /// </summary>
- private static List<SCCLottery> _lotteryTypeList = new List<SCCLottery>
- {
- SCCLottery.QGC_SSQ,
- SCCLottery.QGC_FC3D,
- SCCLottery.QGC_PL3,
- SCCLottery.QGC_DLT
- };
- #region 开奖号相关模板
- /// <summary>
- /// 开奖整体模板
- /// </summary>
- private static string kj_item_tmp = @"
- <div class='kj-item-title'>
- {0}
- {1}
- </div>
- <div class='kj-item-ball'>
- <div class='ball-wrapper'>
- {2}
- </div>
- {3}
- </div>
- <div class='kj-item-bottom'>
- <span class='kj-item-bl'>奖池:{4}元</span>
- <div class='kj-item-br'>
- {5}
- {6}
- </div>
- </div>";
- /// <summary>
- /// 彩种名称模板
- /// </summary>
- private static string lottery_name_tmp = "<span class=\"kj-item-name\">{0}</span>";
- /// <summary>
- /// 期号模板
- /// </summary>
- private static string lottery_periods_tmp = "<span class=\"kj-item-periods\">第{0}期</span>";
- /// <summary>
- /// 红球模板
- /// </summary>
- private static string lottery_redball_tmp = "<span class=\"ball-item redBall\">{0}</span>";
- /// <summary>
- /// 蓝球模板
- /// </summary>
- private static string lottery_blueball_tmp = "<span class=\"ball-item blueBall\">{0}</span>";
- /// <summary>
- /// 奖池模板
- /// </summary>
- private static string lottery_jackpot_tmp = "<span class=\"kj-item-jackpot\">{0}</span>";
- /// <summary>
- /// 更多
- /// </summary>
- private static string more_tmp = "<a href=\"{0}\"><span class=\"more iconfont icon-right\"></span></a>";
- /// <summary>
- /// 开奖详情链接
- /// </summary>
- private static string kj_detail_url_tmp = "<a href=\"{0}\"><span class=\"kj-btn\">开奖详情</span></a>";
- /// <summary>
- /// 开奖走势链接
- /// </summary>
- private static string kj_zs_url_tmp = "<a href=\"{0}\"><span class=\"kj-btn\">走势图表</span></a>";
- /// <summary>
- /// 其他地址
- /// </summary>
- private static Dictionary<SCCLottery, List<string>> other_url = new Dictionary<SCCLottery, List<string>>
- {
- { SCCLottery.QGC_SSQ,new List<string>{ "https://m.55128.cn/m/kj/ssq.html", "https://m.55128.cn/m/kj/ssq.html", "https://m.55128.cn/m/kjzs/ssq.html"}},
- { SCCLottery.QGC_FC3D,new List<string>{ "https://m.55128.cn/m/kj/fc3d.html", "https://m.55128.cn/m/kj/fc3d.html", "https://m.55128.cn/m/kjzs/fc3d.html"}},
- { SCCLottery.QGC_PL3,new List<string>{ "https://m.55128.cn/m/kj/pl3.html", "https://m.55128.cn/m/kj/pl3.html", "https://m.55128.cn/m/kjzs/pl3.html"}},
- { SCCLottery.QGC_DLT,new List<string>{ "https://m.55128.cn/m/kj/dlt.html", "https://m.55128.cn/m/kj/dlt.html", "https://m.55128.cn/m/kjzs/dlt.html"}},
- };
- #endregion
- #region 资讯相关模板
- /// <summary>
- /// 资讯模板
- /// </summary>
- private static string news_tmp = @"
- <a href='https://m.55128.cn/m/zx/{0}/{1}.html' class='news-item border-1px-bottom'>
- <span class='news-item-text'>{2}</span>
- <span class='news-item-time'>{3}</span>
- </a>";
- #endregion
- #endregion
- /// <summary>
- /// 入口
- /// </summary>
- /// <param name="context"></param>
- public void Execute(IJobExecutionContext context)
- {
- Logger(typeof(AutoGenerateM55128CNIndexPage), "AutoGenerateM55128CNIndexPage-作业执行入口", () =>
- {
- _config = this.GetConfigFromDataMap(context);
- Trace.WriteLine(_config.Name + "=============开始执行:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- //资讯
- Dictionary<string, string> news_dict = InitNewsHtml();
- //开奖号
- Dictionary<string, string> lottery_dict = InitLotteryHtml();
- //特征字符串-待填充的数据
- //IEnumerable<KeyValuePair<string, string>> _source = news_dict.Concat(lottery_dict);
- foreach (KeyValuePair<string, string> pair in lottery_dict)
- {
- news_dict.Add(pair.Key, pair.Value);
- }
- if (news_dict.Count > 0)
- {
- //最终文件存放路径
- string filepath = ConfigHelper.GetValue("FileSavePath") + "\\index.html";
- //模板路径
- string templatePath = Utils.GetBaseDirectory("\\Template\\home.html");
- //临时保存路径
- string tempPath = Utils.GetBaseDirectory("\\index.html");
- if (Utils.IsExistFile(tempPath))
- {
- Utils.DeleteFile(tempPath);
- }
- GenerateStaticFileHelper helper = new GenerateStaticFileHelper(templatePath, tempPath);
- bool isSucc = helper.CreateStaticHtmlFile(news_dict);
- if (isSucc)
- {
- //将文件拷贝到最终文件存放目录
- if (Utils.IsExistFile(filepath))
- {
- Utils.DeleteFile(filepath);
- }
- Utils.CopyFile(tempPath, filepath);
- _logHelper.Debug($"生成成功!文件地址:{filepath},时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
- }
- }
- Trace.WriteLine(_config.Name + "=============执行结束:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- }, e =>
- {
- _logHelper.Error("引发异常:" + e.Message);
- }, () =>
- {
- }, ErrorHandel.Continue);
- }
- #region 资讯相关
- /// <summary>
- /// 组装资讯Html
- /// </summary>
- /// <returns></returns>
- private Dictionary<string, string> InitNewsHtml()
- {
- Dictionary<string, string> dict = new Dictionary<string, string>();
- StringBuilder builder = new StringBuilder();
- List<NewsPreview> list = Cache.CacheFactory.GetCacheInstance().GetCache<List<NewsPreview>>("InitNewsHtmlCacheKey");
- if (list == null || list.Count == 0)
- {
- list = GetHomeNewsList();
- Cache.CacheFactory.GetCacheInstance().WriteCache(list, "InitNewsHtmlCacheKey", DateTime.Now.AddHours(1));
- }
- if (list.Count > 0)
- {
- foreach (NewsPreview news in list)
- {
- string type = news.NewsType;
- List<NewsPreviewItem> item = news.NewsPreviewItem;
- foreach (NewsPreviewItem previewItem in item)
- {
- builder.Append(string.Format(news_tmp, type.ToLower(), previewItem.NewsId, previewItem.Title, previewItem.AddTime));
- }
- dict.Add($"${type}$", builder.ToString());
- builder.Clear();
- }
- }
- return dict;
- }
- /// <summary>
- /// 获取首页最新资讯数据
- /// </summary>
- /// <returns></returns>
- private List<NewsPreview> GetHomeNewsList()
- {
- List<NewsPreview> res = new List<NewsPreview>();
- //List<BaseDataItem> dataItems = GetDataItemList().Where(t => _zxtypes.Contains(t.EnCode)).ToList();
- Logger(this.GetType(), "GetHomeNewsList-获取首页最新资讯", () =>
- {
- foreach (string code in _zxtypes)
- {
- List<BaseNews> news = InitNewsData(code);
- if (news.Count > 0)
- {
- NewsPreview preview = new NewsPreview
- {
- NewsType = code,
- ItemName = news[0].Category
- };
- //BaseDataItem tmpbaseitme = dataItems.FirstOrDefault(w => w.EnCode == code);
- //if (tmpbaseitme != null) preview.ItemName = tmpbaseitme.ItemName;
- List<NewsPreviewItem> newsPreviewItem = new List<NewsPreviewItem>();
- foreach (BaseNews n in news)
- {
- NewsPreviewItem preItem = new NewsPreviewItem
- {
- AddTime = n.CreateDate.ToString("yyyy-MM-dd"),
- NewsId = n.PK.ToString(),
- Title = n.FullHead,
- PeriodsNumber = n.PeriodsNumber
- };
- newsPreviewItem.Add(preItem);
- }
- preview.NewsPreviewItem = newsPreviewItem;
- res.Add(preview);
- }
- }
- }, e =>
- {
- Console.WriteLine(e.Message);
- });
- return res;
- }
- /// <summary>
- /// 查询咨询首页的分类下面最新的5条数据 详情看sql语句
- /// </summary>
- /// <returns></returns>
- private static List<BaseNews> InitNewsData(string code)
- {
- string strSql = @"SELECT TOP 5 * FROM Base_News WHERE CategoryId in(SELECT
- d.ItemDetailId
- FROM Base_DataItemDetail d
- LEFT JOIN Base_DataItem i ON i.ItemId = d.ItemId
- WHERE 1 = 1
- AND d.EnabledMark = 1
- AND d.DeleteMark = 0
- AND i.ItemCode ='{0}'
- AND d.ItemValue in('3DYC','P3YC','SSQYC','DLT')
- )AND IsDelete = 0 AND TypeId = 1 ORDER BY CreateDate DESC";
- string sql = string.Format(strSql, code);
- SqlConnection conn = SqlHelper.GetConnection(DbConnectionType.LottomatBaseDB);
- DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
- if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
- {
- return ds.Tables[0].DataTableToList<BaseNews>();
- }
- return null;
- }
- /// <summary>
- /// 获取数据字典列表(给绑定下拉框提供的)
- /// </summary>
- /// <returns></returns>
- private List<BaseDataItem> GetDataItemList()
- {
- string sql = @"SELECT i.ItemId ,
- i.ItemCode AS EnCode ,
- d.ItemDetailId ,
- d.ParentId ,
- d.ItemCode ,
- d.ItemName ,
- d.ItemValue ,
- d.QuickQuery ,
- d.SimpleSpelling ,
- d.IsDefault ,
- d.SortCode ,
- d.EnabledMark,
- d.IsRecommend,
- d.IsHot,
- d.IsShowHomePage
- FROM Base_DataItemDetail d
- LEFT JOIN Base_DataItem i ON i.ItemId = d.ItemId
- WHERE 1 = 1
- AND d.EnabledMark = 1
- AND d.DeleteMark = 0
- ORDER BY d.SortCode ASC";
- SqlConnection conn = SqlHelper.GetConnection(DbConnectionType.LottomatBaseDB);
- DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
- if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
- {
- List<BaseDataItem> res = ds.Tables[0].DataTableToList<BaseDataItem>();
- return res;
- }
- return null;
- }
- #endregion
- #region 开奖号相关
- /// <summary>
- /// 组装开奖号Html
- /// </summary>
- /// <returns></returns>
- private Dictionary<string, string> InitLotteryHtml()
- {
- Dictionary<string, string> dict = new Dictionary<string, string>();
- List<LotteryData> list = GetHomeLotteryData();
- if (list.Count > 0)
- {
- StringBuilder builder = new StringBuilder();
- foreach (LotteryData data in list)
- {
- //彩种名称
- string lottery_name = string.Format(lottery_name_tmp, data.LotteryName);
- //期号
- string lottery_periods = string.Format(lottery_periods_tmp, data.Term);
- //红球
- StringBuilder lottery_redball = new StringBuilder();
- if (data.RedBall != null && data.RedBall.Length > 0)
- {
- for (int i = 0; i < data.RedBall.Length; i++)
- {
- lottery_redball.Append(string.Format(lottery_redball_tmp, data.RedBall[i]));
- }
- }
- //蓝球
- StringBuilder lottery_blueball = new StringBuilder();
- if (data.BlueBall != null && data.BlueBall.Length > 0)
- {
- for (int i = 0; i < data.BlueBall.Length; i++)
- {
- lottery_blueball.Append(string.Format(lottery_blueball_tmp, data.BlueBall[i]));
- }
- }
- string lottery_ball = lottery_redball.ToString() + lottery_blueball.ToString();
- //奖池
- string lottery_jackpot = string.Format(lottery_jackpot_tmp, data.MoneyCount == null ? "0.00" : $"{Convert.ToDouble(data.MoneyCount):N2}");
- //更多
- string more = string.Format(more_tmp, other_url[data.LotteryType][0]);
- //开奖详情
- string kj_detail_url = string.Format(kj_detail_url_tmp, other_url[data.LotteryType][1]);
- //走势图
- string kj_zs_url = string.Format(kj_zs_url_tmp, other_url[data.LotteryType][2]);
- string kj_item = string.Format(kj_item_tmp, lottery_name, lottery_periods, lottery_ball, more,
- lottery_jackpot, kj_detail_url, kj_zs_url);
- builder.Append(kj_item);
- }
- dict.Add("$LotteryItem$", builder.ToString());
- }
- return dict;
- }
- /// <summary>
- /// 首页开奖信息
- /// </summary>
- /// <returns></returns>
- private List<LotteryData> GetHomeLotteryData()
- {
- List<LotteryData> qgc = new List<LotteryData>();
- //双色球
- OpenCode7DTModel ssq = GetLastLotteryItem<OpenCode7DTModel>(SCCLottery.QGC_SSQ);
- if (ssq != null)
- {
- qgc.Add(LottryDataHandle.GetLotteryData(SCCLottery.QGC_SSQ, ssq.Term.ToString().TryToInt32(), ssq.OpenTime, ssq.ID, Array.ConvertAll<string, int>(ssq.GetCodeStr().Split(',').ToArray(), s => s.TryToInt32()), ssq.Spare, "qgc"));
- }
- //福彩3D
- OpenCode3DTModel fc3d = GetLastLotteryItem<OpenCode3DTModel>(SCCLottery.QGC_FC3D);
- if (fc3d != null)
- {
- qgc.Add(LottryDataHandle.GetLotteryData(SCCLottery.QGC_FC3D, fc3d.Term.ToString().TryToInt32(), fc3d.OpenTime, fc3d.ID, Array.ConvertAll<string, int>(fc3d.GetCodeStr().Split(',').ToArray(), s => s.TryToInt32()), fc3d.Spare, "qgc"));
- }
- //排列3
- OpenCode3DTModel pl3 = GetLastLotteryItem<OpenCode3DTModel>(SCCLottery.QGC_PL3);
- if (pl3 != null)
- {
- qgc.Add(LottryDataHandle.GetLotteryData(SCCLottery.QGC_PL3, pl3.Term.ToString().TryToInt32(), pl3.OpenTime, pl3.ID, Array.ConvertAll<string, int>(pl3.GetCodeStr().Split(',').ToArray(), s => s.TryToInt32()), pl3.Spare, "qgc"));
- }
- //大乐透
- OpenCode7DTModel dlt = GetLastLotteryItem<OpenCode7DTModel>(SCCLottery.QGC_DLT);
- if (dlt != null)
- {
- qgc.Add(LottryDataHandle.GetLotteryData(SCCLottery.QGC_DLT, dlt.Term.ToString().TryToInt32(), dlt.OpenTime, dlt.ID, Array.ConvertAll<string, int>(dlt.GetCodeStr().Split(',').ToArray(), s => s.TryToInt32()), dlt.Spare, "qgc"));
- }
- return qgc;
- }
- /// <summary>
- /// 获取最新一条记录
- /// </summary>
- /// <param name="lottery">彩种名称</param>
- /// <returns></returns>
- private T GetLastLotteryItem<T>(SCCLottery lottery) where T : new()
- {
- var tableName = lottery.GetLotteryTableName();
- string LastItemSql = @"SELECT TOP 1 * FROM [dbo].[{0}] WHERE [OpenCode1] != -1 ORDER BY Term DESC ";
- SqlConnection conn = SqlHelper.GetConnection(DbConnectionType.LotteryNumber);
- string sqlString = string.Format(LastItemSql, tableName);
- DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sqlString);
- if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
- {
- var result = ds.Tables[0].DataTableToList<T>();
- return result[0];
- }
- return default(T);
- }
- #endregion
- }
- }
|