WeCustomerMessageServiceImpl.java
8.75 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
package com.linkwechat.wecom.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.enums.ChatType;
import com.linkwechat.common.enums.GroupMessageType;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCustomerMessagePushClient;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeCustomerMessage;
import com.linkwechat.wecom.domain.WeGroup;
import com.linkwechat.wecom.domain.dto.WeMediaDto;
import com.linkwechat.wecom.domain.dto.message.CustomerMessagePushDto;
import com.linkwechat.wecom.domain.dto.message.ImageMessageDto;
import com.linkwechat.wecom.domain.dto.message.SendMessageResultDto;
import com.linkwechat.wecom.domain.dto.message.WeCustomerMessagePushDto;
import com.linkwechat.wecom.mapper.WeCustomerMessageMapper;
import com.linkwechat.wecom.service.IWeCustomerMessageService;
import com.linkwechat.wecom.service.IWeMaterialService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 群发消息 微信消息表service接口
*
* @author kewen
* @date 2020-12-12
*/
@SuppressWarnings("all")
@Service
public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageMapper, WeCustomerMessage> implements IWeCustomerMessageService {
@Autowired
private WeCustomerMessageMapper weCustomerMessageMapper;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private WeCustomerMessagePushClient weCustomerMessagePushClient;
@Autowired
private IWeMaterialService weMaterialService;
@Override
public int updateWeCustomerMessageActualSend(Long messageId, Integer actualSend) {
return weCustomerMessageMapper.updateWeCustomerMessageActualSend(messageId, actualSend);
}
@Override
public void saveWeCustomerMessage(long messageId, long messageOriginalId, CustomerMessagePushDto customerMessagePushDto, int size,String content) {
//保存微信消息
//微信群发消息表 WeCustomerMessage
WeCustomerMessage customerMessage = new WeCustomerMessage();
customerMessage.setOriginalId(messageOriginalId);
customerMessage.setMessageId(messageId);
customerMessage.setChatType(customerMessagePushDto.getPushType());
// customerMessage.setExternalUserid(objectMapper.writeValueAsString(customers));
// customerMessage.setSender(objectMapper.writeValueAsString(senders));
customerMessage.setCheckStatus("0");
customerMessage.setDelFlag(0);
customerMessage.setContent(content);
customerMessage.setCreateBy(SecurityUtils.getUsername());
customerMessage.setMsgid("");
if(StringUtils.isNotEmpty(customerMessagePushDto.getSettingTime())){
customerMessage.setSettingTime(customerMessagePushDto.getSettingTime());
customerMessage.setTimedTask(1);
}else {
customerMessage.setTimedTask(0);
}
customerMessage.setExpectSend(size);
customerMessage.setActualSend(0);
this.save(customerMessage);
}
@Override
public void updateMsgId(long messageId, List<String> msgIds) throws JsonProcessingException {
//通过messageId更新msgIds
WeCustomerMessage weCustomerMessage = new WeCustomerMessage();
weCustomerMessage.setMessageId(messageId);
weCustomerMessage.setMsgid(objectMapper.writeValueAsString(msgIds));
weCustomerMessageMapper.updateWeCustomerMessageMsgIdById(weCustomerMessage);
}
@Override
public void sendMessgae(CustomerMessagePushDto customerMessagePushDto, long messageId,List<WeCustomer> customers,List<WeGroup> groups) throws JsonProcessingException {
List<String> msgid = new ArrayList<>();
//发送群发消息
//发送类类型: 给单个客户发,群发
//发给客户
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_CUSTOMER)) {
WeCustomerMessagePushDto messagePushDto = new WeCustomerMessagePushDto();
messagePushDto.setChat_type(ChatType.of(customerMessagePushDto.getPushType()).getName());
List<String> externalUserIds = customers.stream().map(WeCustomer::getExternalUserid).collect(Collectors.toList());
messagePushDto.setExternal_userid(externalUserIds);
childMessage(messagePushDto, customerMessagePushDto);
SendMessageResultDto sendMessageResultDto = weCustomerMessagePushClient.sendCustomerMessageToUser(messagePushDto);
if (WeConstans.WE_SUCCESS_CODE.equals(sendMessageResultDto.getErrcode())) {
msgid.add(sendMessageResultDto.getMsgid());
}
}
//发给客户群
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_GROUP)) {
if (CollectionUtils.isNotEmpty(groups)) {
List<String> owners = groups.stream().map(WeGroup::getOwner).collect(Collectors.toList());
for (String owner : owners) {
WeCustomerMessagePushDto messagePushDto = new WeCustomerMessagePushDto();
messagePushDto.setChat_type(ChatType.of(customerMessagePushDto.getPushType()).getName());
//客户群的员工id
messagePushDto.setSender(owner);
childMessage(messagePushDto, customerMessagePushDto);
SendMessageResultDto sendMessageResultDto = weCustomerMessagePushClient.sendCustomerMessageToUser(messagePushDto);
if (WeConstans.WE_SUCCESS_CODE.equals(sendMessageResultDto.getErrcode())) {
//发送的msgId
msgid.add(sendMessageResultDto.getMsgid());
}
}
}
}
this.updateMsgId(messageId, msgid);
}
/**
* 子消息体
*
* @param weCustomerMessagePushDto 群发消息体
* @param customerMessagePushDto 群发消息
*/
public void childMessage(WeCustomerMessagePushDto weCustomerMessagePushDto, CustomerMessagePushDto customerMessagePushDto) {
List list = new ArrayList();
// 消息类型 0 文本消息 1 图片消息 2 链接消息 3 小程序消息
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.TEXT.getType())) {
weCustomerMessagePushDto.setText(customerMessagePushDto.getTextMessage());
}
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.IMAGE.getType())) {
ImageMessageDto imageMessage = customerMessagePushDto.getImageMessage();
try {
WeMediaDto weMediaDto = weMaterialService.uploadTemporaryMaterial(imageMessage.getPic_url(), GroupMessageType.IMAGE.getMessageType());
imageMessage.setMedia_id(weMediaDto.getMedia_id());
} catch (Exception e) {
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","image");
jsonObject.put("image",imageMessage);
list.add(jsonObject);
}
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.LINK.getType())) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","link");
jsonObject.put("link",customerMessagePushDto.getLinkMessage());
list.add(jsonObject);
}
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.MINIPROGRAM.getType())) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","miniprogram");
jsonObject.put("miniprogram",customerMessagePushDto.getMiniprogramMessage());
list.add(jsonObject);
}
if(customerMessagePushDto.getMessageType().equals(GroupMessageType.TEXT_IMAGE.getType())){
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","text_image");
jsonObject.put("text_image",customerMessagePushDto.getTextMessage());
list.add(jsonObject);
}
if(customerMessagePushDto.getMessageType().equals(GroupMessageType.VIDEO.getType())){
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype","video");
jsonObject.put("video",customerMessagePushDto.getVideoDto());
list.add(jsonObject);
}
weCustomerMessagePushDto.setAttachments(list);
}
}