Переглянути джерело

修改企业微信同步,切换部门

seyason 2 роки тому
батько
коміт
5582fe268e

+ 81 - 25
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/WeiXinCorpController.java

@@ -372,10 +372,11 @@ public class WeiXinCorpController {
                         SysRole defaultRole = sysRoleMapper.selectOne(new QueryWrapper<SysRole>().eq("company_id", companyId).eq("is_default", 1));
                         //通过通讯录secret获取到员工姓名
                         String remoteCorpConcactAccessToken = getRemoteCorpConcactAccessToken(wxCorpInfo);
+                        String curCorpAccessToken = getCorpAccessToken(wxCorpInfo);
                         org.json.JSONArray departmentArray = jsonObject.getJSONArray("department");
                         Integer curUserWXDeptid = departmentArray.getInt(departmentArray.length() - 1);
-                        JSONObject userObj = remoteGetUserDetail(remoteCorpConcactAccessToken, corpWxUserId, curUserWXDeptid);
-                        if (userObj.getInteger("errcode") == 0) {
+                        JSONObject userObj = remoteGetUserDetail(remoteCorpConcactAccessToken, corpWxUserId, curUserWXDeptid, curCorpAccessToken);
+                        if (userObj != null) {
                             //成功获取到通讯录的个人详情
                             Long id = SnowFlake.nextId();
                             JSONArray department = userObj.getJSONArray("department");
@@ -412,22 +413,23 @@ public class WeiXinCorpController {
                     //只有授权通讯录同步的,才有机会更新部门或者上级
                     if (!StringUtils.isEmpty(wxCorpInfo.getContactSecret())) {
                         String remoteCorpConcactAccessToken = getRemoteCorpConcactAccessToken(wxCorpInfo);
+                        String curCorpAccessToken = getCorpAccessToken(wxCorpInfo);
                         Integer curUserWXDeptid = 0;
                         if (jsonObject.has("Department")) {
-                            Object departmentJ = jsonObject.get("Department");
-                            if (departmentJ instanceof String || departmentJ instanceof Integer) {
-                                curUserWXDeptid = jsonObject.getInt("Department");
-                            } else if (departmentJ instanceof org.json.JSONArray) {
-                                org.json.JSONArray departmentArray = jsonObject.getJSONArray("Department");
-                                curUserWXDeptid = getMaxDeptIdFromArray(departmentArray);
+                            String department = jsonObject.getString("Department");
+                            if (department.contains(",")) {
+                                String[] split = department.split(",");
+                                curUserWXDeptid = getMaxDeptIdFromArray(split);
+                            } else {
+                                curUserWXDeptid = Integer.valueOf(department);
                             }
                         } else {
                             //取主部门
                             curUserWXDeptid = jsonObject.getInt("MainDepartment");
                         }
 
-                        JSONObject userObj = remoteGetUserDetail(remoteCorpConcactAccessToken, corpWxUserId, curUserWXDeptid);
-                        if (userObj.getInteger("errcode") == 0) {
+                        JSONObject userObj = remoteGetUserDetail(remoteCorpConcactAccessToken, corpWxUserId, curUserWXDeptid, curCorpAccessToken);
+                        if (userObj != null) {
                             //成功获取到通讯录的个人详情
                             JSONArray department = userObj.getJSONArray("department");
                             curUserWXDeptid = getMaxDeptIdFromArray(department);
@@ -493,7 +495,18 @@ public class WeiXinCorpController {
         }
         return "success";
     }
-
+    private Integer getMaxDeptIdFromArray(String[] arr) {
+        int deptId=0;
+        for (int i=0;i<arr.length; i++) {
+            if (!StringUtils.isEmpty(arr[i])) {
+                int curDeptId = Integer.parseInt(arr[i]);
+                if (deptId < curDeptId) {
+                    deptId = curDeptId;
+                }
+            }
+        }
+        return deptId;
+    }
     private Integer getMaxDeptIdFromArray(JSONArray department) {
         int deptId=0;
         for (int i=0;i<department.size(); i++) {
@@ -1373,6 +1386,11 @@ public class WeiXinCorpController {
                     oldUser.setName(user.getName());
                     userMapper.updateById(oldUser);
                 }
+//                //判断部门是否要更新
+//                if (oldUser.getDepartmentId() != 0) {
+//                    oldUser.setDepartmentId(0);
+//                    userMapper.updateById(oldUser);
+//                }
             }
         }
 
@@ -1500,12 +1518,23 @@ public class WeiXinCorpController {
                         //姓名也不存在,则插入新记录
                         userMapper.insert(user);
                     }
-                } else if (oldUser.getRoleName().equals("超级管理员") && !oldUser.getName().equals(user.getName())){
-                    //姓名不一致,需要更新
-                    System.out.println("===更新超管姓名==="+user.getName());
-                    user.setId(oldUser.getId());
-                    oldUser.setName(user.getName());
-                    userMapper.updateById(oldUser);
+                } else {
+                    boolean shouldUpdate = false;
+                    if (oldUser.getRoleName().equals("超级管理员") && !oldUser.getName().equals(user.getName())) {
+                        //姓名不一致,需要更新
+                        System.out.println("===更新超管姓名==="+user.getName());
+                        user.setId(oldUser.getId());
+                        oldUser.setName(user.getName());
+                        shouldUpdate = true;
+                    }
+                    if (!oldUser.getDepartmentId().equals(user.getDepartmentId())) {
+                        oldUser.setDepartmentId(user.getDepartmentId());
+                        shouldUpdate = true;
+                    }
+                    if (shouldUpdate) {
+                        userMapper.updateById(oldUser);
+                    }
+
                 }
             }
         }
@@ -1815,14 +1844,41 @@ public class WeiXinCorpController {
         return obj;
     }
 
-    private JSONObject remoteGetUserDetail(String accessToken, String userId, Integer deptId) {
-        String url = TRANSMIT_SERVER_GET_USERDETAIL.replace("ACCESS_TOKEN", accessToken).replace("USERID", userId)
-                        .replace("DEPTID", deptId+"");
-        System.out.println("请求URL="+url);
-        String result = restTemplate.getForObject(url, String.class);
-        System.out.println("远程人员详情:"+result);
-        JSONObject obj = JSONObject.parseObject(JSONObject.parseObject(result).getString("data"));
-        return obj;
+    private JSONObject remoteGetUserDetail(String accessToken, String userId, Integer deptId, String curCorpAccessToken) {
+//        String url = TRANSMIT_SERVER_GET_USERDETAIL.replace("ACCESS_TOKEN", accessToken).replace("USERID", userId)
+//                        .replace("DEPTID", deptId+"");
+//        System.out.println("请求URL="+url);
+//        String result = restTemplate.getForObject(url, String.class);
+//        System.out.println("远程人员详情:"+result);
+//        JSONObject obj = JSONObject.parseObject(JSONObject.parseObject(result).getString("data"));
+//        return obj;
+        JSONArray remoteDeptUserList = remoteGetDeptUserDetail(accessToken, deptId);
+        //做id转化
+        List<String> userIds = new ArrayList<>();
+        for (int i=0;i<remoteDeptUserList.size(); i++) {
+            userIds.add(remoteDeptUserList.getJSONObject(i).getString("userid"));
+        }
+        try {
+            JSONArray array = convertUserIdToOpenUserId(curCorpAccessToken, userIds);
+            for (int i=0;i<array.size(); i++) {
+                JSONObject jsonObject = array.getJSONObject(i);
+                String userid = jsonObject.getString("userid");
+                String openUserid = jsonObject.getString("open_userid");
+                if (openUserid.equals(userId)) {
+                    //匹配到了
+                    for (int m=0;m<remoteDeptUserList.size(); m++) {
+                        JSONObject remoteUser = remoteDeptUserList.getJSONObject(m);
+                        if (remoteUser.getString("userid").equals(userid)) {
+                            remoteUser.put("userid", openUserid);
+                            return remoteUser;
+                        }
+                    }
+                }
+            }
+        } catch (Exception exception) {
+            exception.printStackTrace();
+        }
+        return null;
     }
 
     //通过中转服务器,调用通讯录的accessToken