Przeglądaj źródła

企业微信同步通讯录

seyason 2 lat temu
rodzic
commit
b709fb3e8a

+ 58 - 10
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/WeiXinCorpController.java

@@ -870,7 +870,11 @@ public class WeiXinCorpController {
                         List<Department> sysDeptList = new ArrayList<>();
                         for (int i=0;i<deptObjJSONArray.size(); i++) {
                             int deptId = deptObjJSONArray.getJSONObject(i).getIntValue("id");
+                            int parentId = deptObjJSONArray.getJSONObject(i).getIntValue("parentid");
+
                             Department department = new Department();
+                            department.setCorpwxDeptid(deptId);
+                            department.setCorpwxDeptpid(parentId);
                             department.setDepartmentName(deptObjJSONArray.getJSONObject(i).getString("name"));
                             department.setCompanyId(companyId);
                             departmentMapper.insert(department);
@@ -1466,20 +1470,23 @@ public class WeiXinCorpController {
         return wxCorpInfoService.syncMembByCardTime(wxCorpInfo);
     }
 
-    //老版本
-    @RequestMapping("/getCorpMembsOld")
-    public HttpRespMsg getCorpMembsOld(String corpId) {
-
-        WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectById(corpId);
-
-        Company company = companyMapper.selectById(wxCorpInfo.getCompanyId());
+    //改造老版的接口,从平台获取客户通讯录
+    @RequestMapping("/getCorpMembsFromPlatform")
+    public HttpRespMsg getCorpMembsFromPlatform(Integer companyId) {
+        Company company = companyMapper.selectById(companyId);
+        WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectOne(new QueryWrapper<WxCorpInfo>().eq("company_id", companyId));
+        if (wxCorpInfo == null) {
+            HttpRespMsg msg = new HttpRespMsg();
+            msg.setError("仅企业微信用户支持该操作");
+            return msg;
+        }
         String curCorpAccessToken = null;
         try {
             curCorpAccessToken = getCorpAccessToken(wxCorpInfo);
         } catch (Exception exception) {
             exception.printStackTrace();
         }
-        int companyId = company.getId();
+        //默认普通员工的角色
         //获取公司根部门人员,也就是没有分配部门的人员
         int companyRootDeptId = 1;
         JSONArray unAssignedUserList = getDeptUserSimple(curCorpAccessToken, companyRootDeptId);
@@ -1499,6 +1506,7 @@ public class WeiXinCorpController {
                     .setCompanyId(companyId)
                     .setName(userJson.getString("name"))
                     .setCorpwxUserid(curUserid)
+                    .setJobNumber(curUserid)
                     .setColor(ColorUtil.randomColor());
 
             //检查用户是否已经存在
@@ -1513,6 +1521,18 @@ public class WeiXinCorpController {
 
         for (int i=0;i<deptObjJSONArray.size(); i++) {
             int deptId = deptObjJSONArray.getJSONObject(i).getIntValue("id");
+            int parentId = deptObjJSONArray.getJSONObject(i).getIntValue("parentid");
+            //部门不存在的话要生成部门
+            Department curDept = departmentMapper.selectOne(new QueryWrapper<Department>().eq("company_id", companyId).eq("corpwx_deptid", deptId));
+            if (curDept == null) {
+                Department department = new Department();
+                department.setCorpwxDeptid(deptId);
+                department.setCorpwxDeptpid(parentId);
+                department.setDepartmentName(deptObjJSONArray.getJSONObject(i).getString("name"));
+                department.setCompanyId(companyId);
+                departmentMapper.insert(department);
+                curDept = department;
+            }
 
             JSONArray userList = getDeptUserSimple(curCorpAccessToken, deptId);
             for (int m=0;m<userList.size(); m++) {
@@ -1526,17 +1546,45 @@ public class WeiXinCorpController {
                         .setRoleId(defaultRole.getId())//默认普通员工
                         .setRoleName(defaultRole.getRolename())
                         .setCompanyId(companyId)
-                        .setDepartmentId(0)
+                        .setDepartmentId(curDept.getDepartmentId())
                         .setName(userJson.getString("name"))
                         .setCorpwxUserid(curUserid)
-                        .setColor(ColorUtil.randomColor());
+                        .setColor(ColorUtil.randomColor())
+                        .setJobNumber(curUserid);
 
                 //检查用户是否已经存在
                 if (userMapper.selectCount(new QueryWrapper<User>().eq("corpwx_userid", curUserid)) == 0) {
                     userMapper.insert(user);
+                } 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(curDept.getDepartmentId());
+                    userMapper.updateById(oldUser);
+                }
+            }
+        }
+
+        List<Department> needUpdateDepts = new ArrayList<>();
+        List<Department> allDeptList = departmentMapper.selectList(new QueryWrapper<Department>().eq("company_id", companyId));
+        for (int i=0;i<allDeptList.size(); i++) {
+            Department firstLevel = allDeptList.get(i);
+            if (firstLevel.getCorpwxDeptpid() != null) {
+                Optional<Department> first = allDeptList.stream().filter(one -> firstLevel.getCorpwxDeptpid().equals(one.getCorpwxDeptid())).findFirst();
+                if (first.isPresent()) {
+                    if (!first.get().getDepartmentId().equals(firstLevel.getSuperiorId())) {
+                        //结构发生变动,需要更新
+                        Department updateDpt = new Department();
+                        updateDpt.setDepartmentId(firstLevel.getDepartmentId());
+                        updateDpt.setSuperiorId(first.get().getDepartmentId());
+                        needUpdateDepts.add(updateDpt);
+                    }
                 }
             }
         }
+        if (needUpdateDepts.size() > 0) {
+            departmentService.updateBatchById(needUpdateDepts);
+        }
 
         return new HttpRespMsg();
     }

+ 45 - 39
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/UserServiceImpl.java

@@ -1027,6 +1027,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
             XSSFSheet sheet = workbook.getSheetAt(0);
             //查重检验的手机号列表
             List<String> phoneList = new ArrayList<>();
+            List<String> jobNumList = new ArrayList<>();
             //要插入的账号列表
             List<User> userList = new ArrayList<>();
             //获取已有的部门列表
@@ -1037,38 +1038,59 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
                 if (row == null) {
                     continue;
                 }
-                //此处新建账号 默认密码为000000 默认 姓名第一列 手机号第二列 部门第三列 月薪第四列
+                //此处新建账号 默认密码为000000 默认 新版:工号第一列,姓名第二列 手机号第三列 部门第四列
                 Long id = SnowFlake.nextId();
-                XSSFCell nameCell = row.getCell(0);
-                XSSFCell phoneCell = row.getCell(1);
-                XSSFCell deptCell = row.getCell(2);
+                XSSFCell jobNumberCell = row.getCell(0);
+                XSSFCell nameCell = row.getCell(1);
+                XSSFCell phoneCell = row.getCell(2);
+                XSSFCell deptCell = row.getCell(3);
 //                XSSFCell monthCostCell = row.getCell(3);
 //                XSSFCell costCell = row.getCell(4);
+                String jobNumber = "";
                 if (nameCell == null) continue;//存在空行的情况
+                if (jobNumberCell != null) {
+                    jobNumberCell.setCellType(CellType.STRING);
+                    jobNumber = jobNumberCell.getStringCellValue();
+                    jobNumList.add(jobNumber);
+                }
+
                 nameCell.setCellType(CellType.STRING);
-                phoneCell.setCellType(CellType.STRING);
+                String phone = "";
+                if (phoneCell != null) {
+                    phoneCell.setCellType(CellType.STRING);
+                    phone = phoneCell.getStringCellValue();
+                    System.out.println("phone=="+phone);
+                } else {
+
+                }
 
                 String name = nameCell.getStringCellValue();
-                String phone = phoneCell.getStringCellValue();
+
                 String dept = null;
                 if (deptCell != null) {
                     deptCell.setCellType(CellType.STRING);
                     dept = deptCell.getStringCellValue();
                 }
 
-                if (name.equals("姓名") && phone.equals("手机号") && rowIndex == 0) {
-                    if (!"部门".equals(dept)) {
+                if (rowIndex == 0) {
+                    if (!"工号".equals(jobNumber)) {
                         //httpRespMsg.setError("缺少部门列,请下载最新模板进行导入");
-                        httpRespMsg.setError(MessageUtils.message("Template.lackDepartment"));
+                        httpRespMsg.setError(MessageUtils.message("Template.lackJobNumber"));
                         return httpRespMsg;
+                    } else {
+                        continue;
                     }
-                    continue;
                 }
                 //姓名为空的,不处理
                 if (StringUtils.isEmpty(name)) {
                     continue;
                 }
                 name = name.trim();
+                //检验手机号,不能为空
+                if (StringUtils.isEmpty(phone)) {
+                    httpRespMsg.setError("手机号不能为空");
+                    return httpRespMsg;
+                }
                 Integer deptId;
                 String deptCascade;
                 if (StringUtils.isEmpty(dept)) {
@@ -1090,32 +1112,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
                 int salaryType = 0;
                 BigDecimal monthCost = new BigDecimal(0);
                 BigDecimal cost = new BigDecimal(0);
-//                if (monthCostCell != null) {
-//                    monthCostCell.setCellType(CellType.STRING);
-//                    String monthCostString = monthCostCell.getStringCellValue();
-//
-//                    monthCost = MathUtil.isIntegerOrDigital(monthCostString)? new BigDecimal(monthCostString) : BigDecimal.valueOf(0);
-//                    //计算时薪
-//                    cost = monthCost.divide(monthHours, 2, RoundingMode.HALF_UP);
-//                    if (MathUtil.isIntegerOrDigital(monthCostString)) {
-//                        salaryType = 0;
-//                    } else {
-//                        salaryType = 1;
-//                    }
-//                } else {
-//                    salaryType = 1;
-//                }
-//                if (costCell != null) {
-//                    costCell.setCellType(CellType.STRING);
-//                    String costString = costCell.getStringCellValue();
-//                    cost = MathUtil.isIntegerOrDigital(costString) ? new BigDecimal(costString) : BigDecimal.valueOf(0);
-//                }
-//                salaryType = 1;
                 if (!StringUtils.isEmpty(phone)) {
                     phoneList.add(phone);
                 }
 
-
                 userList.add(new User()
                         .setId(id.toString())
                         .setName(name)
@@ -1129,12 +1129,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
                         .setCompanyId(companyId)
                         .setMonthCost(monthCost)
                         .setCost(cost)
-                        .setSalaryType(salaryType));
+                        .setSalaryType(salaryType).setJobNumber(jobNumber));
             }
-            //最后删掉这个文件
-//            if (!file.delete()) {
-//                System.out.println("临时文件" + file.getName() + "删除失败");
-//            }
             //检查本次导入人数是否超过上限
             if (userList.size() > canImportNum) {
                 //httpRespMsg.setError("仅剩余"+canImportNum+"人可添加,请减少本次导入的人员数量或者联系客服提高人数上限。");
@@ -1142,8 +1138,18 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
             } else {
                 //校验是否有重复账号
                 if (userMapper.selectCount(new QueryWrapper<User>().in("phone", phoneList)) == 0) {
-                    for (User user : userList) {
-                        userMapper.insert(user);
+                    //检查工号是否在当前公司有重复
+                    if (jobNumList.size() > 0) {
+                        int jobNumCnt = userMapper.selectCount(new QueryWrapper<User>().eq("company_id", companyId).in("job_number", jobNumList));
+                        if (jobNumCnt > 0) {
+                            //有重复的
+                            List<User> jobNumUsers = userMapper.selectList(new QueryWrapper<User>().eq("company_id", companyId).in("job_number", jobNumList));
+                            String duplicateJobNums = jobNumUsers.stream().map(User::getJobNumber).collect(Collectors.joining(","));
+
+                            httpRespMsg.setError("工号已存在:"+duplicateJobNums);
+                        } else {
+                            saveBatch(userList);
+                        }
                     }
                 } else {
                     List<User> existList = userMapper.selectList(new QueryWrapper<User>().in("phone", phoneList));

BIN
fhKeeper/formulahousekeeper/management-platform/src/main/resources/upload/人员导入模板.xlsx