|
@@ -533,6 +533,7 @@ public class WeiXinCorpController {
|
|
|
JSONObject userObj = getUserInfo(getCorpAccessToken(wxCorpInfo), corpWxUserId);
|
|
|
//成功获取到通讯录的个人详情
|
|
|
Long id = SnowFlake.nextId();
|
|
|
+ //通过getUserInfo接口获取到的json key是小写的
|
|
|
JSONArray department = userObj.getJSONArray("department");
|
|
|
Integer curUserWXDeptid = getMaxDeptIdFromArray(department);
|
|
|
Department sysDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", curUserWXDeptid));
|
|
@@ -556,11 +557,20 @@ public class WeiXinCorpController {
|
|
|
userMapper.insert(user);
|
|
|
}
|
|
|
} else if (!StringUtils.isEmpty(wxCorpInfo.getContactSecret()) && !StringUtils.isEmpty(wxCorpInfo.getContactServer())) {
|
|
|
- //通过通讯录secret获取到员工姓名
|
|
|
+ //通过通讯录secret获取到员工姓名;这是从内部通讯录的
|
|
|
String remoteCorpConcactAccessToken = getRemoteCorpConcactAccessToken(wxCorpInfo);
|
|
|
String curCorpAccessToken = getCorpAccessToken(wxCorpInfo);
|
|
|
- org.json.JSONArray departmentArray = jsonObject.getJSONArray("department");
|
|
|
- Integer curUserWXDeptid = departmentArray.getInt(departmentArray.length() - 1);
|
|
|
+ Integer curUserWXDeptid = 1;
|
|
|
+ if (jsonObject.has("Department")) {
|
|
|
+ if (jsonObject.get("Department") instanceof String) {
|
|
|
+ String jsonObject1 = jsonObject.getString("Department");
|
|
|
+ curUserWXDeptid = Integer.parseInt(jsonObject1);
|
|
|
+ } else if (jsonObject.get("Department") instanceof org.json.JSONArray) {
|
|
|
+ org.json.JSONArray departmentArray = jsonObject.getJSONArray("Department");
|
|
|
+ curUserWXDeptid = getMaxDeptIdFromArray(departmentArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
JSONObject userObj = remoteGetUserDetail(wxCorpInfo, remoteCorpConcactAccessToken, corpWxUserId, curUserWXDeptid, curCorpAccessToken);
|
|
|
if (userObj != null) {
|
|
|
//成功获取到通讯录的个人详情
|
|
@@ -580,17 +590,13 @@ public class WeiXinCorpController {
|
|
|
.setCorpwxRealUserid(corpWxUserId)
|
|
|
.setJobNumber(userObj.getString("userid"))
|
|
|
.setColor(ColorUtil.randomColor())
|
|
|
+ .setCorpwxDeptid(curUserWXDeptid)
|
|
|
.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));
|
|
|
}
|
|
|
-// 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);
|
|
|
}
|
|
|
}
|
|
@@ -656,7 +662,7 @@ public class WeiXinCorpController {
|
|
|
curUserWXDeptid = getMaxDeptIdFromArray(department);
|
|
|
Department sysDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", curUserWXDeptid));
|
|
|
|
|
|
- User user = userMapper.selectOne(new QueryWrapper<User>().eq("company_id", companyId).eq("corpwx_userid", corpWxUserId));
|
|
|
+ User user = userMapper.selectOne(new QueryWrapper<User>().eq("company_id", companyId).eq("corpwx_userid", openUserId));
|
|
|
User changeUser = new User();
|
|
|
changeUser.setId(user.getId());
|
|
|
|
|
@@ -1699,6 +1705,10 @@ public class WeiXinCorpController {
|
|
|
changeUser.setId(oldUser.getId());
|
|
|
changeUser.setJobNumber(userItem.getJobNumber());
|
|
|
}
|
|
|
+ if (userItem.getCorpwxRealUserid() != null && !userItem.getCorpwxRealUserid().equals(oldUser.getCorpwxRealUserid())) {
|
|
|
+ changeUser.setId(oldUser.getId());
|
|
|
+ changeUser.setCorpwxRealUserid(userItem.getCorpwxRealUserid());
|
|
|
+ }
|
|
|
if (!userItem.getName().equals(oldUser.getName())) {
|
|
|
changeUser.setId(oldUser.getId());
|
|
|
changeUser.setName(userItem.getName());
|
|
@@ -1766,14 +1776,13 @@ public class WeiXinCorpController {
|
|
|
return msg;
|
|
|
}
|
|
|
int companyId = company.getId();
|
|
|
- //获取公司根部门人员,也就是没有分配部门的人员
|
|
|
-// List<JSONObject> hasDirectLdMembs = new ArrayList<>();
|
|
|
SysRole defaultRole = sysRoleMapper.selectOne(
|
|
|
new QueryWrapper<SysRole>().eq("company_id", companyId).eq("is_default", 1));
|
|
|
|
|
|
int companyRootDeptId = 1;
|
|
|
+ List<User> allCorpWxUserList = new ArrayList<>();
|
|
|
if (syncMembs == 1) {
|
|
|
- JSONArray rootCorpUserJsonArray = getDeptUserInfo(curCorpAccessToken, companyRootDeptId);
|
|
|
+ JSONArray rootCorpUserJsonArray = getDeptUserSimple(curCorpAccessToken, companyRootDeptId);
|
|
|
//获取远程的带姓名的详情,通过企业通讯录的token获取
|
|
|
JSONArray remoteUnAUserList = null;
|
|
|
if (rootCorpUserJsonArray.size() > 0) {
|
|
@@ -1805,9 +1814,6 @@ public class WeiXinCorpController {
|
|
|
return msg;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- List<User> updateUserList = new ArrayList<>();
|
|
|
-
|
|
|
for (int m=0;m<rootCorpUserJsonArray.size(); m++) {
|
|
|
JSONObject userJson = rootCorpUserJsonArray.getJSONObject(m);
|
|
|
String curUserid = userJson.getString("userid");
|
|
@@ -1816,7 +1822,13 @@ public class WeiXinCorpController {
|
|
|
System.out.println("userid="+curUserid+", name=" + userJson.getString("name")+", department="+userJson.getJSONArray("department"));
|
|
|
//不存在的人员, 进行插入
|
|
|
User user = new User();
|
|
|
-
|
|
|
+ JSONArray userDeptArray = userJson.getJSONArray("department");
|
|
|
+ //取最末级的,也就是最大的deptId
|
|
|
+ int maxDeptId = 1;
|
|
|
+ for (int i=0;i<userDeptArray.size(); i++) {
|
|
|
+ int curId = userDeptArray.getInteger(i);
|
|
|
+ if (curId > maxDeptId) maxDeptId = curId;
|
|
|
+ }
|
|
|
user.setId(SnowFlake.nextId()+"")
|
|
|
.setRoleId(defaultRole.getId())//默认普通员工
|
|
|
.setRoleName(defaultRole.getRolename())
|
|
@@ -1824,6 +1836,7 @@ public class WeiXinCorpController {
|
|
|
.setName(userJson.getString("name"))
|
|
|
.setPhone(userJson.getString("mobile"))
|
|
|
.setCorpwxUserid(curUserid)
|
|
|
+ .setCorpwxDeptid(maxDeptId)
|
|
|
.setColor(ColorUtil.randomColor());
|
|
|
//获取姓名
|
|
|
if (remoteUnAUserList != null) {
|
|
@@ -1833,47 +1846,21 @@ public class WeiXinCorpController {
|
|
|
user.setName(remoteUserJson.getString("name"));
|
|
|
user.setJobNumber(remoteUserJson.getString("userid"));
|
|
|
user.setPhone(remoteUserJson.getString("mobile"));
|
|
|
+ //设置实际的企业内部UserID
|
|
|
+ user.setCorpwxRealUserid(remoteUserJson.getString("userid"));
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //检查用户是否已经存在
|
|
|
- User oldUser = userMapper.selectOne(new QueryWrapper<User>().select("id, name, job_number, phone").eq("corpwx_userid", curUserid).eq("company_id", companyId));
|
|
|
- if (oldUser == null) {
|
|
|
- updateUserList.add(user);
|
|
|
- } else {
|
|
|
- boolean hasChange = false;
|
|
|
- User updateUser = new User();
|
|
|
- updateUser.setId(oldUser.getId());
|
|
|
- //工号或者手机号更新了
|
|
|
- if (user.getJobNumber() != null && !user.getJobNumber().equals(oldUser.getJobNumber())) {
|
|
|
- hasChange = true;
|
|
|
- updateUser.setJobNumber(user.getJobNumber());
|
|
|
- }
|
|
|
- if (user.getPhone() != null && !user.getPhone().equals(oldUser.getPhone())) {
|
|
|
- hasChange = true;
|
|
|
- updateUser.setPhone(user.getPhone());
|
|
|
- }
|
|
|
- if (user.getName() != null && !user.getName().equals(oldUser.getName())) {
|
|
|
- hasChange = true;
|
|
|
- updateUser.setName(user.getName());
|
|
|
- }
|
|
|
- if (hasChange) {
|
|
|
- //需要更新
|
|
|
- updateUserList.add(updateUser);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (updateUserList.size() > 0) {
|
|
|
- userService.saveOrUpdateBatch(updateUserList);
|
|
|
+ allCorpWxUserList.add(user);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//获取公司全部部门,不需要递归
|
|
|
JSONObject deptObj = getAllDepartments(curCorpAccessToken);
|
|
|
JSONArray deptObjJSONArray = deptObj.getJSONArray("department");
|
|
|
+ List<Department> oldDeptList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId));
|
|
|
|
|
|
-// List<Department> sysDeptList = new ArrayList<>();
|
|
|
for (int i=0;i<deptObjJSONArray.size(); i++) {
|
|
|
int deptId = deptObjJSONArray.getJSONObject(i).getIntValue("id");
|
|
|
Department department = new Department();
|
|
@@ -1881,7 +1868,8 @@ public class WeiXinCorpController {
|
|
|
//设置企业微信的部门id
|
|
|
JSONObject cutDeptJson = deptObjJSONArray.getJSONObject(i);
|
|
|
department.setCorpwxDeptid(cutDeptJson.getInteger("id"));
|
|
|
- department.setCorpwxDeptpid(cutDeptJson.getInteger("parentid"));
|
|
|
+ Integer parentId = cutDeptJson.getInteger("parentid");
|
|
|
+ department.setCorpwxDeptpid(parentId);
|
|
|
System.out.println("开始远程获取部门详情=====");
|
|
|
String url = TRANSMIT_SERVER_GET_DEPTDETAIL.replace(TRANSMIT_SERVER_STR, wxCorpInfo.getContactServer()).replace("ACCESS_TOKEN", corpContactAccessToken).replace("DEPTID", ""+deptId);
|
|
|
String result = restTemplate.getForObject(url, String.class);
|
|
@@ -1896,166 +1884,182 @@ public class WeiXinCorpController {
|
|
|
}
|
|
|
|
|
|
//检查是否已经有了
|
|
|
- Department oldDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", department.getCorpwxDeptid()).last("limit 1"));
|
|
|
- if (oldDept == null) {
|
|
|
+ Optional<Department> first = oldDeptList.stream().filter(old -> old.getCorpwxDeptid() != null && old.getCorpwxDeptid().equals(deptId)).findFirst();
|
|
|
+ if (!first.isPresent()) {
|
|
|
if (deptId != 1) {
|
|
|
//忽略根,根是公司名称
|
|
|
departmentMapper.insert(department);
|
|
|
}
|
|
|
} else {
|
|
|
//比较是否有更新
|
|
|
- boolean hasChange = false;
|
|
|
- if (!oldDept.getDepartmentName().equals(department.getDepartmentName())) {
|
|
|
- oldDept.setDepartmentName(department.getDepartmentName());
|
|
|
- hasChange = true;
|
|
|
- }
|
|
|
- if (!department.getCorpwxDeptpid().equals(oldDept.getCorpwxDeptpid())) {
|
|
|
- oldDept.setCorpwxDeptpid(department.getCorpwxDeptpid());
|
|
|
- Department newParentDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", oldDept.getCorpwxDeptpid()));
|
|
|
- if (newParentDept != null) {
|
|
|
- oldDept.setSuperiorId(newParentDept.getDepartmentId());
|
|
|
- }
|
|
|
- hasChange = true;
|
|
|
- }
|
|
|
- if (hasChange) {
|
|
|
- System.out.println("更新部门结构=="+oldDept.getDepartmentName());
|
|
|
+ Department oldDept = first.get();
|
|
|
+ if (oldDept.getCorpwxDeptpid() == null || !oldDept.getCorpwxDeptpid().equals(department.getCorpwxDeptpid())) {
|
|
|
+ //有父部门需要更新
|
|
|
+ oldDept.setCorpwxDeptpid(parentId);
|
|
|
departmentMapper.updateById(oldDept);
|
|
|
}
|
|
|
department = oldDept;
|
|
|
}
|
|
|
|
|
|
- deptObjJSONArray.getJSONObject(i).put("sys_dept_id", department.getDepartmentId());
|
|
|
Integer departmentId = department.getDepartmentId();
|
|
|
if (syncMembs == 1) {
|
|
|
- JSONArray userList = getDeptUserInfo(curCorpAccessToken, deptId);
|
|
|
+ JSONArray userList = getDeptUserSimple(curCorpAccessToken, deptId);
|
|
|
JSONArray remoteDeptUserList = null;
|
|
|
if (userList.size() > 0) {
|
|
|
- remoteDeptUserList = remoteGetDeptUserDetail(wxCorpInfo, corpContactAccessToken, deptId);
|
|
|
- //做id转化
|
|
|
- List<String> corpUserIds = new ArrayList<>();
|
|
|
- for (int p=0;p<remoteDeptUserList.size(); p++) {
|
|
|
- corpUserIds.add(remoteDeptUserList.getJSONObject(p).getString("userid"));
|
|
|
+ //检查该部门下的人员是否已经存在
|
|
|
+ boolean hasOneNotExist = false;
|
|
|
+ for (int m = 0; m<userList.size(); m++) {
|
|
|
+ JSONObject jsonObject = userList.getJSONObject(m);
|
|
|
+ String openUserid = jsonObject.getString("open_userid");
|
|
|
+ boolean match = allCorpWxUserList.stream().anyMatch(all->all.getCorpwxUserid().equals(openUserid));
|
|
|
+ if (!match) {
|
|
|
+ hasOneNotExist = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- try {
|
|
|
- JSONArray array = convertUserIdToOpenUserId(curCorpAccessToken, corpUserIds);
|
|
|
- for (int w=0;w<array.size(); w++) {
|
|
|
- JSONObject jsonObject = array.getJSONObject(w);
|
|
|
- String userid = jsonObject.getString("userid");
|
|
|
- String openUserid = jsonObject.getString("open_userid");
|
|
|
- //替换userid
|
|
|
- for (int m=0;m<remoteDeptUserList.size(); m++) {
|
|
|
- JSONObject remoteUser = remoteDeptUserList.getJSONObject(m);
|
|
|
- if (remoteUser.getString("userid").equals(userid)) {
|
|
|
- remoteUser.put("openUserid", openUserid);
|
|
|
- break;
|
|
|
+ //至少存在一个不存在的,需要重新获取该部门的成员
|
|
|
+ if (hasOneNotExist) {
|
|
|
+ remoteDeptUserList = remoteGetDeptUserDetail(wxCorpInfo, corpContactAccessToken, deptId);
|
|
|
+ //做id转化
|
|
|
+ List<String> corpUserIds = new ArrayList<>();
|
|
|
+ for (int p=0;p<remoteDeptUserList.size(); p++) {
|
|
|
+ corpUserIds.add(remoteDeptUserList.getJSONObject(p).getString("userid"));
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ JSONArray array = convertUserIdToOpenUserId(curCorpAccessToken, corpUserIds);
|
|
|
+ for (int w=0;w<array.size(); w++) {
|
|
|
+ JSONObject jsonObject = array.getJSONObject(w);
|
|
|
+ String userid = jsonObject.getString("userid");
|
|
|
+ String openUserid = jsonObject.getString("open_userid");
|
|
|
+ //替换userid
|
|
|
+ for (int m=0;m<remoteDeptUserList.size(); m++) {
|
|
|
+ JSONObject remoteUser = remoteDeptUserList.getJSONObject(m);
|
|
|
+ if (remoteUser.getString("userid").equals(userid)) {
|
|
|
+ remoteUser.put("openUserid", openUserid);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ } catch (Exception exception) {
|
|
|
+ exception.printStackTrace();
|
|
|
+ //msg.setError("同步失败:"+exception.getMessage());
|
|
|
+ msg.setError(MessageUtils.message("wx.synError",exception.getMessage()));
|
|
|
+ return msg;
|
|
|
}
|
|
|
- } catch (Exception exception) {
|
|
|
- exception.printStackTrace();
|
|
|
- //msg.setError("同步失败:"+exception.getMessage());
|
|
|
- msg.setError(MessageUtils.message("wx.synError",exception.getMessage()));
|
|
|
- return msg;
|
|
|
- }
|
|
|
- }
|
|
|
- List<User> updateUserList = new ArrayList<>();
|
|
|
- for (int m=0;m<userList.size(); m++) {
|
|
|
- JSONObject userJson = userList.getJSONObject(m);
|
|
|
- String curUserid = userJson.getString("userid");
|
|
|
- if (userJson.getInteger("status") != 1) continue;
|
|
|
- log.info("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
-// if (userJson.getJSONArray("direct_leader").size() > 0) {
|
|
|
-// hasDirectLdMembs.add(userJson);
|
|
|
-// }
|
|
|
- //不存在的人员, 进行插入
|
|
|
- User user = new User();
|
|
|
-
|
|
|
- user.setId(SnowFlake.nextId()+"")
|
|
|
- .setRoleId(defaultRole.getId())
|
|
|
- .setRoleName(defaultRole.getRolename())
|
|
|
- .setCompanyId(companyId)
|
|
|
- .setDepartmentId(departmentId)
|
|
|
- .setPhone(userJson.getString("mobile"))
|
|
|
- .setName(userJson.getString("name"))
|
|
|
- .setCorpwxUserid(curUserid)
|
|
|
- .setColor(ColorUtil.randomColor());
|
|
|
- //获取姓名
|
|
|
- if (remoteDeptUserList != null) {
|
|
|
- for (int t=0;t<remoteDeptUserList.size(); t++) {
|
|
|
- JSONObject remoteUserJson = remoteDeptUserList.getJSONObject(t);
|
|
|
- if (remoteUserJson.getString("openUserid").equals(curUserid)) {
|
|
|
- //匹配到了。设置姓名
|
|
|
- System.out.println("远程的用户匹配到了:"+remoteUserJson.getString("name"));
|
|
|
- user.setName(remoteUserJson.getString("name"));
|
|
|
- user.setJobNumber(remoteUserJson.getString("userid"));
|
|
|
- user.setPhone(remoteUserJson.getString("mobile"));
|
|
|
- break;
|
|
|
+ for (int m=0;m<userList.size(); m++) {
|
|
|
+ JSONObject userJson = userList.getJSONObject(m);
|
|
|
+ String curUserid = userJson.getString("userid");
|
|
|
+ String openUserid = userJson.getString("open_userid");
|
|
|
+ //跳过已经获取过的
|
|
|
+ if (allCorpWxUserList.stream().anyMatch(item->item.getCorpwxUserid().equals(openUserid))) {
|
|
|
+ continue;
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- //检查用户是否已经存在
|
|
|
- User oldUser = userMapper.selectOne(new QueryWrapper<User>().select("id, name, job_number, phone").eq("corpwx_userid", curUserid).eq("company_id", companyId));
|
|
|
- if (oldUser == null) {
|
|
|
- updateUserList.add(user);
|
|
|
- } else {
|
|
|
- boolean hasChange = false;
|
|
|
- User updateUser = new User();
|
|
|
- updateUser.setId(oldUser.getId());
|
|
|
- //工号或者手机号更新了
|
|
|
- if (user.getJobNumber() != null && !user.getJobNumber().equals(oldUser.getJobNumber())) {
|
|
|
- hasChange = true;
|
|
|
- updateUser.setJobNumber(user.getJobNumber());
|
|
|
- }
|
|
|
- if (user.getPhone() != null && !user.getPhone().equals(oldUser.getPhone())) {
|
|
|
- hasChange = true;
|
|
|
- updateUser.setPhone(user.getPhone());
|
|
|
- }
|
|
|
- if (user.getName() != null && !user.getName().equals(oldUser.getName())) {
|
|
|
- hasChange = true;
|
|
|
- updateUser.setName(user.getName());
|
|
|
- }
|
|
|
- if (hasChange) {
|
|
|
- //需要更新
|
|
|
- updateUserList.add(updateUser);
|
|
|
+ log.info("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
+ //不存在的人员, 进行插入
|
|
|
+ User user = new User();
|
|
|
+ JSONArray userDeptArray = userJson.getJSONArray("department");
|
|
|
+ //取最末级的,也就是最大的deptId
|
|
|
+ int maxDeptId = 1;
|
|
|
+ for (int t=0;t<userDeptArray.size(); t++) {
|
|
|
+ int curId = userDeptArray.getInteger(t);
|
|
|
+ if (curId > maxDeptId) maxDeptId = curId;
|
|
|
+ }
|
|
|
+ user.setId(SnowFlake.nextId()+"")
|
|
|
+ .setRoleId(defaultRole.getId())
|
|
|
+ .setRoleName(defaultRole.getRolename())
|
|
|
+ .setCompanyId(companyId)
|
|
|
+ .setDepartmentId(departmentId)
|
|
|
+ .setPhone(userJson.getString("mobile"))
|
|
|
+ .setName(userJson.getString("name"))
|
|
|
+ .setCorpwxUserid(openUserid)
|
|
|
+ .setCorpwxDeptid(maxDeptId)
|
|
|
+ .setColor(ColorUtil.randomColor());
|
|
|
+ //获取姓名
|
|
|
+ if (remoteDeptUserList != null) {
|
|
|
+ for (int t=0;t<remoteDeptUserList.size(); t++) {
|
|
|
+ JSONObject remoteUserJson = remoteDeptUserList.getJSONObject(t);
|
|
|
+ if (remoteUserJson.getString("openUserid").equals(curUserid)) {
|
|
|
+ //匹配到了。设置姓名
|
|
|
+ System.out.println("远程的用户匹配到了:"+remoteUserJson.getString("name"));
|
|
|
+ user.setName(remoteUserJson.getString("name"));
|
|
|
+ user.setJobNumber(remoteUserJson.getString("userid"));
|
|
|
+ user.setPhone(remoteUserJson.getString("mobile"));
|
|
|
+ user.setCorpwxRealUserid(remoteUserJson.getString("userid"));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ allCorpWxUserList.add(user);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if (updateUserList.size() > 0) {
|
|
|
- userService.saveOrUpdateBatch(updateUserList);
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //再来更新部门的层级关系
|
|
|
- List<Department> needUpdateDepts = new ArrayList<>();
|
|
|
- List<Department> allDbDeptList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId));
|
|
|
- for (Department department : allDbDeptList) {
|
|
|
- int corpwxpid = department.getCorpwxDeptpid();
|
|
|
- if (corpwxpid == 1) {
|
|
|
- //父部门是根部门
|
|
|
-// if (department.getSuperiorId() != null) {
|
|
|
-// department.setSuperiorId(0);
|
|
|
-// needUpdateDepts.add(department);
|
|
|
-// }
|
|
|
- } else {
|
|
|
- Optional<Department> first = allDbDeptList.stream().filter(all -> all.getCorpwxDeptid().intValue() == corpwxpid).findFirst();
|
|
|
- if (first.isPresent()) {
|
|
|
- //按照企业微信的部门父部门找到了
|
|
|
- Integer sysPid = first.get().getDepartmentId();
|
|
|
- if (!sysPid.equals(department.getSuperiorId())) {
|
|
|
- department.setSuperiorId(sysPid);
|
|
|
- needUpdateDepts.add(department);
|
|
|
+ //更新部门层级关系
|
|
|
+ updateDeptHierachyByCorpWx(companyId);
|
|
|
+
|
|
|
+ //设置人员所属部门
|
|
|
+ List<Department> allDeptList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId));
|
|
|
+ List<User> existingUsers = userMapper.selectList(new QueryWrapper<User>().eq("company_id", companyId));
|
|
|
+ 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) {
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (newUserList.size() > 0) {
|
|
|
+ userService.saveBatch(newUserList);
|
|
|
+ }
|
|
|
+ //姓名,部门,账号的同步更新
|
|
|
+ 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.getCorpwxRealUserid() != null && !userItem.getCorpwxRealUserid().equals(oldUser.getCorpwxRealUserid())) {
|
|
|
+ changeUser.setId(oldUser.getId());
|
|
|
+ changeUser.setCorpwxRealUserid(userItem.getCorpwxRealUserid());
|
|
|
+ }
|
|
|
+ if (!userItem.getName().equals(oldUser.getName())) {
|
|
|
+ changeUser.setId(oldUser.getId());
|
|
|
+ changeUser.setName(userItem.getName());
|
|
|
+ }
|
|
|
+ if (!userItem.getCorpwxDeptid().equals(oldUser.getCorpwxDeptid())) {
|
|
|
+ changeUser.setId(oldUser.getId());
|
|
|
+ if (userItem.getCorpwxDeptid() != 1) {
|
|
|
+ changeUser.setDepartmentId(allDeptList.stream().filter(d->d.getCorpwxDeptid() != null && d.getCorpwxDeptid().equals(userItem.getCorpwxDeptid())).findFirst().get().getDepartmentId());
|
|
|
+ //设置层级部门
|
|
|
+ changeUser.setDepartmentCascade(convertDepartmentIdToCascade(changeUser.getDepartmentId(), allDeptList));
|
|
|
+ } else {
|
|
|
+ changeUser.setDepartmentId(0);
|
|
|
+ changeUser.setDepartmentCascade("0");
|
|
|
}
|
|
|
}
|
|
|
+ if (oldUser.getCorpwxRealUserid() == null || !userItem.getCorpwxRealUserid().equals(oldUser.getCorpwxRealUserid())) {
|
|
|
+ changeUser.setId(oldUser.getId());
|
|
|
+ changeUser.setCorpwxRealUserid(userItem.getCorpwxRealUserid());
|
|
|
+ }
|
|
|
+ if (changeUser.getId() != null) {
|
|
|
+ //有变动
|
|
|
+ finalUpdateUserList.add(changeUser);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("没找到用户:"+userItem.getName()+", "+userItem.getCorpwxUserid());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (needUpdateDepts.size() > 0) {
|
|
|
- departmentService.updateBatchById(needUpdateDepts);
|
|
|
+ if (finalUpdateUserList.size() > 0) {
|
|
|
+ userService.updateBatchById(finalUpdateUserList);
|
|
|
}
|
|
|
- //更新人员的deptcascade
|
|
|
- updateUserDeptCascade(companyId);
|
|
|
-
|
|
|
return new HttpRespMsg();
|
|
|
}
|
|
|
|