AutoRemindingTheForthcomingLotteryManager.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Lottomat.Application.Code;
  8. using Lottomat.Application.SystemAutoJob.Interface;
  9. using Lottomat.Util;
  10. using Lottomat.Util.Extension;
  11. using Lottomat.Utils;
  12. using Lottomat.Utils.Date;
  13. namespace Lottomat.Application.SystemAutoJob
  14. {
  15. /// <summary>
  16. /// 自动提醒即将开奖彩种
  17. /// </summary>
  18. public class AutoRemindingTheForthcomingLotteryManager : ISchedulerJob
  19. {
  20. /// <summary>
  21. /// 对象锁
  22. /// </summary>
  23. private static readonly object _lock = new object();
  24. /// <summary>
  25. /// 配置信息
  26. /// </summary>
  27. private static List<SCCConfig> LotteryConfig => InitLotteryConfig.Init();
  28. /// <summary>
  29. /// 待开奖彩种字典
  30. /// </summary>
  31. private static Dictionary<string, SCCConfig> dictionary = new Dictionary<string, SCCConfig>();
  32. /// <summary>
  33. /// 已经发送过邮件的彩种
  34. /// </summary>
  35. private static List<string> sendEmailSuccList = new List<string>();
  36. /// <summary>
  37. /// 入口程序
  38. /// </summary>
  39. public void Execute()
  40. {
  41. lock (_lock)
  42. {
  43. DoSomething();
  44. }
  45. }
  46. /// <summary>
  47. /// 真正操作逻辑
  48. /// </summary>
  49. private void DoSomething()
  50. {
  51. GetTodayLotteryDict();
  52. //批量发送邮件
  53. SendEmail();
  54. }
  55. /// <summary>
  56. /// 获取今日开奖彩种
  57. /// </summary>
  58. /// <returns></returns>
  59. public static Dictionary<string, SCCConfig> GetTodayLotteryDict()
  60. {
  61. //当前时间
  62. DateTime now = DateTimeHelper.Now;
  63. //今天是星期几
  64. string week = now.DayOfWeek.ToString("d");
  65. //获取当前彩种配置信息(全国彩和地方彩)
  66. List<SCCConfig> configList = LotteryConfig.Where(s => s.Name.Contains("DFC_") || s.Name.Contains("QGC_")).OrderBy(s => s.LotteryName.Length).ToList();
  67. foreach (SCCConfig config in configList)
  68. {
  69. //当前彩种每周开奖时间
  70. string[] openThePrizeOnTheDayOfTheWeek = config.KJTime.Split(",".ToCharArray());
  71. //今天星期在数组中的索引
  72. int pointer = Array.IndexOf(openThePrizeOnTheDayOfTheWeek, week.ToString());
  73. if (pointer != -1)//今天要开奖
  74. {
  75. //当前彩种今天真实开始开奖时间
  76. DateTime todayRealStartOpentime = (now.ToString("yyyy-MM-dd") + " " + config.StartHour + ":" + config.StartMinute).TryToDateTime();
  77. //如果时间差小于半个小时,则提醒系统管理员,有彩种即将开奖
  78. TimeSpan timeSpan = todayRealStartOpentime - now;
  79. Trace.WriteLine(string.Format("【{1}】时间差为:{0}分钟,开奖时间为:{2}.", timeSpan.TotalMinutes, config.LotteryName, todayRealStartOpentime));
  80. if (timeSpan.TotalMinutes > 0 && timeSpan.TotalMinutes <= 30)
  81. {
  82. if (!dictionary.ContainsKey(config.EnumCode))
  83. {
  84. dictionary.Add(config.EnumCode, config);
  85. }
  86. }
  87. else
  88. {
  89. if (dictionary.Count > 0)
  90. {
  91. if (dictionary.ContainsKey(config.EnumCode))
  92. {
  93. dictionary.Remove(config.EnumCode);
  94. //移除已经发送邮件的彩种
  95. sendEmailSuccList.Remove(config.EnumCode);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. return dictionary;
  102. }
  103. /// <summary>
  104. /// 批量发送邮件
  105. /// </summary>
  106. private void SendEmail()
  107. {
  108. if (dictionary.Count > 0)
  109. {
  110. //当前时间
  111. DateTime now = DateTimeHelper.Now;
  112. Task task = Task.Factory.StartNew(() =>
  113. {
  114. StringBuilder builder = new StringBuilder();
  115. foreach (KeyValuePair<string, SCCConfig> pair in dictionary)
  116. {
  117. if (!sendEmailSuccList.Contains(pair.Key))
  118. {
  119. SCCConfig config = pair.Value;
  120. //当前彩种今天真实开始开奖时间
  121. string todayRealStartOpentime = now.ToString("yyyy-MM-dd") + " " + config.StartHour.RepairZero() + ":" + config.StartMinute.RepairZero();
  122. builder.Append(config.LotteryName + ",开奖时间:" + todayRealStartOpentime + ";<br />");//<a href='" + config.MainUrl + " target='_blank''>[参考网址]</a>
  123. //添加到已经开奖集合,下一次就需要发送邮件了
  124. sendEmailSuccList.Add(pair.Key);
  125. Trace.WriteLine(config.LotteryName + " 添加成功!");
  126. }
  127. }
  128. if (!string.IsNullOrEmpty(builder.ToString()))
  129. {
  130. string body = "管理员请注意,以下彩种将在30分钟后开奖:<br /><br />" + StringHelper.DelLastChar(builder.ToString(), ";") + "。";
  131. //发送邮件
  132. string address = ConfigHelper.GetValue("ErrorReportTo");
  133. string subject = "开奖提醒";
  134. MailHelper.SendByThread(address, subject, body);
  135. }
  136. });
  137. task.Wait();
  138. }
  139. }
  140. }
  141. }