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

using Cronos;
using Shentun.Sms.Enums;
using Shentun.Sms.SmsSends;
using Shentun.Sms.SmsTasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp;
using Microsoft.Extensions.Configuration;
using Shentun.Sms.Service.Sms;
using Volo.Abp.Caching;
using Microsoft.Extensions.Logging;
namespace Shentun.Sms.Jobs
{
/// <summary>
/// 扫描短信发送记录,发送短信
/// </summary>
[RemoteService(false)]
public class SmsSendJob : ApplicationService, ISmsSendJob
{
//private readonly IServiceProvider _service;
private readonly IRepository<SmsSend, Guid> _smsSendRepository;
private readonly IRepository<SmsTask, Guid> _smsTaskRepository;
private readonly SmsSendManager _smsSendManager;
private readonly IConfiguration _configuration;
private readonly SmsFactory _smsFactory;
private readonly ILogger<SmsSendJob> _logger;
public SmsSendJob(
SmsSendManager smsSendManager,
IRepository<SmsSend, Guid> smsSendRepository,
IConfiguration configuration,
IRepository<SmsTask, Guid> smsTaskRepository,
ILogger<SmsSendJob> logger,
SmsFactory smsFactory)
{
_smsSendManager = smsSendManager;
_smsSendRepository = smsSendRepository;
_configuration = configuration;
_smsTaskRepository = smsTaskRepository;
_logger = logger;
_smsFactory = smsFactory;
}
public async Task DoWork()
{
try
{
var smsSendQuery = from a in await _smsSendRepository.GetQueryableAsync()
join b in await _smsTaskRepository.GetQueryableAsync() on a.SmsTaskId equals b.Id
where a.IsComplete == 'N' && a.IsActive == 'Y' && a.PlanSendTime < DateTime.Now
select new
{
a,
CountryCode = b.CountryCode,
Content = b.Content
};
List<SmsSend> smsSendUpdateList = new List<SmsSend>();
foreach (var item in smsSendQuery.ToList())
{
string CountryCode = item.CountryCode;
string Content = item.Content;
SmsSendAsync(item.a, CountryCode, Content);
smsSendUpdateList.Add(item.a);
}
await _smsSendRepository.UpdateManyAsync(smsSendUpdateList);
_logger.LogInformation($"------扫描处理了【{smsSendUpdateList.Count}】条SmsSend记录------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
catch (Exception ex)
{
_logger.LogInformation(ex.ToString());
}
}
/// <summary>
/// 发送短信
/// </summary>
/// <param name="smsSend"></param>
/// <param name="CountryCode"></param>
public void SmsSendAsync(SmsSend smsSend, string CountryCode, string Content)
{
if (!string.IsNullOrEmpty(CountryCode) && !string.IsNullOrEmpty(smsSend.MobileTelephone))
{
SmsBase smsBase = _smsFactory.CreateSms();
string[] phoneNumber = { $"+{CountryCode}{smsSend.MobileTelephone}" };
string[] templateParam = Content.Trim('|').Split("|", StringSplitOptions.RemoveEmptyEntries);
smsBase.Send(phoneNumber, templateParam);
}
smsSend.IsActive = 'N';
smsSend.IsComplete = 'Y';
}
}
}