From 8c4124882a1f7235278323778f7a65757c3ff83d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Tue, 6 Jun 2023 16:02:28 +0800 Subject: [PATCH] 'commit' --- src/main/java/UnitTest/TestDingTalk.java | 82 ++--------- .../dsideal/FengHuang/Util/CommonUtil.java | 6 +- .../dsideal/FengHuang/Util/DingTalkUtil.java | 133 ++++++++++++++++-- 3 files changed, 135 insertions(+), 86 deletions(-) diff --git a/src/main/java/UnitTest/TestDingTalk.java b/src/main/java/UnitTest/TestDingTalk.java index bcc7c9b..982a57e 100644 --- a/src/main/java/UnitTest/TestDingTalk.java +++ b/src/main/java/UnitTest/TestDingTalk.java @@ -2,6 +2,7 @@ package UnitTest; import com.alibaba.druid.filter.stat.StatFilter; import com.alibaba.fastjson.JSONObject; +import com.dsideal.FengHuang.Util.CommonUtil; import com.dsideal.FengHuang.Util.DingTalkUtil; import com.jfinal.kit.PropKit; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; @@ -12,6 +13,7 @@ import com.jfinal.plugin.druid.DruidPlugin; import com.jfinal.plugin.redis.RedisPlugin; import com.taobao.api.ApiException; +import java.sql.SQLOutput; import java.util.*; public class TestDingTalk { @@ -60,81 +62,13 @@ public class TestDingTalk { // 用于缓存模块的redis服务 RedisPlugin redis = new RedisPlugin("myRedis", PropKit.get("redis_ip"), PropKit.getInt("redis_port"), 10 * 1000); redis.start(); - //AccessToken - String accessToken = DingTalkUtil.getAccessToken(appKey, appSecret); - //查询钉钉中有哪些部门 - String orgName = "长春市东光学校"; - int orgId = DingTalkUtil.getOrgIdByOrgName(orgName); - - //获取部门信息 - DingTalkUtil.deptList.clear(); - long dept_id = DingTalkUtil.getDtDeptId(orgId); - DingTalkUtil.getDeptList(accessToken, dept_id); - - if (DingTalkUtil.deptList.size() == 0) { - DingTalkUtil.createDept(accessToken, orgName, 1, 1); - } - // 云平台->集合A,钉钉->集合B - // (1) x在A中存在,在B中也存在,内容也一致:不处理 - // (2) x在A中存在,在B中也存在,内容不一致:更新处理 - // (3) x在A中存在,在B中不存在,新增 - // (4) x在A中不存在,在B中存在,删除 - - Set keysOfA = new HashSet<>(); - Map mapOfA = new HashMap<>(); - Set keysOfB = new HashSet<>(); - Map mapOfB = new HashMap<>(); - - //A:钉钉 - for (int i = 0; i < DingTalkUtil.deptList.size(); i++) { - long deptId = DingTalkUtil.deptList.get(i).getLong("dept_id"); - String org_name = DingTalkUtil.deptList.get(i).getStr("name"); - keysOfA.add(deptId); - JSONObject jo = new JSONObject(); - jo.put("org_name", org_name); - mapOfA.put(deptId, jo); - } - //B:云平台 - List list = DingTalkUtil.getOrgList(orgId); - for (int i = 0; i < list.size(); i++) { - long deptId = list.get(i).getLong("org_id"); - String org_name = list.get(i).getStr("org_name"); - int sortId = list.get(i).getInt("sort_id"); - int parentId = list.get(i).getInt("parent_id"); - keysOfB.add(deptId); + //accessToken + String accessToken = DingTalkUtil.getAccessToken(appKey, appSecret); - JSONObject jo = new JSONObject(); - jo.put("org_name", org_name); - jo.put("sort_id", sortId); - jo.put("parent_id", parentId); - mapOfB.put(deptId, jo); - } - //在A不在B - for (Long key : keysOfA) { - if (!keysOfB.contains(key)) { - // key只存在于A中 - // 删除 - DingTalkUtil.delDept(accessToken, key); - } else { - // key在A和B中都存在 - DingTalkUtil.updateDept(accessToken, key, mapOfB.get(key).getString("org_name"), mapOfB.get(key).getInteger("sort_id")); - } - } - //在B不在A - for (Long key : keysOfB) { - if (!keysOfA.contains(key)) { - // key只存在于B中 - // 增加 - String org_name = mapOfB.get(key).getString("org_name"); - int sortId = mapOfB.get(key).getInteger("sort_id"); - int parentId = mapOfB.get(key).getInteger("parent_id"); - if (parentId == -1) parentId = orgId; - long dingtalk_dept_id = DingTalkUtil.getDtDeptId(parentId); - DingTalkUtil.createDept(accessToken, org_name, dingtalk_dept_id, sortId); - System.out.println("成功创建部门:" + org_name); - } - } - System.out.println("恭喜,所有操作成功完成!"); + //同步钉钉与云平台中部门信息 + String orgName = "长春市东光学校"; + DingTalkUtil.syncOrg(accessToken, orgName); + CommonUtil.Print("恭喜,所有操作成功完成!"); } } diff --git a/src/main/java/com/dsideal/FengHuang/Util/CommonUtil.java b/src/main/java/com/dsideal/FengHuang/Util/CommonUtil.java index 1d2165e..6f75406 100644 --- a/src/main/java/com/dsideal/FengHuang/Util/CommonUtil.java +++ b/src/main/java/com/dsideal/FengHuang/Util/CommonUtil.java @@ -551,7 +551,7 @@ public class CommonUtil { */ public static String getClassPath() throws URISyntaxException { String path = PathKit.class.getClassLoader().getResource("").toURI().getPath(); - if (getOsSystemType() == OsSystemConst.WINDOWS){ + if (getOsSystemType() == OsSystemConst.WINDOWS) { if (path.startsWith("/")) path = path.substring(1); } return path; @@ -566,4 +566,8 @@ public class CommonUtil { if (ip.equals(ips[i])) flag = true; return flag; } + + public static void Print(String msg) { + System.out.println(DateTime.now() + " " + msg); + } } \ No newline at end of file diff --git a/src/main/java/com/dsideal/FengHuang/Util/DingTalkUtil.java b/src/main/java/com/dsideal/FengHuang/Util/DingTalkUtil.java index 6f887a2..d546322 100644 --- a/src/main/java/com/dsideal/FengHuang/Util/DingTalkUtil.java +++ b/src/main/java/com/dsideal/FengHuang/Util/DingTalkUtil.java @@ -15,7 +15,7 @@ import com.jfinal.plugin.activerecord.Record; import com.jfinal.plugin.redis.Redis; import com.taobao.api.ApiException; -import java.util.List; +import java.util.*; public class DingTalkUtil { /** @@ -197,7 +197,7 @@ public class DingTalkUtil { OapiV2DepartmentDeleteRequest req = new OapiV2DepartmentDeleteRequest(); req.setDeptId(deptId); OapiV2DepartmentDeleteResponse rsp = client.execute(req, access_token); - System.out.println(rsp.getBody()); + //System.out.println(rsp.getBody()); } /** @@ -217,7 +217,7 @@ public class DingTalkUtil { req.setName(deptName); req.setLanguage("zh_CN"); OapiV2DepartmentUpdateResponse rsp = client.execute(req, access_token); - System.out.println(rsp.getBody()); + //System.out.println(rsp.getBody()); } /** @@ -269,7 +269,7 @@ public class DingTalkUtil { req.setTitle(zhiWei); req.setDeptIdList(deptId); OapiV2UserCreateResponse rsp = client.execute(req, access_token); - System.out.println(rsp.getBody()); + //System.out.println(rsp.getBody()); } /** @@ -284,7 +284,7 @@ public class DingTalkUtil { OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest(); req.setUserid(userId); OapiV2UserDeleteResponse rsp = client.execute(req, access_token); - System.out.println(rsp.getBody()); + //System.out.println(rsp.getBody()); } @@ -300,16 +300,14 @@ public class DingTalkUtil { return r.get("dingtalk_dept_id"); } - public static int getOrgIdByOrgName(String org_name) { - String sql = "select org_id,org_name from t_base_organization where org_name = ?"; - Record record = Db.findFirst(sql, org_name); - int orgId = record.getInt("org_id"); - return orgId; + public static Record getOrgByOrgName(String org_name) { + String sql = "select org_id,org_name,sort_id from t_base_organization where org_name = ?"; + return Db.findFirst(sql, org_name); } public static List getOrgList(int orgId) { // 学校及学校下的部门 - String sql = "select org_id,org_name,parent_id,sort_id,dingtalk_dept_id from t_base_organization where bureau_id=? order by org_id"; + String sql = "select org_id,org_name,parent_id,sort_id,dingtalk_dept_id from t_base_organization where bureau_id=? and org_id<>bureau_id order by org_id"; List list = Db.find(sql, orgId); return list; } @@ -342,4 +340,117 @@ public class DingTalkUtil { writeDtDeptId(orgId, String.valueOf(dt_dept_id)); } } + + public static JSONObject getDeptInfo(String access_token, long deptId){ + JSONObject jo; + try { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get"); + OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest(); + req.setDeptId(deptId); + req.setLanguage("zh_CN"); + OapiV2DepartmentGetResponse rsp = client.execute(req, access_token); + jo = JSONObject.parseObject(rsp.getBody()); + } catch (Exception err) { + jo = JSONObject.parseObject(err.toString()); + } + return jo; + } + + public static void syncOrg(String accessToken,String orgName) throws ApiException { + Record rOrg = getOrgByOrgName(orgName); + int bureauId = rOrg.getInt("org_id"); + int sortId = rOrg.getInt("sort_id"); + //获取部门信息 + deptList.clear(); + //获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下 + long DT_BureauId = getDtDeptId(bureauId); + + JSONObject jRes = getDeptInfo(accessToken, DT_BureauId); + if (jRes.getLong("errcode") > 0) { + DT_BureauId = createDept(accessToken, orgName, 1, sortId); + writeDtDeptId(bureauId, String.valueOf(DT_BureauId));//记得要回写 + CommonUtil.Print("单位名称:"+orgName+"不存在!已创建!"); + } else { + CommonUtil.Print("单位名称:"+orgName+"已存在,保留!"); + } + //获取下属部门的列表 + getDeptList(accessToken, DT_BureauId); + + // 云平台->集合A,钉钉->集合B + // (1) x在A中存在,在B中也存在,内容也一致:不处理 + // (2) x在A中存在,在B中也存在,内容不一致:更新处理 + // (3) x在A中存在,在B中不存在,新增 + // (4) x在A中不存在,在B中存在,删除 + + Set keysOfA = new HashSet<>(); + Map mapOfA = new HashMap<>(); + Set keysOfB = new HashSet<>(); + Map mapOfB = new HashMap<>(); + //A:钉钉 + for (int i = 0; i < deptList.size(); i++) { + long deptId = deptList.get(i).getLong("dept_id"); + String org_name = deptList.get(i).getStr("name"); + keysOfA.add(deptId); + JSONObject jo = new JSONObject(); + jo.put("org_name", org_name); + mapOfA.put(deptId, jo); + } + //B:云平台 + List list = getOrgList(bureauId); + for (int i = 0; i < list.size(); i++) { + if (list.get(i).get("dingtalk_dept_id") != null) { + long deptId = list.get(i).getLong("dingtalk_dept_id"); + String org_name = list.get(i).getStr("org_name"); + sortId = list.get(i).getInt("sort_id"); + int org_id = list.get(i).getInt("org_id"); + int parentId = list.get(i).getInt("parent_id"); + keysOfB.add(deptId); + + JSONObject jo = new JSONObject(); + jo.put("org_id", org_id); + jo.put("org_name", org_name); + jo.put("sort_id", sortId); + jo.put("parent_id", parentId); + mapOfB.put(deptId, jo); + } + } + //在A不在B + for (Long key : keysOfA) { + if (!keysOfB.contains(key)) { + // key只存在于A中 + // 删除 + CommonUtil.Print("发现钉钉中多出的dept_id:" + key + ",将删除掉..."); + delDept(accessToken, key); + } else { + // key在A和B中都存在 + String aName = mapOfA.get(key).getString("org_name"); + String bName = mapOfB.get(key).getString("org_name"); + int sId = mapOfB.get(key).getInteger("sort_id"); + if (aName.equals(bName)) { + CommonUtil.Print("发现钉钉与云平台间部门名称一致,不需要修改,aName=" + aName + ",bName=" + bName); + continue; + } + CommonUtil.Print("发现钉钉与云平台间部门名称不一致,将修改,aName=" + aName + ",bName=" + bName); + updateDept(accessToken, key, bName, sId); + } + } + + //在B不在A + for (Long key : keysOfB) { + if (!keysOfA.contains(key)) { + // key只存在于B中 + // 增加 + String org_name = mapOfB.get(key).getString("org_name"); + System.out.println(org_name); + int sId = mapOfB.get(key).getInteger("sort_id"); + int parentId = mapOfB.get(key).getInteger("parent_id"); + int org_id = mapOfB.get(key).getInteger("org_id"); + //这个部门是在哪个部门下的 + long dingTalkParentDeptId = getDtDeptId(parentId); + long dt_dept_id = createDept(accessToken, org_name, dingTalkParentDeptId, sId); + writeDtDeptId(org_id, String.valueOf(dt_dept_id)); + CommonUtil.Print("成功创建部门:" + org_name); + } + } + } }