Commit a06d14b2 authored by 钱坤's avatar 钱坤

修复从企微同步时,多插入无效的role_id记录

parent fe3bbea8
......@@ -21,6 +21,8 @@ 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;
......@@ -30,6 +32,7 @@ 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
......@@ -62,8 +65,8 @@ public class OrgSyncByWxcpJob {
@Value("${tanpu.sync.job.skipped:true}")
private boolean jobSkipped;
@Scheduled(cron="0 30 6,10,15 * * ?") //每日凌晨6点30执行
public void execute(){
@Scheduled(cron = "0 30 6,10,15 * * ?") //每日凌晨6点30执行
public void execute() {
log.info("====== 开始执行OrgSyncByWxcpJob ======");
try {
if (!jobSkipped) {
......@@ -77,6 +80,7 @@ public class OrgSyncByWxcpJob {
/**
* 执行 部门 及 员工 同步任务
*
* @param isAuto 任务触发类型 Y 定时任务 / N 手动
*/
public void orgSyncByWxcp(String isAuto) {
......@@ -97,7 +101,7 @@ public class OrgSyncByWxcpJob {
List<WxCpDepartDto> wxCpDepartDtoList;//自定义的 部门信息
try {
wxCpDepartList = getWxCpDepartList(wxCpDefaultConfig);
wxCpDepartDtoList = getWxCpUserList(wxCpDepartList,wxCpDefaultConfig);
wxCpDepartDtoList = getWxCpUserList(wxCpDepartList, wxCpDefaultConfig);
} catch (Exception e) {
log.error("======从企业微信获取组织信息及员工信息失败->机构:{} -> 错误信息-> {}======", orgId, e.getMessage());
continue;
......@@ -132,10 +136,10 @@ public class OrgSyncByWxcpJob {
}
/**
* @description: 整理 员工与角色关联关系 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:08 下午
*/
* @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<>();
......@@ -144,7 +148,7 @@ public class OrgSyncByWxcpJob {
//获取 数据库 部门员工关系 信息
List<EmployeeRoleEntity> employeeRoleList = employeeRoleService.findInfoByOrgId(orgId);
// 获取 role
Map<String, String> roleMap =roleService.findInfoNoAdmin();
Map<String, String> roleMap = roleService.findInfoNoAdmin();
// 预处理 新数据
HashMap<String, WxCpUser> hashMap = new HashMap<>();
for (WxCpDepartDto wxCpDepartDto : wxCpDepartDtoList) {
......@@ -169,10 +173,18 @@ public class OrgSyncByWxcpJob {
// 员工 角色关联关系没有 更新
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);
......@@ -182,15 +194,15 @@ public class OrgSyncByWxcpJob {
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
return workDataDto;
}
/**
* @description: 整理 部门与员工关联关系 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:08 下午
*/
* @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<>();
......@@ -204,7 +216,7 @@ public class OrgSyncByWxcpJob {
List<WxCpUser> wxCpUserList = wxCpDepartDto.getWxCpUserList();
for (WxCpUser wxCpUser : wxCpUserList) {
// key department_id + '&' + employee_id
hashMap.put(orgId + "_" + wxCpDepartDto.getId()+ "&" + corpId + "_" + wxCpUser.getUserId(), wxCpUser);
hashMap.put(orgId + "_" + wxCpDepartDto.getId() + "&" + corpId + "_" + wxCpUser.getUserId(), wxCpUser);
}
}
......@@ -219,7 +231,7 @@ public class OrgSyncByWxcpJob {
continue;
}
//更新
String type = wxCpUser.getIsLeader() == 1 ? EmployeeDutyEnum.DIRECTOR.code: EmployeeDutyEnum.STAFF.code;
String type = wxCpUser.getIsLeader() == 1 ? EmployeeDutyEnum.DIRECTOR.code : EmployeeDutyEnum.STAFF.code;
if (!departmentEmployee.getType().equals(type)) {
departmentEmployee.setType(type);
updateList.add(departmentEmployee);
......@@ -228,7 +240,7 @@ public class OrgSyncByWxcpJob {
}
for (String s : hashMap.keySet()) {
WxCpUser wxCpUser = hashMap.get(s);
String type = wxCpUser.getIsLeader() == 1 ? EmployeeDutyEnum.DIRECTOR.code: EmployeeDutyEnum.STAFF.code;
String type = wxCpUser.getIsLeader() == 1 ? EmployeeDutyEnum.DIRECTOR.code : EmployeeDutyEnum.STAFF.code;
String[] split = s.split("&");
String departmentId = split[0];
String employeeId = split[1];
......@@ -242,14 +254,14 @@ public class OrgSyncByWxcpJob {
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
return workDataDto;
}
/**
* @description: 整理 部门机构 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:09 下午
*/
* @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<>();
......@@ -274,10 +286,10 @@ public class OrgSyncByWxcpJob {
}
//更新
Long parentId1 = wxCpDepartDto.getParentId();
String parentId = ObjectUtil.isNull(hashMap2.get(parentId1)) ? null :orgId + "_" + parentId1;
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.getMembers().equals(wxCpDepartDto.getMembers()) || !level.equals(department.getLevel())) {
department.setDepartmentName(wxCpDepartDto.getName());
department.setParentDepartId(parentId);
department.setMembers(wxCpDepartDto.getMembers());
......@@ -290,7 +302,7 @@ public class OrgSyncByWxcpJob {
for (String departmentId : hashMap.keySet()) {
WxCpDepartDto wxCpDepartDto = hashMap.get(departmentId);
Long parentId1 = wxCpDepartDto.getParentId();
String parentId = ObjectUtil.isNull(hashMap2.get(parentId1)) ? null :orgId + "_" + parentId1;
String parentId = ObjectUtil.isNull(hashMap2.get(parentId1)) ? null : orgId + "_" + parentId1;
DepartmentEntity department = new DepartmentEntity();
department.setDepartmentId(orgId + "_" + wxCpDepartDto.getId());
department.setParentDepartId(parentId);
......@@ -303,27 +315,27 @@ public class OrgSyncByWxcpJob {
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
return workDataDto;
}
/**
* @description: 获取部门等级
* @Author: zejia zj wu
* @date: 2021/5/27 3:09 下午
*/
* @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);
return 1 + getLevel(hashMap2, parentId);
}
/**
* @description: 整理 员工 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:09 下午
*/
* @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<>();
......@@ -381,14 +393,14 @@ public class OrgSyncByWxcpJob {
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
return workDataDto;
}
/**
* @description: 整理 用户表 数据
* @Author: zejia zj wu
* @date: 2021/5/27 3:10 下午
*/
* @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<>();
......@@ -458,38 +470,37 @@ public class OrgSyncByWxcpJob {
userInfo.setUiRegisterTime(userInfo.getCreatetime());
userInfo.setUiChannel(null);
userInfo.setWxcpUid(wxCpUser.getUserId());
userInfo.setUiWechatXcxQrcode(orgSyncService.createWechatXcxQrcode(userInfo.getId(),userInfo.getUiHeadimgMp()));
userInfo.setUiWechatXcxQrcode(orgSyncService.createWechatXcxQrcode(userInfo.getId(), userInfo.getUiHeadimgMp()));
insertList.add(userInfo);
}
workDataDto.setDeleteList(deleteList);
workDataDto.setInsertList(insertList);
workDataDto.setUpdateList(updateList);
return workDataDto;
return workDataDto;
}
/**
* @description: 判断是否需要执行 并返回数据MD5
* @Author: zejia zj wu
* @date: 2021/5/27 3:10 下午
*/
* @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)){
if (digestHex.equals(md5WxcpData)) {
return null;
}
return digestHex;
}
/**
* @description: 获取 企业微信 用户列表信息
* @Author: zejia zj wu
* @date: 2021/5/27 3:11 下午
*/
* @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);
......@@ -511,10 +522,10 @@ public class OrgSyncByWxcpJob {
}
/**
* @description: 获取 企业微信 部门聊表信息
* @Author: zejia zj wu
* @date: 2021/5/27 3:11 下午
*/
* @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);
......@@ -533,7 +544,7 @@ public class OrgSyncByWxcpJob {
WxCpDefaultConfigImpl wxCpDefaultConfig = JSONUtil.toBean(keyStr, WxCpDefaultConfigImpl.class);
List<WxCpDepart> wxCpDepartList = orgSyncByWxcpJob.getWxCpDepartList(wxCpDefaultConfig); // 机构信息
List<WxCpDepartDto> wxCpDepartDtoList = orgSyncByWxcpJob.getWxCpUserList(wxCpDepartList,wxCpDefaultConfig);
List<WxCpDepartDto> wxCpDepartDtoList = orgSyncByWxcpJob.getWxCpUserList(wxCpDepartList, wxCpDefaultConfig);
log.info("=========================");
// log.info(JSONUtil.toJsonStr(wxCpDepartDtoList));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment