MassageNoticeScheduleService.java
8.33 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
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
package com.pipihelper.project.scheduled;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.pipihelper.project.feishu.bo.PushPainBO;
import com.pipihelper.project.feishu.dto.FeiShuConfig;
import com.pipihelper.project.feishu.dto.chat.FeiShuChatDTO;
import com.pipihelper.project.feishu.dto.department.FeiShuDepartmentDTO;
import com.pipihelper.project.feishu.dto.msg.FeiShuMsgDTO;
import com.pipihelper.project.feishu.entity.Employee;
import com.pipihelper.project.feishu.entity.LastMaxPain;
import com.pipihelper.project.feishu.entity.Pain;
import com.pipihelper.project.feishu.service.ChatMessageService;
import com.pipihelper.project.feishu.service.EmployeeService;
import com.pipihelper.project.feishu.service.FeiShuApiService;
import com.pipihelper.project.feishu.service.LastMaxPainService;
import com.pipihelper.project.feishu.service.PainService;
import com.pipihelper.project.feishu.service.massage.MassageService;
import com.pipihelper.project.utils.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* @Description: TODO
* @author: charles
* @date: 2022年10月15日 17:26
*/
@Slf4j
@Component
public class MassageNoticeScheduleService {
@Autowired
private PainService painService;
@Autowired
private MassageService massageService;
@Autowired
private FeiShuApiService feiShuApiService;
@Autowired
private EmployeeService employeeService;
@Autowired
private LastMaxPainService lastMaxPainService;
@Autowired
private ChatMessageService chatMessageService;
@Resource
private FeiShuConfig feiShuConfig;
private final static Map<Integer, Integer> floorCountMap = new HashMap<>();
static {
floorCountMap.put(8, 10);
floorCountMap.put(14, 30);
}
// /**
// * 每周四定时生成要按摩的人员名单,并发送大群和单人消息
// */
// @Async
@Scheduled(cron = "0 0 11 ? * THU")
public void sendMsgCardToPipiChat() {
employeeService.uprsetAllEmployee();
chatMessageService.deleteAll();
for (Integer floor : Lists.newArrayList(14)) {
LastMaxPain maxPain = lastMaxPainService.findByFloor(floor);
int startEmployeeId = 0;
if (maxPain != null) {
startEmployeeId = maxPain.getEmployeeId();
}
int countByFloor = employeeService.findCountByFloor(floor);
int limit = floorCountMap.get(floor);
if (countByFloor < limit) {
limit = countByFloor;
}
List<Employee> employeeList = employeeService.findStartList(startEmployeeId, floor, limit);
Integer maxEmploeeId;
if (employeeList.size() == limit) {
maxEmploeeId = employeeList.stream().map(Employee::getId).max(Comparator.comparing(x -> x)).orElse(0);
} else {
List<Employee> employees = employeeService.findStartList(0, floor, limit - employeeList.size());
maxEmploeeId = employees.stream().map(Employee::getId).max(Comparator.comparing(x -> x)).orElse(0);
employeeList.addAll(employees);
}
Map<String, FeiShuDepartmentDTO> departmentMap = new HashMap<>();
List<String> departmentIdList = employeeList.stream().map(Employee::getDepartmentId).distinct().collect(Collectors.toList());
departmentIdList.forEach(it -> {
FeiShuDepartmentDTO department = feiShuApiService.getDepartment(it);
departmentMap.put(it, department);
});
painService.deleteAllByFloor(floor);
if (CollectionUtil.isEmpty(employeeList)) {
return;
}
List<PushPainBO> pushPainBOList = new ArrayList<>();
String[] userIdList = new String[employeeList.size()];
AtomicInteger i = new AtomicInteger(1);
employeeList.forEach(it -> {
Pain pain = new Pain();
pain.setOpenId(it.getOpenId());
pain.setStatus(0);
pain.setFloor(Optional.ofNullable(it.getFloor()).orElse(floor));
int increment = i.getAndIncrement();
pain.setPindex(increment);
pain.setName(it.getName());
String departMentName = departmentMap.getOrDefault(it.getDepartmentId(), new FeiShuDepartmentDTO()).getName();
pain.setDepartMentName(departMentName);
painService.create(pain);
PushPainBO pushPainBO = new PushPainBO();
pushPainBO.setIndex(increment);
pushPainBO.setName(it.getName());
pushPainBO.setDepartMentName(departMentName);
pushPainBO.setOpenId(it.getOpenId());
pushPainBO.setTimeRange(painService.getTimeRange(increment, floor));
pushPainBOList.add(pushPainBO);
userIdList[increment - 1] = it.getOpenId();
});
FeiShuChatDTO feiShuChatDTO = new FeiShuChatDTO();
feiShuChatDTO.setName("按摩群 " + DateUtil.today());
feiShuChatDTO.setOwnerId(feiShuConfig.getXiaoshanOpenId());
feiShuChatDTO.setUserIdList(userIdList);
String chatId = feiShuApiService.createChatList(feiShuChatDTO);
massageService.sendMassageMsgCardToPiPiChat(pushPainBOList, chatId);
massageService.sendMassageMsgCardToPiPiChat(pushPainBOList, feiShuConfig.getChatId());
if (maxPain != null) {
maxPain.setEmployeeId(maxEmploeeId);
lastMaxPainService.update(maxPain);
} else {
maxPain = new LastMaxPain();
maxPain.setEmployeeId(maxEmploeeId);
maxPain.setFloor(floor);
lastMaxPainService.create(maxPain);
}
}
}
/**
* 每周四18:00删除当天的群聊
*/
// @Async
@Scheduled(cron = "0 0 19 * * ?")
public void deleteChatList() {
String chatId = feiShuApiService.queryChatId();
if (StringUtils.isEmpty(chatId) || chatId.equals(feiShuConfig.getChatId())) {
return;
}
feiShuApiService.deleteChatList(chatId);
}
/**
* 当天时间2:55,给第一波三个人更新卡片,并发送应用内提醒
*/
// @Async
@Scheduled(cron = "0 55 14 ? * THU")
public void massageStart() {
List<Pain> pains = painService.findListAsc(3);
// String msg = "{\"text\":\"当前按摩进度:<at user_id=\\\"%s\\\">%s</at> <at user_id=\\\"%s\\\">%s</at> <at user_id=\\\"%s\\\">%s</at>\"}";
StringBuilder userString = new StringBuilder();
for (Pain pain : pains) {
String messageId = pain.getMessageId();
massageService.updateSingleMassageMsgCardWhenBegin(messageId);
userString.append(String.format("<at user_id=\"%s\">%s</at>", pain.getOpenId(), pain.getName()));
//1代表正在按
pain.setStatus(1);
painService.update(pain);
//发送应用内提醒
JSONObject noticeMsg = new JSONObject();
noticeMsg.put("user_id_list", Arrays.asList(pain.getOpenId()));
log.info("发送应用内容消息提醒");
feiShuApiService.patchUrgentApp(messageId, noticeMsg);
}
//按摩群中同步发送按摩进度信息
FeiShuMsgDTO feiShuMsgDTO = new FeiShuMsgDTO();
String msg = "当前按摩进度:" + userString;
JSONObject content = new JSONObject();
content.put("text", msg);
feiShuMsgDTO.setMsgType("text");
feiShuMsgDTO.setContent(content.toString());
String chatId = feiShuApiService.queryChatId();
feiShuMsgDTO.setReceiveId(chatId);
feiShuApiService.sendMsg(feiShuMsgDTO, "chat_id");
}
}