|
@@ -17,6 +17,7 @@ import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.*;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
@@ -680,6 +681,16 @@ public class WeiXinCorpController {
|
|
|
private JSONArray getDeptUserSimple(String accessToken, int deptId) {
|
|
|
String url = GET_DEPARTMENT_USER_SIMPLE_URL.replace("ACCESS_TOKEN", accessToken).replace("DEPARTMENT_ID", ""+deptId);
|
|
|
String result = restTemplate.getForObject(url, String.class);
|
|
|
+ System.out.println("部门人员概要:"+result);
|
|
|
+ JSONObject obj = JSONObject.parseObject(result);
|
|
|
+ JSONArray userlist = obj.getJSONArray("userlist");
|
|
|
+
|
|
|
+ return userlist;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONArray getDeptUserInfo(String accessToken, int deptId) {
|
|
|
+ String url = GET_DEPARTMENT_USER_DETAIL_URL.replace("ACCESS_TOKEN", accessToken).replace("DEPARTMENT_ID", ""+deptId);
|
|
|
+ String result = restTemplate.getForObject(url, String.class);
|
|
|
System.out.println("部门人员详情:"+result);
|
|
|
JSONObject obj = JSONObject.parseObject(result);
|
|
|
JSONArray userlist = obj.getJSONArray("userlist");
|
|
@@ -968,8 +979,8 @@ public class WeiXinCorpController {
|
|
|
@RequestMapping("/getCorpMembs")
|
|
|
public HttpRespMsg getCorpMembs(String corpId) {
|
|
|
WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
if (wxCorpInfo.getContactSecret() == null) {
|
|
|
- HttpRespMsg msg = new HttpRespMsg();
|
|
|
msg.setError("请先设置企业通讯录同步Secret");
|
|
|
return msg;
|
|
|
}
|
|
@@ -979,18 +990,25 @@ public class WeiXinCorpController {
|
|
|
curCorpAccessToken = getCorpConcactAccessToken(wxCorpInfo);
|
|
|
} catch (Exception exception) {
|
|
|
exception.printStackTrace();
|
|
|
+ msg.setError("同步失败:"+exception.getMessage());
|
|
|
+ return msg;
|
|
|
}
|
|
|
int companyId = company.getId();
|
|
|
//获取公司根部门人员,也就是没有分配部门的人员
|
|
|
int companyRootDeptId = 1;
|
|
|
- JSONArray unAssignedUserList = getDeptUserSimple(curCorpAccessToken, companyRootDeptId);
|
|
|
+ JSONArray unAssignedUserList = getDeptUserInfo(curCorpAccessToken, companyRootDeptId);
|
|
|
SysRole defaultRole = sysRoleMapper.selectOne(
|
|
|
new QueryWrapper<SysRole>().eq("company_id", companyId).eq("is_default", 1));
|
|
|
-
|
|
|
+ List<JSONObject> hasDirectLdMembs = new ArrayList<>();
|
|
|
for (int m=0;m<unAssignedUserList.size(); m++) {
|
|
|
JSONObject userJson = unAssignedUserList.getJSONObject(m);
|
|
|
String curUserid = userJson.getString("userid");
|
|
|
- System.out.println("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
+ //跳过非激活状态的员工
|
|
|
+ if (userJson.getInteger("status") != 1) continue;
|
|
|
+ System.out.println("userid="+curUserid+", name=" + userJson.getString("name")+", department="+userJson.getJSONArray("department"));
|
|
|
+ if (userJson.getJSONArray("direct_leader").size() > 0) {
|
|
|
+ hasDirectLdMembs.add(userJson);
|
|
|
+ }
|
|
|
//不存在的人员, 进行插入
|
|
|
User user = new User();
|
|
|
|
|
@@ -999,6 +1017,7 @@ public class WeiXinCorpController {
|
|
|
.setRoleName(defaultRole.getRolename())
|
|
|
.setCompanyId(companyId)
|
|
|
.setName(userJson.getString("name"))
|
|
|
+ .setPhone(userJson.getString("mobile"))
|
|
|
.setCorpwxUserid(curUserid)
|
|
|
.setColor(ColorUtil.randomColor());
|
|
|
|
|
@@ -1011,22 +1030,32 @@ public class WeiXinCorpController {
|
|
|
userMapper.insert(user);
|
|
|
} else {
|
|
|
//姓名已经存在,更新wxcorpid
|
|
|
- List<User> updateUser = new ArrayList<User>();
|
|
|
- userList.forEach(u-> {
|
|
|
- if (u.getCorpwxUserid() == null) {
|
|
|
- u.setCorpwxUserid(user.getCorpwxUserid());
|
|
|
- updateUser.add(u);
|
|
|
- }
|
|
|
- });
|
|
|
- if (updateUser.size() > 0) {
|
|
|
- userService.updateBatchById(updateUser);
|
|
|
+ User updateUser = userList.get(0);
|
|
|
+ if (updateUser.getCorpwxUserid() == null) {
|
|
|
+ updateUser.setCorpwxUserid(user.getCorpwxUserid());
|
|
|
+ userMapper.updateById(updateUser);
|
|
|
}
|
|
|
}
|
|
|
+ }else {
|
|
|
+// //更新信息
|
|
|
+ User oldUser = userMapper.selectList(new QueryWrapper<User>().eq("corpwx_userid", curUserid).eq("company_id", companyId).orderByDesc("create_time")).get(0);
|
|
|
+ boolean shouldUpdate = false;
|
|
|
+ if (!oldUser.getName().equals(userJson.getString("name"))) {
|
|
|
+ oldUser.setName(userJson.getString("name"));
|
|
|
+ shouldUpdate = true;
|
|
|
+ }
|
|
|
+ if ((oldUser.getPhone() == null && !StringUtils.isEmpty(userJson.getString("mobile")) && !oldUser.getPhone().equals(userJson.getString("mobile")))) {
|
|
|
+ oldUser.setPhone(userJson.getString("mobile"));
|
|
|
+ shouldUpdate = true;
|
|
|
+ }
|
|
|
+ if (shouldUpdate) {
|
|
|
+ userMapper.updateById(oldUser);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //获取部门
|
|
|
- JSONObject deptObj = getDepartments(curCorpAccessToken);
|
|
|
+ //获取公司全部部门,不需要递归
|
|
|
+ JSONObject deptObj = getAllDepartments(curCorpAccessToken);
|
|
|
JSONArray deptObjJSONArray = deptObj.getJSONArray("department");
|
|
|
|
|
|
List<Department> sysDeptList = new ArrayList<>();
|
|
@@ -1049,11 +1078,15 @@ public class WeiXinCorpController {
|
|
|
sysDeptList.add(department);
|
|
|
deptObjJSONArray.getJSONObject(i).put("sys_dept_id", department.getDepartmentId());
|
|
|
Integer departmentId = department.getDepartmentId();
|
|
|
- JSONArray userList = getDeptUserSimple(curCorpAccessToken, deptId);
|
|
|
+ JSONArray userList = getDeptUserInfo(curCorpAccessToken, deptId);
|
|
|
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();
|
|
|
|
|
@@ -1061,6 +1094,7 @@ public class WeiXinCorpController {
|
|
|
.setRole(0)//默认普通员工
|
|
|
.setCompanyId(companyId)
|
|
|
.setDepartmentId(departmentId)
|
|
|
+ .setPhone(userJson.getString("mobile"))
|
|
|
.setName(userJson.getString("name"))
|
|
|
.setCorpwxUserid(curUserid)
|
|
|
.setColor(ColorUtil.randomColor());
|
|
@@ -1074,23 +1108,27 @@ public class WeiXinCorpController {
|
|
|
userMapper.insert(user);
|
|
|
} else {
|
|
|
//姓名已经存在,更新wxcorpid
|
|
|
- List<User> updateUser = new ArrayList<User>();
|
|
|
- userList2.forEach(u-> {
|
|
|
- if (u.getCorpwxUserid() == null) {
|
|
|
- u.setCorpwxUserid(user.getCorpwxUserid());
|
|
|
- updateUser.add(u);
|
|
|
- }
|
|
|
- });
|
|
|
- if (updateUser.size() > 0) {
|
|
|
- userService.updateBatchById(updateUser);
|
|
|
+ User updateUser2 = userList2.get(0);
|
|
|
+ if (updateUser2.getCorpwxUserid() == null) {
|
|
|
+ updateUser2.setCorpwxUserid(user.getCorpwxUserid());
|
|
|
+ userMapper.updateById(updateUser2);
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
-// //更新信息
|
|
|
-// User oldUser = userMapper.selectList(new QueryWrapper<User>().eq("corpwx_userid", curUserid).eq("company_id", companyId).orderByDesc("create_time")).get(0);
|
|
|
-// oldUser.setName(userJson.getString("name"));
|
|
|
-// oldUser.setDepartmentId(departmentId);
|
|
|
-// userMapper.updateById(oldUser);
|
|
|
+// //更新信息
|
|
|
+ User oldUser = userMapper.selectList(new QueryWrapper<User>().eq("corpwx_userid", curUserid).eq("company_id", companyId).orderByDesc("create_time")).get(0);
|
|
|
+ boolean shouldUpdate = false;
|
|
|
+ if (!oldUser.getName().equals(userJson.getString("name"))) {
|
|
|
+ oldUser.setName(userJson.getString("name"));
|
|
|
+ shouldUpdate = true;
|
|
|
+ }
|
|
|
+ if (oldUser.getPhone() == null || (!StringUtils.isEmpty(userJson.getString("mobile")) && !oldUser.getPhone().equals(userJson.getString("mobile")))) {
|
|
|
+ oldUser.setPhone(userJson.getString("mobile"));
|
|
|
+ shouldUpdate = true;
|
|
|
+ }
|
|
|
+ if (shouldUpdate) {
|
|
|
+ userMapper.updateById(oldUser);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -1103,71 +1141,55 @@ public class WeiXinCorpController {
|
|
|
if (pid != 1) {
|
|
|
//根部门Id = 1
|
|
|
Integer sysDeptId = deptJson.getInteger("sys_dept_id");
|
|
|
- Department department = sysDeptList.stream().filter(d -> d.getDepartmentId().equals(sysDeptId)).findFirst().get();
|
|
|
- //从deptjson数组中寻找parent item
|
|
|
- for (int m=0;m<deptObjJSONArray.size(); m++) {
|
|
|
- JSONObject item = deptObjJSONArray.getJSONObject(m);
|
|
|
- if (item.getInteger("id").equals(pid)) {
|
|
|
- department.setSuperiorId(item.getInteger("sys_dept_id"));
|
|
|
- break;
|
|
|
+ if (sysDeptId != null) {
|
|
|
+ Department department = sysDeptList.stream().filter(d -> d.getDepartmentId() != null && d.getDepartmentId().equals(sysDeptId)).findFirst().get();
|
|
|
+ //从deptjson数组中寻找parent item
|
|
|
+ for (int m=0;m<deptObjJSONArray.size(); m++) {
|
|
|
+ JSONObject item = deptObjJSONArray.getJSONObject(m);
|
|
|
+ if (item.getInteger("id").equals(pid)) {
|
|
|
+ department.setSuperiorId(item.getInteger("sys_dept_id"));
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
+ needUpdateDepts.add(department);
|
|
|
}
|
|
|
- needUpdateDepts.add(department);
|
|
|
}
|
|
|
}
|
|
|
if (needUpdateDepts.size() > 0) {
|
|
|
departmentService.updateBatchById(needUpdateDepts);
|
|
|
}
|
|
|
-
|
|
|
-// //获取部门
|
|
|
-// JSONObject deptObj = getAllDepartments(curCorpAccessToken);
|
|
|
-// JSONArray deptObjJSONArray = deptObj.getJSONArray("department");
|
|
|
-//
|
|
|
-// for (int i=0;i<deptObjJSONArray.size(); i++) {
|
|
|
-// int deptId = deptObjJSONArray.getJSONObject(i).getIntValue("id");
|
|
|
-//
|
|
|
-// JSONArray userList = getDeptUserSimple(curCorpAccessToken, deptId);
|
|
|
-// for (int m=0;m<userList.size(); m++) {
|
|
|
-// JSONObject userJson = userList.getJSONObject(m);
|
|
|
-// System.out.println(userJson);
|
|
|
-// String curUserid = userJson.getString("userid");
|
|
|
-// System.out.println("userid="+curUserid+", name=" + userJson.getString("name")+", mobile="+userJson.getString("mobile"));
|
|
|
-// //不存在的人员, 进行插入
|
|
|
-// User user = new User();
|
|
|
-//
|
|
|
-// user.setId(SnowFlake.nextId()+"")
|
|
|
-// .setRoleId(defaultRole.getId())//默认普通员工
|
|
|
-// .setRoleName(defaultRole.getRolename())
|
|
|
-// .setCompanyId(companyId)
|
|
|
-// .setDepartmentId(0)
|
|
|
-// .setName(userJson.getString("name"))
|
|
|
-// .setCorpwxUserid(curUserid)
|
|
|
-// .setColor(ColorUtil.randomColor());
|
|
|
-//
|
|
|
-// //检查用户是否已经存在
|
|
|
-// if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
|
|
|
-// //检查姓名,该公司下面这个员工的姓名是否已经存在
|
|
|
-// List<User> userList2 = userMapper.selectList(new QueryWrapper<User>().eq("company_id", companyId).eq("name", user.getName()));
|
|
|
-// if (userList2.size() == 0) {
|
|
|
-// //姓名也没有存在,就插入,检查人员是否已经超过上限
|
|
|
-// userMapper.insert(user);
|
|
|
-// } else {
|
|
|
-// //姓名已经存在,更新wxcorpid
|
|
|
-// List<User> updateUser = new ArrayList<User>();
|
|
|
-// userList2.forEach(u-> {
|
|
|
-// if (u.getCorpwxUserid() == null) {
|
|
|
-// u.setCorpwxUserid(user.getCorpwxUserid());
|
|
|
-// updateUser.add(u);
|
|
|
-// }
|
|
|
-// });
|
|
|
-// if (updateUser.size() > 0) {
|
|
|
-// userService.updateBatchById(updateUser);
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
+ //更新人员的直属上级
|
|
|
+ if (hasDirectLdMembs.size() > 0) {
|
|
|
+ List<String> corpwxUids = new ArrayList<>();
|
|
|
+ for (JSONObject userJson : hasDirectLdMembs) {
|
|
|
+ String curUserid = userJson.getString("userid");
|
|
|
+ //取第一个leaderId
|
|
|
+ JSONArray directLeader = userJson.getJSONArray("direct_leader");
|
|
|
+ String string = directLeader.getString(0);
|
|
|
+ corpwxUids.add(curUserid);
|
|
|
+ if (!corpwxUids.contains(string)) {
|
|
|
+ corpwxUids.add(string);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<User> userList = userMapper.selectList(new QueryWrapper<User>().select("id, corpwx_userid, superior_id").in("corpwx_userid", corpwxUids));
|
|
|
+ List<User> updateUserList = new ArrayList<>();
|
|
|
+ for (JSONObject userJson : hasDirectLdMembs) {
|
|
|
+ String curUserid = userJson.getString("userid");
|
|
|
+ User user = userList.stream().filter(u -> u.getCorpwxUserid().equals(curUserid)).findFirst().get();
|
|
|
+ JSONArray directLeader = userJson.getJSONArray("direct_leader");
|
|
|
+ String leaderCorpWxuid = directLeader.getString(0);
|
|
|
+ //查找leader
|
|
|
+ User leader = userList.stream().filter(u -> u.getCorpwxUserid().equals(leaderCorpWxuid)).findFirst().get();
|
|
|
+ if (!leader.getId().equals(user.getSuperiorId())) {
|
|
|
+ user.setSuperiorId(leader.getId());
|
|
|
+ updateUserList.add(user);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (updateUserList.size() > 0) {
|
|
|
+ //批量更新上级领导
|
|
|
+ userService.updateBatchById(updateUserList);
|
|
|
+ }
|
|
|
+ }
|
|
|
return new HttpRespMsg();
|
|
|
}
|
|
|
|