Commit babf89dc authored by honghy's avatar honghy

短信接收 Job

parent 9452b7e6
......@@ -48,9 +48,9 @@ public interface SmsLogMapper extends BaseMapperX<SmsLogDO> {
"DELETE FROM system_sms_log WHERE date(create_time) <= date(date_sub(now(), interval 90 day))"
})
int clearLog(Integer day);
default List<SmsLogDO> selectReceiveList() {
default List<SmsLogDO> selectReceiveList(Integer time) {
return selectList(new QueryWrapperX<SmsLogDO>()
.apply("create_time >= NOW() - INTERVAL 20 HOUR")
.apply(String.format("create_time >= NOW() - INTERVAL %d HOUR",time))
.eq("template_type", 2)
.lt("num", 1)
.ne("receive_status", 10));
......
package cn.iocoder.yudao.module.system.job.sms;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.system.service.sms.SmsNodeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 短信接收 Job
*
* @author Jayden
* @date 2024/11/21
*/
@Component
@Slf4j
public class SmsReceiveJob implements JobHandler {
@Autowired
private SmsNodeService smsNodeService;
@Override
public String execute(String time) {
if (StringUtils.isBlank(time)) {
log.error("短信接收参数错误:{},此参数不能为空", time);
return "短信接收参数错误,此参数不能为空";
}
smsNodeService.scheduleReceiveRefresh(Integer.valueOf(time));
return "短信接收 Job 执行成功";
}
}
......@@ -112,7 +112,7 @@ public interface SmsLogService {
void updateBatchSmsLog(List<SmsLogDO> resendSmsLogList);
List<SmsLogDO> selectReceiveList();
List<SmsLogDO> selectReceiveList(Integer time);
/**
* 更新sendchamp回调状态
......
......@@ -155,8 +155,8 @@ public class SmsLogServiceImpl implements SmsLogService {
}
@Override
public List<SmsLogDO> selectReceiveList() {
return smsLogMapper.selectReceiveList();
public List<SmsLogDO> selectReceiveList(Integer time) {
return smsLogMapper.selectReceiveList(time);
}
@Override
......
......@@ -81,4 +81,9 @@ public interface SmsNodeService extends IService<SmsNodeDO> {
* @return 缓存键
*/
String buildCacheKey(SmsNodeDO smsNodeDO);
/**
* 短信重发定时任务
*/
public void scheduleReceiveRefresh(Integer time);
}
......@@ -18,7 +18,6 @@ import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsNodeMapper;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
......@@ -112,14 +111,12 @@ public class SmsNodeServiceImpl extends AbstractService<SmsNodeMapper, SmsNodeDO
}
/**
* 每半小时执行一次
* 短信重发定时任务
*/
@Scheduled(cron = "0 0/30 * * * ?")
public void scheduleReceiveRefresh() {
public void scheduleReceiveRefresh(Integer time) {
try {
// 获取需要重发短信的日志列表
List<SmsLogDO> smsLogs = smsLogService.selectReceiveList();
List<SmsLogDO> smsLogs = smsLogService.selectReceiveList(time);
if (smsLogs != null) {
for (SmsLogDO smsLog : smsLogs) {
try {
......@@ -144,12 +141,18 @@ public class SmsNodeServiceImpl extends AbstractService<SmsNodeMapper, SmsNodeDO
private void processSmsLog(SmsLogDO smsLog) {
// 根据渠道ID获取短信客户端
SmsClient smsClient = smsClientFactory.getSmsClient(smsLog.getChannelId());
// 获取短信接收状态
SmsLogDTO receiveStatus = smsClient.getReceiveStatus(SmsLogConvert.INSTANCE.toDto(smsLog));
smsLog.setReceiveStatus(receiveStatus.getReceiveStatus());
try {
// 获取短信接收状态
SmsLogDTO receiveStatus = smsClient.getReceiveStatus(SmsLogConvert.INSTANCE.toDto(smsLog));
smsLog.setReceiveStatus(receiveStatus.getReceiveStatus());
} catch (Exception e) {
smsLog.setReceiveStatus(ReceiveStatusEnum.Receive_STATUS_20.getValue());
log.error("获取短信接收状态失败: {}", smsLog, e);
}
// 接收失败,则重发
if (ReceiveStatusEnum.Receive_STATUS_20.getValue() == receiveStatus.getReceiveStatus()) {
smsLog.setReceiveTime(receiveStatus.getReceiveTime());
if (ReceiveStatusEnum.Receive_STATUS_20.getValue() == smsLog.getReceiveStatus()) {
smsLog.setReceiveTime(smsLog.getReceiveTime());
smsLog.setNum(1);
// 从Redis中获取短信节点信息
String node = redisHelper.get(SYSTEM_SMS_NODE_KEY + smsLog.getNodeId());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment