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
187 lines
6.5 KiB
using Cronos;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Shentun.Sms.Enums;
|
|
using Shentun.Sms.Service.Sms;
|
|
using Shentun.Sms.SmsApps;
|
|
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 TencentCloud.Ic.V20190307.Models;
|
|
using Volo.Abp;
|
|
using Volo.Abp.Application.Services;
|
|
using Volo.Abp.Caching;
|
|
using Volo.Abp.Domain.Repositories;
|
|
using Volo.Abp.Identity;
|
|
|
|
namespace Shentun.Sms.Jobs
|
|
{
|
|
/// <summary>
|
|
/// 扫描短信计划,生成发送短信记录
|
|
/// </summary>
|
|
public class SmsTaskJob : ApplicationService, ISmsTaskJob
|
|
{
|
|
|
|
|
|
private readonly IRepository<SmsTask, Guid> _smsTaskRepository;
|
|
private readonly IRepository<SmsSend, Guid> _smsSendRepository;
|
|
|
|
private readonly SmsSendManager _smsSendManager;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
private readonly ILogger<SmsTaskJob> _logger;
|
|
|
|
private readonly SmsFactory _smsFactory;
|
|
public SmsTaskJob(
|
|
IRepository<SmsTask, Guid> smsTaskRepository,
|
|
SmsSendManager smsSendManager,
|
|
IConfiguration configuration,
|
|
IRepository<SmsSend, Guid> smsSendRepository,
|
|
SmsFactory smsFactory,
|
|
ILogger<SmsTaskJob> logger)
|
|
{
|
|
_smsTaskRepository = smsTaskRepository;
|
|
_smsSendManager = smsSendManager;
|
|
_configuration = configuration;
|
|
_smsSendRepository = smsSendRepository;
|
|
_smsFactory = smsFactory;
|
|
_logger = logger;
|
|
}
|
|
|
|
|
|
//[RemoteService(false)]
|
|
public async Task DoWork()
|
|
{
|
|
try
|
|
{
|
|
//_logger.LogInformation("测试");
|
|
var smsTaskList = (from a in await _smsTaskRepository.GetQueryableAsync()
|
|
join b in await _smsSendRepository.GetQueryableAsync() on a.Id equals b.SmsTaskId into bb
|
|
from ab in bb.DefaultIfEmpty()
|
|
where a.IsActive == 'Y' && ab == null
|
|
select a).ToList();
|
|
|
|
List<SmsSend> smsSendList = new List<SmsSend>();
|
|
|
|
foreach (var smsTask in smsTaskList)
|
|
{
|
|
CreateSmsSendAsync(smsTask, smsSendList);
|
|
}
|
|
|
|
if (smsSendList.Any())
|
|
{
|
|
await _smsSendRepository.InsertManyAsync(smsSendList);
|
|
}
|
|
|
|
if (smsTaskList.Any())
|
|
{
|
|
await _smsTaskRepository.UpdateManyAsync(smsTaskList);
|
|
}
|
|
|
|
_logger.LogInformation($"------扫描处理了【{smsTaskList.Count}】条SmsTask记录,生成了【{smsSendList.Count}】条短信记录------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogInformation(ex.ToString());
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成SmsSend记录
|
|
/// </summary>
|
|
/// <param name="smsTask"></param>
|
|
/// <param name="smsSendList"></param>
|
|
/// <returns></returns>
|
|
[RemoteService(false)]
|
|
public void CreateSmsSendAsync(SmsTask smsTask, List<SmsSend> smsSendList)
|
|
{
|
|
string SmsInterfaceId = _configuration.GetValue<string>("Sms:SmsInterfaceId");
|
|
if (smsTask.TaskCycleType == TaskCycleTypeFlag.TimelySend)
|
|
{
|
|
var smsSendEntity = new SmsSend
|
|
{
|
|
Content = smsTask.Content,
|
|
IsActive = 'N',
|
|
IsComplete = 'Y',
|
|
SmsInterfaceId = SmsInterfaceId,
|
|
SmsTaskId = smsTask.Id,
|
|
PlanSendTime = DateTime.Now
|
|
};
|
|
|
|
_smsSendManager.CreateAsync(smsSendEntity);
|
|
|
|
//发送短信
|
|
// SmsFactory smsFactory = new SmsFactory(_configuration);
|
|
|
|
SmsBase smsBase = _smsFactory.CreateSms();
|
|
|
|
string[] phoneNumber = { $"+{smsTask.CountryCode}{smsTask.MobileTelephone}" };
|
|
string[] templateParam = smsTask.Content.Trim('|').Split("|", StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
smsBase.Send(phoneNumber, templateParam, "");
|
|
|
|
smsSendList.Add(smsSendEntity);
|
|
|
|
}
|
|
else if (smsTask.TaskCycleType == TaskCycleTypeFlag.CornSend)
|
|
{
|
|
var taskCorn = smsTask.TaskCorn;
|
|
if (!string.IsNullOrWhiteSpace(taskCorn) && smsTask.StopTime != null)
|
|
{
|
|
#region 解析Cron表达式
|
|
|
|
|
|
try
|
|
{
|
|
var schedule = CronExpression.Parse(taskCorn, CronFormat.IncludeSeconds);
|
|
var occurrences = schedule.GetOccurrences(DateTime.UtcNow, smsTask.StopTime.Value.ToUniversalTime()); //获取截止时间前所有的计划时间
|
|
|
|
//if (occurrences.Count() < 10)
|
|
//{
|
|
foreach (var occurrence in occurrences)
|
|
{
|
|
var smsSendEntity = new SmsSend
|
|
{
|
|
Content = smsTask.Content,
|
|
IsActive = 'Y',
|
|
IsComplete = 'N',
|
|
SmsInterfaceId = SmsInterfaceId,
|
|
SmsTaskId = smsTask.Id,
|
|
MobileTelephone = smsTask.MobileTelephone,
|
|
PlanSendTime = occurrence
|
|
};
|
|
|
|
_smsSendManager.CreateAsync(smsSendEntity);
|
|
|
|
smsSendList.Add(smsSendEntity);
|
|
|
|
}
|
|
//}
|
|
//else
|
|
//{
|
|
// _logger.LogInformation($"------Id为【{smsTask.Id}】的SmsTask记录,Corn表达式记录超出10条,已跳过------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogInformation($"------处理Corn表达式异常------,{ex.ToString()}");
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|
|
|
|
smsTask.IsActive = 'N';
|
|
}
|
|
}
|
|
}
|