WeGroupServiceImpl.java
6.82 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
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.linkwechat.common.constant.WeConstans;
import com.linkwechat.wecom.client.WeCustomerGroupClient;
import com.linkwechat.wecom.client.WeUserClient;
import com.linkwechat.wecom.domain.WeAllocateGroup;
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.*;
import org.springframework.beans.factory.annotation.Autowired;
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.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 IWeUserService weUserService;
@Autowired
private IWeCustomerService weCustomerService;
@Autowired
private IWeAllocateGroupService iWeAllocateGroupService;
@Autowired
private WeUserClient weUserClient;
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
@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.getGroup_chat_list())){
List<WeGroup> weGroups=new ArrayList<>();
List<WeGroupMember> weGroupMembers=new ArrayList<>();
customerGroupList.getGroup_chat_list().stream().forEach(k->{
CustomerGroupDetail customerGroupDetail = weCustomerGroupClient.groupChatDetail(
new CustomerGroupDetail().new Params(k.getChat_id())
);
if(customerGroupDetail.getErrcode().equals(WeConstans.WE_SUCCESS_CODE)
&& CollectionUtil.isNotEmpty(customerGroupDetail.getGroup_chat())){
customerGroupDetail.getGroup_chat().stream().forEach(kk->{
weGroups.add(
WeGroup.builder()
.chatId(kk.getChat_id())
.groupName(kk.getName())
.notice(kk.getNotice())
.owner(kk.getOwner())
.createTime(new Date(kk.getCreate_time() * 1000L))
.status(k.getStatus())
.build()
);
List<CustomerGroupMember> memberLists = kk.getMember_list();
if(CollectionUtil.isNotEmpty(memberLists)){
memberLists.stream().forEach(member->{
weGroupMembers.add(
WeGroupMember.builder()
.chatId(kk.getChat_id())
.userId(member.getUserid())
.joinTime(new Date(member.getJoin_time() * 1000L))
.joinScene(member.getJoin_scene())
.joinType(member.getType())
.unionId(member.getUnionid())
.build()
);
});
}
});
}
});
//同步组织架构成员
weUserService.synchWeUser();
//同步客户
weCustomerService.synchWeCustomer();
this.saveOrUpdateBatch(weGroups);
iWeGroupMemberService.remove(new LambdaQueryWrapper<WeGroupMember>().in(WeGroupMember::getChatId,
weGroups.stream().map(WeGroup::getChatId).collect(Collectors.toList())));
iWeGroupMemberService.saveOrUpdateBatch(weGroupMembers);
}
}
}