WelcomeService.java
6.72 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
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.enums.MessageType;
import com.linkwechat.wecom.client.WeCustomerClient;
import com.linkwechat.wecom.domain.WeQrAttachments;
import com.linkwechat.wecom.domain.WeQrCode;
import com.linkwechat.wecom.domain.WeTag;
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.WeMakeCustomerTag;
import com.linkwechat.wecom.domain.vo.qr.WeQrCodeDetailVo;
import com.linkwechat.wecom.domain.vo.tag.WeTagVo;
import com.linkwechat.wecom.service.IWeCustomerService;
import com.linkwechat.wecom.service.IWeMaterialService;
import com.linkwechat.wecom.service.IWeQrCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 IWeCustomerService weCustomerService;
@Autowired
private WeCustomerClient weCustomerClient;
@Autowired
private IWeMaterialService weMaterialService;
@Override
@Async
public void onApplicationEvent(WeCustomerWelcomeQuery query) {
log.info("开始发送欢迎语:query:{}", JSONObject.toJSONString(query));
WeQrCode weQrCode = weQrCodeService.getOne(new LambdaQueryWrapper<WeQrCode>()
.eq(WeQrCode::getState, query.getState())
.eq(WeQrCode::getDelFlag, 0).last("limit 1"));
List<WeQrAttachments> qrAttachments = new ArrayList<>();
if (weQrCode != null) {
WeQrCodeDetailVo qrDetail = weQrCodeService.getQrDetail(weQrCode.getId());
qrAttachments.addAll(qrDetail.getQrAttachments());
makeCustomerTag(query.getExternalUserId(), query.getUserId(), qrDetail.getQrTags());
} else {
log.info("未查询到对应活码信息");
}
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) {
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){
welcomeMsg.setText(WeWelcomeMsg.Text.builder().content(weQrAttachments.getContent()).build());
}
getMediaId(qrAttachments);
welcomeMsg.setAttachments(qrAttachments);
}else {
welcomeMsg.setText(WeWelcomeMsg.Text.builder().content("默认欢迎语").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());
}
});
}
}