77 lines
2.7 KiB
Java
77 lines
2.7 KiB
Java
|
package com.hzs.third.job;
|
||
|
|
||
|
import cn.hutool.core.collection.CollectionUtil;
|
||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||
|
import com.hzs.common.core.constant.MagicNumberConstants;
|
||
|
import com.hzs.common.core.constant.SystemFieldConstants;
|
||
|
import com.hzs.common.core.enums.ESmsSendResult;
|
||
|
import com.hzs.common.core.enums.ESmsSendStatus;
|
||
|
import com.hzs.common.core.enums.ESmsSendType;
|
||
|
import com.hzs.common.domain.third.sms.TSmsRecord;
|
||
|
import com.hzs.third.sms.service.ITSmsRecordService;
|
||
|
import com.hzs.third.sms.util.SmsUtil;
|
||
|
import com.xxl.job.core.handler.annotation.XxlJob;
|
||
|
import lombok.extern.slf4j.Slf4j;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||
|
import org.springframework.stereotype.Component;
|
||
|
|
||
|
import java.util.Date;
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* @Description: 短信定时任务
|
||
|
* @Author: jiang chao
|
||
|
* @Time: 2023/3/11 16:22
|
||
|
* @Classname: SmsJob
|
||
|
* @PackageName: com.hzs.third.job
|
||
|
*/
|
||
|
@Slf4j
|
||
|
@ConditionalOnProperty(name = "xxl-job.start", havingValue = "true")
|
||
|
@Component
|
||
|
public class SmsJob {
|
||
|
|
||
|
@Autowired
|
||
|
private ITSmsRecordService itSmsRecordService;
|
||
|
|
||
|
/**
|
||
|
* 定时发送短信
|
||
|
*/
|
||
|
// @Scheduled(cron = "0 * * * * ?")
|
||
|
@XxlJob("sendSms")
|
||
|
public void sendSms() {
|
||
|
log.info("定时发送短信任务开始!");
|
||
|
|
||
|
Date date = new Date();
|
||
|
|
||
|
QueryWrapper<TSmsRecord> queryWrapper = new QueryWrapper<>();
|
||
|
queryWrapper.eq("SEND_TYPE", ESmsSendType.SCHEDULED.getValue());
|
||
|
queryWrapper.eq("SEND_STATUS", ESmsSendStatus.NOT_SEND.getValue());
|
||
|
queryWrapper.le("SEND_TIME", date);
|
||
|
queryWrapper.orderByAsc(SystemFieldConstants.CREATION_TIME);
|
||
|
List<TSmsRecord> list = itSmsRecordService.list(queryWrapper);
|
||
|
if (CollectionUtil.isNotEmpty(list)) {
|
||
|
log.info("本次共需要发送短信{}条", list.size());
|
||
|
|
||
|
for (TSmsRecord tSmsRecord : list) {
|
||
|
String str = SmsUtil.sendSms(tSmsRecord.getPhone(), tSmsRecord.getContent());
|
||
|
if (null == str) {
|
||
|
// 发送成功
|
||
|
tSmsRecord.setSendResult(ESmsSendResult.SUCCESS.getValue());
|
||
|
} else {
|
||
|
tSmsRecord.setSendResult(ESmsSendResult.FAIL.getValue());
|
||
|
}
|
||
|
tSmsRecord.setSendStatus(ESmsSendStatus.SEND.getValue());
|
||
|
tSmsRecord.setPkModified(MagicNumberConstants.PK_ADMIN);
|
||
|
tSmsRecord.setModifiedTime(date);
|
||
|
itSmsRecordService.updateById(tSmsRecord);
|
||
|
}
|
||
|
} else {
|
||
|
log.info("本次没有需要发送的短信");
|
||
|
}
|
||
|
|
||
|
log.info("定时发送短信任务结束!");
|
||
|
}
|
||
|
|
||
|
}
|