CatchRepository.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using CB.Common;
  6. using CB.Entity;
  7. using CB.LotteryCatchData.Entity;
  8. using CB.LotteryCatchData.Interface;
  9. namespace CB.LotteryCatchData
  10. {
  11. /// <summary>
  12. /// 彩票数据抓取抽象类
  13. /// </summary>
  14. /// <typeparam name="T"></typeparam>
  15. public abstract class CatchRepository : ICatchDataProvider
  16. {
  17. #region ICatchDataProvider接口
  18. public abstract LotteryParamKeys GetLotteryParamKeys(string fileName);
  19. public abstract LotteryOpenCode GetLatestOpenCode();
  20. public abstract IList<LotteryOpenCode> CatchData(LotteryConfigInfo config, LotteryParamKeys keys);
  21. public abstract int SaveData(IList<LotteryOpenCode> list, ref LotteryParamKeys keys);
  22. #endregion
  23. /// <summary>
  24. /// 是否继续抓取数据
  25. /// </summary>
  26. /// <param name="config">彩种配置</param>
  27. /// <param name="keys">彩种临时配置</param>
  28. /// <returns></returns>
  29. protected virtual bool IsCatchData(LotteryConfigInfo config, LotteryParamKeys keys)
  30. {
  31. DateTime now = DateTime.Now;
  32. if (now < now.Date.Add(config.StartTime) || now > now.Date.Add(config.EndTime))
  33. return false;
  34. if (0 == keys.IsCatch)
  35. return true;
  36. DateTime thisOpenTime;
  37. if (DateTime.MinValue == keys.LastOpenTime)
  38. {
  39. //默认初始化开奖时间
  40. thisOpenTime = Convert.ToDateTime(now.ToString(config.OpenTimeFomart)).Add(config.StartTime).AddMinutes(config.OpenPeriod);
  41. }
  42. else
  43. {
  44. thisOpenTime = keys.LastOpenTime.AddMinutes(config.OpenPeriod);
  45. }
  46. return now >= thisOpenTime;
  47. }
  48. /// <summary>
  49. /// 通用获取临时配置数据
  50. /// </summary>
  51. /// <param name="fileName"></param>
  52. /// <returns></returns>
  53. protected virtual LotteryParamKeys GetLotteryParamKeysDefault(string fileName)
  54. {
  55. var entity = new LotteryParamKeys()
  56. {
  57. IsCatch = 0,
  58. LocalTerm = "",
  59. LastOpenTime = DateTime.MinValue,
  60. otherKeys = new Dictionary<string, string>()
  61. };
  62. if (File.Exists(fileName))
  63. {
  64. IDictionary<string, string> data = new Dictionary<string, string>();
  65. using (StreamReader sr = new StreamReader(fileName, Encoding.UTF8))
  66. {
  67. string s = sr.ReadLine();
  68. string[] d;
  69. while (null != s)
  70. {
  71. if (0 < s.Length)
  72. {
  73. d = s.Split('=');
  74. data.Add(d[0], d[1]);
  75. }
  76. s = sr.ReadLine();
  77. }
  78. }
  79. if (data.Keys.Contains("IsCatch"))
  80. entity.IsCatch = TypeConverter.StrToInt(data["IsCatch"]);
  81. if (data.Keys.Contains("LocalTerm"))
  82. entity.LocalTerm = data["LocalTerm"];
  83. if (data.Keys.Contains("LastOpenTime"))
  84. entity.LastOpenTime = TypeConverter.StrToDateTime(data["LastOpenTime"], DateTime.MinValue);
  85. entity.otherKeys = data;
  86. }
  87. if (!entity.otherKeys.Keys.Contains("IsCatch"))
  88. entity.otherKeys.Add("IsCatch", "0");
  89. if (!entity.otherKeys.Keys.Contains("LocalTerm"))
  90. entity.otherKeys.Add("LocalTerm", "");
  91. if (!entity.otherKeys.Keys.Contains("LastOpenTime"))
  92. entity.otherKeys.Add("LastOpenTime", DateTime.MinValue.ToString("yyyy-MM-dd HH:mm:ss"));
  93. return entity;
  94. }
  95. /// <summary>
  96. /// 计算开奖时间
  97. /// </summary>
  98. /// <param name="openPeriod">彩种开奖周期</param>
  99. /// <param name="localTerm">当前期数</param>
  100. /// <param name="openTime">开奖时间</param>
  101. /// <param name="startIndex">截取期数[localTerm]字符串起始位置</param>
  102. /// <param name="length">截取期数[localTerm]字符串长度</param>
  103. /// <returns></returns>
  104. protected virtual DateTime GetRealOpenTime(TimeSpan startTime, int openPeriod, string localTerm, DateTime openTime, int startIndex, int length)
  105. {
  106. int num = TypeConverter.StrToInt(localTerm.Substring(startIndex, length));
  107. return openTime.Date.Add(startTime).AddMinutes(openPeriod * num);
  108. }
  109. }
  110. }