UpgradeClassJob.java
4.19 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.meishu.job;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meishu.mapper.ClassesDictHistoryMapper;
import com.meishu.mapper.ClassesDictMapper;
import com.meishu.model.ClassesDictDO;
import com.meishu.model.ClassesDictHistoryDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* 定时任务 每天凌晨查看班级里是否有需要晋升的
*/
@Component
public class UpgradeClassJob {
@Autowired
private ClassesDictMapper classesDictMapper;
@Autowired
private ClassesDictHistoryMapper classesDictHistoryMapper;
@Scheduled(cron = "0 10 0 * * ?")
public void updateGrade(){
List<ClassesDictDO> classesDictDOS = classesDictMapper.selectList(new QueryWrapper<ClassesDictDO>()
.lambda()
.eq(ClassesDictDO::getStatus,"在读"));
for (ClassesDictDO classesDictDO : classesDictDOS){
//获取班级的晋升时间
Date date = classesDictDO.getUpgradeDate();
Date current = new Date();
if (null!=date && date.getYear() == current.getYear() && date.getMonth() == current.getMonth() && date.getDate() == current.getDate()){
//将history表里该班级的记录置为归档
ClassesDictHistoryDO classesDictHistoryDO = classesDictHistoryMapper.selectOne(new QueryWrapper<ClassesDictHistoryDO>()
.lambda()
.eq(ClassesDictHistoryDO::getStatus,"在读")
.eq(ClassesDictHistoryDO::getClassDictId,classesDictDO.getId()));
if (null != classesDictHistoryDO){
classesDictHistoryDO.setStatus("归档");
classesDictHistoryMapper.updateById(classesDictHistoryDO);
}
//将classdict表里的grade修改为新的年级
if ("高三".equals(classesDictDO.getGrade())){
classesDictDO.setStatus("归档");
classesDictMapper.updateById(classesDictDO);
}else {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;//获取月份
if (month>=7){
date.setYear(year-1899);
date.setMonth(7);
date.setDate(1);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
}else {
date.setYear(year-1900);
date.setMonth(7);
date.setDate(1);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
}
classesDictDO.setUpgradeDate(date);
classesDictDO.setGrade(changeGrade(classesDictDO.getGrade()));
classesDictDO.setClassName(changeClassName(classesDictDO.getClassName()));
classesDictMapper.updateById(classesDictDO);
//在history中插入新的记录
ClassesDictHistoryDO classesDictHistoryDO1 = new ClassesDictHistoryDO();
BeanUtils.copyProperties(classesDictDO,classesDictHistoryDO1);
classesDictHistoryDO1.setClassDictId(classesDictDO.getId());
classesDictHistoryMapper.insert(classesDictHistoryDO1);
}
}
}
}
public String changeGrade(String grade){
if("高一".equals(grade)){
return "高二";
}else {
return "高三";
}
}
public String changeClassName(String className){
String newName = "";
if (className.contains("高一")){
newName = className.replace("高一","高二");
}else {
newName = className.replace("高二","高三");
}
return newName;
}
}