Commit a06d14b2 authored by 钱坤's avatar 钱坤

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

parent fe3bbea8
...@@ -21,6 +21,8 @@ import me.chanjar.weixin.cp.bean.Gender; ...@@ -21,6 +21,8 @@ import me.chanjar.weixin.cp.bean.Gender;
import me.chanjar.weixin.cp.bean.WxCpDepart; import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser; import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; 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.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -30,6 +32,7 @@ import java.util.ArrayList; ...@@ -30,6 +32,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @author zejia zj wu * @author zejia zj wu
...@@ -62,8 +65,8 @@ public class OrgSyncByWxcpJob { ...@@ -62,8 +65,8 @@ public class OrgSyncByWxcpJob {
@Value("${tanpu.sync.job.skipped:true}") @Value("${tanpu.sync.job.skipped:true}")
private boolean jobSkipped; private boolean jobSkipped;
@Scheduled(cron="0 30 6,10,15 * * ?") //每日凌晨6点30执行 @Scheduled(cron = "0 30 6,10,15 * * ?") //每日凌晨6点30执行
public void execute(){ public void execute() {
log.info("====== 开始执行OrgSyncByWxcpJob ======"); log.info("====== 开始执行OrgSyncByWxcpJob ======");
try { try {
if (!jobSkipped) { if (!jobSkipped) {
...@@ -77,6 +80,7 @@ public class OrgSyncByWxcpJob { ...@@ -77,6 +80,7 @@ public class OrgSyncByWxcpJob {
/** /**
* 执行 部门 及 员工 同步任务 * 执行 部门 及 员工 同步任务
*
* @param isAuto 任务触发类型 Y 定时任务 / N 手动 * @param isAuto 任务触发类型 Y 定时任务 / N 手动
*/ */
public void orgSyncByWxcp(String isAuto) { public void orgSyncByWxcp(String isAuto) {
...@@ -97,7 +101,7 @@ public class OrgSyncByWxcpJob { ...@@ -97,7 +101,7 @@ public class OrgSyncByWxcpJob {
List<WxCpDepartDto> wxCpDepartDtoList;//自定义的 部门信息 List<WxCpDepartDto> wxCpDepartDtoList;//自定义的 部门信息
try { try {
wxCpDepartList = getWxCpDepartList(wxCpDefaultConfig); wxCpDepartList = getWxCpDepartList(wxCpDefaultConfig);
wxCpDepartDtoList = getWxCpUserList(wxCpDepartList,wxCpDefaultConfig); wxCpDepartDtoList = getWxCpUserList(wxCpDepartList, wxCpDefaultConfig);
} catch (Exception e) { } catch (Exception e) {
log.error("======从企业微信获取组织信息及员工信息失败->机构:{} -> 错误信息-> {}======", orgId, e.getMessage()); log.error("======从企业微信获取组织信息及员工信息失败->机构:{} -> 错误信息-> {}======", orgId, e.getMessage());
continue; continue;
...@@ -144,7 +148,7 @@ public class OrgSyncByWxcpJob { ...@@ -144,7 +148,7 @@ public class OrgSyncByWxcpJob {
//获取 数据库 部门员工关系 信息 //获取 数据库 部门员工关系 信息
List<EmployeeRoleEntity> employeeRoleList = employeeRoleService.findInfoByOrgId(orgId); List<EmployeeRoleEntity> employeeRoleList = employeeRoleService.findInfoByOrgId(orgId);
// 获取 role // 获取 role
Map<String, String> roleMap =roleService.findInfoNoAdmin(); Map<String, String> roleMap = roleService.findInfoNoAdmin();
// 预处理 新数据 // 预处理 新数据
HashMap<String, WxCpUser> hashMap = new HashMap<>(); HashMap<String, WxCpUser> hashMap = new HashMap<>();
for (WxCpDepartDto wxCpDepartDto : wxCpDepartDtoList) { for (WxCpDepartDto wxCpDepartDto : wxCpDepartDtoList) {
...@@ -169,10 +173,18 @@ public class OrgSyncByWxcpJob { ...@@ -169,10 +173,18 @@ public class OrgSyncByWxcpJob {
// 员工 角色关联关系没有 更新 // 员工 角色关联关系没有 更新
hashMap.remove(key); 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()) { for (String key : hashMap.keySet()) {
String[] split = key.split("&"); String[] split = key.split("&");
String employeeId = split[0]; String employeeId = split[0];
String roleId = split[1]; 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(); EmployeeRoleEntity employeeRole = new EmployeeRoleEntity();
employeeRole.setEmployeeId(employeeId); employeeRole.setEmployeeId(employeeId);
employeeRole.setRoleId(roleId); employeeRole.setRoleId(roleId);
...@@ -204,7 +216,7 @@ public class OrgSyncByWxcpJob { ...@@ -204,7 +216,7 @@ public class OrgSyncByWxcpJob {
List<WxCpUser> wxCpUserList = wxCpDepartDto.getWxCpUserList(); List<WxCpUser> wxCpUserList = wxCpDepartDto.getWxCpUserList();
for (WxCpUser wxCpUser : wxCpUserList) { for (WxCpUser wxCpUser : wxCpUserList) {
// key department_id + '&' + employee_id // 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 { ...@@ -219,7 +231,7 @@ public class OrgSyncByWxcpJob {
continue; 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)) { if (!departmentEmployee.getType().equals(type)) {
departmentEmployee.setType(type); departmentEmployee.setType(type);
updateList.add(departmentEmployee); updateList.add(departmentEmployee);
...@@ -228,7 +240,7 @@ public class OrgSyncByWxcpJob { ...@@ -228,7 +240,7 @@ public class OrgSyncByWxcpJob {
} }
for (String s : hashMap.keySet()) { for (String s : hashMap.keySet()) {
WxCpUser wxCpUser = hashMap.get(s); 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[] split = s.split("&");
String departmentId = split[0]; String departmentId = split[0];
String employeeId = split[1]; String employeeId = split[1];
...@@ -274,7 +286,7 @@ public class OrgSyncByWxcpJob { ...@@ -274,7 +286,7 @@ public class OrgSyncByWxcpJob {
} }
//更新 //更新
Long parentId1 = wxCpDepartDto.getParentId(); 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()); Integer level = getLevel(hashMap2, wxCpDepartDto.getId());
if (!department.getDepartmentName().equals(wxCpDepartDto.getName()) || StrUtil.compareIgnoreCase(department.getParentDepartId(), parentId, false) != 0 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())) {
...@@ -290,7 +302,7 @@ public class OrgSyncByWxcpJob { ...@@ -290,7 +302,7 @@ public class OrgSyncByWxcpJob {
for (String departmentId : hashMap.keySet()) { for (String departmentId : hashMap.keySet()) {
WxCpDepartDto wxCpDepartDto = hashMap.get(departmentId); WxCpDepartDto wxCpDepartDto = hashMap.get(departmentId);
Long parentId1 = wxCpDepartDto.getParentId(); 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(); DepartmentEntity department = new DepartmentEntity();
department.setDepartmentId(orgId + "_" + wxCpDepartDto.getId()); department.setDepartmentId(orgId + "_" + wxCpDepartDto.getId());
department.setParentDepartId(parentId); department.setParentDepartId(parentId);
...@@ -458,7 +470,7 @@ public class OrgSyncByWxcpJob { ...@@ -458,7 +470,7 @@ public class OrgSyncByWxcpJob {
userInfo.setUiRegisterTime(userInfo.getCreatetime()); userInfo.setUiRegisterTime(userInfo.getCreatetime());
userInfo.setUiChannel(null); userInfo.setUiChannel(null);
userInfo.setWxcpUid(wxCpUser.getUserId()); userInfo.setWxcpUid(wxCpUser.getUserId());
userInfo.setUiWechatXcxQrcode(orgSyncService.createWechatXcxQrcode(userInfo.getId(),userInfo.getUiHeadimgMp())); userInfo.setUiWechatXcxQrcode(orgSyncService.createWechatXcxQrcode(userInfo.getId(), userInfo.getUiHeadimgMp()));
insertList.add(userInfo); insertList.add(userInfo);
} }
workDataDto.setDeleteList(deleteList); workDataDto.setDeleteList(deleteList);
...@@ -468,7 +480,6 @@ public class OrgSyncByWxcpJob { ...@@ -468,7 +480,6 @@ public class OrgSyncByWxcpJob {
} }
/** /**
* @description: 判断是否需要执行 并返回数据MD5 * @description: 判断是否需要执行 并返回数据MD5
* @Author: zejia zj wu * @Author: zejia zj wu
...@@ -479,7 +490,7 @@ public class OrgSyncByWxcpJob { ...@@ -479,7 +490,7 @@ public class OrgSyncByWxcpJob {
if (!"Y".equals(isAuto)) { if (!"Y".equals(isAuto)) {
return digestHex; return digestHex;
} }
if (digestHex.equals(md5WxcpData)){ if (digestHex.equals(md5WxcpData)) {
return null; return null;
} }
return digestHex; return digestHex;
...@@ -533,7 +544,7 @@ public class OrgSyncByWxcpJob { ...@@ -533,7 +544,7 @@ public class OrgSyncByWxcpJob {
WxCpDefaultConfigImpl wxCpDefaultConfig = JSONUtil.toBean(keyStr, WxCpDefaultConfigImpl.class); WxCpDefaultConfigImpl wxCpDefaultConfig = JSONUtil.toBean(keyStr, WxCpDefaultConfigImpl.class);
List<WxCpDepart> wxCpDepartList = orgSyncByWxcpJob.getWxCpDepartList(wxCpDefaultConfig); // 机构信息 List<WxCpDepart> wxCpDepartList = orgSyncByWxcpJob.getWxCpDepartList(wxCpDefaultConfig); // 机构信息
List<WxCpDepartDto> wxCpDepartDtoList = orgSyncByWxcpJob.getWxCpUserList(wxCpDepartList,wxCpDefaultConfig); List<WxCpDepartDto> wxCpDepartDtoList = orgSyncByWxcpJob.getWxCpUserList(wxCpDepartList, wxCpDefaultConfig);
log.info("========================="); log.info("=========================");
// log.info(JSONUtil.toJsonStr(wxCpDepartDtoList)); // 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