@Service
@Transactional
public class taskService {
public void dataPush() {
List<CommonJob> messageJobs = commonJobMapper.selectByLimitForSendMessageJob();
messageJobs.forEach(o -> {
String result = commonTaskService.sendCommonMessage(o.getNo(), o.getContent());
o.setValid(1);
o.setUpdateTime(new Date());
o.setDescription(o.getDescription() + "--" + result);
if(StringUtil.isNotEmpty(result) && result.contains("短信发送失败")){
o.setValid(0);
o.setErrorTimes(o.getErrorTimes() + 1);
}
commonJobMapper.updateByPrimaryKeySelective(o);
});
}
public int deleteData() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -20);
CommonJobExample commonJobExample = new CommonJobExample();
commonJobExample.createCriteria().andTypeEqualTo(3).andValidEqualTo(1).andCreateTimeLessThanOrEqualTo(cal.getTime());
return commonJobMapper.deleteByExample(commonJobExample);
}
}
@Service
public class CommonTask {
@Autowired
private CommonTaskService commonTaskService;
@Scheduled(cron = "0 0/20 8-20 * * ?")
public void runSendNote(){
try {
logger.info("CommonTask runSendNote begin");
commonTaskService.sendNote();
logger.info("CommonTask runSendNote end");
}catch (Exception e){
logger.error("CommonTask runSendNote e = " + e);
}
}
@Scheduled(cron = "0 30 14 ? * TUE")
public void deleteData(){
try {
long startTime = System.currentTimeMillis();
int count = commonTaskService.deleteData();
logger.info("deleteData本次删除短信数据耗时(ms):{}。--删除条数:{}", (System.currentTimeMillis() - startTime), count);
} catch (Exception e) {
logger.error("本次数据删除失败!", e);
}
}
}