xuhy
2025-12-31 ffef20d703aaa9b60c9234dd0b42f3d006426dc7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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<TOldster> oldsterList = oldsterService.list();
        // 查询预警阈值
        TOtherConfig otherConfig = otherConfigService.getById(1);
        Integer elderlyAge = otherConfig.getElderlyAge();
        Integer days = otherConfig.getDays();
        List<TOldsterWarning> 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);
        }
    }
 
}