WeChatContactMappingServiceImpl.java
7.45 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
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.WeChatContactMapping;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeUser;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* 聊天关系映射Service业务层处理
*
* @author ruoyi
* @date 2020-12-27
*/
@Slf4j
@Service
public class WeChatContactMappingServiceImpl implements IWeChatContactMappingService {
@Autowired
private WeChatContactMappingMapper weChatContactMappingMapper;
@Autowired
private WeUserMapper weUserMapper;
@Autowired
private WeCustomerMapper weCustomerMapper;
/**
* 查询聊天关系映射
*
* @param id 聊天关系映射ID
* @return 聊天关系映射
*/
@Override
public WeChatContactMapping selectWeChatContactMappingById(Long id) {
return weChatContactMappingMapper.selectWeChatContactMappingById(id);
}
/**
* 查询聊天关系映射列表
*
* @param weChatContactMapping 聊天关系映射
* @return 聊天关系映射
*/
@Override
public List<WeChatContactMapping> selectWeChatContactMappingList(WeChatContactMapping weChatContactMapping) {
return weChatContactMappingMapper.selectWeChatContactMappingList(weChatContactMapping);
}
/**
* 新增聊天关系映射
*
* @param weChatContactMapping 聊天关系映射
* @return 结果
*/
@Override
public int insertWeChatContactMapping(WeChatContactMapping weChatContactMapping) {
return weChatContactMappingMapper.insertWeChatContactMapping(weChatContactMapping);
}
/**
* 修改聊天关系映射
*
* @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);
}
@Override
public void saveWeChatContactMapping(List<ElasticSearchEntity> query) {
/*List<String> toListIds = query.stream().map(chatData -> JSONObject.parseObject(JSONObject.toJSONString(chatData.getData())))
.filter(chatData -> CollectionUtil.isNotEmpty(chatData.getJSONArray("tolist")))
.map(chatData -> chatData.getJSONArray("tolist"))
.map(item -> JSONArray.parseArray(item.toJSONString(), String.class))
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toCollection(ArrayList::new));*/
query.stream().forEach(elasticSearchEntity -> {
//发送人映射数据
WeChatContactMapping fromWeChatContactMapping = new WeChatContactMapping();
//接收人映射数据
WeChatContactMapping reveiceWeChatContactMapping = new WeChatContactMapping();
JSONObject chatData = JSONObject.parseObject(JSONObject.toJSONString(elasticSearchEntity.getData()));
int fromType = StringUtils.weCustomTypeJudgment(chatData.getString("from"));
fromWeChatContactMapping.setFromId(chatData.getString("from"));
getFromUserOrCustomInfo(fromWeChatContactMapping, chatData.getString("from"), fromType);
getReveiceUserOrCustomInfo(reveiceWeChatContactMapping, chatData.getString("from"), fromType);
JSONArray tolist = chatData.getJSONArray("tolist");
if (CollectionUtil.isNotEmpty(tolist)) {
//如果是单聊,tolist唯一,只有一个接收人,并判断接收人的类型
if (StringUtils.isEmpty(chatData.getString("roomid"))) {
String idStr = (String) tolist.get(0);
int reveiceType = StringUtils.weCustomTypeJudgment(idStr);
getFromUserOrCustomInfo(reveiceWeChatContactMapping, idStr, reveiceType);
getReveiceUserOrCustomInfo(fromWeChatContactMapping, idStr, fromType);
}else {
fromWeChatContactMapping.setRoomId(chatData.getString("roomid"));
}
}
});
}
/**
* 获取发送人成员或者客户详情
*
* @param id
* @return
*/
private WeChatContactMapping getFromUserOrCustomInfo(WeChatContactMapping weChatContactMapping, String id, int type) {
if (WeConstans.ID_TYPE_USER.equals(type)) {
WeUser weUser = weUserMapper.selectOne(new LambdaQueryWrapper<WeUser>().eq(WeUser::getUserId, id));
weChatContactMapping.setFromAvatar(weUser.getAvatarMediaid());
weChatContactMapping.setFromName(weUser.getName());
weChatContactMapping.setFromGender(weUser.getGender());
} else if (WeConstans.ID_TYPE_EX.equals(type)) {
WeCustomer weCustomer = weCustomerMapper.selectWeCustomerById(id);
weChatContactMapping.setFromAvatar(weCustomer.getAvatar());
weChatContactMapping.setFromName(weCustomer.getName());
weChatContactMapping.setFromGender(weCustomer.getGender());
} else if (WeConstans.ID_TYPE_MACHINE.equals(type)) {
}
return weChatContactMapping;
}
/**
* 获取接收人成员或者客户详情
*
* @param id
* @return
*/
private WeChatContactMapping getReveiceUserOrCustomInfo(WeChatContactMapping weChatContactMapping, String id, int type) {
if (WeConstans.ID_TYPE_USER.equals(type)) {
WeUser weUser = weUserMapper.selectOne(new LambdaQueryWrapper<WeUser>().eq(WeUser::getUserId, id));
weChatContactMapping.setReviceAvatar(weUser.getAvatarMediaid());
weChatContactMapping.setReviceName(weUser.getName());
weChatContactMapping.setReviceGender(weUser.getGender());
} else if (WeConstans.ID_TYPE_EX.equals(type)) {
WeCustomer weCustomer = weCustomerMapper.selectWeCustomerById(id);
weChatContactMapping.setReviceAvatar(weCustomer.getAvatar());
weChatContactMapping.setReviceName(weCustomer.getName());
weChatContactMapping.setReviceGender(weCustomer.getGender());
} else if (WeConstans.ID_TYPE_MACHINE.equals(type)) {
}
return weChatContactMapping;
}
}