-
Notifications
You must be signed in to change notification settings - Fork 41
/
Timer.java
221 lines (197 loc) · 7.73 KB
/
Timer.java
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.softeng.dingtalk.component;
import com.softeng.dingtalk.component.dingApi.MessageApi;
import com.softeng.dingtalk.dao.repository.*;
import com.softeng.dingtalk.enums.Position;
import com.softeng.dingtalk.enums.PracticeStateEnum;
import com.softeng.dingtalk.entity.*;
import com.softeng.dingtalk.service.*;
import com.softeng.dingtalk.utils.stuNumUtils.StuNumParser;
import com.softeng.dingtalk.utils.stuNumUtils.StuNumParserFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
/**
* @author zhanyeye
* @description 定时器
* @create 2/9/2020 12:30 PM
*/
@Slf4j
@Component
@Transactional
public class Timer {
@Autowired
VoteRepository voteRepository;
@Autowired
VoteService voteService;
@Autowired
InternalPaperRepository internalPaperRepository;
@Autowired
InitService initService;
@Autowired
ExternalPaperRepository externalPaperRepository;
@Autowired
WeeklyReportService weeklyReportService;
@Autowired
AcRecordRepository acRecordRepository;
@Autowired
MessageApi messageApi;
@Autowired
SystemService systemService;
@Resource
private PracticeRepository practiceRepository;
@Resource
private UserRepository userRepository;
/**
* 每周周日23点扫描一次,给当天还未提交周报的博士、硕士发送提醒消息
*/
@Scheduled(cron = "0 0 23 ? * SUN")
public void weeklyReportAndPerformanceFillingReminder() {
log.info(LocalDate.now() + "定时扫描提醒周报和绩效填写");
systemService.manualReminderToSubmitWeeklyReport(LocalDate.now());
}
/**
* 每周一凌晨2点扫描一次,查询周日一整天没有提交周报的博士、硕士,每人扣 1 ac
*/
@Scheduled(cron = "0 0 2 ? * MON")
public void deductedPointsUnSubmittedWeeklyReport() {
log.info(LocalDate.now() + " 定时扫描扣分");
systemService.manualDeductedPointsUnSubmittedWeeklyReport(LocalDate.now().minusDays(1));
}
//每天凌晨扫描一次,会议的AC结算
@Scheduled(cron = "0 0 0 * * ?")
public void calculateScheduleAC(){
log.info(LocalDate.now() + " 定时扫描会议未参加者的扣分");
systemService.calculateScheduleAC();
}
/**
* 每20s扫描一次,看是否有待启动的投票
*/
@Scheduled(cron = "0/20 * * * * ?")
public void checkVote() {
LocalDateTime now = LocalDateTime.now();
// (针对外部评审投票) 检查是否有投票需要开始
List<Vote> upcomingVotes = voteService.listUpcomingVote(now);
upcomingVotes.forEach(v -> {
// 标注该投票已经开始
v.setStarted(true);
voteRepository.save(v);
ExternalPaper externalPaper = externalPaperRepository.findByVid(v.getId());
if (externalPaper != null) {
// 发送投票开始的消息
String markdown = startVoteInfo(externalPaper.getTitle(), v.getEndTime());
String url = generateVoteDetailUrl(true, externalPaper.getId());
messageApi.sendActionCard("外部评审投票", markdown, "前往投票", url);
}
});
}
/**
* 每20s扫描一次,看是否有待结束的投票
*/
@Scheduled(cron = "0/20 * * * * ?")
public void checkVote2() {
// 检查是否有需要被结束的投票
LocalDateTime now = LocalDateTime.now();
List<Vote> votes = voteService.listUnderwayVote(now);
votes.forEach(v -> {
// 更新汇总投票结果
v = voteService.updateVote(v);
Paper paper = v.isExternal() ?
externalPaperRepository.findByVid(v.getId()) :
internalPaperRepository.findByVid(v.getId());
messageApi.sendActionCard(
"投票结果",
voteResultInfo(paper.getTitle(), v.getResult(), v.getAccept(), v.getTotal()),
"查看详情",
generateVoteDetailUrl(paper.isExternal(), paper.getId())
);
});
}
/**
* 每月1日3点执行 initDcDummary 方法
*/
@Scheduled(cron = "0 0 3 1 * ?")
public void initMonthlyDcSummary() {
initService.initDcSummary();
}
/**
* 投票消息卡片跳转的 url
*
* @param isExternal 是否是外部论文
* @param pid 论文id
* @return
*/
private String generateVoteDetailUrl(boolean isExternal, int pid) {
return (isExternal ? "/paper/ex-detail/" : "/paper/in-detail/") + pid + "/vote";
}
/**
* 确认投票结果的消息模板
*
* @param title 论文标题
* @param result 投票预测结果
* @param acceptCnt accept数量
* @param totalCnt 总投票数量
* @return
*/
private String voteResultInfo(String title, int result, int acceptCnt, int totalCnt) {
return new StringBuilder().append(" #### 投票结果 \n ##### 论文: ").append(title)
.append(" \n 最终结果: ").append(result == 1 ? "accept" : result == 0 ? "reject" : "flat")
.append(" \n Accept: ").append(acceptCnt).append(" 票 \n ")
.append("Reject: ").append(totalCnt - acceptCnt).append(" 票 \n ")
.append("已参与人数: ").append(totalCnt).append("人 \n ").toString();
}
/**
* 发起投票的消息模板
*
* @param title 论文标题
* @param dateTime 截至时间
* @return
*/
private String startVoteInfo(String title, LocalDateTime dateTime) {
return " #### 投票 \n ##### 论文: " + title + " \n 截止时间: " + dateTime.toLocalTime().toString();
}
// @Scheduled(cron = "0/20 * * * * ?")
// public void test() {
// log.info(LocalDate.now() + "test");
// }
/**
* 每天凌晨一点根据审核通过的实习申请修改对应用户的工作状态
*/
@Scheduled(cron = "0 0 1 * * ?")
public void editWorkState() {
log.info("每天凌晨一点根据审核通过的实习申请修改对应用户的工作状态");
List<Practice> practiceList = practiceRepository.findAllByStateEquals(PracticeStateEnum.ACCEPTED.getValue());
LocalDate cur = LocalDate.now();
practiceList.forEach(practice -> {
LocalDate start = practice.getStart();
LocalDate end = practice.getEnd();
User user = practice.getUser();
user.setWorkState((cur.isAfter(start) || cur.isEqual(start)) && (cur.isBefore(end) || cur.isEqual(end)));
userRepository.save(user);
});
}
/**
* 每年8月1日凌晨两点进行用户职位的变更
*/
@Scheduled(cron = "0 0 2 1 8 ?")
public void updateUserPosition() {
log.info("每年8月1日进行用户职位的变更");
userRepository.findAllValidUser()
.stream()
// 筛选出设置了学号的用户
.filter((user -> user.getStuNum() != null))
.forEach((user -> {
// 排除教师用户
if(user.getPosition() == Position.TEACHER) return;
StuNumParser parser= StuNumParserFactory.generateParser(user.getStuNum().length());
assert parser != null;
user.setPosition(parser.parse(user.getStuNum()));
userRepository.save(user);
}));
}
}