Browse Source

【企业微信】【组织部门对接接口升级】

wuyx 1 year ago
parent
commit
5a4150230e

+ 54 - 5
qywx-sdk/src/main/java/com/usoftchina/qywx/sdk/AddrBookSdk.java

@@ -6,6 +6,7 @@ import com.usoftchina.qywx.sdk.dto.*;
 import org.springframework.http.ResponseEntity;
 import org.springframework.ui.ModelMap;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
 
 import java.util.List;
 
@@ -146,7 +147,27 @@ public class AddrBookSdk extends BaseSdk {
         assertOK(resp);
         return resp.getBody().getUserlist();
     }
-
+    /**
+     * 获取部门成员详情
+     * @param cursor 用于分页查询的游标,字符串类型,由上一次调用返回,首次调用不填
+     * limit   分页,预期请求的数据量,取值范围 1 ~ 10000 默认10000
+     * @return
+     */
+    public List<GetSimpleUserIDListResp.User> getUserIdList(List<GetSimpleUserIDListResp.User> userList,String cursor) {
+        ResponseEntity<GetSimpleUserIDListResp> resp = restTemplate.getForEntity(baseUrl + "/cgi-bin/user/list_id?access_token={access_token}&cursor={cursor}&limit={limit}",
+                GetSimpleUserIDListResp.class,
+                new ModelMap("access_token", getAccessToken(ADDRESS_BOOK_AGENT_CODE))
+                        .addAttribute("cursor", cursor)
+                        .addAttribute("limit", 10000));
+        assertOK(resp);
+        if(StringUtils.hasText(resp.getBody().getNext_cursor())){
+            System.out.println("Next_cursor:"+resp.getBody().getNext_cursor());
+            return getUserIdList(resp.getBody().getDept_user(),resp.getBody().getNext_cursor());
+        }else {
+            userList.addAll(resp.getBody().getDept_user());
+            return userList;
+        }
+    }
     /**
      * userid转openid
      * <p>该接口使用场景为企业支付,在使用企业红包和向员工付款时,需要自行将企业微信的userid转成openid。</p>
@@ -274,10 +295,11 @@ public class AddrBookSdk extends BaseSdk {
 
     /**
      * 获取部门
-     *
+     * 企业微信升级,方法不可用
      * @param departmentId
      * @return
      */
+    @Deprecated
     public GetDepartmentListResp.Department getDepartment(int departmentId) {
         List<GetDepartmentListResp.Department> departmentList = getDepartmentList(departmentId);
         if (!CollectionUtils.isEmpty(departmentList)) {
@@ -292,10 +314,11 @@ public class AddrBookSdk extends BaseSdk {
 
     /**
      * 获取部门,包括下级
-     *
+     * 企业微信升级,方法不可用
      * @param departmentId
      * @return
      */
+    @Deprecated
     public List<GetDepartmentListResp.Department> getDepartmentList(int departmentId) {
         ResponseEntity<GetDepartmentListResp> resp = restTemplate.getForEntity(baseUrl + "/cgi-bin/department/list?access_token={access_token}&id={id}",
                 GetDepartmentListResp.class,
@@ -310,9 +333,10 @@ public class AddrBookSdk extends BaseSdk {
 
     /**
      * 获取部门列表
-     *
+     * 企业微信升级,方法不可用
      * @return
      */
+    @Deprecated
     public List<GetDepartmentListResp.Department> getDepartmentList() {
         ResponseEntity<GetDepartmentListResp> resp = restTemplate.getForEntity(baseUrl + "/cgi-bin/department/list?access_token={access_token}",
                 GetDepartmentListResp.class,
@@ -320,7 +344,32 @@ public class AddrBookSdk extends BaseSdk {
         assertOK(resp);
         return resp.getBody().getDepartment();
     }
-
+    /**
+     * 获取部门列表
+     *
+     * @return
+     */
+    public List<GetDepartmentSimpleListResp.Department> getSimpleDepartmentList() {
+        ResponseEntity<GetDepartmentSimpleListResp> resp = restTemplate.getForEntity(baseUrl + "/cgi-bin/department/simplelist?access_token={access_token}&id={id}",
+                GetDepartmentSimpleListResp.class,
+                new ModelMap("access_token", getAccessToken(ADDRESS_BOOK_AGENT_CODE))
+                        .addAttribute("id", 0));
+        assertOK(resp);
+        return resp.getBody().getDepartment_id();
+    }
+    /**
+     * 获取部门列表
+     *
+     * @return
+     */
+    public List<GetDepartmentSimpleListResp.Department> getSimpleDepartmentList(Integer departmentId) {
+        ResponseEntity<GetDepartmentSimpleListResp> resp = restTemplate.getForEntity(baseUrl + "/cgi-bin/department/simplelist?access_token={access_token}&id={id}",
+                GetDepartmentSimpleListResp.class,
+                new ModelMap("access_token", getAccessToken(ADDRESS_BOOK_AGENT_CODE))
+                        .addAttribute("id", departmentId));
+        assertOK(resp);
+        return resp.getBody().getDepartment_id();
+    }
     /**
      * 获取访问用户身份
      *

+ 54 - 0
qywx-sdk/src/main/java/com/usoftchina/qywx/sdk/dto/GetDepartmentSimpleListResp.java

@@ -0,0 +1,54 @@
+package com.usoftchina.qywx.sdk.dto;
+
+import java.util.List;
+
+/**
+ * @author wuyx
+ */
+public class GetDepartmentSimpleListResp extends BaseResp {
+    private List<Department> department_id;
+
+    public List<Department> getDepartment_id() {
+        return department_id;
+    }
+
+    public void setDepartment_id(List<Department> department_id) {
+        this.department_id = department_id;
+    }
+
+    public static class Department {
+        private Integer id;
+        /**
+         * 父亲部门id。根部门为1
+         */
+        private Integer parentid;
+        /**
+         * 在父部门中的次序值。order值大的排序靠前。值范围是[0, 2^32)
+         */
+        private Long order;
+
+        public Integer getId() {
+            return id;
+        }
+
+        public void setId(Integer id) {
+            this.id = id;
+        }
+
+        public Integer getParentid() {
+            return parentid;
+        }
+
+        public void setParentid(Integer parentid) {
+            this.parentid = parentid;
+        }
+
+        public Long getOrder() {
+            return order;
+        }
+
+        public void setOrder(Long order) {
+            this.order = order;
+        }
+    }
+}

+ 53 - 0
qywx-sdk/src/main/java/com/usoftchina/qywx/sdk/dto/GetSimpleUserIDListResp.java

@@ -0,0 +1,53 @@
+package com.usoftchina.qywx.sdk.dto;
+
+import java.util.List;
+
+/**
+ * @author wuyx
+ */
+public class GetSimpleUserIDListResp extends BaseResp{
+
+    private String next_cursor;
+
+    private List<User> dept_user;
+
+    public String getNext_cursor() {
+        return next_cursor;
+    }
+
+    public void setNext_cursor(String next_cursor) {
+        this.next_cursor = next_cursor;
+    }
+
+    public List<User> getDept_user() {
+        return dept_user;
+    }
+
+    public void setDept_user(List<User> dept_user) {
+        this.dept_user = dept_user;
+    }
+
+    public static class User {
+        private String userid;
+        /**
+         * 成员所属部门列表。列表项为部门ID,32位整型
+         */
+        private Integer department;
+
+        public String getUserid() {
+            return userid;
+        }
+
+        public void setUserid(String userid) {
+            this.userid = userid;
+        }
+
+        public Integer getDepartment() {
+            return department;
+        }
+
+        public void setDepartment(Integer department) {
+            this.department = department;
+        }
+    }
+}

+ 27 - 20
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/listener/UasOrgListener.java

@@ -1,12 +1,11 @@
 package com.usoftchina.uas.office.qywx.listener;
 
 import com.usoftchina.qywx.sdk.AddrBookSdk;
-import com.usoftchina.qywx.sdk.dto.GetDepartmentListResp;
-import com.usoftchina.qywx.sdk.dto.GetUserListResp;
+import com.usoftchina.qywx.sdk.dto.GetDepartmentSimpleListResp;
+import com.usoftchina.qywx.sdk.dto.GetSimpleUserIDListResp;
 import com.usoftchina.uas.office.dto.UasEvent;
 import com.usoftchina.uas.office.entity.DataCenter;
 import com.usoftchina.uas.office.listener.UasEventListener;
-import com.usoftchina.uas.office.qywx.entity.Employee;
 import com.usoftchina.uas.office.qywx.entity.HrOrg;
 import com.usoftchina.uas.office.qywx.service.UasEmployeeService;
 import com.usoftchina.uas.office.qywx.service.UasOrgService;
@@ -14,7 +13,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author yingp
@@ -45,7 +46,7 @@ public class UasOrgListener {
         }
         if (null != org && null != org.getOr_qywx() && !addrBookSdk.isReadonly()) {
             // 检查部门下面是否有人
-            checkUser(org);
+            checkUser(org.getOr_qywx());
             // 删除,检查部门下面是否有子部门,一并删除
             deleteWithChildren(org.getOr_qywx());
         }
@@ -65,23 +66,28 @@ public class UasOrgListener {
     /**
      * 检查部门下面是否有人
      *
-     * @param org
+     * @param qywxId
      */
-    private void checkUser(HrOrg org) {
-        List<GetUserListResp.User> userList = addrBookSdk.getUserList(org.getOr_qywx(), true);
+    private void checkUser(Integer qywxId) {
+        List<GetSimpleUserIDListResp.User> userList = new ArrayList<>();
+        userList = addrBookSdk.getUserIdList(userList,"");
         if (!CollectionUtils.isEmpty(userList)) {
-            for (GetUserListResp.User user : userList) {
-                Employee employee = employeeService.getByQywx(user.getUserid());
-                if (null == employee) {
-                    addrBookSdk.deleteUser(user.getUserid());
-                } else {
-                    if (!employeeService.isMemberOfOrg(org, employee)) {
-                        employeeService.sync(employee);
-                    } else {
-                        throw new RuntimeException("部门下面还有有效人员,请先删除人员后再继续!");
-                    }
-                }
+            List<GetSimpleUserIDListResp.User> depUserList = userList.stream().filter(u->u.getDepartment().equals(qywxId)).collect(Collectors.toList());
+            if(!CollectionUtils.isEmpty(depUserList)){
+                throw new RuntimeException("部门下面还有有效人员,请先删除人员后再继续!");
             }
+//            for (GetSimpleUserIDListResp.User user : userList) {
+//                Employee employee = employeeService.getByQywx(user.getUserid());
+//                if (null == employee) {
+//                    addrBookSdk.deleteUser(user.getUserid());
+//                } else {
+//                    if (!employeeService.isMemberOfOrg(org, employee)) {
+//                        employeeService.sync(employee);
+//                    } else {
+//                        throw new RuntimeException("部门下面还有有效人员,请先删除人员后再继续!");
+//                    }
+//                }
+//            }
         }
     }
 
@@ -91,15 +97,16 @@ public class UasOrgListener {
      * @param departmentId
      */
     private void deleteWithChildren(Integer departmentId) {
-        List<GetDepartmentListResp.Department> departmentList = addrBookSdk.getDepartmentList(departmentId);
+        List<GetDepartmentSimpleListResp.Department> departmentList = addrBookSdk.getSimpleDepartmentList();
         if (!CollectionUtils.isEmpty(departmentList)) {
             if (departmentList.size() > 1) {
-                for (GetDepartmentListResp.Department department : departmentList) {
+                for (GetDepartmentSimpleListResp.Department department : departmentList) {
                     if (!department.getId().equals(departmentId)) {
                         deleteWithChildren(department.getId());
                     }
                 }
             }
+            checkUser(departmentId);
             addrBookSdk.deleteDepartment(departmentId);
         }
     }

+ 32 - 29
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/service/UasOrgService.java

@@ -2,10 +2,7 @@ package com.usoftchina.uas.office.qywx.service;
 
 import com.alibaba.fastjson.JSON;
 import com.usoftchina.qywx.sdk.AddrBookSdk;
-import com.usoftchina.qywx.sdk.dto.CreateDepartmentReq;
-import com.usoftchina.qywx.sdk.dto.GetDepartmentListResp;
-import com.usoftchina.qywx.sdk.dto.GetSimpleUserListResp;
-import com.usoftchina.qywx.sdk.dto.UpdateDepartmentReq;
+import com.usoftchina.qywx.sdk.dto.*;
 import com.usoftchina.uas.office.qywx.entity.HrOrg;
 import com.usoftchina.uas.office.service.AbstractService;
 import org.slf4j.Logger;
@@ -90,10 +87,10 @@ public class UasOrgService extends AbstractService {
                 }
             }
         } else {
-            List<GetDepartmentListResp.Department> departmentList = addrBookSdk.getDepartmentList();
+            List<GetDepartmentSimpleListResp.Department> departmentList = addrBookSdk.getSimpleDepartmentList();
             if (null == org.getOr_subof() || 0 == org.getOr_subof()) {
-                GetDepartmentListResp.Department rootDept = null;
-                for (GetDepartmentListResp.Department department : departmentList) {
+                GetDepartmentSimpleListResp.Department rootDept = null;
+                for (GetDepartmentSimpleListResp.Department department : departmentList) {
                     if (department.getParentid() == 0) {
                         rootDept = department;
                         break;
@@ -113,14 +110,16 @@ public class UasOrgService extends AbstractService {
                 try {
                     // 只需考虑父级组织已同步的情况
                     HrOrg parentOrg = getBindById(org.getOr_subof());
-                    GetDepartmentListResp.Department dept = null;
-                    for (GetDepartmentListResp.Department department : departmentList) {
-                        if (department.getParentid().equals(parentOrg.getOr_qywx()) && department.getName().equals(org.getOr_name())) {
-                            dept = department;
-                            break;
-                        }
-                    }
-                    if (null != dept) {
+//                    GetDepartmentListResp.Department dept = null;
+//                    for (GetDepartmentListResp.Department department : departmentList) {
+//                        if (department.getParentid().equals(parentOrg.getOr_qywx())
+//                                && department.getName().equals(org.getOr_name())
+//                        ) {
+//                            dept = department;
+//                            break;
+//                        }
+//                    }
+                   /* if (null != dept) {
                         if (!addrBookSdk.isReadonly()) {
                             UpdateDepartmentReq req = new UpdateDepartmentReq()
                                     .id(dept.getId())
@@ -132,19 +131,22 @@ public class UasOrgService extends AbstractService {
                             jdbcTemplate.update("update HrOrg set or_qywx=null where or_qywx=? and or_statuscode='DISABLE'", dept.getId());
                         }
                         updateOrgQywxId(org, dept.getId());
-                    } else {
-                        if (!addrBookSdk.isReadonly()) {
-                            CreateDepartmentReq req = new CreateDepartmentReq()
-                                    .name(org.getOr_name())
-                                    .parent(parentOrg.getOr_qywx());
-                            logger.debug("create qywx department " + JSON.toJSONString(req.build()));
-                            Integer departmentId = addrBookSdk.createDepartment(req);
-                            updateOrgQywxId(org, departmentId);
-                        }
+                    } else {*/
+                   //如果上级没有对接不触发对接操作
+                    if (!addrBookSdk.isReadonly()
+                            && parentOrg.getOr_qywx() != null && !parentOrg.getOr_qywx().equals(0)) {
+                        CreateDepartmentReq req = new CreateDepartmentReq()
+                                .name(org.getOr_name())
+                                .parent(parentOrg.getOr_qywx());
+                        logger.debug("create qywx department " + JSON.toJSONString(req.build()));
+                        Integer departmentId = addrBookSdk.createDepartment(req);
+                        updateOrgQywxId(org, departmentId);
                     }
+//                    }
                 } catch (EmptyResultDataAccessException e) {
                 }
             }
+
         }
     }
 
@@ -189,7 +191,8 @@ public class UasOrgService extends AbstractService {
         List<GetDepartmentListResp.Department> departmentList = deptMap.get(parentDeptId);
         if (null != orgList) {
             for (HrOrg HrOrg : orgList) {
-                GetDepartmentListResp.Department department = findDeptByName(departmentList, HrOrg.getOr_name());
+                GetDepartmentListResp.Department department = null;
+//                        findDeptByName(departmentList, HrOrg.getOr_name());
                 if (null == department) {
                     if (null != HrOrg.getOr_qywx() && HrOrg.getOr_qywx() > 0) {
                         department = findDeptById(departmentList, HrOrg.getOr_qywx());
@@ -235,7 +238,7 @@ public class UasOrgService extends AbstractService {
         return null;
     }
 
-    private GetDepartmentListResp.Department findDeptByName(List<GetDepartmentListResp.Department> departmentList, String name) {
+    /*private GetDepartmentListResp.Department findDeptByName(List<GetDepartmentListResp.Department> departmentList, String name) {
         if (null != departmentList) {
             for (GetDepartmentListResp.Department department : departmentList) {
                 if (department.getName().equals(name)) {
@@ -244,7 +247,7 @@ public class UasOrgService extends AbstractService {
             }
         }
         return null;
-    }
+    }*/
 
     /**
      * 清除下面没有人员的部门
@@ -293,8 +296,8 @@ public class UasOrgService extends AbstractService {
         }
         List<Integer> orgList = queryForList("select distinct or_qywx from HrOrg where or_qywx is not null", Integer.class);
         if (!CollectionUtils.isEmpty(orgList)) {
-            List<GetDepartmentListResp.Department> departmentList = addrBookSdk.getDepartmentList();
-            Set<Integer> idSet = departmentList.stream().map(GetDepartmentListResp.Department::getId).collect(Collectors.toSet());
+            List<GetDepartmentSimpleListResp.Department> departmentList = addrBookSdk.getSimpleDepartmentList();
+            Set<Integer> idSet = departmentList.stream().map(GetDepartmentSimpleListResp.Department::getId).collect(Collectors.toSet());
             for (Integer org : orgList) {
                 if (!idSet.contains(org)) {
                     jdbcTemplate.update("update HrOrg set or_qywx=null where or_qywx=?", org);