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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package com.tanpu.feo.feojob.jobs;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONUtil;
import com.tanpu.feo.feojob.constant.BaseConstant;
import com.tanpu.feo.feojob.constant.OrgExtConstant;
import com.tanpu.feo.feojob.dao.user.entity.*;
import com.tanpu.feo.feojob.dto.WorkDataDto;
import com.tanpu.feo.feojob.dto.WxCpDepartDto;
import com.tanpu.feo.feojob.enums.EmployeeDutyEnum;
import com.tanpu.feo.feojob.enums.RoleTypeEnum;
import com.tanpu.feo.feojob.service.*;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.Gender;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author zejia zj wu
* @date 2021-05-18
* @Description 从企业微信 同步 组织信息 及 员工信息
*/
@Component
@Slf4j
public class OrgSyncByWxcpJob {
final static String keyStr = "{\"corpId\":\"ww08675a3a48c9e8a2\",\"agentId\":\"1000013\",\"corpSecret\":\"l7eLNC5DDNxmucc9emHsEaSXym4VKDy_D5w4IoOpvqk\",\"token\":\"E1zDvDkXWt\",\"aesKey\":\"43P8lbP5yt8AHLOUPGBPS252c60K9Vzofzm5NOxHHeP\",\"sendMessageAppId\":\"wxe177c62fa1e1c602\"}\n";
@Resource
private OrgExtService orgExtService;
@Resource
private OrgService orgService;
@Resource
private UserInfoService userInfoService;
@Resource
private DepartmentService departmentService;
@Resource
private DepartmentEmployeeService departmentEmployeeService;
@Resource
private EmployeeService employeeService;
@Resource
private EmployeeRoleService employeeRoleService;
@Resource
private RoleService roleService;
@Resource
private OrgSyncService orgSyncService;
@Value("${tanpu.sync.job.skipped:true}")
private boolean jobSkipped;
@Scheduled(cron = "0 30 6,10,15 * * ?") //每日凌晨6点30执行
public void execute() {
log.info("====== 开始执行OrgSyncByWxcpJob ======");
try {
if (!jobSkipped) {
orgSyncByWxcp("Y");
}
} catch (Exception e) {
log.error("====== 异常结束执行OrgSyncByWxcpJob,错误:{}=======", e.getMessage());
}
log.info("====== 结束执行OrgSyncByWxcpJob ======");
}
/**
* 执行 部门 及 员工 同步任务
*
* @param isAuto 任务触发类型 Y 定时任务 / N 手动
*/
public void orgSyncByWxcp(String isAuto) {
// 1 扫描 org_ext 表 获取需要 执行同步的 机构
List<OrgExtEntity> orgExtList = orgExtService.findOrgExtByModel(OrgExtConstant.MODEL_ONE);
for (OrgExtEntity orgExtEntity : orgExtList) {
String orgId = orgExtEntity.getOrgId();
log.info("======开始同步机构:{} 的信息======", orgId);
//2 根据orgId 查找机构 相关信息
OrgEntity orgEntity = orgService.findById(orgId);
//3 从企业微信获取组织信息及员工信息
log.info("======从企业微信获取组织信息及员工信息 开始======");
WxCpDefaultConfigImpl wxCpDefaultConfig = JSONUtil.toBean(orgExtEntity.getJsonWxcpKey(), WxCpDefaultConfigImpl.class);
List<WxCpDepart> wxCpDepartList; // 机构信息
List<WxCpDepartDto> wxCpDepartDtoList;//自定义的 部门信息
try {
wxCpDepartList = getWxCpDepartList(wxCpDefaultConfig);
wxCpDepartDtoList = getWxCpUserList(wxCpDepartList, wxCpDefaultConfig);
} catch (Exception e) {
log.error("======从企业微信获取组织信息及员工信息失败->机构:{} -> 错误信息-> {}======", orgId, e.getMessage());
continue;
}
log.info("======从企业微信获取组织信息及员工信息 结束======");
//4 判断是否需要 同步
String digestHex = isExecute(isAuto, wxCpDepartDtoList, orgExtEntity.getMd5WxcpData());
if (StrUtil.isBlank(digestHex)) {
log.info("====== 机构:{} 不需要执行同步=======", orgId);
continue;
}
//5 开始比对 5张表 数据 user_info employee department department_employee employee_role
log.info("======开始比对 5张表 数据 ======");
String corpId = wxCpDefaultConfig.getCorpId();
WorkDataDto<UserInfoEntity> workUserInfo = userInfoWork(orgEntity.getId(), wxCpDepartDtoList, corpId);
WorkDataDto<EmployeeEntity> workEmployee = employeeWork(orgEntity.getId(), wxCpDepartDtoList, corpId);
WorkDataDto<DepartmentEntity> workDepartment = departmentWork(orgEntity.getId(), wxCpDepartDtoList);
WorkDataDto<DepartmentEmployeeEntity> workDepartmentEmployee = departmentEmployeeWork(orgEntity.getId(), wxCpDepartDtoList, corpId);
WorkDataDto<EmployeeRoleEntity> workEmployeeRole = employeeRoleWork(orgEntity.getId(), wxCpDepartDtoList, corpId);
log.info("======结束比对 5张表 数据 ======");
//6 数据 更新
orgSyncService.updateData(orgId, workUserInfo, workEmployee, workDepartment, workDepartmentEmployee, workEmployeeRole);
//7 讲最新的md5值 存至 orgExtEntity 表中
orgExtEntity.setMd5WxcpData(digestHex);
orgExtService.updateById(orgExtEntity);
}
}
/**
* @description: 整理 员工与角色关联关系 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:08 下午
*/
private WorkDataDto<EmployeeRoleEntity> employeeRoleWork(String orgId, List<WxCpDepartDto> wxCpDepartDtoList, String corpId) {
WorkDataDto<EmployeeRoleEntity> workDataDto = new WorkDataDto<>();
List<EmployeeRoleEntity> insertList = new ArrayList<>();
List<EmployeeRoleEntity> deleteList = new ArrayList<>();
List<EmployeeRoleEntity> updateList = new ArrayList<>();
//获取 数据库 部门员工关系 信息
List<EmployeeRoleEntity> employeeRoleList = employeeRoleService.findInfoByOrgId(orgId);
// 获取 role
Map<String, String> roleMap = roleService.findInfoNoAdmin();
// 预处理 新数据
HashMap<String, WxCpUser> hashMap = new HashMap<>();
for (WxCpDepartDto wxCpDepartDto : wxCpDepartDtoList) {
List<WxCpUser> wxCpUserList = wxCpDepartDto.getWxCpUserList();
for (WxCpUser wxCpUser : wxCpUserList) {
// key employee_id + '&' + role_id
String type = wxCpUser.getIsLeader() == 1 ? RoleTypeEnum.TEAM.code : RoleTypeEnum.IFA.code;
hashMap.put(corpId + "_" + wxCpUser.getUserId() + "&" + roleMap.get(type), wxCpUser);
}
}
for (EmployeeRoleEntity employeeRole : employeeRoleList) {
String employeeId = employeeRole.getEmployeeId();
String roleId = employeeRole.getRoleId();
String key = employeeId + "&" + roleId;
WxCpUser wxCpUser = hashMap.get(key);
//删除
if (ObjectUtil.isNull(wxCpUser)) {
// 删除
deleteList.add(employeeRole);
continue;
}
// 员工 角色关联关系没有 更新
hashMap.remove(key);
}
Map<String, List<EmployeeRoleEntity>> map = employeeRoleList.stream().collect(Collectors.groupingBy(EmployeeRoleEntity::getEmployeeId));
String adminRoleId = roleMap.get(RoleTypeEnum.ADMIN.code);
for (String key : hashMap.keySet()) {
String[] split = key.split("&");
String employeeId = split[0];
String roleId = split[1];
// 如果已经有一个admin的角色,那么就不要插入新的
List<EmployeeRoleEntity> roleList = map.get(employeeId);
if (CollectionUtils.isNotEmpty(roleList) && roleList.stream().anyMatch(p -> StringUtils.equals(p.getRoleId(), adminRoleId))) {
log.info("成员已经有管理员角色了,不需要再插入, userId: {}, orgId:{}", employeeId, orgId);
continue;
}
EmployeeRoleEntity employeeRole = new EmployeeRoleEntity();
employeeRole.setEmployeeId(employeeId);
employeeRole.setRoleId(roleId);
employeeRole.setOrgId(orgId);
insertList.add(employeeRole);
}
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
}
/**
* @description: 整理 部门与员工关联关系 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:08 下午
*/
private WorkDataDto<DepartmentEmployeeEntity> departmentEmployeeWork(String orgId, List<WxCpDepartDto> wxCpDepartDtoList, String corpId) {
WorkDataDto<DepartmentEmployeeEntity> workDataDto = new WorkDataDto<>();
List<DepartmentEmployeeEntity> insertList = new ArrayList<>();
List<DepartmentEmployeeEntity> deleteList = new ArrayList<>();
List<DepartmentEmployeeEntity> updateList = new ArrayList<>();
//获取 数据库 部门员工关系 信息
List<DepartmentEmployeeEntity> departmentEmployeeList = departmentEmployeeService.findInfoByOrgId(orgId);
// 预处理 新数据
HashMap<String, WxCpUser> hashMap = new HashMap<>();
for (WxCpDepartDto wxCpDepartDto : wxCpDepartDtoList) {
List<WxCpUser> wxCpUserList = wxCpDepartDto.getWxCpUserList();
for (WxCpUser wxCpUser : wxCpUserList) {
// key department_id + '&' + employee_id
hashMap.put(orgId + "_" + wxCpDepartDto.getId() + "&" + corpId + "_" + wxCpUser.getUserId(), wxCpUser);
}
}
for (DepartmentEmployeeEntity departmentEmployee : departmentEmployeeList) {
String departmentId = departmentEmployee.getDepartmentId();
String employeeId = departmentEmployee.getEmployeeId();
String key = departmentId + "&" + employeeId;
WxCpUser wxCpUser = hashMap.get(key);
if (ObjectUtil.isNull(wxCpUser)) {
// 删除
deleteList.add(departmentEmployee);
continue;
}
//更新
String type = wxCpUser.getIsLeader() == 1 ? EmployeeDutyEnum.DIRECTOR.code : EmployeeDutyEnum.STAFF.code;
if (!departmentEmployee.getType().equals(type)) {
departmentEmployee.setType(type);
updateList.add(departmentEmployee);
}
hashMap.remove(key);
}
for (String s : hashMap.keySet()) {
WxCpUser wxCpUser = hashMap.get(s);
String type = wxCpUser.getIsLeader() == 1 ? EmployeeDutyEnum.DIRECTOR.code : EmployeeDutyEnum.STAFF.code;
String[] split = s.split("&");
String departmentId = split[0];
String employeeId = split[1];
DepartmentEmployeeEntity departmentEmployee = new DepartmentEmployeeEntity();
departmentEmployee.setEmployeeId(employeeId);
departmentEmployee.setDepartmentId(departmentId);
departmentEmployee.setType(type);
departmentEmployee.setOrgId(orgId);
insertList.add(departmentEmployee);
}
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
}
/**
* @description: 整理 部门机构 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:09 下午
*/
private WorkDataDto<DepartmentEntity> departmentWork(String orgId, List<WxCpDepartDto> wxCpDepartDtoList) {
WorkDataDto<DepartmentEntity> workDataDto = new WorkDataDto<>();
List<DepartmentEntity> insertList = new ArrayList<>();
List<DepartmentEntity> deleteList = new ArrayList<>();
List<DepartmentEntity> updateList = new ArrayList<>();
//获取 数据库 部门 信息
List<DepartmentEntity> departmentList = departmentService.findDepartmentByOrgId(orgId);
// 预处理 新数据
HashMap<String, WxCpDepartDto> hashMap = new HashMap<>();
HashMap<Long, WxCpDepartDto> hashMap2 = new HashMap<>();
for (WxCpDepartDto wxCpDepartDto : wxCpDepartDtoList) {
hashMap.put(orgId + "_" + wxCpDepartDto.getId(), wxCpDepartDto);
hashMap2.put(wxCpDepartDto.getId(), wxCpDepartDto);
}
for (DepartmentEntity department : departmentList) {
String departmentId = department.getDepartmentId();
WxCpDepartDto wxCpDepartDto = hashMap.get(departmentId);
//删除
if (ObjectUtil.isNull(wxCpDepartDto)) {
deleteList.add(department);
continue;
}
//更新
Long parentId1 = wxCpDepartDto.getParentId();
String parentId = ObjectUtil.isNull(hashMap2.get(parentId1)) ? null : orgId + "_" + parentId1;
Integer level = getLevel(hashMap2, wxCpDepartDto.getId());
if (!department.getDepartmentName().equals(wxCpDepartDto.getName()) || StrUtil.compareIgnoreCase(department.getParentDepartId(), parentId, false) != 0
|| !department.getMembers().equals(wxCpDepartDto.getMembers()) || !level.equals(department.getLevel())) {
department.setDepartmentName(wxCpDepartDto.getName());
department.setParentDepartId(parentId);
department.setMembers(wxCpDepartDto.getMembers());
department.setLevel(level);
updateList.add(department);
}
hashMap.remove(departmentId);
}
//修改
for (String departmentId : hashMap.keySet()) {
WxCpDepartDto wxCpDepartDto = hashMap.get(departmentId);
Long parentId1 = wxCpDepartDto.getParentId();
String parentId = ObjectUtil.isNull(hashMap2.get(parentId1)) ? null : orgId + "_" + parentId1;
DepartmentEntity department = new DepartmentEntity();
department.setDepartmentId(orgId + "_" + wxCpDepartDto.getId());
department.setParentDepartId(parentId);
department.setDepartmentName(wxCpDepartDto.getName());
department.setLevel(getLevel(hashMap2, wxCpDepartDto.getId()));
department.setMembers(wxCpDepartDto.getMembers());
department.setOrgId(orgId);
insertList.add(department);
}
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
}
/**
* @description: 获取部门等级
* @Author: zejia zj wu
* @date: 2021/5/27 3:09 下午
*/
private Integer getLevel(HashMap<Long, WxCpDepartDto> hashMap2, Long id) {
Long parentId = hashMap2.get(id).getParentId();
if (ObjectUtil.isNull(hashMap2.get(parentId))) {
return 1;
}
return 1 + getLevel(hashMap2, parentId);
}
/**
* @description: 整理 员工 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:09 下午
*/
private WorkDataDto<EmployeeEntity> employeeWork(String orgId, List<WxCpDepartDto> wxCpDepartDtoList, String corpId) {
WorkDataDto<EmployeeEntity> workDataDto = new WorkDataDto<>();
List<EmployeeEntity> insertList = new ArrayList<>();
List<EmployeeEntity> deleteList = new ArrayList<>();
List<EmployeeEntity> updateList = new ArrayList<>();
//获取 数据库 员工 信息
List<EmployeeEntity> employeeList = employeeService.getEmployeeSListByOrgId(orgId);
// 预处理 新数据
HashMap<String, WxCpUser> hashMap = new HashMap<>();
for (WxCpDepartDto wxCpDepartDto : wxCpDepartDtoList) {
List<WxCpUser> wxCpUserList = wxCpDepartDto.getWxCpUserList();
for (WxCpUser wxCpUser : wxCpUserList) {
hashMap.put(corpId + "_" + wxCpUser.getUserId(), wxCpUser);
}
}
// 计算 变化数据
for (EmployeeEntity employee : employeeList) {
String employeeId = employee.getEmployeeId();
WxCpUser wxCpUser = hashMap.get(employeeId);
//删除
if (ObjectUtil.isNull(wxCpUser)) {
deleteList.add(employee);
continue;
}
//更新
String status = wxCpUser.getStatus() == 1 ? "on" : "off";
if (!employee.getName().equals(wxCpUser.getName()) || !employee.getMail().equals(wxCpUser.getEmail()) ||
StrUtil.isBlank(employee.getBoundWechat()) || !employee.getStatus().equals(status)) {
employee.setName(wxCpUser.getName());
employee.setMail(wxCpUser.getEmail());
if (StrUtil.isBlankIfStr(employee.getBoundWechat())) {
employee.setBoundWechat(orgId + "_" + wxCpUser.getUserId());
}
employee.setStatus(status);
updateList.add(employee);
}
hashMap.remove(employeeId);
}
//修改
for (String key : hashMap.keySet()) {
WxCpUser wxCpUser = hashMap.get(key);
EmployeeEntity employee = new EmployeeEntity();
employee.setEmployeeId(corpId + "_" + wxCpUser.getUserId());
employee.setName(wxCpUser.getName());
employee.setPhone(wxCpUser.getMobile());
employee.setMail(wxCpUser.getEmail());
employee.setNumber(null);
employee.setOrgId(orgId);
employee.setBoundWechat(orgId + "_" + wxCpUser.getUserId());
employee.setStatus(wxCpUser.getStatus() == 1 ? BaseConstant.EmployeeStatus.ON_JOB : BaseConstant.EmployeeStatus.DEPARTED);
insertList.add(employee);
}
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
}
/**
* @description: 整理 用户表 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:10 下午
*/
private WorkDataDto<UserInfoEntity> userInfoWork(String orgId, List<WxCpDepartDto> wxCpDepartDtoList, String corpId) {
WorkDataDto<UserInfoEntity> workDataDto = new WorkDataDto<>();
List<UserInfoEntity> insertList = new ArrayList<>();
List<UserInfoEntity> deleteList = new ArrayList<>();
List<UserInfoEntity> updateList = new ArrayList<>();
//获取 数据库用户信息
List<UserInfoEntity> userInfoList = userInfoService.getUserInfoListByOrgId(orgId);
// 预处理 新数据
HashMap<String, WxCpUser> hashMap = new HashMap<>();
for (WxCpDepartDto wxCpDepartDto : wxCpDepartDtoList) {
List<WxCpUser> wxCpUserList = wxCpDepartDto.getWxCpUserList();
for (WxCpUser wxCpUser : wxCpUserList) {
hashMap.put(corpId + "_" + wxCpUser.getUserId(), wxCpUser);
}
}
// 计算 变化数据
for (UserInfoEntity userInfo : userInfoList) {
String id = userInfo.getId();
WxCpUser wxCpUser = hashMap.get(id);
// 删除
if (ObjectUtil.isNull(wxCpUser)) {
deleteList.add(userInfo);
continue;
}
// 更新
if (!userInfo.getUiUsername().equals(wxCpUser.getName()) || StrUtil.isBlankIfStr(userInfo.getUiHeadimgMp())
|| !wxCpUser.getUserId().equals(userInfo.getWxcpUid()) || StrUtil.isBlankIfStr(userInfo.getUiWechatXcxQrcode())
|| !userInfo.getUiShenfen().equals(String.valueOf(wxCpUser.getIsLeader()))) {
userInfo.setUiUsername(wxCpUser.getName());
userInfo.setUiHeadimgMp(wxCpUser.getThumbAvatar());
userInfo.setWxcpUid(wxCpUser.getUserId());
userInfo.setUiShenfen(String.valueOf(wxCpUser.getIsLeader()));
userInfo.setUiWechatXcxQrcode(orgSyncService.createWechatXcxQrcode(userInfo.getId(), wxCpUser.getThumbAvatar()));
updateList.add(userInfo);
}
hashMap.remove(id);
}
for (String key : hashMap.keySet()) {
WxCpUser wxCpUser = hashMap.get(key);
UserInfoEntity userInfo = new UserInfoEntity();
userInfo.setUiTelphone(wxCpUser.getMobile());
userInfo.setUiUsername(wxCpUser.getName());
Gender gender = wxCpUser.getGender();
String sex = gender.getCode().equals("1") ? BaseConstant.Gender.MAN : BaseConstant.Gender.WOMEN;
userInfo.setUiSex(sex);
userInfo.setUiHeadimg(wxCpUser.getThumbAvatar());
userInfo.setUiHeadimgMp(wxCpUser.getThumbAvatar());
userInfo.setOrgId(orgId);
userInfo.setId(corpId + "_" + wxCpUser.getUserId());
userInfo.setUiMobilephoneMp(wxCpUser.getMobile());
userInfo.setDeletetag("0");
userInfo.setUiShenfen(String.valueOf(wxCpUser.getIsLeader()));
userInfo.setUiEmailMp(wxCpUser.getEmail());
String nickName = wxCpUser.getName();
userInfo.setUiUsername(nickName);
userInfo.setUiUsernameMp(nickName);
userInfo.setUiRzstatus(1);
//默认小名片 0:小名片 1:大名片
userInfo.setUiTypeMp("1");
userInfo.setLevel(2);
// 0: 白银 1:黄金 2:钻石
userInfo.setUiGrade("0");
userInfo.setUiSource("pc");
userInfo.setUiRegisterTime(userInfo.getCreatetime());
userInfo.setUiChannel(null);
userInfo.setWxcpUid(wxCpUser.getUserId());
userInfo.setUiWechatXcxQrcode(orgSyncService.createWechatXcxQrcode(userInfo.getId(), userInfo.getUiHeadimgMp()));
insertList.add(userInfo);
}
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
}
/**
* @description: 判断是否需要执行 并返回数据MD5
* @Author: zejia zj wu
* @date: 2021/5/27 3:10 下午
*/
private String isExecute(String isAuto, List<WxCpDepartDto> wxCpDepartDtoList, String md5WxcpData) {
String digestHex = MD5.create().digestHex(JSONUtil.toJsonStr(wxCpDepartDtoList));
if (!"Y".equals(isAuto)) {
return digestHex;
}
if (digestHex.equals(md5WxcpData)) {
return null;
}
return digestHex;
}
/**
* @description: 获取 企业微信 用户列表信息
* @Author: zejia zj wu
* @date: 2021/5/27 3:11 下午
*/
private List<WxCpDepartDto> getWxCpUserList(List<WxCpDepart> wxCpDepartList, WxCpDefaultConfigImpl wxCpDefaultConfig) throws Exception {
WxCpService cpService = new WxCpServiceImpl();
cpService.setWxCpConfigStorage(wxCpDefaultConfig);
ArrayList<WxCpDepartDto> wxCpDepartDtoArrayList = new ArrayList<>();
for (WxCpDepart wxCpDepart : wxCpDepartList) {
try {
List<WxCpUser> wxCpUsers = cpService.getUserService().listByDepartment(wxCpDepart.getId(), Boolean.FALSE, 0);
WxCpDepartDto wxCpDepartDto = new WxCpDepartDto();
BeanUtil.copyProperties(wxCpDepart, wxCpDepartDto, true);
wxCpDepartDto.setWxCpUserList(wxCpUsers);
wxCpDepartDto.setMembers(wxCpUsers.size());
wxCpDepartDtoArrayList.add(wxCpDepartDto);
} catch (WxErrorException e) {
log.error("======根据企业微信key 获取企业微信员工信息调用失败,错误:{}=======", e.getMessage());
throw new Exception("根据企业微信key 获取企业微信组织结构调用失败");
}
}
return wxCpDepartDtoArrayList;
}
/**
* @description: 获取 企业微信 部门聊表信息
* @Author: zejia zj wu
* @date: 2021/5/27 3:11 下午
*/
private List<WxCpDepart> getWxCpDepartList(WxCpDefaultConfigImpl wxCpDefaultConfig) throws Exception {
WxCpService cpService = new WxCpServiceImpl();
cpService.setWxCpConfigStorage(wxCpDefaultConfig);
List<WxCpDepart> wxCpDeparts;
try {
wxCpDeparts = cpService.getDepartmentService().list(null);
} catch (WxErrorException e) {
log.error("======根据企业微信key 获取企业微信组织结构调用失败,错误:{}=======", e.getMessage());
throw new Exception("根据企业微信key 获取企业微信组织结构调用失败");
}
return wxCpDeparts;
}
public static void main(String[] args) throws Exception {
OrgSyncByWxcpJob orgSyncByWxcpJob = new OrgSyncByWxcpJob();
WxCpDefaultConfigImpl wxCpDefaultConfig = JSONUtil.toBean(keyStr, WxCpDefaultConfigImpl.class);
List<WxCpDepart> wxCpDepartList = orgSyncByWxcpJob.getWxCpDepartList(wxCpDefaultConfig); // 机构信息
List<WxCpDepartDto> wxCpDepartDtoList = orgSyncByWxcpJob.getWxCpUserList(wxCpDepartList, wxCpDefaultConfig);
log.info("=========================");
// log.info(JSONUtil.toJsonStr(wxCpDepartDtoList));
String digestHex = MD5.create().digestHex(JSONUtil.toJsonStr(wxCpDepartDtoList));
log.info(digestHex);
}
}