parent
298a694881
commit
ec741b4249
@ -0,0 +1,74 @@ |
||||
package digital.laboratory.platform.sewage.task; |
||||
|
||||
import digital.laboratory.platform.sewage.entity.SewageJob; |
||||
import digital.laboratory.platform.sewage.service.SewageJobService; |
||||
import lombok.extern.java.Log; |
||||
import org.springframework.scheduling.annotation.Scheduled; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.time.LocalDate; |
||||
import java.time.Year; |
||||
|
||||
@Log |
||||
@Component |
||||
public class JobTask { |
||||
|
||||
@Resource |
||||
private SewageJobService sewageJobService; |
||||
|
||||
/** |
||||
* 每天凌晨 0 点执行:判断当天所属季度任务是否存在,不存在则创建 |
||||
*/ |
||||
@Scheduled(cron = "0 0 0 * * ?") |
||||
public void runAtMidnight() { |
||||
log.info(">>> 每天凌晨定时任务开始执行"); |
||||
|
||||
int year = Year.now().getValue(); |
||||
int quarter = getCurrentQuarter(); |
||||
// 计算本季度的开始日期
|
||||
int month = (quarter - 1) * 3 + 1; // 季度对应的第一个月 (1,4,7,10)
|
||||
LocalDate startDate = LocalDate.of(year, month, 1); |
||||
// 计算本季度的结束日期
|
||||
LocalDate endDate = startDate.plusMonths(3).minusDays(1); |
||||
|
||||
boolean exists = sewageJobService.lambdaQuery() |
||||
.eq(SewageJob::getJobYear, year) |
||||
.eq(SewageJob::getJobSeason, quarter) |
||||
.exists(); |
||||
|
||||
if (exists) { |
||||
log.info(">>> 季度任务已存在:" + "year" + "年第" + quarter + "季度"); |
||||
} else { |
||||
log.info(">>> 未找到季度任务,开始创建:" + "year" + "年第" + quarter + "季度"); |
||||
|
||||
SewageJob job = new SewageJob(); |
||||
job.setJobYear(year); |
||||
job.setJobSeason(quarter); |
||||
job.setName(year + "年第" + quarter + "季度污水监测任务"); |
||||
job.setJobNo(sewageJobService.getNewJobNo(year, quarter)); |
||||
job.setStatus(1); |
||||
job.setDescription("贵阳市" + year + "年第" + quarter + "季度污水监测任务"); |
||||
job.setLauncheOrgId("0"); |
||||
job.setStartDate(startDate.atStartOfDay()); |
||||
job.setExpirationDate(endDate.atStartOfDay()); |
||||
|
||||
boolean saved = sewageJobService.save(job); |
||||
if (saved) { |
||||
log.info(">>> 创建成功:" + "year" + "年第" + quarter + "季度"); |
||||
} else { |
||||
log.info(">>> 创建失败:" + "year" + "年第" + quarter + "季度"); |
||||
} |
||||
} |
||||
|
||||
log.info(">>> 每天凌晨定时任务执行完毕"); |
||||
} |
||||
|
||||
/** |
||||
* 根据当前月份获取季度(1~4) |
||||
*/ |
||||
private int getCurrentQuarter() { |
||||
int month = LocalDate.now().getMonthValue(); |
||||
return (month - 1) / 3 + 1; |
||||
} |
||||
} |
Loading…
Reference in new issue