Browse Source

Merge branch 'master' of ssh://10.10.100.21/source/uas-office-integration

RaoMeng 5 years ago
parent
commit
f0a3d08f1f

+ 19 - 1
qywx-sdk/src/test/java/com/usoftchina/qywx/sdk/test/AddrBookSdkTest.java

@@ -6,6 +6,7 @@ import com.usoftchina.qywx.sdk.dto.*;
 import org.junit.Test;
 import org.springframework.util.CollectionUtils;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
@@ -57,7 +58,15 @@ public class AddrBookSdkTest extends BaseTest {
 
     @Test
     public void deleteUser() {
-        sdk.deleteUser("qy01d3ffd8cd39d7002a66ffc62c");
+        String[] userIds = new String[]{"T03547","QIULZ","XUGH","T02573","T00012","T02599","T02676","T02659","RD14102","T02608","T02582","U0718","T00227","ZHENGJ","ZHANGMR","qy0155ff9ccd32d7012a4b21ee7d","qy018cff21cd15d7012a945a2cb3","LB","T02629","T02633","WANGDY","SHIYL","T02637","qy0125ff9ccd32d7012a57faa7dc","T02652","WXR","LINDA_WANG","XHD","T02679","T02680","WUJM","CHENJJ","ZHB","T03566","T03552","T03557","T03622","T03553","T03918","T04010","T03677","T03900","T03901","T03740","T03782","T03839","T03821","T03843","T04013","T03953","T03962","T03736","qy0171ffa2cd9dd7002a7d00e8eb","T03649","T03851","T03778","T03788","T03697","T03780","T03936","T04026","T03702","T04014","T03777","qy0108ff8ccde5d7012a6eb40046","T03646","qy012dffbfcdb8d7012a83563b91","T03684","T04028","T03942","T04247","qy015fff03cd83d7072a024ca3a7","qy01d5ffbdcd47d7012a7c766201","T04289","qy015bff7acd44d7002abc7e608d","qy014fffa1cd00d7072ae26316bb","T04223","qy014bff65cd9fd7012aa2706550","qy0100ff42cdf3d7062ae603c7a2","qy0193ff38cd5fd7012aba55c61a","T04117","qy01baff0fcda8d7012aa79faff3","T04267","qy01afff8acdbad7062a141068db","qy01ffffa9cdead7062a577ae9fb","T04224","T04263","qy0164ffffcd65d7042ae805caba"};
+        for (String userId : userIds) {
+            try {
+                sdk.deleteUser(userId);
+                System.out.println("delete " + userId);
+            } catch (Exception e) {
+
+            }
+        }
     }
 
     @Test
@@ -88,4 +97,13 @@ public class AddrBookSdkTest extends BaseTest {
         sdk.updateDepartment(new UpdateDepartmentReq().id(1).name("优软科技事业部"));
     }
 
+    @Test
+    public void createDepartment() {
+        CreateDepartmentReq req = new CreateDepartmentReq()
+                .name("华商龙香港仓管科")
+                .parent(1543022789);
+        Integer departmentId = sdk.createDepartment(req);
+        System.out.println(departmentId);
+    }
+
 }

+ 1 - 1
qywx-sdk/src/test/java/com/usoftchina/qywx/sdk/test/BaseTest.java

@@ -25,7 +25,7 @@ public abstract class BaseTest {
         // 日程
         agents.add(new Agent("Schedule", null, "nBfWqPZS8GXNnkBovhtjMl8UCKI5XoaXUuZnz_Oxkao"));
         // 打卡
-        agents.add(new Agent("Checkin", 3010011, "UqGEo3694hopQIfsd3fEDVx0b0rscrl5QzNsOUeZ4lU"));
+        agents.add(new Agent("Checkin", 3010011, "UqGEo3694hopQIfsd3fEDSTJ0tmHAehavWzv082fkp0"));
         // 审批
         agents.add(new Agent("Approval", 3010040, "sSwb62-S-bGftHcCVChnxASz8SnqnvobtIU8Q42kT2w"));
 

+ 19 - 0
uas-office-dingtalk/src/main/java/com/usoftchina/uas/office/dingtalk/service/UasEmployeeService.java

@@ -353,4 +353,23 @@ public class UasEmployeeService extends AbstractService {
             }
         }
     }
+
+    /**
+     * 清除在uas有绑定em_ding,而在钉钉已删除的人员
+     */
+    public void clearInvalidBinding() {
+        List<String> empList = queryForList("select distinct em_ding from employee where em_ding is not null", String.class);
+        if (!CollectionUtils.isEmpty(empList)) {
+            HrOrg root = getRootOrg();
+            if (null != root && null != root.getOr_ding()) {
+                List<OapiUserListbypageResponse.Userlist> userList = addrBookSdk.getUserList("Uas", root.getOr_ding(), true);
+                Set<String> idSet = userList.stream().map(OapiUserListbypageResponse.Userlist::getUserid).collect(Collectors.toSet());
+                for (String emp : empList) {
+                    if (!idSet.contains(emp)) {
+                        jdbcTemplate.update("update employee set em_ding=null where em_ding=?", emp);
+                    }
+                }
+            }
+        }
+    }
 }

+ 18 - 0
uas-office-dingtalk/src/main/java/com/usoftchina/uas/office/dingtalk/service/UasOrgService.java

@@ -15,6 +15,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @author yingp
@@ -244,6 +245,7 @@ public class UasOrgService extends AbstractService {
                         List<String> users = addrBookSdk.getUserIdList("Uas", department.getId());
                         if (CollectionUtils.isEmpty(users)) {
                             addrBookSdk.deleteDepartment("Uas", department.getId());
+                            jdbcTemplate.update("update HrOrg set or_ding=null where or_ding=?", department.getId());
                             group.get(department.getParentid()).remove(department);
                             if (group.get(department.getParentid()).isEmpty()) {
                                 group.remove(department.getParentid());
@@ -259,4 +261,20 @@ public class UasOrgService extends AbstractService {
             }
         }
     }
+
+    /**
+     * 清除在uas有绑定or_ding,而在钉钉已删除的部门
+     */
+    public void clearInvalidBinding() {
+        List<Long> orgList = queryForList("select distinct or_ding from HrOrg where or_ding is not null and or_ding<>1", Long.class);
+        if (!CollectionUtils.isEmpty(orgList)) {
+            List<OapiDepartmentListResponse.Department> departmentList = addrBookSdk.getDepartmentList("Uas", 1);
+            Set<Long> idSet = departmentList.stream().map(OapiDepartmentListResponse.Department::getId).collect(Collectors.toSet());
+            for (Long org : orgList) {
+                if (!idSet.contains(org)) {
+                    jdbcTemplate.update("update HrOrg set or_ding=null where or_ding=?", org);
+                }
+            }
+        }
+    }
 }

+ 40 - 0
uas-office-dingtalk/src/main/java/com/usoftchina/uas/office/dingtalk/task/DingAddrBookTask.java

@@ -0,0 +1,40 @@
+package com.usoftchina.uas.office.dingtalk.task;
+
+import com.usoftchina.uas.office.dingtalk.service.UasEmployeeService;
+import com.usoftchina.uas.office.dingtalk.service.UasOrgService;
+import com.usoftchina.uas.office.entity.DataCenter;
+import com.usoftchina.uas.office.jdbc.DataSourceHolder;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author yingp
+ * @date 2020/5/14
+ */
+@Component
+public class DingAddrBookTask {
+
+    @Autowired
+    private UasEmployeeService uasEmployeeService;
+
+    @Autowired
+    private UasOrgService uasOrgService;
+
+    /**
+     * 清除在uas有绑定,而在钉钉已删除的部门、人员
+     */
+    @Scheduled(cron = "0 0 23 * * ?  ")
+    public void clearInvalid() {
+        DataCenter dataCenter = DataCenter.INSTANCE;
+        if (null != dataCenter.getUsername() && null != dataCenter.getPassword() && null != dataCenter.getUrl()) {
+            try {
+                DataSourceHolder.set(dataCenter);
+                uasOrgService.clearInvalidBinding();
+                uasEmployeeService.clearInvalidBinding();
+            } finally {
+                DataSourceHolder.clear();
+            }
+        }
+    }
+}

+ 11 - 11
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/listener/UasEmployeeListener.java

@@ -46,16 +46,16 @@ public class UasEmployeeListener {
     /**
      * uas人员资料反审核,禁用企业微信通讯录人员
      */
-    @UasEventListener(caller = "Employeemanager", operation = "RESAUDIT")
-    public void onEmployeeUnAudit(UasEvent event) {
-        if (!addrBookSdk.enabled() || !event.getMaster().equals(DataCenter.INSTANCE.getUsername())) {
-            return;
-        }
-        Employee employee = employeeService.getById(Integer.parseInt(event.getKey().toString()));
-        if (null != employee && null != employee.getEm_qywx() && !addrBookSdk.isReadonly()) {
-            addrBookSdk.updateUser(new UpdateUserReq().userId(employee.getEm_qywx()).enable(false));
-        }
-    }
+//    @UasEventListener(caller = "Employeemanager", operation = "RESAUDIT")
+//    public void onEmployeeUnAudit(UasEvent event) {
+//        if (!addrBookSdk.enabled() || !event.getMaster().equals(DataCenter.INSTANCE.getUsername())) {
+//            return;
+//        }
+//        Employee employee = employeeService.getById(Integer.parseInt(event.getKey().toString()));
+//        if (null != employee && null != employee.getEm_qywx() && !addrBookSdk.isReadonly()) {
+//            addrBookSdk.updateUser(new UpdateUserReq().userId(employee.getEm_qywx()).enable(false));
+//        }
+//    }
 
     /**
      * uas人员资料删除,删除企业微信通讯录人员
@@ -66,7 +66,7 @@ public class UasEmployeeListener {
             return;
         }
         Employee employee = employeeService.getRemovedById(Integer.parseInt(event.getKey().toString()));
-        if (null != employee && null != employee.getEm_qywx() && !addrBookSdk.isReadonly()) {
+        if (null != employee && null != employee.getEm_qywx() && !addrBookSdk.isReadonly() && "离职".equals(employee.getEm_class())) {
             addrBookSdk.deleteUser(employee.getEm_qywx());
         }
     }

+ 24 - 0
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/service/UasEmployeeService.java

@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @author yingp
@@ -380,4 +381,27 @@ public class UasEmployeeService extends AbstractService {
         QywxUserFactory qywxUserFactory = new QywxUserFactory(getRootOrg().getOr_qywx());
         sync(employee, qywxUserFactory);
     }
+
+    /**
+     * 清除在uas有绑定em_qywx,而在企业微信已删除的人员
+     */
+    public void clearInvalidBinding() {
+        if (addrBookSdk.isReadonly()) {
+            return;
+        }
+        List<String> empList = queryForList("select distinct em_qywx from employee where em_qywx is not null", String.class);
+        if (!CollectionUtils.isEmpty(empList)) {
+            HrOrg root = getRootOrg();
+            if (null != root && null != root.getOr_qywx()) {
+                List<GetUserListResp.User> userList = addrBookSdk.getUserList(root.getOr_qywx(), true);
+                Set<String> idSet = userList.stream().map(GetUserListResp.User::getUserid).collect(Collectors.toSet());
+                for (String emp : empList) {
+                    if (!idSet.contains(emp)) {
+                        jdbcTemplate.update("update employee set em_qywx=null where em_qywx=?", emp);
+                        logger.debug("clear invalid employee {}", emp);
+                    }
+                }
+            }
+        }
+    }
 }

+ 25 - 3
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/service/UasOrgService.java

@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @author yingp
@@ -56,10 +57,10 @@ public class UasOrgService extends AbstractService {
      * 同步全部 uas组织资料 -> 企业微信部门资料
      */
     public void sync() {
-        List<HrOrg> HrOrgList = queryForBeanList("select * from HrOrg where or_statuscode<>'DISABLE'", HrOrg.class);
-        if (!CollectionUtils.isEmpty(HrOrgList)) {
+        List<HrOrg> orgList = queryForBeanList("select * from HrOrg where or_statuscode<>'DISABLE'", HrOrg.class);
+        if (!CollectionUtils.isEmpty(orgList)) {
             List<GetDepartmentListResp.Department> departmentList = addrBookSdk.getDepartmentList();
-            checkOrg(groupOrgList(HrOrgList), 0, groupDepartmentList(departmentList), 0);
+            checkOrg(groupOrgList(orgList), 0, groupDepartmentList(departmentList), 0);
         }
     }
 
@@ -266,6 +267,7 @@ public class UasOrgService extends AbstractService {
                         List<GetSimpleUserListResp.User> users = addrBookSdk.getSimpleUserList(department.getId(), true);
                         if (CollectionUtils.isEmpty(users)) {
                             addrBookSdk.deleteDepartment(department.getId());
+                            jdbcTemplate.update("update HrOrg set or_qywx=null where or_qywx=?", department.getId());
                             group.get(department.getParentid()).remove(department);
                             if (group.get(department.getParentid()).isEmpty()) {
                                 group.remove(department.getParentid());
@@ -281,4 +283,24 @@ public class UasOrgService extends AbstractService {
             }
         }
     }
+
+    /**
+     * 清除在uas有绑定or_qywx,而在企业微信已删除的部门
+     */
+    public void clearInvalidBinding() {
+        if (addrBookSdk.isReadonly()) {
+            return;
+        }
+        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());
+            for (Integer org : orgList) {
+                if (!idSet.contains(org)) {
+                    jdbcTemplate.update("update HrOrg set or_qywx=null where or_qywx=?", org);
+                    logger.debug("clear invalid org {}", org);
+                }
+            }
+        }
+    }
 }

+ 40 - 0
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/task/QywxAddrBookTask.java

@@ -0,0 +1,40 @@
+package com.usoftchina.uas.office.qywx.task;
+
+import com.usoftchina.uas.office.entity.DataCenter;
+import com.usoftchina.uas.office.jdbc.DataSourceHolder;
+import com.usoftchina.uas.office.qywx.service.UasEmployeeService;
+import com.usoftchina.uas.office.qywx.service.UasOrgService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author yingp
+ * @date 2020/5/14
+ */
+@Component
+public class QywxAddrBookTask {
+
+    @Autowired
+    private UasEmployeeService uasEmployeeService;
+
+    @Autowired
+    private UasOrgService uasOrgService;
+
+    /**
+     * 清除在uas有绑定,而在企业微信已删除的部门、人员
+     */
+    @Scheduled(cron = "0 0 23 * * ?  ")
+    public void clearInvalid() {
+        DataCenter dataCenter = DataCenter.INSTANCE;
+        if (null != dataCenter.getUsername() && null != dataCenter.getPassword() && null != dataCenter.getUrl()) {
+            try {
+                DataSourceHolder.set(dataCenter);
+                uasOrgService.clearInvalidBinding();
+                uasEmployeeService.clearInvalidBinding();
+            } finally {
+                DataSourceHolder.clear();
+            }
+        }
+    }
+}

+ 1 - 1
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/task/QywxPushMsgTask.java

@@ -39,7 +39,7 @@ public class QywxPushMsgTask {
             return;
         }
         DataCenter dataCenter = DataCenter.INSTANCE;
-        if (null != dataCenter.getUsername() && null != dataCenter.getPassword() && null != dataCenter.getUrl()) {
+        if (null != dataCenter && null != dataCenter.getUsername() && null != dataCenter.getPassword() && null != dataCenter.getUrl()) {
             try {
                 DataSourceHolder.set(dataCenter);
                 try {