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.

109 lines
3.8 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
  1. using Cronos;
  2. using Shentun.Sms.Enums;
  3. using Shentun.Sms.SmsSends;
  4. using Shentun.Sms.SmsTasks;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Volo.Abp.Application.Services;
  11. using Volo.Abp.Domain.Repositories;
  12. using Volo.Abp;
  13. using Microsoft.Extensions.Configuration;
  14. using Shentun.Sms.Service.Sms;
  15. using Volo.Abp.Caching;
  16. using Microsoft.Extensions.Logging;
  17. namespace Shentun.Sms.Jobs
  18. {
  19. /// <summary>
  20. /// 扫描短信发送记录,发送短信
  21. /// </summary>
  22. [RemoteService(false)]
  23. public class SmsSendJob : ApplicationService, ISmsSendJob
  24. {
  25. //private readonly IServiceProvider _service;
  26. private readonly IRepository<SmsSend, Guid> _smsSendRepository;
  27. private readonly IRepository<SmsTask, Guid> _smsTaskRepository;
  28. private readonly SmsSendManager _smsSendManager;
  29. private readonly IConfiguration _configuration;
  30. private readonly SmsFactory _smsFactory;
  31. private readonly ILogger<SmsSendJob> _logger;
  32. public SmsSendJob(
  33. SmsSendManager smsSendManager,
  34. IRepository<SmsSend, Guid> smsSendRepository,
  35. IConfiguration configuration,
  36. IRepository<SmsTask, Guid> smsTaskRepository,
  37. ILogger<SmsSendJob> logger,
  38. SmsFactory smsFactory)
  39. {
  40. _smsSendManager = smsSendManager;
  41. _smsSendRepository = smsSendRepository;
  42. _configuration = configuration;
  43. _smsTaskRepository = smsTaskRepository;
  44. _logger = logger;
  45. _smsFactory = smsFactory;
  46. }
  47. public async Task DoWork()
  48. {
  49. try
  50. {
  51. var smsSendQuery = from a in await _smsSendRepository.GetQueryableAsync()
  52. join b in await _smsTaskRepository.GetQueryableAsync() on a.SmsTaskId equals b.Id
  53. where a.IsComplete == 'N' && a.IsActive == 'Y' && a.PlanSendTime < DateTime.Now
  54. select new
  55. {
  56. a,
  57. CountryCode = b.CountryCode,
  58. Content = b.Content
  59. };
  60. List<SmsSend> smsSendUpdateList = new List<SmsSend>();
  61. foreach (var item in smsSendQuery.ToList())
  62. {
  63. string CountryCode = item.CountryCode;
  64. string Content = item.Content;
  65. SmsSendAsync(item.a, CountryCode, Content);
  66. smsSendUpdateList.Add(item.a);
  67. }
  68. await _smsSendRepository.UpdateManyAsync(smsSendUpdateList);
  69. _logger.LogInformation($"------扫描处理了【{smsSendUpdateList.Count}】条SmsSend记录------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  70. }
  71. catch (Exception ex)
  72. {
  73. _logger.LogInformation(ex.ToString());
  74. }
  75. }
  76. /// <summary>
  77. /// 发送短信
  78. /// </summary>
  79. /// <param name="smsSend"></param>
  80. /// <param name="CountryCode"></param>
  81. public void SmsSendAsync(SmsSend smsSend, string CountryCode, string Content)
  82. {
  83. if (!string.IsNullOrEmpty(CountryCode) && !string.IsNullOrEmpty(smsSend.MobileTelephone))
  84. {
  85. SmsBase smsBase = _smsFactory.CreateSms();
  86. string[] phoneNumber = { $"+{CountryCode}{smsSend.MobileTelephone}" };
  87. string[] templateParam = Content.Trim('|').Split("|", StringSplitOptions.RemoveEmptyEntries);
  88. smsBase.Send(phoneNumber, templateParam);
  89. }
  90. smsSend.IsActive = 'N';
  91. smsSend.IsComplete = 'Y';
  92. }
  93. }
  94. }