123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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
- {
- /// <summary>
- /// 彩票数据抓取抽象类
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public abstract class CatchRepository : ICatchDataProvider
- {
- #region ICatchDataProvider接口
- public abstract LotteryParamKeys GetLotteryParamKeys(string fileName);
- public abstract LotteryOpenCode GetLatestOpenCode();
- public abstract IList<LotteryOpenCode> CatchData(LotteryConfigInfo config, LotteryParamKeys keys);
- public abstract int SaveData(IList<LotteryOpenCode> list, ref LotteryParamKeys keys);
- #endregion
- /// <summary>
- /// 是否继续抓取数据
- /// </summary>
- /// <param name="config">彩种配置</param>
- /// <param name="keys">彩种临时配置</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 通用获取临时配置数据
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- protected virtual LotteryParamKeys GetLotteryParamKeysDefault(string fileName)
- {
- var entity = new LotteryParamKeys()
- {
- IsCatch = 0,
- LocalTerm = "",
- LastOpenTime = DateTime.MinValue,
- otherKeys = new Dictionary<string, string>()
- };
- if (File.Exists(fileName))
- {
- IDictionary<string, string> data = new Dictionary<string, string>();
- 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;
- }
- /// <summary>
- /// 计算开奖时间
- /// </summary>
- /// <param name="openPeriod">彩种开奖周期</param>
- /// <param name="localTerm">当前期数</param>
- /// <param name="openTime">开奖时间</param>
- /// <param name="startIndex">截取期数[localTerm]字符串起始位置</param>
- /// <param name="length">截取期数[localTerm]字符串长度</param>
- /// <returns></returns>
- 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);
- }
- }
- }
|