WeGroupServiceImpl.java
14.5 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCustomerClient;
import com.linkwechat.wecom.client.WeCustomerGroupClient;
import com.linkwechat.wecom.client.WeUserClient;
import com.linkwechat.wecom.domain.WeAllocateGroup;
import com.linkwechat.wecom.domain.WeCustomerAddGroup;
import com.linkwechat.wecom.domain.WeGroup;
import com.linkwechat.wecom.domain.WeGroupMember;
import com.linkwechat.wecom.domain.dto.AllocateWeGroupDto;
import com.linkwechat.wecom.domain.dto.customer.CustomerGroupDetail;
import com.linkwechat.wecom.domain.dto.customer.CustomerGroupList;
import com.linkwechat.wecom.domain.dto.customer.CustomerGroupMember;
import com.linkwechat.wecom.domain.vo.WeLeaveUserInfoAllocateVo;
import com.linkwechat.wecom.mapper.WeGroupMapper;
import com.linkwechat.wecom.service.IWeAllocateGroupService;
import com.linkwechat.wecom.service.IWeGroupMemberService;
import com.linkwechat.wecom.service.IWeGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @Author: Robin
* @Description:
* @Date: create in 2020/9/22 0022 0:03
*/
@Service
public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper, WeGroup> implements IWeGroupService {
@Autowired
private WeCustomerGroupClient weCustomerGroupClient;
@Autowired
private IWeGroupMemberService iWeGroupMemberService;
@Autowired
private IWeAllocateGroupService iWeAllocateGroupService;
@Autowired
private WeUserClient weUserClient;
@Autowired
private WeCustomerClient weCustomerClient;
@Override
public List<WeGroup> selectWeGroupList(WeGroup weGroup) {
return this.baseMapper.selectWeGroupList(weGroup);
}
/**
* 离职员工群分配
*
* @param weLeaveUserInfoAllocateVo
* @return
*/
@Override
@Transactional
public void allocateWeGroup(WeLeaveUserInfoAllocateVo weLeaveUserInfoAllocateVo) {
List<WeGroup> weGroups
= this.list(new LambdaQueryWrapper<WeGroup>().eq(WeGroup::getOwner, weLeaveUserInfoAllocateVo.getHandoverUserid()));
if (CollectionUtil.isNotEmpty(weGroups)) {
List<WeAllocateGroup> weAllocateGroups = new ArrayList<>();
//更改本地群主
weGroups.stream().forEach(k -> {
k.setOwner(weLeaveUserInfoAllocateVo.getTakeoverUserid());
weAllocateGroups.add(WeAllocateGroup.builder()
.allocateTime(new Date())
.chatId(k.getChatId())
.newOwner(weLeaveUserInfoAllocateVo.getTakeoverUserid())
.oldOwner(weLeaveUserInfoAllocateVo.getHandoverUserid())
.build());
});
this.saveOrUpdateBatch(weGroups);
//分配记录保存
if (CollectionUtil.isNotEmpty(weAllocateGroups)) {
if (iWeAllocateGroupService.saveOrUpdateBatch(weAllocateGroups)) {
// //同步企业微信端
weUserClient.allocateGroup(
AllocateWeGroupDto.builder()
.chat_id_list(ArrayUtil.toArray(weAllocateGroups.stream().map(WeAllocateGroup::getChatId).collect(Collectors.toList()), String.class))
.new_owner(weLeaveUserInfoAllocateVo.getTakeoverUserid())
.build()
);
}
}
}
}
/**
* 客户群同步
*/
@Override
@Async
@Transactional(rollbackFor = Exception.class)
public void synchWeGroup() {
CustomerGroupList customerGroupList =
weCustomerGroupClient.groupChatLists(new CustomerGroupList().new Params());
if (customerGroupList.getErrcode().equals(WeConstans.WE_SUCCESS_CODE)
&& CollectionUtil.isNotEmpty(customerGroupList.getGroupChatList())) {
List<WeGroup> weGroups = new ArrayList<>();
List<WeGroupMember> weGroupMembers = new ArrayList<>();
customerGroupList.getGroupChatList().stream().forEach(k -> {
CustomerGroupDetail customerGroupDetail = weCustomerGroupClient.groupChatDetail(
new CustomerGroupDetail().new Params(k.getChatId(), 1)
);
if (customerGroupDetail.getErrcode().equals(WeConstans.WE_SUCCESS_CODE)
&& CollectionUtil.isNotEmpty(customerGroupDetail.getGroupChat())) {
customerGroupDetail.getGroupChat().stream().forEach(kk -> {
weGroups.add(
WeGroup.builder()
.chatId(kk.getChatId())
.groupName(StringUtils.isNotEmpty(kk.getName())? kk.getName() : "@微信群")
.notice(kk.getNotice())
.owner(kk.getOwner())
.addTime(new Date(kk.getCreateTime() * 1000L))
.status(k.getStatus())
.delFlag(new Integer(0))
.adminUserId(Optional.ofNullable(kk.getAdminList()).orElseGet(ArrayList::new).stream().map(admin -> admin.getString("userid")).collect(Collectors.joining(",")))
.build()
);
List<CustomerGroupMember> memberLists = kk.getMemberList();
if (CollectionUtil.isNotEmpty(memberLists)) {
memberLists.stream().forEach(member -> {
weGroupMembers.add(
WeGroupMember.builder()
.chatId(kk.getChatId())
.userId(member.getUserId())
.joinTime(new Date(member.getJoinTime() * 1000L))
.joinScene(member.getJoinScene())
.type(member.getType())
.unionId(member.getUnionId())
.groupNickName(member.getGroupNickName())
.name(member.getName())
.delFlag(new Integer(0))
.invitorUserId(member.getInvitor() == null ? null : member.getInvitor().getString("userid"))
.build()
);
});
}
});
}
});
insertBatchGroupAndMember(weGroups, weGroupMembers);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void createWeGroup(String chatId) {
List<WeGroup> weGroups = new ArrayList<>();
List<WeGroupMember> weGroupMembers = new ArrayList<>();
CustomerGroupDetail customerGroupDetail = weCustomerGroupClient.groupChatDetail(
new CustomerGroupDetail().new Params(chatId, 1)
);
if (CollectionUtil.isNotEmpty(customerGroupDetail.getGroupChat())) {
customerGroupDetail.getGroupChat().stream().forEach(kk -> {
weGroups.add(
WeGroup.builder()
.chatId(kk.getChatId())
.groupName(kk.getName())
.notice(kk.getNotice())
.owner(kk.getOwner())
.addTime(new Date(kk.getCreateTime() * 1000L))
.adminUserId(Optional.ofNullable(kk.getAdminList()).orElseGet(ArrayList::new).stream().map(admin -> admin.getString("userid")).collect(Collectors.joining(",")))
.build()
);
List<CustomerGroupMember> memberLists = kk.getMemberList();
if (CollectionUtil.isNotEmpty(memberLists)) {
memberLists.stream().forEach(member -> {
weGroupMembers.add(
WeGroupMember.builder()
.chatId(kk.getChatId())
.userId(member.getUserId())
.joinTime(new Date(member.getJoinTime() * 1000L))
.joinScene(member.getJoinScene())
.type(member.getType())
.unionId(member.getUnionId())
.groupNickName(member.getGroupNickName())
.name(member.getName())
.invitorUserId(member.getInvitor() == null ? null : member.getInvitor().getString("userid"))
.build()
);
});
}
});
insertBatchGroupAndMember(weGroups, weGroupMembers);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateWeGroup(String chatId) {
List<WeGroup> weGroups = new ArrayList<>();
List<WeGroupMember> weGroupMembers = new ArrayList<>();
CustomerGroupDetail customerGroupDetail = weCustomerGroupClient.groupChatDetail(
new CustomerGroupDetail().new Params(chatId, 1)
);
if (CollectionUtil.isNotEmpty(customerGroupDetail.getGroupChat())) {
customerGroupDetail.getGroupChat().stream().forEach(kk -> {
weGroups.add(
WeGroup.builder()
.chatId(kk.getChatId())
.groupName(kk.getName())
.notice(kk.getNotice())
.owner(kk.getOwner())
.addTime(new Date(kk.getCreateTime() * 1000L))
.adminUserId(Optional.ofNullable(kk.getAdminList()).orElseGet(ArrayList::new).stream().map(admin -> admin.getString("userid")).collect(Collectors.joining(",")))
.build()
);
List<CustomerGroupMember> memberLists = kk.getMemberList();
if (CollectionUtil.isNotEmpty(memberLists)) {
memberLists.stream().forEach(member -> {
weGroupMembers.add(
WeGroupMember.builder()
.chatId(kk.getChatId())
.userId(member.getUserId())
.joinTime(new Date(member.getJoinTime() * 1000L))
.joinScene(member.getJoinScene())
.type(member.getType())
.unionId(member.getUnionId())
.groupNickName(member.getGroupNickName())
.name(member.getName())
.invitorUserId(member.getInvitor() == null ? null : member.getInvitor().getString("userid"))
.build()
);
});
}
});
insertBatchGroupAndMember(weGroups, weGroupMembers);
}
}
/**
* 批量添加
*
* @param weGroups 客户群
* @param weGroupMembers 群成员
*/
private void insertBatchGroupAndMember(List<WeGroup> weGroups, List<WeGroupMember> weGroupMembers) {
if (CollectionUtil.isNotEmpty(weGroups)) {
List<List<WeGroup>> lists = Lists.partition(weGroups, 500);
for (List<WeGroup> groupList : lists) {
this.baseMapper.insertBatch(groupList);
this.remove(new LambdaQueryWrapper<WeGroup>()
.notIn(WeGroup::getChatId, groupList.stream().map(WeGroup::getChatId).collect(Collectors.toList())));
}
}else{
List<WeGroup> oldWeGroups = this.list();
if(CollectionUtil.isNotEmpty(oldWeGroups)){
this.removeByIds(
oldWeGroups.stream().map(WeGroup::getChatId).collect(Collectors.toList())
);
}
//无群无人
List<WeGroupMember> oldWeGroupMembers = iWeGroupMemberService.list();
if(CollectionUtil.isNotEmpty(oldWeGroupMembers)){
iWeGroupMemberService.removeByIds(
oldWeGroupMembers.stream().map(WeGroupMember::getId).collect(Collectors.toList())
);
}
}
if (CollectionUtil.isNotEmpty(weGroupMembers)) {
List<List<WeGroupMember>> lists = Lists.partition(weGroupMembers, 500);
for (List<WeGroupMember> groupMemberList : lists) {
iWeGroupMemberService.remove(
new LambdaQueryWrapper<WeGroupMember>()
.notIn(WeGroupMember::getUserId,groupMemberList.stream().map(WeGroupMember::getUserId).collect(Collectors.toList()))
);
iWeGroupMemberService.insertBatch(groupMemberList);
}
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteWeGroup(String chatId) {
this.baseMapper.delete(new LambdaQueryWrapper<WeGroup>().eq(WeGroup::getChatId, chatId));
iWeGroupMemberService.remove(new LambdaQueryWrapper<WeGroupMember>().eq(WeGroupMember::getChatId, chatId));
}
@Override
public List<WeCustomerAddGroup> findWeGroupByCustomer(String userId, String externalUserid) {
return this.baseMapper.findWeGroupByCustomer(userId, externalUserid);
}
}