using System; using System.Collections.Generic; using System.IO; using System.Text; using CB.Common; using CB.Entity; using CB.LotteryCatchData.Entity; using CB.LotteryCatchData.Interface; namespace CB.LotteryCatchData { /// /// 彩票数据抓取抽象类 /// /// public abstract class CatchRepository : ICatchDataProvider { #region ICatchDataProvider接口 public abstract LotteryParamKeys GetLotteryParamKeys(string fileName); public abstract LotteryOpenCode GetLatestOpenCode(); public abstract IList CatchData(LotteryConfigInfo config, LotteryParamKeys keys); public abstract int SaveData(IList list, ref LotteryParamKeys keys); #endregion /// /// 是否继续抓取数据 /// /// 彩种配置 /// 彩种临时配置 /// protected virtual bool IsCatchData(LotteryConfigInfo config, LotteryParamKeys keys) { DateTime now = DateTime.Now; if (now < now.Date.Add(config.StartTime) || now > now.Date.Add(config.EndTime)) return false; if (0 == keys.IsCatch) return true; DateTime thisOpenTime; if (DateTime.MinValue == keys.LastOpenTime) { //默认初始化开奖时间 thisOpenTime = Convert.ToDateTime(now.ToString(config.OpenTimeFomart)).Add(config.StartTime).AddMinutes(config.OpenPeriod); } else { thisOpenTime = keys.LastOpenTime.AddMinutes(config.OpenPeriod); } return now >= thisOpenTime; } /// /// 通用获取临时配置数据 /// /// /// protected virtual LotteryParamKeys GetLotteryParamKeysDefault(string fileName) { var entity = new LotteryParamKeys() { IsCatch = 0, LocalTerm = "", LastOpenTime = DateTime.MinValue, otherKeys = new Dictionary() }; if (File.Exists(fileName)) { IDictionary data = new Dictionary(); using (StreamReader sr = new StreamReader(fileName, Encoding.UTF8)) { string s = sr.ReadLine(); string[] d; while (null != s) { if (0 < s.Length) { d = s.Split('='); data.Add(d[0], d[1]); } s = sr.ReadLine(); } } if (data.Keys.Contains("IsCatch")) entity.IsCatch = TypeConverter.StrToInt(data["IsCatch"]); if (data.Keys.Contains("LocalTerm")) entity.LocalTerm = data["LocalTerm"]; if (data.Keys.Contains("LastOpenTime")) entity.LastOpenTime = TypeConverter.StrToDateTime(data["LastOpenTime"], DateTime.MinValue); entity.otherKeys = data; } if (!entity.otherKeys.Keys.Contains("IsCatch")) entity.otherKeys.Add("IsCatch", "0"); if (!entity.otherKeys.Keys.Contains("LocalTerm")) entity.otherKeys.Add("LocalTerm", ""); if (!entity.otherKeys.Keys.Contains("LastOpenTime")) entity.otherKeys.Add("LastOpenTime", DateTime.MinValue.ToString("yyyy-MM-dd HH:mm:ss")); return entity; } /// /// 计算开奖时间 /// /// 彩种开奖周期 /// 当前期数 /// 开奖时间 /// 截取期数[localTerm]字符串起始位置 /// 截取期数[localTerm]字符串长度 /// protected virtual DateTime GetRealOpenTime(TimeSpan startTime, int openPeriod, string localTerm, DateTime openTime, int startIndex, int length) { int num = TypeConverter.StrToInt(localTerm.Substring(startIndex, length)); return openTime.Date.Add(startTime).AddMinutes(openPeriod * num); } } }