You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
6.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using Cronos;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.Logging;
  4. using Shentun.Sms.Enums;
  5. using Shentun.Sms.Service.Sms;
  6. using Shentun.Sms.SmsApps;
  7. using Shentun.Sms.SmsSends;
  8. using Shentun.Sms.SmsTasks;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using TencentCloud.Ic.V20190307.Models;
  15. using Volo.Abp;
  16. using Volo.Abp.Application.Services;
  17. using Volo.Abp.Caching;
  18. using Volo.Abp.Domain.Repositories;
  19. using Volo.Abp.Identity;
  20. namespace Shentun.Sms.Jobs
  21. {
  22. /// <summary>
  23. /// 扫描短信计划,生成发送短信记录
  24. /// </summary>
  25. public class SmsTaskJob : ApplicationService, ISmsTaskJob
  26. {
  27. private readonly IRepository<SmsTask, Guid> _smsTaskRepository;
  28. private readonly IRepository<SmsSend, Guid> _smsSendRepository;
  29. private readonly SmsSendManager _smsSendManager;
  30. private readonly IConfiguration _configuration;
  31. private readonly ILogger<SmsTaskJob> _logger;
  32. private readonly SmsFactory _smsFactory;
  33. public SmsTaskJob(
  34. IRepository<SmsTask, Guid> smsTaskRepository,
  35. SmsSendManager smsSendManager,
  36. IConfiguration configuration,
  37. IRepository<SmsSend, Guid> smsSendRepository,
  38. SmsFactory smsFactory,
  39. ILogger<SmsTaskJob> logger)
  40. {
  41. _smsTaskRepository = smsTaskRepository;
  42. _smsSendManager = smsSendManager;
  43. _configuration = configuration;
  44. _smsSendRepository = smsSendRepository;
  45. _smsFactory = smsFactory;
  46. _logger = logger;
  47. }
  48. //[RemoteService(false)]
  49. public async Task DoWork()
  50. {
  51. try
  52. {
  53. //_logger.LogInformation("测试");
  54. var smsTaskList = (from a in await _smsTaskRepository.GetQueryableAsync()
  55. join b in await _smsSendRepository.GetQueryableAsync() on a.Id equals b.SmsTaskId into bb
  56. from ab in bb.DefaultIfEmpty()
  57. where a.IsActive == 'Y' && ab == null
  58. select a).ToList();
  59. List<SmsSend> smsSendList = new List<SmsSend>();
  60. foreach (var smsTask in smsTaskList)
  61. {
  62. CreateSmsSendAsync(smsTask, smsSendList);
  63. }
  64. if (smsSendList.Any())
  65. {
  66. await _smsSendRepository.InsertManyAsync(smsSendList);
  67. }
  68. if (smsTaskList.Any())
  69. {
  70. await _smsTaskRepository.UpdateManyAsync(smsTaskList);
  71. }
  72. _logger.LogInformation($"------扫描处理了【{smsTaskList.Count}】条SmsTask记录,生成了【{smsSendList.Count}】条短信记录------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  73. }
  74. catch (Exception ex)
  75. {
  76. _logger.LogInformation(ex.ToString());
  77. }
  78. }
  79. /// <summary>
  80. /// 生成SmsSend记录
  81. /// </summary>
  82. /// <param name="smsTask"></param>
  83. /// <param name="smsSendList"></param>
  84. /// <returns></returns>
  85. [RemoteService(false)]
  86. public void CreateSmsSendAsync(SmsTask smsTask, List<SmsSend> smsSendList)
  87. {
  88. string SmsInterfaceId = _configuration.GetValue<string>("Sms:SmsInterfaceId");
  89. if (smsTask.TaskCycleType == TaskCycleTypeFlag.TimelySend)
  90. {
  91. var smsSendEntity = new SmsSend
  92. {
  93. Content = smsTask.Content,
  94. IsActive = 'N',
  95. IsComplete = 'Y',
  96. SmsInterfaceId = SmsInterfaceId,
  97. SmsTaskId = smsTask.Id,
  98. PlanSendTime = DateTime.Now
  99. };
  100. _smsSendManager.CreateAsync(smsSendEntity);
  101. //发送短信
  102. // SmsFactory smsFactory = new SmsFactory(_configuration);
  103. SmsBase smsBase = _smsFactory.CreateSms();
  104. string[] phoneNumber = { $"+{smsTask.CountryCode}{smsTask.MobileTelephone}" };
  105. string[] templateParam = smsTask.Content.Trim('|').Split("|", StringSplitOptions.RemoveEmptyEntries);
  106. smsBase.Send(phoneNumber, templateParam, "");
  107. smsSendList.Add(smsSendEntity);
  108. }
  109. else if (smsTask.TaskCycleType == TaskCycleTypeFlag.CornSend)
  110. {
  111. var taskCorn = smsTask.TaskCorn;
  112. if (!string.IsNullOrWhiteSpace(taskCorn) && smsTask.StopTime != null)
  113. {
  114. #region 解析Cron表达式
  115. try
  116. {
  117. var schedule = CronExpression.Parse(taskCorn, CronFormat.IncludeSeconds);
  118. var occurrences = schedule.GetOccurrences(DateTime.UtcNow, smsTask.StopTime.Value.ToUniversalTime()); //获取截止时间前所有的计划时间
  119. //if (occurrences.Count() < 10)
  120. //{
  121. foreach (var occurrence in occurrences)
  122. {
  123. var smsSendEntity = new SmsSend
  124. {
  125. Content = smsTask.Content,
  126. IsActive = 'Y',
  127. IsComplete = 'N',
  128. SmsInterfaceId = SmsInterfaceId,
  129. SmsTaskId = smsTask.Id,
  130. MobileTelephone = smsTask.MobileTelephone,
  131. PlanSendTime = occurrence
  132. };
  133. _smsSendManager.CreateAsync(smsSendEntity);
  134. smsSendList.Add(smsSendEntity);
  135. }
  136. //}
  137. //else
  138. //{
  139. // _logger.LogInformation($"------Id为【{smsTask.Id}】的SmsTask记录,Corn表达式记录超出10条,已跳过------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  140. //}
  141. }
  142. catch (Exception ex)
  143. {
  144. _logger.LogInformation($"------处理Corn表达式异常------,{ex.ToString()}");
  145. }
  146. #endregion
  147. }
  148. }
  149. smsTask.IsActive = 'N';
  150. }
  151. }
  152. }