|
@@ -4,6 +4,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.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.google.gson.JsonObject;
|
|
|
import com.management.platform.constant.Constant;
|
|
@@ -809,5 +810,189 @@ public class FeishuInfoController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @RequestMapping("/getFeishuMembsFromPlatform")
|
|
|
+ public HttpRespMsg getFeishuMembsFromPlatform(HttpServletRequest request){
|
|
|
+ HttpRespMsg msg=new HttpRespMsg();
|
|
|
+ Integer companyId = userMapper.selectById(request.getHeader("token")).getCompanyId();
|
|
|
+ FeishuInfo feishuInfo = feishuInfoMapper.selectOne(new LambdaQueryWrapper<FeishuInfo>().eq(FeishuInfo::getCompanyId, companyId));
|
|
|
+ List<User> allUserList = userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getCompanyId, companyId));
|
|
|
+ String corpId = feishuInfo.getCorpid();
|
|
|
+ if(feishuInfo==null){
|
|
|
+ msg.setData("飞书配置暂未完成,请联系服务商");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ SysRole role = sysRoleMapper.selectOne(new QueryWrapper<SysRole>().eq("company_id",feishuInfo.getCompanyId()).eq("rolename","普通员工"));
|
|
|
+ //获取应用可用范围内的部门
|
|
|
+ Map<String, Object> availableRange = feishuInfoService.getAvailableRange(feishuInfo, null);
|
|
|
+ JSONArray deptArray = (JSONArray) availableRange.get("department_ids");
|
|
|
+ JSONArray departmentInfoArrays=new JSONArray();
|
|
|
+ for (int i = 0; i < deptArray.size(); i++) {
|
|
|
+ String dpId = deptArray.getString(i);
|
|
|
+ JSONArray departmentInfo = feishuInfoService.getDepartmentInfo(feishuInfo, dpId);
|
|
|
+ if(departmentInfo!=null&&departmentInfo.size()>0){
|
|
|
+ departmentInfoArrays.addAll(departmentInfo);
|
|
|
+ }
|
|
|
+ JSONArray subDepartmentList =getSubDepartmentList(feishuInfo,dpId);
|
|
|
+ if(subDepartmentList!=null&&subDepartmentList.size()>0){
|
|
|
+ departmentInfoArrays.addAll(subDepartmentList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //todo:处理部门
|
|
|
+ JSONArray departmentArray = departmentInfoArrays;
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<Department> departmentList=new ArrayList<>();
|
|
|
+ List<DepartmentFeishu> departmentFeishuList=new ArrayList<>();
|
|
|
+ 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"));
|
|
|
+ String openDepartmentId = String.valueOf(ob.getString("open_department_id"));
|
|
|
+ String departmentParentId =String.valueOf(ob.getString("parent_department_id"));
|
|
|
+ JSONArray leaders = ob.getJSONArray("leaders");
|
|
|
+ Integer cut = departmentFeishuMapper.selectCount(new QueryWrapper<DepartmentFeishu>().eq("corpid",feishuInfo.getCorpid()).eq("feishu_deptid",departmentId));
|
|
|
+ if(cut==0&&!departmentId.equals("0")){
|
|
|
+ System.out.println("join===========");
|
|
|
+ DepartmentFeishu departmentFeishu=new DepartmentFeishu();
|
|
|
+ departmentFeishu.setCorpid(feishuInfo.getCorpid())
|
|
|
+ .setName(departmentName)
|
|
|
+ .setFeishuParentid(departmentParentId)
|
|
|
+ .setFeishuDeptid(departmentId)
|
|
|
+ .setFeishuOpenDeptid(openDepartmentId);
|
|
|
+ Department department = new Department();
|
|
|
+ department.setCompanyId(feishuInfo.getCompanyId());
|
|
|
+ department.setDepartmentName(departmentName);
|
|
|
+ department.setFeishuDeptid(openDepartmentId);
|
|
|
+ departmentMapper.insert(department);
|
|
|
+ List<DepartmentOtherManager> odList=new ArrayList<>();
|
|
|
+ if(leaders!=null&&leaders.size()>0){
|
|
|
+ //todo:处理主要负责人
|
|
|
+ Optional<Object> first = leaders.stream().filter(l -> ((JSONObject) l).getIntValue("leaderType") == 1).findFirst();
|
|
|
+ if(first.isPresent()){
|
|
|
+ JSONObject firstOb = (JSONObject) first.get();
|
|
|
+ Optional<User> leaderID = allUserList.stream().filter(ul -> ul.getFeishuUserid().equals(firstOb.getString("leaderID"))).findFirst();
|
|
|
+ if(leaderID.isPresent()){
|
|
|
+ department.setManagerId(leaderID.get().getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //todo:处理次要负责人
|
|
|
+ List<Object> othObs = leaders.stream().filter(l -> ((JSONObject) l).getIntValue("leaderType") == 2).collect(Collectors.toList());
|
|
|
+ for (Object othOb : othObs) {
|
|
|
+ JSONObject oth = (JSONObject) othOb;
|
|
|
+ Optional<User> leaderID = allUserList.stream().filter(ul -> ul.getFeishuUserid().equals(oth.getString("leaderID"))).findFirst();
|
|
|
+ if(leaderID.isPresent()){
|
|
|
+ DepartmentOtherManager departmentOtherManager=new DepartmentOtherManager();
|
|
|
+ departmentOtherManager.setCompanyId(feishuInfo.getCompanyId());
|
|
|
+ departmentOtherManager.setDepartmentId(department.getDepartmentId());
|
|
|
+ departmentOtherManager.setOtherManagerId(leaderID.get().getId());
|
|
|
+ odList.add(departmentOtherManager);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ departmentMapper.updateById(department);
|
|
|
+ departmentList.add(department);
|
|
|
+ departmentFeishu.setSysDeptid(department.getDepartmentId());
|
|
|
+ departmentFeishuMapper.insert(departmentFeishu);
|
|
|
+ departmentFeishuList.add(departmentFeishu);
|
|
|
+ departmentOtherManagerService.saveBatch(odList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<User> newUserList=new ArrayList<>();
|
|
|
+ for (DepartmentFeishu departmentFeishu : departmentFeishuList) {
|
|
|
+ Optional<Department> first = departmentList.stream().filter(dl -> dl.getDepartmentId().equals(departmentFeishu.getSysDeptid())).findFirst();
|
|
|
+ //获取到当前部门的上级部门
|
|
|
+ Optional<DepartmentFeishu> wx = departmentFeishuList.stream().filter(dl -> dl.getFeishuOpenDeptid().equals(departmentFeishu.getFeishuParentid())).findFirst();
|
|
|
+ if(first.isPresent()){
|
|
|
+ if(wx.isPresent()){
|
|
|
+ Optional<Department> dp = departmentList.stream().filter(dl -> dl.getDepartmentId().equals(wx.get().getSysDeptid())).findFirst();
|
|
|
+ if(dp.isPresent()){
|
|
|
+ first.get().setSuperiorId(dp.get().getDepartmentId());
|
|
|
+ departmentMapper.updateById(first.get());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //处理部门下的人员
|
|
|
+ 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();
|
|
|
+ if(dp.isPresent()){
|
|
|
+ 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 = (JSONArray) availableRange.get("user_ids");
|
|
|
+ if(userArrays!=null&&userArrays.size()>0){
|
|
|
+ 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.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);
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|