zss
12 小时以前 30c56354b41b6d3bffa19fd69786654b83fd8a1a
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
package com.ruoyi;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.project.system.domain.SysNotice;
import com.ruoyi.project.system.mapper.SysNoticeMapper;
import com.ruoyi.safe.mapper.SafeTrainingMapper;
import com.ruoyi.safe.pojo.SafeTraining;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
 
@Component
//定时任务汇总
public class ScheduleTask {
 
    @Autowired
    private SafeTrainingMapper safeTrainingMapper;
 
    @Autowired
    private SysNoticeMapper noticeMapper;
 
    //定时任务(15分钟执行一次--判断培训计划数据,状态做变更)
    @Scheduled(cron = "0 0/15 * * * ?")
    public void testScheduleTask() {
        List<SafeTraining> safeTrainings = safeTrainingMapper.selectList(Wrappers.<SafeTraining>lambdaQuery().ne(SafeTraining::getState, 2));
        if (safeTrainings.size() > 0) {
            for (SafeTraining safeTraining : safeTrainings) {
                //根据时间判断培训状态
                String trainingDate = safeTraining.getTrainingDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                LocalDateTime openingTime = LocalDateTime.parse((trainingDate + safeTraining.getOpeningTime()), DateTimeFormatter.ofPattern("yyyy-MM-ddHH:mm:ss"));
                LocalDateTime endTime = LocalDateTime.parse((trainingDate + safeTraining.getEndTime()), DateTimeFormatter.ofPattern("yyyy-MM-ddHH:mm:ss"));
                if (LocalDateTime.now().isBefore(openingTime)) {
                    //未开始
                    safeTraining.setState(0);
                } else if (LocalDateTime.now().isAfter(endTime)) {
                    //已结束
                    safeTraining.setState(2);
                } else {
                    //进行中
                    safeTraining.setState(1);
                }
                safeTrainingMapper.updateById(safeTraining);
            }
        }
    }
 
    //已读数据做一个定时任务(每月1号1点清理一次上个月已读数据)
    @Scheduled(cron = "0 0 1 1 * ?")
    public void cleanReadData() {
        noticeMapper.delete(Wrappers.<SysNotice>lambdaQuery()
                .eq(SysNotice::getStatus,"1")
                .lt(SysNotice::getCreateTime, LocalDateTime.now()));
    }
}