package com.yingji.quartz; import cn.hutool.core.util.StrUtil; import com.utils.SmsUtil; import com.yingji.entity.QuartzLog; import com.yingji.service.AccidentService; import com.yingji.service.QuartzLogService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import javax.annotation.Resource; import java.time.Duration; import java.time.LocalDateTime; /** * 120定时任务保存数据 * * @author wu * @since 2024/5/10 上午10:05 */ @Configuration public class AccidentQuartz { public static final Logger log = LoggerFactory.getLogger(AccidentQuartz.class); @Resource private AccidentService accidentService; @Resource private QuartzLogService quartzLogService; @Async @Scheduled(cron = "0 */1 * * * ? ") public void savaData() { // 获取数据库中最新的创建时间 LocalDateTime newTime = accidentService.findNewTime(); // 获取当前时间 LocalDateTime currentTime = LocalDateTime.now(); // 计算两者之间的持续时间 Duration duration = Duration.between(newTime, currentTime); // 判断时间差是否大于24小时 if (duration.toHours() > 24) { // 调用短信接口 SmsUtil.sendSms("120原始接口超过24小时无数据", null); } QuartzLog quartzLog = new QuartzLog(); quartzLog.setInterfaceName(3); quartzLog.setCreateTime(LocalDateTime.now()); try { String accessToken = accidentService.getAuthorizeToken(); if (StrUtil.isNotEmpty(accessToken)) { accidentService.getAccidentUrl(accessToken); quartzLog.setStatus(1); log.info("=================120定时任务结束==============="); } else { quartzLog.setStatus(2); // 调用短信接口 SmsUtil.sendSms("120原始接口获取token失败", null); log.info("=================120定时任务获取token失败==============="); } } catch (Exception e) { // 调用短信接口 SmsUtil.sendSms("120原始接口调用失败", null); e.printStackTrace(); quartzLog.setStatus(2); } finally { quartzLogService.save(quartzLog); } } }