GHSyncData.java
20.4 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package cn.com.glsx.entmicro.service;
import cn.com.glsx.entmicro.entity.EntmicroAuthorize;
import cn.com.glsx.entmicro.entity.EntmicroDepartment;
import cn.com.glsx.entmicro.entity.EntmicroStaff;
import cn.com.glsx.entmicro.mapper.*;
import cn.com.glsx.entmicro.model.request.*;
import cn.com.glsx.entmicro.model.response.CustomerInfoResponse;
import cn.com.glsx.entmicro.model.response.CustomerListResponse;
import cn.com.glsx.entmicro.model.response.EntmicroStaffResponse;
import cn.com.glsx.entmicro.service.async.AsyncCustomerService;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.RpcAssert;
import cn.com.glsx.scrm.wechat.dto.workwechat.external.request.GetWorkWechatExternalContactRequestDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.external.response.WorkWechatExternalContactDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.external.response.WorkWechatExternalContactFollowUserDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.external.response.WorkWechatExternalContactFollowUserTagDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.external.response.WorkWechatExternalContactResponseDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.request.GetWorkWechatUserListRequestDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.request.GetWorkWechatUserRequestDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.response.WorkWechatUserListResponseDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.user.response.WorkWechatUserResponseDTO;
import cn.com.glsx.scrm.wechat.remote.WorkWechatExternalContactRemote;
import cn.com.glsx.scrm.wechat.remote.WorkWechatUserRemote;
import cn.com.glsx.wika.dto.StaffInfoDTO;
import cn.com.glsx.wika.dto.WeChatContactsDTO;
import cn.com.glsx.wika.model.request.record.GetVikaRecordRequestDTO;
import cn.com.glsx.wika.model.response.record.VikaRecordBaseResponseDTO;
import cn.com.glsx.wika.remote.VikaRecordRemote;
import cn.com.glsx.wika.remote.VikaWeChatContactsRemote;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.glsx.plat.common.utils.StringUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.oreframework.distribute.lock.annotation.GlsxDistributeLockable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
* @author zhouhaibao
* @version 1.0
* @description: TODO
* @date 2022/1/21 4:12
*/
@Slf4j
@Component
public class GHSyncData {
@Autowired
private EntmicroDepartmentPathService entmicroDepartmentPathService;
@Autowired
private EntmicroStaffService entmicroStaffService;
@Autowired
private SyncDataService syncDataService;
@Autowired
private CustomerBaseService customerBaseService;
@Autowired
private EntmicroDepartmentMapper entmicroDepartmentMapper;
@Autowired
private EntmicroStaffMapper entmicroStaffMapper;
@Autowired
private MerchantService merchantService;
@Autowired
private Environment environment;
@Autowired
private EntmicroAuthorizeMapper entmicroAuthorizeMapper;
@Autowired
private EntmicroDepartmentPathMapper entmicroDepartmentPathMapper;
@Autowired
private VikaWeChatContactsRemote vikaWeChatContactsRemote;
@Autowired
private WorkWechatUserRemote workWechatUserRemote;
@Resource
private VikaRecordRemote vikaRecordRemote;
@Resource
private AsyncCustomerService asyncCustomerService;
@Resource
private CustomerService customerService;
@Resource
private RopCommonServie ropCommonServie;
@Resource
private WorkWechatExternalContactRemote workWechatExternalContactRemote;
@Resource
private CustomerMapper customerMapper;
/**
* 同步企微数据
*
* @param mechantId 商户id
* @param departmentId 部门id
* @param staffMerchantId 员工所属id
* @param entmicroAuthorizeRequest
* @param syncUpdateCustomer
*/
@Async("taskExecutor")
public void syncMerchantStaff(int mechantId, int departmentId, int staffMerchantId, EntmicroAuthorizeRequest entmicroAuthorizeRequest, Boolean syncUpdateCustomer) {
// TODO 更新关联关系
entmicroAuthorizeMapper.insertUseGeneratedKeys(new EntmicroAuthorize(entmicroAuthorizeRequest.getMerchantId(), entmicroAuthorizeRequest.getEntmicroName(), null));
updateMerchantBind(departmentId, staffMerchantId);
List<Integer> subordinateDep = entmicroDepartmentPathService.findSubordinateDep(departmentId);
EntmicroDepartment entmicroDepartment = entmicroDepartmentMapper.selectByPrimaryKey(departmentId);
log.info("开始同步,部门id{}", entmicroDepartment);
// 2024-01-15 屏蔽商户号控制,之前只针对广汇使用,当前所有集团可以使用
List<EntmicroStaff> entmicroStaffList = entmicroStaffService.syncEntmicroStaff(mechantId, true, entmicroDepartment.getDepartmentId(), true);
// 同步商户下面的员工的客户和客户群
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(entmicroStaffList)) {
log.info("员工同步完毕{}", entmicroStaffList.size());
List<String> userIds = entmicroStaffList.stream().map(EntmicroStaff::getUserId).collect(Collectors.toList());
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(userIds)) {
syncDataService.syncEntmicroCustomer(mechantId, userIds, syncUpdateCustomer);
log.info("客户同步完毕{}", entmicroStaffList.size());
customerBaseService.syncEntmicroCustomerBase(mechantId, userIds);
log.info("客户群同步完毕{}", entmicroStaffList.size());
}
}
List<EntmicroStaffResponse> staffByDepartment = entmicroStaffMapper.findStaffByDepartment(mechantId, subordinateDep);
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(staffByDepartment)) {
// 1.更新员工的部门
List<Integer> ids = staffByDepartment.stream().map(EntmicroStaffResponse::getId).collect(Collectors.toList());
entmicroStaffMapper.updateStaffDepart(ids, staffMerchantId, subordinateDep);
// 2.同步企微数据
List<EntmicroStaff> collect = staffByDepartment.stream().map(index -> {
EntmicroStaff entmicroStaff = new EntmicroStaff();
BeanUtils.copyProperties(index, entmicroStaff);
entmicroStaff.setMerchantid(staffMerchantId);
entmicroStaff.setType("add");
return entmicroStaff;
}).collect(Collectors.toList());
List<EntmicroStaff> collects = collect.stream().filter(index -> StringUtils.isNotEmpty(index.getStaffPhone())).collect(Collectors.toList());
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(collects)) {
merchantService.syncEntmicroStaff(collects);
}
}
}
private void updateMerchantBind(int departmentId, int staffMerchantId) {
List<Integer> list = entmicroDepartmentPathMapper.findByParentId(departmentId);
if (CollectionUtils.isNotEmpty(list)) {
entmicroDepartmentMapper.updateMerchantBind(list, staffMerchantId);
}
}
/**
* 维格表同步指定门店企微客户 2023-07-20
* 1.查询商户的授权商户
* 2.查询门店的员工列表
* 3.查询门店的客户列表
* 4.组装参数,同步到维格表
*
* @param request 请求参数
*/
@Async("taskExecutor")
@GlsxDistributeLockable(name = "'syncCustomer2Vika' + #request.merchantId", timeoutException = RpcServiceException.class, time = 10 * 1000)
public void syncCustomer2Vika(SyncDepartmentRpcRequest request) {
Integer merchantId = request.getInputMerchantId();
Integer departmentId = request.getDepartmentId();
RpcAssert.assertNotNull(merchantId, DefaultErrorEnum.DATA_NULL, "merchantId must not be null");
RpcAssert.assertNotNull(departmentId, DefaultErrorEnum.DATA_NULL, "departmentId must not be null");
Integer supMerchantNo = entmicroStaffService.findSupMerchantNo(merchantId);
List<EntmicroStaff> entmicroStaffList = entmicroStaffService.getEntmicroStaff(supMerchantNo, departmentId);
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(entmicroStaffList)) {
GetWorkWechatUserListRequestDTO getWorkWechatUserRequestDTO = new GetWorkWechatUserListRequestDTO();
getWorkWechatUserRequestDTO.setMerchantId(supMerchantNo);
getWorkWechatUserRequestDTO.setDepartmentId(1);
log.info("获取企微所有员工入参: {}", JSONArray.toJSONString(getWorkWechatUserRequestDTO));
RpcResponse<WorkWechatUserListResponseDTO> userList = workWechatUserRemote.getUserList(getWorkWechatUserRequestDTO);
if (userList.isSuccess()) {
List<WorkWechatUserResponseDTO> workWechatUserResponseDTOS = userList.getResult().getUserList();
if (CollectionUtils.isNotEmpty(workWechatUserResponseDTOS)) {
Map<String, String> staffIdNameMap = workWechatUserResponseDTOS.stream().collect(Collectors.toMap(WorkWechatUserResponseDTO::getUserId, WorkWechatUserResponseDTO::getName, (o1, o2) -> o1));
log.info("员工同步完毕{}", entmicroStaffList.size());
List<String> userIds = entmicroStaffList.stream().map(EntmicroStaff::getUserId).collect(Collectors.toList());
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(userIds)) {
List<CustomerStandardRequest> entmicroCustomer = syncDataService.getEntmicroCustomer(merchantId, userIds);
log.info("客户同步完毕,客户总数{}", entmicroCustomer.size());
if (CollectionUtils.isNotEmpty(entmicroCustomer)) {
if (request.getFilterExists() != null && request.getFilterExists()) {
entmicroCustomer = entmicroCustomer.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toCollection(()
-> new TreeSet<>(Comparator.comparing(o -> o.getCustomerInfo().getUserid()))), ArrayList::new));
// 过滤已同步客户
entmicroCustomer = filterEntmicroCustomer(entmicroCustomer);
}
log.info("客户过滤结果 entmicroCustomer:{}", entmicroCustomer.size());
int i = 0;
for (CustomerStandardRequest customerStandardRequest : entmicroCustomer) {
try {
CustomerInfoRequest customerInfo = customerStandardRequest.getCustomerInfo();
List<CustomerFollowerStaffRequest> followerStaff = customerStandardRequest.getFollowerStaff();
WeChatContactsDTO weChatContactsDTO = buildWeChatContactsDTO(customerInfo, followerStaff, staffIdNameMap);
log.info("syncVikaWeChatContacts_sync:{}", JSON.toJSONString(weChatContactsDTO));
asyncCustomerService.syncVikaWeChatContacts(weChatContactsDTO);
if (i++ % 5 == 0) {
Thread.sleep(10 * 1000);
}
} catch (Exception e) {
e.printStackTrace();
log.error("syncVikaWeChatContacts_sync error customerStandardRequest:{}", customerStandardRequest, e);
}
}
log.info("同步结束");
}
}
}
}
log.info("获取企微所有员工失败{}", userList.getMessage());
}
}
private List<CustomerStandardRequest> filterEntmicroCustomer(List<CustomerStandardRequest> entmicroCustomer) {
GetVikaRecordRequestDTO requestDTO = new GetVikaRecordRequestDTO();
requestDTO.setDatasheetId("dstT8aGi0HqrYgGD7y");
RpcResponse<List<VikaRecordBaseResponseDTO>> groupRecordList = vikaRecordRemote.getRecordList(requestDTO);
List<String> list = groupRecordList.getResult().stream().map(s -> {
if (s != null) {
Object fields = s.getFields();
if (fields != null) {
Object obj = JSON.parseObject(JSON.toJSONString(fields)).get("微信ID");
return obj.toString();
}
}
return null;
}).collect(Collectors.toList());
entmicroCustomer = entmicroCustomer.stream().filter(s -> !list.contains(s.getExternalUserId())).collect(Collectors.toList());
return entmicroCustomer;
}
private static WeChatContactsDTO buildWeChatContactsDTO(CustomerInfoRequest customerInfo, List<CustomerFollowerStaffRequest> followList,
Map<String, String> staffIdNameMap) {
WeChatContactsDTO weChatContactsDTO = new WeChatContactsDTO();
weChatContactsDTO.setWeChatId(customerInfo.getUserid());
weChatContactsDTO.setNickName(customerInfo.getCustomerName());
weChatContactsDTO.setAvatar(customerInfo.getAvatar());
weChatContactsDTO.setPhone(customerInfo.getCustomerPhone());
if (org.springframework.util.CollectionUtils.isEmpty(followList)) {
return weChatContactsDTO;
}
List<StaffInfoDTO> staffInfoList = followList.stream()
.map(e -> {
List<String> cpWxTagIds = Lists.newArrayList();
if (!org.springframework.util.CollectionUtils.isEmpty(e.getTagList())) {
cpWxTagIds = e.getTagList().stream().map(FollowTagRequest::getTagId).collect(Collectors.toList());
}
return new StaffInfoDTO(staffIdNameMap.get(e.getUserId()), cpWxTagIds);
}).collect(Collectors.toList());
weChatContactsDTO.setStaffInfoList(staffInfoList);
return weChatContactsDTO;
}
@Async("taskExecutor")
@GlsxDistributeLockable(name = "'syncCustomer2VikaByTag' + #rpcRequest.merchantId", timeoutException = RpcServiceException.class, time = 10 * 1000)
public void syncCustomer2VikaByTag(SyncDepartmentRpcRequest rpcRequest) {
Integer merchantId = rpcRequest.getInputMerchantId();
Integer supMerchantNo = entmicroStaffService.findSupMerchantNo(merchantId);
List<CustomerInfoResponse> customerByOneidList = getOneIdByTag(merchantId);
for (CustomerInfoResponse customerInfo : customerByOneidList) {
try {
GetWorkWechatExternalContactRequestDTO request = new GetWorkWechatExternalContactRequestDTO();
request.setMerchantId(supMerchantNo);
request.setExternalUserId(customerInfo.getUserid());
RpcResponse<WorkWechatExternalContactResponseDTO> contact = workWechatExternalContactRemote.getExternalContact(request);
log.info("contact:{}", contact);
WorkWechatExternalContactResponseDTO result = contact.getResult();
WorkWechatExternalContactDTO externalContact = result.getExternalContact();
List<WorkWechatExternalContactFollowUserDTO> followUser = result.getFollowUser();
WeChatContactsDTO weChatContactsDTO = new WeChatContactsDTO();
weChatContactsDTO.setWeChatId(externalContact.getExternalUserId());
weChatContactsDTO.setNickName(externalContact.getName());
weChatContactsDTO.setAvatar(externalContact.getAvatar());
weChatContactsDTO.setPhone(customerInfo.getCustomerPhone());
GetWorkWechatUserRequestDTO requestDTO = new GetWorkWechatUserRequestDTO();
requestDTO.setMerchantId(supMerchantNo);
if (!CollectionUtils.isEmpty(followUser)) {
List<StaffInfoDTO> staffInfoList = followUser.stream()
.map(e -> {
List<String> cpWxTagIds = Lists.newArrayList();
if (!org.springframework.util.CollectionUtils.isEmpty(e.getTagList())) {
cpWxTagIds = e.getTagList().stream().map(WorkWechatExternalContactFollowUserTagDTO::getTagId).collect(Collectors.toList());
}
String name = null;
requestDTO.setUserId(e.getUserId());
RpcResponse<WorkWechatUserResponseDTO> user = workWechatUserRemote.getUser(requestDTO);
if (user != null && user.getResult() != null) {
name = user.getResult().getName();
}
return new StaffInfoDTO(name, cpWxTagIds);
}).collect(Collectors.toList());
weChatContactsDTO.setStaffInfoList(staffInfoList);
}
log.info("syncVikaWeChatContacts_sync:{}", JSON.toJSONString(weChatContactsDTO));
vikaWeChatContactsRemote.syncVikaWeChatContacts(weChatContactsDTO);
} catch (Exception e) {
e.printStackTrace();
log.error("syncCustomer2VikaByTag error customerInfo:{}", customerInfo, e);
}
}
}
public List<CustomerInfoResponse> getOneIdByTag(Integer merchantId) {
RpcPagination<CustomerSearchRequest> pagination = new RpcPagination<>();
pagination.setPageNum(1);
pagination.setPageSize(Integer.MAX_VALUE);
CustomerSearchRequest condition = new CustomerSearchRequest();
condition.setCombineType(2);
ArrayList<TagSearchRequest> tagLists = new ArrayList<>();
// 测试环境
if (merchantId.equals(44210895)) {
TagSearchRequest request4 = new TagSearchRequest();
request4.setGroupId(13629);
request4.setGroupType("企业标签");
request4.setLabelId("37307");
tagLists.add(request4);
TagSearchRequest request5 = new TagSearchRequest();
request5.setGroupId(13629);
request5.setGroupType("企业标签");
request5.setLabelId("37308");
tagLists.add(request5);
TagSearchRequest request6 = new TagSearchRequest();
request6.setGroupId(13629);
request6.setGroupType("企业标签");
request6.setLabelId("37309");
tagLists.add(request6);
} else {
TagSearchRequest request1 = new TagSearchRequest();
request1.setGroupId(2177);
request1.setGroupType("自店标签");
request1.setLabelId("15782");
tagLists.add(request1);
TagSearchRequest request2 = new TagSearchRequest();
request2.setGroupId(2177);
request2.setGroupType("自店标签");
request2.setLabelId("16914");
tagLists.add(request2);
TagSearchRequest request3 = new TagSearchRequest();
request3.setGroupId(2177);
request3.setGroupType("自店标签");
request3.setLabelId("16915");
tagLists.add(request3);
}
condition.setTagLists(tagLists);
condition.setMerchantId(merchantId);
pagination.setCondition(condition);
Page<String> oneIdsByTag = ropCommonServie.getOneIdsByTag(pagination);
List<CustomerInfoResponse> customerByOneidList = new ArrayList<>();
if (oneIdsByTag != null && CollectionUtils.isNotEmpty(oneIdsByTag.getResult())) {
customerByOneidList = customerMapper.findCustomerByOneidList(oneIdsByTag.getResult(), merchantId);
}
return customerByOneidList;
}
}