EduSendMsgJob.java
5.2 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.skua.modules.edu.job;
import com.skua.core.context.BaseContextHandler;
import com.skua.core.util.DateUtils;
import com.skua.core.util.push.IPushService;
import com.skua.core.util.push.MessageEntity;
import com.skua.core.util.push.PushMessageFactory;
import com.skua.modules.edu.entity.Paper;
import com.skua.modules.edu.service.IPaperService;
import com.skua.modules.edu.service.IUserPaperService;
import com.skua.modules.system.entity.SysUser;
import com.skua.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 发送消息任务
* (2)、考试开始时间前1天推送一次
* (3)、考试结束时间前1天推送一次(如果还未完成的话)
*/
@Slf4j
public class EduSendMsgJob implements Job {
@Autowired
private ISysUserService userService;
@Autowired
private IPaperService paperService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info(String.format(" Jeecg-Boot 发送消息任务 SendMsgJob ! 时间:" + DateUtils.getTimestamp()));
// 获取当前日期
LocalDate today = LocalDate.now();
// 获取昨天的日期
LocalDate yesterday = today.minusDays(1);
LocalDate tomorrowDay = today.plusDays(1);
// 创建一个DateTimeFormatter对象,指定格式为yyyy-MM-dd
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 使用formatter格式化LocalDate对象
String yesterDate = yesterday.format(formatter);
String tomorrowDate = tomorrowDay.format(formatter);
String messageTitle = null;
String messageBody = null;
String userIds = null;
List<Paper> paperList = paperService.queryExamUserIds(yesterDate,null);
if(paperList != null && !paperList.isEmpty()){
for(Paper paper : paperList){
messageTitle = "考试任务:<"+paper.getPaperTitle() +">明天开始,请参加考试";
//messageBody = "明天考试任务:"+paper.getPaperTitle() +",请参加考试";
messageBody = "考试任务:<"+paper.getPaperTitle() +">明天开始,考试时间:"+paper.getStartTime() +"至"+paper.getEndTime()+",请准时参加考试";
userIds = getNoExamUserIds(paper.getExamUserIds(),paper.getUserIds());
if(StringUtils.isNotEmpty(userIds)){
pushMsgNoProcess(messageTitle,messageBody,userIds);
}
}
}
paperList = paperService.queryExamUserIds(null,tomorrowDate);
if(paperList != null && !paperList.isEmpty()){
for(Paper paper : paperList){
messageTitle = "考试任务:<"+paper.getPaperTitle() +">明天结束,请尽快考试";
//messageBody = "考试:"+paper.getPaperTitle() +"明天结束,请尽快参加考试";
messageBody = "考试任务:<"+paper.getPaperTitle() +">明天结束,考试时间:"+paper.getStartTime() +"至"+paper.getEndTime()+",请准时参加考试";
userIds = getNoExamUserIds(paper.getExamUserIds(),paper.getUserIds());
if(StringUtils.isNotEmpty(userIds)){
pushMsgNoProcess(messageTitle,messageBody,userIds);
}
}
}
}
private void pushMsgNoProcess(String messageTitle ,String messageBody ,String userIds) {
if(StringUtils.isNotEmpty(userIds)){
List<SysUser> sysUserList = userService.getUserByUserIds(userIds);
List<String> userCidList = sysUserList.stream().map(SysUser::getCid).filter(v -> StringUtils.isNotBlank(v)).collect(Collectors.toList());
List<String> userIdList = sysUserList.stream().map(SysUser::getId).collect(Collectors.toList());
MessageEntity messageEntity = new MessageEntity();
//messageEntity.setMessageTitle("考试马上开始");
//messageEntity.setMessageBody("**考试于*****开始考试,请尽快准备!");
messageEntity.setMessageTitle(messageTitle);
messageEntity.setMessageBody(messageBody);
messageEntity.setReceiveUser(userIdList);
messageEntity.setReceiveUserCid(userCidList);
messageEntity.setSendUser(BaseContextHandler.getUserName());
messageEntity.setForwardTag("historyRecordRemind");//消息提醒类型 保持唯一
IPushService appPushService = PushMessageFactory.getPushService("MOB");
IPushService webPushService = PushMessageFactory.getPushService("WEB");
appPushService.pushMessage(messageEntity);
webPushService.pushMessage(messageEntity);
}
}
/***
* 获取第一个字符串列表中存在而第二个字符串列表中不存在的元素
* @param string1
* @param string2
* @return
*/
private String getNoExamUserIds(String string1 , String string2){
// 分割字符串
String[] array1 = string1.split(",");
String[] array2 = string2.split(",");
// 将array2转换为Set集合
Set<String> set2 = new HashSet<>(Arrays.asList(array2));
// 获取在array1中但不在set2中的元素
String result = Arrays.stream(array1)
.filter(element -> !set2.contains(element))
.collect(Collectors.joining(","));
return result;
}
}