Browse Source

飞书事件订阅 通讯录范围变更 补充提交

Min 2 years ago
parent
commit
bbeca09282

+ 93 - 15
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/FeishuInfoController.java

@@ -1,6 +1,7 @@
 package com.management.platform.controller;
 
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -21,14 +22,15 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.*;
 import org.springframework.util.StringUtils;
-import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.*;
 
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
 import java.util.*;
@@ -101,8 +103,7 @@ public class FeishuInfoController {
         //查找公司对应的记录是否已经添加
         FeishuInfo feishuInfo = feishuInfoMapper.selectOne(new QueryWrapper<FeishuInfo>().eq("app_id",appId));
         if (feishuInfo == null) {
-            //msg.setError("请在company_dingding表中增加corpid:" + corpid);
-            msg.setError("请在company_dingding表中增加appid:" + appId);
+            msg.setError("请在feishu_info表中增加appid:" + appId);
         } else if (feishuInfo.getCompanyId() != null) {
             //msg.setError("companyId已存在,如需重新初始化请先重置company_dingding中该条数据的companyId为null");
             msg.setError("companyId已存在,如需重新初始化请先重置feishu_info中该条数据的companyId为null");
@@ -592,9 +593,21 @@ public class FeishuInfoController {
     //"企业微信数据回调"
     @RequestMapping(value = "/dataCallback", method = RequestMethod.POST)
     @ResponseBody
-    public String dataCallback(String schema,String header,String event){
-        JSONObject headerOb = JSONObject.parseObject(header);
-        JSONObject eventOb = JSONObject.parseObject(event);
+    public void dataCallback(@RequestBody JSONObject jsonParam, HttpServletResponse response,String schema,String header,String event) throws IOException {
+        PrintWriter out = null;
+        JSONObject jsonObject=new JSONObject();
+        jsonObject.put("challenge",jsonParam.getString("challenge"));
+        //设定类容为json的格式
+        response.setContentType("application/json;charset=UTF-8");
+        out = response.getWriter();
+        //写到客户端
+        out.write(jsonObject.toJSONString());
+        out.flush();
+        JSONObject headerOb =jsonParam.getJSONObject("header");
+        if(headerOb==null){
+            return;
+        }
+        JSONObject eventOb = jsonParam.getJSONObject("event");
         log.info("========schema========="+schema);
         String appId = headerOb.getString("app_id");
         log.info("========app_id========"+appId);
@@ -609,12 +622,21 @@ public class FeishuInfoController {
             SysRole role = sysRoleMapper.selectOne(new QueryWrapper<SysRole>().eq("company_id",feishuInfo.getCompanyId()).eq("rolename","普通员工"));
             //通讯录权限范围变更
             //todo:新增的
-            if(eventOb.getJSONObject("added")!=null){
+            if(eventOb!=null&&eventOb.getJSONObject("added")!=null){
                 JSONObject addedOb = eventOb.getJSONObject("added");
                 //todo:处理部门
                 JSONArray departmentArray = addedOb.getJSONArray("departments");
-                for (int j = 0; j < departmentArray.size(); j++) {
-                    JSONObject ob = departmentArray.getJSONObject(j);
+                JSONArray allNeedTodo=new JSONArray();
+                allNeedTodo.addAll(departmentArray);
+                for (int i = 0; i < departmentArray.size(); i++) {
+                    JSONObject ob = departmentArray.getJSONObject(i);
+                    JSONArray jsonArray = getSubDepartmentList(feishuInfo, ob.getString("open_department_id"));
+                    if(jsonArray!=null&&jsonArray.size()>0){
+                        allNeedTodo.addAll(jsonArray);
+                    }
+                }
+                for (int j = 0; j < allNeedTodo.size(); j++) {
+                    JSONObject ob = allNeedTodo.getJSONObject(j);
                     String departmentName = String.valueOf(ob.get("name"));
                     System.out.println("synchronizationDP========="+departmentName);
                     String departmentId = String.valueOf(ob.getString("department_id"));
@@ -668,6 +690,7 @@ public class FeishuInfoController {
                 }
                 List<Department> departmentList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id",feishuInfo.getCompanyId()));
                 List<DepartmentFeishu> departmentFeishuList = departmentFeishuMapper.selectList(new QueryWrapper<DepartmentFeishu>().eq("corpid", feishuInfo.getCorpid()));
+                List<User> newUserList=new ArrayList<>();
                 for (DepartmentFeishu departmentFeishu : departmentFeishuList) {
                     Optional<Department> first = departmentList.stream().filter(dl -> dl.getDepartmentId().equals(departmentFeishu.getSysDeptid())).findFirst();
                     //获取到当前部门的上级部门
@@ -681,24 +704,79 @@ public class FeishuInfoController {
                             }
                         }
                     }
+                    //处理部门下的人员
+                    JSONArray userInfoWithDepartment=feishuInfoService.getUserInfoWithDepartment(feishuInfo,departmentFeishu.getFeishuOpenDeptid(),null);
+                    for (int m=0;m<userInfoWithDepartment.size(); m++) {
+                        JSONObject userJson = userInfoWithDepartment.getJSONObject(m);
+                        String curUserid = userJson.getString("user_id");
+                        String openUserid = userJson.getString("open_id");
+                        if(!userJson.getJSONObject("status").getBoolean("is_activated")){
+                            continue;
+                        }
+                        List<String> departments = (List<String>) userJson.get("department_ids");
+                        System.out.println("user info======:"+userJson.toString());
+                        boolean b = allUserList.stream().anyMatch(al -> al.getFeishuUserid() != null && al.getFeishuUserid().equals(userJson.getString("open_id")));
+                        if(!b){
+                            //不存在的人员, 进行插入
+                            User user = new User();
+                            //在当前部门下的员工
+                            user.setId(SnowFlake.nextId()+"")
+                                    .setRoleId(role.getId())//默认普通员工
+                                    .setRoleName(role.getRolename())
+                                    .setCompanyId(feishuInfo.getCompanyId())
+                                    .setName(userJson.getString("name"))
+                                    .setFeishuUserid(openUserid)
+                                    .setColor(ColorUtil.randomColor())
+                                    .setJobNumber(curUserid)
+                                    .setPassword(MD5Util.getPassword("000000"))
+                                    .setFeishuDeptid(departmentFeishu.getFeishuDeptid());
+                            String max = departments.get(0);
+                            Optional<DepartmentFeishu> dpFs = departmentFeishuList.stream().filter(dl -> dl.getFeishuOpenDeptid().equals(max)).findFirst();
+                            if(dpFs.isPresent()){
+                                Optional<Department> dp = departmentList.stream().filter(dl -> dl.getDepartmentId().equals(dpFs.get().getSysDeptid())).findFirst();
+                                user.setDepartmentName(dp.get().getDepartmentName());
+                                user.setDepartmentId(dp.get().getDepartmentId());
+                                user.setDepartmentCascade(convertDepartmentIdToCascade(dp.get().getDepartmentId(),departmentList));
+                            }
+                            boolean c = newUserList.stream().anyMatch(ul ->ul.getFeishuUserid()!=null&&ul.getFeishuUserid().equals(user.getFeishuUserid()));
+                            if(!c){
+                                newUserList.add(user);
+                            }
+                        }
+                    }
                 }
                 //todo:处理人员
                 JSONArray userArrays = addedOb.getJSONArray("users");
-                List<User> newUserList=new ArrayList<>();
                 for (int i = 0; i < userArrays.size(); i++) {
                     JSONObject userOb = userArrays.getJSONObject(i);
                     boolean b = allUserList.stream().anyMatch(al -> al.getFeishuUserid() != null && al.getFeishuUserid().equals(userOb.getString("open_id")));
                     if(!b){
-                        User user=new User();
+                        //不存在的人员, 进行插入
+                        User user = new User();
+                        user.setId(SnowFlake.nextId()+"")
+                                .setRoleId(role.getId())//默认普通员工
+                                .setRoleName(role.getRolename())
+                                .setCompanyId(feishuInfo.getCompanyId())
+                                .setName(userOb.getString("name"))
+                                .setFeishuUserid(userOb.getString("open_id"))
+                                .setColor(ColorUtil.randomColor())
+                                .setJobNumber(userOb.getString("user_id"))
+                                .setPassword(MD5Util.getPassword("000000"));
+                        boolean c = newUserList.stream().anyMatch(ul ->ul.getFeishuUserid()!=null&&ul.getFeishuUserid().equals(user.getFeishuUserid()));
+                        if(!c){
+                            newUserList.add(user);
+                        }
                     }
                 }
+                if(newUserList.size()>0){
+                    userService.saveBatch(newUserList);
+                }
             }
             //todo:移除的
             if(eventOb.getJSONObject("removed")!=null){
 
             }
         }
-        return null;
     }
 
 }