WelcomeService.java
8.54 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
package com.linkwechat.wecom.service.welcome;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.enums.MessageType;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCustomerClient;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeQrAttachments;
import com.linkwechat.wecom.domain.WeQrCode;
import com.linkwechat.wecom.domain.WeTag;
import com.linkwechat.wecom.domain.dto.WeEmpleCodeDto;
import com.linkwechat.wecom.domain.dto.WeMediaDto;
import com.linkwechat.wecom.domain.dto.WeResultDto;
import com.linkwechat.wecom.domain.dto.WeWelcomeMsg;
import com.linkwechat.wecom.domain.query.WeCustomerWelcomeQuery;
import com.linkwechat.wecom.domain.vo.WeCommunityWeComeMsgVo;
import com.linkwechat.wecom.domain.vo.WeMakeCustomerTag;
import com.linkwechat.wecom.domain.vo.qr.WeQrCodeDetailVo;
import com.linkwechat.wecom.domain.vo.tag.WeTagVo;
import com.linkwechat.wecom.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author danmo
* @description 发送欢迎语业务
* @date 2021/11/13 18:51
**/
@Service
@Slf4j
public class WelcomeService implements ApplicationListener<WeCustomerWelcomeQuery> {
@Autowired
private IWeQrCodeService weQrCodeService;
@Autowired
private IWeCommunityNewGroupService weCommunityNewGroupService;
@Autowired
private IWeCustomerService weCustomerService;
@Autowired
private WeCustomerClient weCustomerClient;
@Autowired
private IWeMaterialService weMaterialService;
@Value("${wecome.welcome-msg-default}")
private String welcomMsgDefault;
@Override
@Async
public void onApplicationEvent(WeCustomerWelcomeQuery query) {
log.info("开始发送欢迎语:query:{}", JSONObject.toJSONString(query));
List<WeQrAttachments> qrAttachments = new ArrayList<>();
if(StringUtils.isNotEmpty(query.getState()) && query.getState().startsWith(WeConstans.WE_QR_CODE_PREFIX)){
WeQrCode weQrCode = weQrCodeService.getOne(new LambdaQueryWrapper<WeQrCode>()
.eq(WeQrCode::getState, query.getState())
.eq(WeQrCode::getDelFlag, 0).last("limit 1"));
if (weQrCode != null) {
WeQrCodeDetailVo qrDetail = weQrCodeService.getQrDetail(weQrCode.getId());
qrAttachments.addAll(qrDetail.getQrAttachments());
makeCustomerTag(query.getExternalUserId(), query.getUserId(), qrDetail.getQrTags());
} else {
log.warn("未查询到对应员工活码信息");
}
}else if(StringUtils.isNotEmpty(query.getState()) && query.getState().startsWith(WeConstans.WE_QR_XKLQ_PREFIX)){
WeCommunityWeComeMsgVo welcomeMsgByState = weCommunityNewGroupService.getWelcomeMsgByState(query.getState());
if(welcomeMsgByState != null){
WeQrAttachments textAtt = new WeQrAttachments();
textAtt.setMsgType(MessageType.TEXT.getMessageType());
textAtt.setContent(welcomeMsgByState.getWelcomeMsg());
qrAttachments.add(textAtt);
WeQrAttachments imageAtt = new WeQrAttachments();
imageAtt.setMsgType(MessageType.IMAGE.getMessageType());
imageAtt.setMediaId(welcomeMsgByState.getCodeUrl());
qrAttachments.add(imageAtt);
makeCustomerTag(query.getExternalUserId(), query.getUserId(), welcomeMsgByState.getTagList());
}
}
WeResultDto resultDto = sendWelcomeMsg(query, qrAttachments);
log.info("结束发送欢迎语:result:{}", JSONObject.toJSONString(resultDto));
}
/**
* 客户打标签
*
* @param externaUserId 客户id
* @param userId 员工id
* @param qrTags 标签id
*/
private void makeCustomerTag(String externaUserId, String userId, List<WeTagVo> qrTags) {
if (CollectionUtil.isNotEmpty(qrTags)) {
List<WeTag> weTagList = qrTags.stream().map(tag -> {
WeTag weTag = new WeTag();
weTag.setName(tag.getTagName());
weTag.setTagId(tag.getTagId());
return weTag;
}).collect(Collectors.toList());
WeMakeCustomerTag makeCustomerTag = new WeMakeCustomerTag();
makeCustomerTag.setExternalUserid(externaUserId);
makeCustomerTag.setUserId(userId);
makeCustomerTag.setAddTag(weTagList);
try {
weCustomerService.makeLabel(makeCustomerTag);
} catch (Exception e) {
log.info("发送欢迎语客户打标签失败 ex:{}", e);
}
}
}
/**
* 发送欢迎语
*
* @param query 客户信息
* @param qrAttachments 素材
* @return
*/
private WeResultDto sendWelcomeMsg(WeCustomerWelcomeQuery query, List<WeQrAttachments> qrAttachments) {
String welcomText = welcomMsgDefault;
WeWelcomeMsg welcomeMsg = WeWelcomeMsg.builder().welcome_code(query.getWelcomeCode()).build();
if(CollectionUtil.isNotEmpty(qrAttachments)){
WeQrAttachments weQrAttachments = qrAttachments.stream()
.filter(qrAttachment -> ObjectUtil.equal(MessageType.TEXT.getMessageType(), qrAttachment.getMsgType()))
.findFirst().orElseGet(null);
if(weQrAttachments != null){
welcomText = weQrAttachments.getContent();
}
getMediaId(qrAttachments);
welcomeMsg.setAttachments(qrAttachments);
}
WeCustomer weCustomer = weCustomerService.getOne(new LambdaQueryWrapper<WeCustomer>().eq(WeCustomer::getFirstUserId,query.getUserId())
.eq(WeCustomer::getExternalUserid,query.getExternalUserId()).eq(WeCustomer::getDelFlag,0).last("limit 1"));
if(weCustomer != null){
String customerName = weCustomer.getCustomerName();
welcomText = welcomText.replaceAll("#客户昵称#",customerName);
}
welcomeMsg.setText(WeWelcomeMsg.Text.builder().content(welcomText).build());
return weCustomerClient.sendWelcomeMsg(welcomeMsg);
}
void getMediaId(List<WeQrAttachments> messageTemplates) {
Optional.ofNullable(messageTemplates).orElseGet(ArrayList::new).forEach(messageTemplate -> {
if (ObjectUtil.equal(MessageType.IMAGE.getMessageType(), messageTemplate.getMsgType())) {
WeMediaDto weMedia = weMaterialService.uploadTemporaryMaterial(messageTemplate.getMediaId()
, MessageType.IMAGE.getMessageType()
, FileUtil.getName(messageTemplate.getMediaId()));
messageTemplate.setMediaId(weMedia.getMedia_id());
} else if (ObjectUtil.equal(MessageType.MINIPROGRAM.getMessageType(), messageTemplate.getMsgType())) {
WeMediaDto weMedia = weMaterialService.uploadTemporaryMaterial(messageTemplate.getMediaId()
, MessageType.IMAGE.getMessageType()
, FileUtil.getName(messageTemplate.getMediaId()));
messageTemplate.setMediaId(weMedia.getMedia_id());
} else if (ObjectUtil.equal(MessageType.VIDEO.getMessageType(), messageTemplate.getMsgType())) {
WeMediaDto weMedia = weMaterialService.uploadTemporaryMaterial(messageTemplate.getMediaId()
, MessageType.IMAGE.getMessageType()
, FileUtil.getName(messageTemplate.getMediaId()));
messageTemplate.setMediaId(weMedia.getMedia_id());
} else if (ObjectUtil.equal(MessageType.FILE.getMessageType(), messageTemplate.getMsgType())) {
WeMediaDto weMedia = weMaterialService.uploadTemporaryMaterial(messageTemplate.getMediaId()
, MessageType.IMAGE.getMessageType()
, FileUtil.getName(messageTemplate.getMediaId()));
messageTemplate.setMediaId(weMedia.getMedia_id());
}
});
}
}