WeChatContactMappingServiceImpl.java
11.3 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCustomerClient;
import com.linkwechat.wecom.client.WeCustomerGroupClient;
import com.linkwechat.wecom.client.WeMsgAuditClient;
import com.linkwechat.wecom.client.WeUserClient;
import com.linkwechat.wecom.domain.WeChatContactMapping;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.dto.WeUserDto;
import com.linkwechat.wecom.domain.dto.customer.CustomerGroupDetail;
import com.linkwechat.wecom.domain.dto.msgaudit.GroupChatVo;
import com.linkwechat.wecom.mapper.WeChatContactMappingMapper;
import com.linkwechat.wecom.mapper.WeCustomerMapper;
import com.linkwechat.wecom.mapper.WeUserMapper;
import com.linkwechat.wecom.service.IWeChatContactMappingService;
import com.linkwechat.wecom.service.IWeConversationArchiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* 聊天关系映射Service业务层处理
*
* @author ruoyi
* @date 2020-12-27
*/
@Slf4j
@Service
public class WeChatContactMappingServiceImpl extends ServiceImpl<WeChatContactMappingMapper, WeChatContactMapping> implements IWeChatContactMappingService {
@Autowired
private WeChatContactMappingMapper weChatContactMappingMapper;
@Autowired
private WeUserMapper weUserMapper;
@Autowired
private WeCustomerMapper weCustomerMapper;
@Autowired
private WeUserClient weUserClient;
@Autowired
private WeCustomerClient weCustomerClient;
@Autowired
private WeCustomerGroupClient weCustomerGroupClient;
@Autowired
private WeMsgAuditClient weMsgAuditClient;
@Autowired
private IWeConversationArchiveService weConversationArchiveService;
/**
* 查询聊天关系映射
*
* @param id 聊天关系映射ID
* @return 聊天关系映射
*/
@Override
public WeChatContactMapping selectWeChatContactMappingById(Long id) {
return weChatContactMappingMapper.selectWeChatContactMappingById(id);
}
/**
* 查询聊天关系映射列表
*
* @param weChatContactMapping 聊天关系映射
* @return 聊天关系映射
*/
@Override
public List<WeChatContactMapping> selectWeChatContactMappingList(WeChatContactMapping weChatContactMapping) {
List<WeChatContactMapping> weChatMappingList = weChatContactMappingMapper.selectWeChatContactMappingList(weChatContactMapping);
Optional.ofNullable(weChatMappingList).ifPresent(weChatMappingListVo -> {
weChatMappingListVo.stream().forEach(item -> {
if (StringUtils.isNotEmpty(item.getReceiveId())) {
if (WeConstans.ID_TYPE_USER.equals(item.getIsCustom())) {
//成员信息
item.setReceiveWeUser(weUserMapper.selectOne(new LambdaQueryWrapper<WeUser>().eq(WeUser::getUserId, item.getReceiveId())));
} else if (WeConstans.ID_TYPE_EX.equals(item.getIsCustom())) {
//获取外部联系人信息
item.setReceiveWeCustomer(weCustomerMapper.selectWeCustomerById(item.getReceiveId()));
} else if (WeConstans.ID_TYPE_MACHINE.equals(item.getIsCustom())) {
//拉去机器人信息暂不处理
}
item.setFinalChatContext(weConversationArchiveService.getFinalChatContactInfo(item.getFromId(), item.getReceiveId()));
} else if (StringUtils.isNotEmpty(item.getRoomId())) {
//获取群信息
CustomerGroupDetail customerGroupDetail = weCustomerGroupClient.groupChatDetail(new CustomerGroupDetail().new Params(item.getRoomId()));
GroupChatVo groupChatVo = new GroupChatVo();
BeanUtil.copyProperties(customerGroupDetail.getGroup_chat().get(0), groupChatVo);
item.setRoomInfo(groupChatVo);
item.setFinalChatContext(weConversationArchiveService.getFinalChatRoomContactInfo(item.getFromId(), item.getRoomId()));
}
});
});
return weChatMappingList;
}
/**
* 新增聊天关系映射
*
* @param weChatContactMapping 聊天关系映射
* @return 结果
*/
@Override
public int insertWeChatContactMapping(WeChatContactMapping weChatContactMapping) {
List<WeChatContactMapping> list = weChatContactMappingMapper.selectWeChatContactMappingList(weChatContactMapping);
if (CollectionUtil.isEmpty(list)) {
return weChatContactMappingMapper.insertWeChatContactMapping(weChatContactMapping);
}
return 0;
}
/**
* 修改聊天关系映射
*
* @param weChatContactMapping 聊天关系映射
* @return 结果
*/
@Override
public int updateWeChatContactMapping(WeChatContactMapping weChatContactMapping) {
return weChatContactMappingMapper.updateWeChatContactMapping(weChatContactMapping);
}
/**
* 批量删除聊天关系映射
*
* @param ids 需要删除的聊天关系映射ID
* @return 结果
*/
@Override
public int deleteWeChatContactMappingByIds(Long[] ids) {
return weChatContactMappingMapper.deleteWeChatContactMappingByIds(ids);
}
/**
* 删除聊天关系映射信息
*
* @param id 聊天关系映射ID
* @return 结果
*/
@Override
public int deleteWeChatContactMappingById(Long id) {
return weChatContactMappingMapper.deleteWeChatContactMappingById(id);
}
/**
* 需后期优化
*
* @param query
*/
@Override
public List<ElasticSearchEntity> saveWeChatContactMapping(List<JSONObject> query) {
List<ElasticSearchEntity> resultList = new ArrayList<>();
query.stream().filter(chatData -> StringUtils.isNotEmpty(chatData.getString("from"))).forEach(chatData -> {
ElasticSearchEntity elasticSearchEntity = new ElasticSearchEntity();
elasticSearchEntity.setId(chatData.getString("msgid"));
//发送人映射数据
WeChatContactMapping fromWeChatContactMapping = new WeChatContactMapping();
String fromId = chatData.getString("from");
//发送人类型
int fromType = StringUtils.weCustomTypeJudgment(fromId);
fromWeChatContactMapping.setFromId(fromId);
getUserOrCustomerInfo(chatData, fromId, fromType, "fromInfo");
JSONArray tolist = chatData.getJSONArray("tolist");
if (CollectionUtil.isNotEmpty(tolist)) {
//如果是单聊,tolist唯一,只有一个接收人,并判断接收人的类型
if (StringUtils.isEmpty(chatData.getString("roomid"))) {
String idStr = String.valueOf(tolist.get(0));
//接收人类型
int reveiceType = StringUtils.weCustomTypeJudgment(idStr);
fromWeChatContactMapping.setReceiveId(idStr);
//获取接收人信息
getUserOrCustomerInfo(chatData, idStr, reveiceType, "toListInfo");
fromWeChatContactMapping.setIsCustom(reveiceType);
} else {
fromWeChatContactMapping.setRoomId(chatData.getString("roomid"));
CustomerGroupDetail customerGroupDetail = weCustomerGroupClient
.groupChatDetail(new CustomerGroupDetail().new Params(chatData.getString("roomid")));
GroupChatVo groupChatVo = new GroupChatVo();
BeanUtil.copyProperties(customerGroupDetail.getGroup_chat().get(0), groupChatVo);
chatData.put("roomInfo",JSONObject.parse(JSONObject.toJSONString(groupChatVo)));
}
}
//接收人映射数据
WeChatContactMapping reveiceWeChatContactMapping = from2ReveiceData(fromType, fromWeChatContactMapping);
insertWeChatContactMapping(fromWeChatContactMapping);
insertWeChatContactMapping(reveiceWeChatContactMapping);
elasticSearchEntity.setData(chatData);
resultList.add(elasticSearchEntity);
});
return resultList;
}
private void getUserOrCustomerInfo(JSONObject chatData, String fromId, int fromType, String key) {
//获取发送人信息
if (WeConstans.ID_TYPE_USER.equals(fromType)) {
//成员信息
WeUser weUser = weUserMapper.selectOne(new LambdaQueryWrapper<WeUser>().eq(WeUser::getUserId, fromId));
chatData.put(key, JSONObject.parse(JSONObject.toJSONString(weUser)));
} else if (WeConstans.ID_TYPE_EX.equals(fromType)) {
//获取外部联系人信息
WeCustomer weCustomer = weCustomerMapper.selectWeCustomerById(fromId);
chatData.put(key,JSONObject.parse(JSONObject.toJSONString(weCustomer)));
} else if (WeConstans.ID_TYPE_MACHINE.equals(fromType)) {
//拉去机器人信息暂不处理
}
}
@Override
public PageInfo<WeCustomer> listByCustomer() {
LambdaQueryWrapper<WeChatContactMapping> lambdaQueryWrapper = new LambdaQueryWrapper<WeChatContactMapping>()
.eq(WeChatContactMapping::getIsCustom, WeConstans.ID_TYPE_EX);
List<WeChatContactMapping> weChatContactMappingList = this.baseMapper.selectList(lambdaQueryWrapper);
List<WeCustomer> resultList = Optional.ofNullable(weChatContactMappingList).orElseGet(ArrayList::new)
.stream().map(item -> weCustomerMapper.selectWeCustomerById(item.getReceiveId())).collect(Collectors.toList());
PageInfo<WeCustomer> weCustomerPageInfo = new PageInfo<>(resultList);
weCustomerPageInfo.setTotal(new PageInfo<>(weChatContactMappingList).getTotal());
return weCustomerPageInfo;
}
/**
* 发送人转接收人
*
* @param fromType
* @param fromWeChatContactMapping
* @return
*/
private WeChatContactMapping from2ReveiceData(int fromType, WeChatContactMapping fromWeChatContactMapping) {
//接收人映射数据
WeChatContactMapping reveiceWeChatContactMapping = new WeChatContactMapping();
if (StringUtils.isNotEmpty(fromWeChatContactMapping.getRoomId())) {
reveiceWeChatContactMapping.setFromId(fromWeChatContactMapping.getRoomId());
} else {
reveiceWeChatContactMapping.setFromId(fromWeChatContactMapping.getReceiveId());
}
reveiceWeChatContactMapping.setReceiveId(fromWeChatContactMapping.getFromId());
reveiceWeChatContactMapping.setIsCustom(fromType);
return reveiceWeChatContactMapping;
}
}