From 4fb12298ae1b95ba81d3233fb3c00169dac825d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Wed, 7 Jun 2023 08:24:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dsideal/FengHuang/Util/DingTalkUtil.java | 153 +++++++++++++----- 1 file changed, 110 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/dsideal/FengHuang/Util/DingTalkUtil.java b/src/main/java/com/dsideal/FengHuang/Util/DingTalkUtil.java index 71ba356..8e7eb1c 100644 --- a/src/main/java/com/dsideal/FengHuang/Util/DingTalkUtil.java +++ b/src/main/java/com/dsideal/FengHuang/Util/DingTalkUtil.java @@ -252,44 +252,6 @@ public class DingTalkUtil { } } - /** - * 功能:创建人员 - * - * @param access_token - * @param deptId - * @param userId - * @param personName - * @param tel - * @param zhiWei - * @throws ApiException - */ - public static void createPerson(String access_token, String deptId, String userId, String personName, String tel, String zhiWei) throws ApiException { - DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/create"); - OapiV2UserCreateRequest req = new OapiV2UserCreateRequest(); - req.setUserid(userId); - req.setName(personName); - req.setMobile(tel); - req.setTitle(zhiWei); - req.setDeptIdList(deptId); - OapiV2UserCreateResponse rsp = client.execute(req, access_token); - //System.out.println(rsp.getBody()); - } - - /** - * 功能:删除人员 - * - * @param access_token - * @param userId - * @throws ApiException - */ - public static void delPerson(String access_token, String userId) throws ApiException { - DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/delete"); - OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest(); - req.setUserid(userId); - OapiV2UserDeleteResponse rsp = client.execute(req, access_token); - //System.out.println(rsp.getBody()); - } - public static JSONObject getDeptInfo(String access_token, long deptId) { JSONObject jo; try { @@ -403,8 +365,56 @@ public class DingTalkUtil { } } - public static List personList = new ArrayList(); + /** + * 功能:创建人员 + * + * @param access_token + * @param deptId + * @param userId + * @param personName + * @param tel + * @param zhiWei + * @throws ApiException + */ + public static void createPerson(String access_token, String deptId, String userId, String personName, String tel, String zhiWei) throws ApiException { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/create"); + OapiV2UserCreateRequest req = new OapiV2UserCreateRequest(); + req.setUserid(userId); + req.setName(personName); + req.setMobile(tel); + req.setTitle(zhiWei); + req.setDeptIdList(deptId); + OapiV2UserCreateResponse rsp = client.execute(req, access_token); + //System.out.println(rsp.getBody()); + } + + /** + * 功能:删除人员 + * + * @param access_token + * @param userId + * @throws ApiException + */ + public static void delPerson(String access_token, String userId) throws ApiException { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/delete"); + OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest(); + req.setUserid(userId); + OapiV2UserDeleteResponse rsp = client.execute(req, access_token); + //System.out.println(rsp.getBody()); + } + + public static void updatePerson(String access_token, String person_id, String person_name, String tel) throws ApiException { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/update"); + OapiV2UserUpdateRequest req = new OapiV2UserUpdateRequest(); + req.setUserid(person_id); + req.setName(person_name); + req.setMobile(tel); + OapiV2UserUpdateResponse rsp = client.execute(req, access_token); + System.out.println(rsp.getBody()); + } + + public static List personList = new ArrayList(); public static void getDeptPerson(String access_token, long dept_id) throws ApiException { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listsimple"); @@ -456,22 +466,79 @@ public class DingTalkUtil { } } + /** + * 功能:同步人员信息 + * + * @param accessToken + * @param orgName + * @throws ApiException + */ public static void syncPerson(String accessToken, String orgName) throws ApiException { Record rOrg = getOrgByOrgName(orgName); int bureauId = rOrg.getInt("org_id"); - List list = DingTalkUtil.getDeptPerson(bureauId); + Set keysOfA = new HashSet<>(); + Map mapOfA = new HashMap<>(); + Set keysOfB = new HashSet<>(); + Map mapOfB = new HashMap<>(); + + //A:钉钉 + for (int i = 0; i < personList.size(); i++) { + String userid = personList.get(i).getStr("userid"); + String name = personList.get(i).getStr("name"); + keysOfA.add(userid); + + JSONObject jo = new JSONObject(); + jo.put("person_id", userid); + jo.put("person_name", name); + mapOfA.put(userid, jo); + } + + //B:云平台 + List list = getDeptPerson(bureauId); for (int i = 0; i < list.size(); i++) { Record r = list.get(i); - long deptId = r.getLong("dingtalk_dept_id"); int person_id = r.getInt("person_id"); String person_name = r.getStr("person_name"); + long dingtalk_dept_id = r.getLong("dingtalk_dept_id"); String tel = r.getStr("tel"); if (StrKit.isBlank(tel) || !PhoneUtil.isMobile(tel)) { tel = ChineseMobileNumberGenerator.getInstance().generate();//生成一个随机临时测试用的手机号 } - //DingTalkUtil.createPerson(accessToken, String.valueOf(deptId), String.valueOf(person_id), person_name, tel, "教师"); - //CommonUtil.Print("成功加入人员:" + person_name); + keysOfB.add(String.valueOf(person_id)); + JSONObject jo = new JSONObject(); + jo.put("person_id", person_id); + jo.put("person_name", person_name); + jo.put("dingtalk_dept_id", dingtalk_dept_id); + jo.put("tel", tel); + mapOfB.put(String.valueOf(person_id), jo); + } + //在A不在B + for (String key : keysOfA) { + if (!keysOfB.contains(key)) { + // key只存在于A中 + // 删除 + CommonUtil.Print("发现钉钉中多出的user_id:" + key + ",将删除掉..."); + delPerson(accessToken, key); + } else { + // key在A和B中都存在 + updatePerson(accessToken, key, mapOfB.get(key).getString("person_name"), mapOfB.get(key).getString("tel")); + CommonUtil.Print("暴力修改钉钉与云平台间部门人员姓名、手机号!,person_name=" + mapOfB.get(key).getString("person_name")); + } + } + + //在B不在A + for (String key : keysOfB) { + if (!keysOfA.contains(key)) { + // key只存在于B中 + // 增加 + String person_id = mapOfB.get(key).getString("person_id"); + String person_name = mapOfB.get(key).getString("person_name"); + String tel = mapOfB.get(key).getString("tel"); + long deptId = mapOfB.get(key).getLong("dingtalk_dept_id"); + DingTalkUtil.createPerson(accessToken, String.valueOf(deptId), person_id, person_name, tel, "教师"); + CommonUtil.Print("成功加入人员:" + person_name); + } } }