package com.ruoyi.web.controller.task; import com.ruoyi.system.model.TOldster; import com.ruoyi.system.model.TOldsterWarning; import com.ruoyi.system.model.TOtherConfig; import com.ruoyi.system.service.TOldsterService; import com.ruoyi.system.service.TOldsterWarningService; import com.ruoyi.system.service.TOtherConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; /** * @author xiaochen * @date 2025/10/11 8:39 */ @Component public class TaskUtil { @Autowired private TOldsterService oldsterService; @Autowired private TOldsterWarningService oldsterWarningService; @Autowired private TOtherConfigService otherConfigService; /** * 每日0点定时任务 */ @Scheduled(cron = "0 0 0 * * ?") public void generateOldsterWarning(){ // 查询所有老人信息 List oldsterList = oldsterService.list(); // 查询预警阈值 TOtherConfig otherConfig = otherConfigService.getById(1); Integer elderlyAge = otherConfig.getElderlyAge(); Integer days = otherConfig.getDays(); List oldsterWarningList = new ArrayList<>(); for (TOldster oldster : oldsterList) { LocalDate birthDate = oldster.getBirthDate(); // 判断年龄是否与elderlyAge是否为同一年 if(birthDate.plusYears(elderlyAge).getYear() == LocalDate.now().getYear() && birthDate.plusYears(elderlyAge).minusDays(days).isEqual(LocalDate.now())){ // 添加预警信息 TOldsterWarning oldsterWarning = new TOldsterWarning(); oldsterWarning.setOldsterId(oldster.getId()); oldsterWarning.setWarningTime(LocalDateTime.now()); oldsterWarningList.add(oldsterWarning); } } if(!CollectionUtils.isEmpty(oldsterWarningList)){ oldsterWarningService.saveBatch(oldsterWarningList); } } }