MassageService.java
6.87 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
package com.pipihelper.project.feishu.service.massage;
import com.alibaba.fastjson.JSONObject;
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.ChatMessage;
import com.pipihelper.project.feishu.entity.Pain;
import com.pipihelper.project.feishu.service.ChatMessageService;
import com.pipihelper.project.feishu.service.FeiShuApiService;
import com.pipihelper.project.feishu.service.PainService;
import com.pipihelper.project.utils.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: TODO
* @author: charles
* @date: 2022年10月15日 17:21
*/
@Service
@Transactional
@Slf4j
public class MassageService {
@Autowired
private MassageMsgCardSerivce massageMsgCardSerivce;
@Autowired
private FeiShuApiService feiShuApiService;
@Autowired
private FeiShuConfig feiShuConfig;
@Autowired
private PainService painService;
@Autowired
private ChatMessageService chatMessageService;
//给群里发送消息
public void sendMassageMsgCardToPiPiChat(List<PushPainBO> pushPainBOList, String chatId) {
List<List<String>> pushUser = new ArrayList<>();
for (PushPainBO pushPainBO : pushPainBOList) {
List<String> user = new ArrayList<>();
if (chatId.equals(feiShuConfig.getChatId())) {
//给单个用户发送
String singleContent = massageMsgCardSerivce.genMassageMsgCardForSingle();
log.info("给单个用户发送按摩消息:{}", singleContent);
FeiShuMsgDTO feiShuMsgDTO = new FeiShuMsgDTO();
feiShuMsgDTO.setMsgType("interactive");
feiShuMsgDTO.setContent(singleContent);
feiShuMsgDTO.setReceiveId(pushPainBO.getOpenId());
log.info(feiShuMsgDTO.toString());
JSONObject sendMsgResponse = feiShuApiService.sendMsg(feiShuMsgDTO, "open_id");
String messageId = sendMsgResponse.getJSONObject("data").getString("message_id");
//更新按摩记录表中的messageId
Pain pain = painService.findByOpenId(pushPainBO.getOpenId());
pain.setMessageId(messageId);
painService.update(pain);
}
//构建给大群发送的名单
user.add(pushPainBO.getIndex().toString());
user.add(pushPainBO.getName());
user.add(pushPainBO.getDepartMentName());
user.add(pushPainBO.getTimeRange());
user.add("");
pushUser.add(user);
}
//给大群发送消息
String content = massageMsgCardSerivce.genMassageMsgCardForCompany(pushUser);
log.info("给大群发送按摩消息:{}", content);
FeiShuMsgDTO feiShuMsgDTO = new FeiShuMsgDTO();
feiShuMsgDTO.setMsgType("interactive");
feiShuMsgDTO.setContent(content);
feiShuMsgDTO.setReceiveId(chatId);
log.info(feiShuMsgDTO.toString());
JSONObject rep = feiShuApiService.sendMsg(feiShuMsgDTO, "chat_id");
String messageId = rep.getJSONObject("data").getString("message_id");
ChatMessage chatMessage = new ChatMessage();
chatMessage.setChatId(chatId);
chatMessage.setMessageId(messageId);
chatMessage.setType(1);
chatMessageService.create(chatMessage);
}
public void updateSingleMassageMsgCardWhenBegin(String messageId) {
JSONObject patchMsg = new JSONObject();
String msgCardContent = massageMsgCardSerivce.genMassageMsgCardForSingleStart();
patchMsg.put("content", msgCardContent);
feiShuApiService.patchMsg(messageId, patchMsg);
}
//更新群消息
@Async
public void updateMassageMsgCardToPiPiChat(String chatId) {
List<Pain> pains = painService.findAllReorderByFloor(14);
System.out.println(pains);
String messageId = chatMessageService.findListChatAndType(chatId, 1).get(0).getMessageId();
List<List<String>> pushUser = new ArrayList<>();
for (Pain pain : pains) {
List<String> user = new ArrayList<>();
//构建给大群发送的名单
user.add(pain.getPindex().toString());
user.add(pain.getName());
user.add(pain.getDepartMentName());
user.add(pain.getTimeRange());
user.add("");
pushUser.add(user);
}
//给大群更新发送消息
String content = massageMsgCardSerivce.genMassageMsgCardForCompany(pushUser);
log.info("给大群发送按摩消息:{}", content);
JSONObject patchMsg = new JSONObject();
patchMsg.put("content", content);
feiShuApiService.patchMsg(messageId, patchMsg);
}
@Async
public void robSingle(String robOpenId, String robDepartmentId, Integer givUpPainId) {
FeiShuChatDTO feiShuChatDTO = new FeiShuChatDTO();
feiShuChatDTO.setChatId((String) CacheUtil.get("chatId"));
feiShuChatDTO.setIdList(new String[]{robOpenId});
feiShuApiService.joinChatList(feiShuChatDTO);
/**
* 更新部门名
*/
FeiShuDepartmentDTO department = feiShuApiService.getDepartment(robDepartmentId);
Pain pain = new Pain();
pain.setId(givUpPainId);
pain.setDepartMentName(department.getName());
painService.update(pain);
sendSingle(robOpenId);
// 更新大群和按摩群的大卡片,异步
// 大群
updateMassageMsgCardToPiPiChat(feiShuConfig.getChatId());
//按摩群
updateMassageMsgCardToPiPiChat((String) CacheUtil.get("chatId"));
}
public void sendSingle(String openId) {
//给单个用户发送
String singleContent = massageMsgCardSerivce.genMassageMsgCardForSingle();
log.info("给单个用户发送按摩消息:{}", singleContent);
FeiShuMsgDTO feiShuMsgDTO = new FeiShuMsgDTO();
feiShuMsgDTO.setMsgType("interactive");
feiShuMsgDTO.setContent(singleContent);
feiShuMsgDTO.setReceiveId(openId);
log.info(feiShuMsgDTO.toString());
JSONObject sendMsgResponse = feiShuApiService.sendMsg(feiShuMsgDTO, "open_id");
String messageId = sendMsgResponse.getJSONObject("data").getString("message_id");
//更新按摩记录表中的messageId
Pain pain = painService.findByOpenId(openId);
pain.setMessageId(messageId);
painService.update(pain);
}
}