|
@@ -423,12 +423,14 @@ public class WeiXinCorpController {
|
|
|
// "SuiteId":"ww4e237fd6abb635af","InfoType":"change_contact","AuthCorpId":"wwf11426cf618e1703",
|
|
|
// "TimeStamp":1655908762,"OpenUserID":"woy9TkCAAApdqSxsfJbmK4cBJhbzI5Ug"}}
|
|
|
String changeType = jsonObject.getString("ChangeType");
|
|
|
+ String corpId = jsonObject.getString("AuthCorpId");
|
|
|
+ WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
|
+ if (wxCorpInfo == null) return "success";
|
|
|
+ Integer companyId = wxCorpInfo.getCompanyId();
|
|
|
if ("delete_user".equals(changeType)) {
|
|
|
//监听到员工离职
|
|
|
- String corpId = jsonObject.getString("AuthCorpId");
|
|
|
- String corpWxUserId = jsonObject.getString("UserID");
|
|
|
- Integer companyId = wxCorpInfoMapper.selectById(corpId).getCompanyId();
|
|
|
- User user = userMapper.selectOne(new QueryWrapper<User>().eq("corpwx_userid", corpWxUserId).eq("company_id", companyId));
|
|
|
+ String openUserId = jsonObject.getString("OpenUserID");
|
|
|
+ User user = userMapper.selectOne(new QueryWrapper<User>().eq("corpwx_userid", openUserId).eq("company_id", companyId));
|
|
|
if (user != null && user.getIsActive() == 1) {
|
|
|
user.setIsActive(0);
|
|
|
user.setInactiveDate(LocalDate.now());
|
|
@@ -436,12 +438,36 @@ public class WeiXinCorpController {
|
|
|
}
|
|
|
} else if ("create_user".equals(changeType)) {
|
|
|
//新增员工
|
|
|
- String corpId = jsonObject.getString("AuthCorpId");
|
|
|
+ SysRole defaultRole = sysRoleMapper.selectOne(new QueryWrapper<SysRole>().eq("company_id", companyId).eq("is_default", 1));
|
|
|
String corpWxUserId = jsonObject.getString("UserID");
|
|
|
- WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
|
- if (!StringUtils.isEmpty(wxCorpInfo.getContactSecret()) && !StringUtils.isEmpty(wxCorpInfo.getContactSecret())) {
|
|
|
- Integer companyId = wxCorpInfo.getCompanyId();
|
|
|
- SysRole defaultRole = sysRoleMapper.selectOne(new QueryWrapper<SysRole>().eq("company_id", companyId).eq("is_default", 1));
|
|
|
+ String openUserId = jsonObject.getString("OpenUserID");
|
|
|
+ if (wxCorpInfo.getSaasSyncContact() == 1) {
|
|
|
+ //直接通过企业微信通讯录同步
|
|
|
+ JSONObject userObj = getUserInfo(getCorpAccessToken(wxCorpInfo), corpWxUserId);
|
|
|
+ //成功获取到通讯录的个人详情
|
|
|
+ Long id = SnowFlake.nextId();
|
|
|
+ JSONArray department = userObj.getJSONArray("department");
|
|
|
+ Integer curUserWXDeptid = getMaxDeptIdFromArray(department);
|
|
|
+ Department sysDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", curUserWXDeptid));
|
|
|
+
|
|
|
+ User user = new User()
|
|
|
+ .setId(id.toString())
|
|
|
+ .setRoleId(defaultRole.getId())
|
|
|
+ .setRoleName(defaultRole.getRolename())
|
|
|
+ .setName(userObj.getString("name"))
|
|
|
+ .setPhone(userObj.getString("mobile"))
|
|
|
+ .setPassword(MD5Util.getPassword("000000"))
|
|
|
+ .setCorpwxUserid(openUserId)
|
|
|
+ .setJobNumber(userObj.getString("userid"))
|
|
|
+ .setColor(ColorUtil.randomColor())
|
|
|
+ .setCompanyId(companyId);
|
|
|
+ if (sysDept != null) {
|
|
|
+ user.setDepartmentId(sysDept.getDepartmentId());
|
|
|
+ List<Department> allDeptList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId));
|
|
|
+ user.setDepartmentCascade(convertDepartmentIdToCascade(user.getDepartmentId(), allDeptList));
|
|
|
+ userMapper.insert(user);
|
|
|
+ }
|
|
|
+ } else if (!StringUtils.isEmpty(wxCorpInfo.getContactSecret()) && !StringUtils.isEmpty(wxCorpInfo.getContactServer())) {
|
|
|
//通过通讯录secret获取到员工姓名
|
|
|
String remoteCorpConcactAccessToken = getRemoteCorpConcactAccessToken(wxCorpInfo);
|
|
|
String curCorpAccessToken = getCorpAccessToken(wxCorpInfo);
|
|
@@ -462,30 +488,60 @@ public class WeiXinCorpController {
|
|
|
.setName(userObj.getString("name"))
|
|
|
.setPhone(userObj.getString("mobile"))
|
|
|
.setPassword(MD5Util.getPassword("000000"))
|
|
|
- .setCorpwxUserid(corpWxUserId)
|
|
|
+ .setCorpwxUserid(openUserId)
|
|
|
.setJobNumber(userObj.getString("userid"))
|
|
|
.setColor(ColorUtil.randomColor())
|
|
|
.setCompanyId(companyId);
|
|
|
if (sysDept != null) {
|
|
|
user.setDepartmentId(sysDept.getDepartmentId());
|
|
|
}
|
|
|
- if (userObj.containsKey("direct_leader") && userObj.getJSONArray("direct_leader").size() > 0) {
|
|
|
- String directLeader = userObj.getJSONArray("direct_leader").getString(0);
|
|
|
- User leader = userMapper.selectOne(new QueryWrapper<User>().eq("company_id", companyId).eq("corpwx_userid", directLeader));
|
|
|
- if (leader != null) {
|
|
|
- user.setSuperiorId(leader.getId());
|
|
|
- }
|
|
|
- }
|
|
|
+// if (userObj.containsKey("direct_leader") && userObj.getJSONArray("direct_leader").size() > 0) {
|
|
|
+// String directLeader = userObj.getJSONArray("direct_leader").getString(0);
|
|
|
+// User leader = userMapper.selectOne(new QueryWrapper<User>().eq("company_id", companyId).eq("corpwx_userid", directLeader));
|
|
|
+// if (leader != null) {
|
|
|
+// user.setSuperiorId(leader.getId());
|
|
|
+// }
|
|
|
+// }
|
|
|
userMapper.insert(user);
|
|
|
}
|
|
|
}
|
|
|
} else if ("update_user".equals(changeType)) {
|
|
|
- String corpId = jsonObject.getString("AuthCorpId");
|
|
|
String corpWxUserId = jsonObject.getString("UserID");
|
|
|
- WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
|
- Integer companyId = wxCorpInfo.getCompanyId();
|
|
|
+ String openUserId = jsonObject.getString("OpenUserID");
|
|
|
+ if (wxCorpInfo.getSaasSyncContact() == 1) {
|
|
|
+ Integer curUserWXDeptid = 0;
|
|
|
+ if (jsonObject.has("Department")) {
|
|
|
+ String department = jsonObject.getString("Department");
|
|
|
+ if (department.contains(",")) {
|
|
|
+ String[] split = department.split(",");
|
|
|
+ curUserWXDeptid = getMaxDeptIdFromArray(split);
|
|
|
+ } else {
|
|
|
+ curUserWXDeptid = Integer.valueOf(department);
|
|
|
+ }
|
|
|
+ } else if (jsonObject.has("MainDepartment")) {
|
|
|
+ //取主部门
|
|
|
+ curUserWXDeptid = jsonObject.getInt("MainDepartment");
|
|
|
+ } else {
|
|
|
+ System.out.println("部门未变更,不处理");
|
|
|
+ }
|
|
|
+ if (curUserWXDeptid != 0) {
|
|
|
+ User user = userMapper.selectOne(new QueryWrapper<User>().eq("company_id", companyId).eq("corpwx_userid", openUserId));
|
|
|
+ User changeUser = new User();
|
|
|
+ changeUser.setId(user.getId());
|
|
|
+ if (!curUserWXDeptid.equals(user.getCorpwxDeptid())) {
|
|
|
+ changeUser.setCorpwxDeptid(curUserWXDeptid);
|
|
|
+ Department department = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", curUserWXDeptid));
|
|
|
+ if (department != null) {
|
|
|
+ changeUser.setDepartmentId(department.getDepartmentId());
|
|
|
+ List<Department> allDeptList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId));
|
|
|
+ changeUser.setDepartmentCascade(convertDepartmentIdToCascade(changeUser.getDepartmentId(), allDeptList));
|
|
|
+ userMapper.updateById(changeUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
//只有授权通讯录同步的,才有机会更新部门或者上级
|
|
|
- if (!StringUtils.isEmpty(wxCorpInfo.getContactSecret()) && !StringUtils.isEmpty(wxCorpInfo.getContactSecret())) {
|
|
|
+ else if (!StringUtils.isEmpty(wxCorpInfo.getContactSecret()) && !StringUtils.isEmpty(wxCorpInfo.getContactServer())) {
|
|
|
String remoteCorpConcactAccessToken = getRemoteCorpConcactAccessToken(wxCorpInfo);
|
|
|
String curCorpAccessToken = getCorpAccessToken(wxCorpInfo);
|
|
|
Integer curUserWXDeptid = 0;
|
|
@@ -540,14 +596,24 @@ public class WeiXinCorpController {
|
|
|
}
|
|
|
}
|
|
|
} else if ("create_party".equals(changeType)) {
|
|
|
- //创建部门,调用中转服务接口获取部门详情
|
|
|
- String corpId = jsonObject.getString("AuthCorpId");
|
|
|
Integer deptId = jsonObject.getInt("Id");
|
|
|
Integer parentDeptId = jsonObject.getInt("ParentId");
|
|
|
- WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
|
- if (wxCorpInfo != null && !StringUtils.isEmpty(wxCorpInfo.getContactSecret()) && !StringUtils.isEmpty(wxCorpInfo.getContactSecret())) {
|
|
|
+ if (wxCorpInfo.getSaasSyncContact() == 1) {
|
|
|
+ Department department = new Department();
|
|
|
+ department.setCompanyId(companyId);
|
|
|
+ department.setCorpwxDeptid(deptId);
|
|
|
+ department.setCorpwxDeptpid(parentDeptId);
|
|
|
+ department.setDepartmentName(jsonObject.getString("Name"));
|
|
|
+ if (parentDeptId != 1) {
|
|
|
+ Department parentDept = department.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", parentDeptId));
|
|
|
+ if (parentDept != null) {
|
|
|
+ department.setSuperiorId(parentDept.getDepartmentId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ departmentMapper.insert(department);
|
|
|
+ } else if (wxCorpInfo != null && !StringUtils.isEmpty(wxCorpInfo.getContactSecret()) && !StringUtils.isEmpty(wxCorpInfo.getContactSecret())) {
|
|
|
+ //创建部门,调用中转服务接口获取部门详情
|
|
|
String remoteCorpConcactAccessToken = getRemoteCorpConcactAccessToken(wxCorpInfo);
|
|
|
- Integer companyId = wxCorpInfo.getCompanyId();
|
|
|
Department department = new Department();
|
|
|
department.setCompanyId(companyId);
|
|
|
department.setCorpwxDeptid(deptId);
|
|
@@ -566,11 +632,21 @@ public class WeiXinCorpController {
|
|
|
}
|
|
|
} else if ("update_party".equals(changeType)) {
|
|
|
//变更部门,父部门变化
|
|
|
- String corpId = jsonObject.getString("AuthCorpId");
|
|
|
Integer deptId = jsonObject.getInt("Id");
|
|
|
Integer parentDeptId = jsonObject.getInt("ParentId");
|
|
|
- WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
|
- if (wxCorpInfo != null && !StringUtils.isEmpty(wxCorpInfo.getContactSecret()) && !StringUtils.isEmpty(wxCorpInfo.getContactSecret())) {
|
|
|
+ if (wxCorpInfo.getSaasSyncContact() == 1) {
|
|
|
+ if (parentDeptId != null) {
|
|
|
+ //发生父部门的结构变化了
|
|
|
+ Department department = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", wxCorpInfo.getCompanyId()).eq("corpwx_deptid", deptId));
|
|
|
+ if (department != null) {
|
|
|
+ Department parentDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", wxCorpInfo.getCompanyId()).eq("corpwx_deptid", parentDeptId));
|
|
|
+ if (parentDept != null) {
|
|
|
+ department.setSuperiorId(parentDept.getDepartmentId());
|
|
|
+ departmentMapper.updateById(department);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (wxCorpInfo != null && !StringUtils.isEmpty(wxCorpInfo.getContactSecret()) && !StringUtils.isEmpty(wxCorpInfo.getContactServer())) {
|
|
|
if (parentDeptId != null) {
|
|
|
//发生父部门的结构变化了
|
|
|
Department department = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", wxCorpInfo.getCompanyId()).eq("corpwx_deptid", deptId));
|
|
@@ -1424,6 +1500,7 @@ public class WeiXinCorpController {
|
|
|
JSONObject userJson = usersUnderRootArray.getJSONObject(m);
|
|
|
System.out.println(userJson.toString());
|
|
|
String curUserid = userJson.getString("userid");
|
|
|
+ String openUserid = userJson.getString("open_userid");
|
|
|
//跳过非激活状态的员工
|
|
|
if (userJson.getInteger("status") != 1) continue;
|
|
|
//不存在的人员, 进行插入
|
|
@@ -1441,7 +1518,7 @@ public class WeiXinCorpController {
|
|
|
.setCompanyId(companyId)
|
|
|
.setName(userJson.getString("name"))
|
|
|
.setPhone(userJson.getString("mobile"))
|
|
|
- .setCorpwxUserid(curUserid)
|
|
|
+ .setCorpwxUserid(openUserid)
|
|
|
.setColor(ColorUtil.randomColor())
|
|
|
.setJobNumber(curUserid)
|
|
|
.setCorpwxDeptid(maxDeptId);
|
|
@@ -1481,10 +1558,11 @@ public class WeiXinCorpController {
|
|
|
for (int m=0;m<userList.size(); m++) {
|
|
|
JSONObject userJson = userList.getJSONObject(m);
|
|
|
String curUserid = userJson.getString("userid");
|
|
|
+ String openUserid = userJson.getString("open_userid");
|
|
|
System.out.println("人员信息:"+userJson.toString());
|
|
|
//不存在的人员, 进行插入
|
|
|
User user = new User();
|
|
|
- Optional<User> first = allCorpWxUserList.stream().filter(all -> all.getCorpwxUserid().equals(curUserid)).findFirst();
|
|
|
+ Optional<User> first = allCorpWxUserList.stream().filter(all -> all.getCorpwxUserid().equals(openUserid)).findFirst();
|
|
|
if (first.isPresent()) {
|
|
|
user = first.get();
|
|
|
user.setCorpwxDeptid(deptId);
|
|
@@ -1495,9 +1573,10 @@ public class WeiXinCorpController {
|
|
|
.setRoleName(defaultRole.getRolename())
|
|
|
.setCompanyId(companyId)
|
|
|
.setName(userJson.getString("name"))
|
|
|
- .setCorpwxUserid(curUserid)
|
|
|
+ .setCorpwxUserid(openUserid)
|
|
|
.setColor(ColorUtil.randomColor())
|
|
|
- .setJobNumber(curUserid);
|
|
|
+ .setJobNumber(curUserid)
|
|
|
+ .setCorpwxDeptid(curDept.getCorpwxDeptid());
|
|
|
allCorpWxUserList.add(user);
|
|
|
}
|
|
|
}
|
|
@@ -1512,7 +1591,7 @@ public class WeiXinCorpController {
|
|
|
List<User> newUserList = allCorpWxUserList.stream().filter(newItem -> !existingUsers.stream().anyMatch(existingItem -> newItem.getCorpwxUserid().equals(existingItem.getCorpwxUserid()))).collect(Collectors.toList());
|
|
|
newUserList.forEach(newItem->{
|
|
|
if (newItem.getCorpwxDeptid() != 1) {
|
|
|
- int deptId = allDeptList.stream().filter(all->all.getCorpwxDeptid().equals(newItem.getCorpwxDeptid())).findFirst().get().getDepartmentId();
|
|
|
+ Integer deptId = allDeptList.stream().filter(all->all.getCorpwxDeptid() != null && all.getCorpwxDeptid().equals(newItem.getCorpwxDeptid())).findFirst().get().getDepartmentId();
|
|
|
newItem.setDepartmentId(deptId);
|
|
|
newItem.setDepartmentCascade(convertDepartmentIdToCascade(deptId, allDeptList));
|
|
|
}
|
|
@@ -1520,28 +1599,41 @@ public class WeiXinCorpController {
|
|
|
if (newUserList.size() > 0) {
|
|
|
userService.saveBatch(newUserList);
|
|
|
}
|
|
|
- List<User> updateUserList = allCorpWxUserList.stream().filter(newItem->existingUsers.stream().anyMatch(existingItem->newItem.getCorpwxUserid().equals(existingItem.getCorpwxUserid())
|
|
|
- && (!newItem.getName().equals(existingItem.getName()) || (newItem.getCorpwxDeptid() != null && !newItem.getCorpwxDeptid().equals(existingItem.getCorpwxDeptid()))))).collect(Collectors.toList());
|
|
|
- List<Integer> corpWxDeptIds = updateUserList.stream().map(User::getCorpwxDeptid).collect(Collectors.toList());
|
|
|
- List<Department> departmentList = null;
|
|
|
- if (corpWxDeptIds.size() > 0) {
|
|
|
- departmentList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId).in("corpwx_deptid", corpWxDeptIds));
|
|
|
- }
|
|
|
- final List<Department> fDeptList = departmentList;
|
|
|
- updateUserList.forEach(u->{
|
|
|
- if (u.getCorpwxDeptid() != 1) {
|
|
|
- if (fDeptList != null) {
|
|
|
- u.setDepartmentId(fDeptList.stream().filter(d->d.getCorpwxDeptid().equals(u.getCorpwxDeptid())).findFirst().get().getDepartmentId());
|
|
|
+ //姓名,部门,账号的同步更新
|
|
|
+ List<User> finalUpdateUserList = new ArrayList<>();
|
|
|
+ for (User userItem : allCorpWxUserList) {
|
|
|
+ Optional<User> first = existingUsers.stream().filter(ex -> userItem.getCorpwxUserid().equals(ex.getCorpwxUserid())).findFirst();
|
|
|
+ if (first.isPresent()) {
|
|
|
+ User oldUser = first.get();
|
|
|
+ User changeUser = new User();
|
|
|
+ if (!userItem.getJobNumber().equals(oldUser.getJobNumber())) {
|
|
|
+ changeUser.setId(oldUser.getId());
|
|
|
+ changeUser.setJobNumber(userItem.getJobNumber());
|
|
|
+ }
|
|
|
+ if (!userItem.getName().equals(oldUser.getName())) {
|
|
|
+ changeUser.setId(oldUser.getId());
|
|
|
+ changeUser.setName(oldUser.getName());
|
|
|
+ }
|
|
|
+ if (!userItem.getCorpwxDeptid().equals(oldUser.getCorpwxDeptid())) {
|
|
|
+ changeUser.setId(oldUser.getId());
|
|
|
+ if (userItem.getCorpwxDeptid() != 1) {
|
|
|
+ changeUser.setDepartmentId(allDeptList.stream().filter(d->d.getCorpwxDeptid().equals(userItem.getCorpwxDeptid())).findFirst().get().getDepartmentId());
|
|
|
+ //设置层级部门
|
|
|
+ changeUser.setDepartmentCascade(convertDepartmentIdToCascade(changeUser.getDepartmentId(), allDeptList));
|
|
|
+ } else {
|
|
|
+ changeUser.setDepartmentId(0);
|
|
|
+ changeUser.setDepartmentCascade("0");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (changeUser.getId() != null) {
|
|
|
+ //有变动
|
|
|
+ finalUpdateUserList.add(changeUser);
|
|
|
}
|
|
|
- //设置层级部门
|
|
|
- u.setDepartmentCascade(convertDepartmentIdToCascade(u.getDepartmentId(), allDeptList));
|
|
|
- } else {
|
|
|
- u.setDepartmentId(0);
|
|
|
- u.setDepartmentCascade("0");
|
|
|
}
|
|
|
- });
|
|
|
- if (updateUserList.size() > 0) {
|
|
|
- userService.updateBatchById(updateUserList);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (finalUpdateUserList.size() > 0) {
|
|
|
+ userService.updateBatchById(finalUpdateUserList);
|
|
|
}
|
|
|
return new HttpRespMsg();
|
|
|
}
|