You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

459 lines
20 KiB

2 years ago
package com.dsideal.FengHuang.DingTalk;
2 years ago
2 years ago
import cn.binarywang.tools.generator.ChineseMobileNumberGenerator;
2 years ago
import cn.hutool.core.util.PhoneUtil;
2 years ago
import com.alibaba.fastjson.JSONArray;
2 years ago
import com.alibaba.fastjson.JSONObject;
2 years ago
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
2 years ago
import com.aliyun.tea.TeaException;
2 years ago
import com.aspose.slides.Collections.ArrayList;
2 years ago
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
2 years ago
import com.dsideal.FengHuang.Util.CommonUtil;
2 years ago
import com.jfinal.kit.Kv;
2 years ago
import com.jfinal.kit.StrKit;
2 years ago
import com.jfinal.plugin.activerecord.Record;
2 years ago
import com.taobao.api.ApiException;
2 years ago
import java.util.*;
2 years ago
2 years ago
public class OrgPerson {
2 years ago
/**
*
*
* @param access_token
* @throws ApiException
*/
2 years ago
public static long createDept(String access_token, String deptName, long parentId, long orderId, boolean isRoot) throws ApiException {
2 years ago
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/create");
OapiV2DepartmentCreateRequest req = new OapiV2DepartmentCreateRequest();
req.setParentId(parentId);
req.setOrder(orderId);
req.setName(deptName);
2 years ago
if (isRoot) {
req.setOuterDept(true);
req.setOuterDeptOnlySelf(true);
}
2 years ago
OapiV2DepartmentCreateResponse rsp = client.execute(req, access_token);
JSONObject jo = JSONObject.parseObject(rsp.getBody());
if (jo.getLong("errcode") > 0) {
return -1;
}
return jo.getJSONObject("result").getLong("dept_id");
}
/**
*
*
* @param access_token
* @param deptId
* @throws ApiException
*/
public static void delDept(String access_token, long deptId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/delete");
OapiV2DepartmentDeleteRequest req = new OapiV2DepartmentDeleteRequest();
req.setDeptId(deptId);
OapiV2DepartmentDeleteResponse rsp = client.execute(req, access_token);
2 years ago
//System.out.println(rsp.getBody());
2 years ago
}
/**
*
*
* @param access_token
* @param deptId
* @param deptName
* @param orderId
* @throws ApiException
*/
public static void updateDept(String access_token, long deptId, String deptName, long orderId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/update");
OapiV2DepartmentUpdateRequest req = new OapiV2DepartmentUpdateRequest();
req.setDeptId(deptId);
req.setOrder(orderId);
req.setName(deptName);
req.setLanguage("zh_CN");
OapiV2DepartmentUpdateResponse rsp = client.execute(req, access_token);
2 years ago
//System.out.println(rsp.getBody());
2 years ago
}
/**
*
*
* @param access_token
* @throws ApiException
*/
2 years ago
public static List<Kv> deptList = new ArrayList();
2 years ago
public static void getDeptList(String access_token, long dept_id) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
req.setDeptId(dept_id);
req.setLanguage("zh_CN");
OapiV2DepartmentListsubResponse rsp = client.execute(req, access_token);
2 years ago
JSONObject jo = JSONObject.parseObject(rsp.getBody());
JSONArray ja = jo.getJSONArray("result");
2 years ago
if (ja == null) return;
2 years ago
for (int i = 0; i < ja.size(); i++) {
JSONObject j = ja.getJSONObject(i);
Kv kv = Kv.create();
long childDeptId = j.getLongValue("dept_id");
kv.set("dept_id", childDeptId);
kv.set("name", j.getString("name"));
deptList.add(kv);
getDeptList(access_token, childDeptId);//递归
}
2 years ago
}
2 years ago
public static void delAllDept(String access_token) throws ApiException {
2 years ago
for (int i = 0; i < deptList.size(); i++) {
delDept(access_token, deptList.get(i).getLong("dept_id"));
2 years ago
CommonUtil.Print("成功删除部门:"+deptList.get(i).getStr("name"));
2 years ago
}
}
2 years ago
public static JSONObject getDeptInfo(String access_token, long deptId) {
2 years ago
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;
}
2 years ago
public static void syncOrg(String accessToken, Record rOrg) throws ApiException {
String orgName = rOrg.getStr("org_name");
int bureau_id = rOrg.getInt("org_id");
int sort_id = rOrg.getInt("sort_id");
2 years ago
//获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下
2 years ago
long dingtalk_dept_id = Model.getDtDeptId(bureau_id);
2 years ago
2 years ago
JSONObject jRes = getDeptInfo(accessToken, dingtalk_dept_id);
2 years ago
if (jRes.getLong("errcode") > 0) {
2 years ago
dingtalk_dept_id = createDept(accessToken, orgName, 1, sort_id, true);
Model.writeDtDeptId(bureau_id, String.valueOf(dingtalk_dept_id));//记得要回写
2 years ago
CommonUtil.Print("单位名称:" + orgName + "不存在!已创建!");
2 years ago
} else {
2 years ago
CommonUtil.Print("单位名称:" + orgName + "已存在,保留!");
2 years ago
//获取下属部门的列表
getDeptList(accessToken, dingtalk_dept_id);
2 years ago
}
// 云平台->集合A,钉钉->集合B
// (1) x在A中存在在B中也存在内容也一致不处理
// (2) x在A中存在在B中也存在内容不一致更新处理
// (3) x在A中存在在B中不存在新增
// (4) x在A中不存在在B中存在删除
Set<Long> keysOfA = new HashSet<>();
Map<Long, JSONObject> mapOfA = new HashMap<>();
Set<Long> keysOfB = new HashSet<>();
Map<Long, JSONObject> 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:云平台
2 years ago
List<Record> list = Model.getOrgList(bureau_id);
2 years ago
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");
2 years ago
sort_id = list.get(i).getInt("sort_id");
2 years ago
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);
2 years ago
jo.put("sort_id", sort_id);
2 years ago
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");
//这个部门是在哪个部门下的
2 years ago
long dingTalkParentDeptId = Model.getDtDeptId(parentId);
2 years ago
long dt_dept_id = createDept(accessToken, org_name, dingTalkParentDeptId, sId, false);
2 years ago
Model.writeDtDeptId(org_id, String.valueOf(dt_dept_id));
2 years ago
CommonUtil.Print("成功创建部门:" + org_name);
}
}
2 years ago
//重新获取下属部门的列表,使得后续的代码不再获取部门列表
getDeptList(accessToken, dingtalk_dept_id);
2 years ago
}
2 years ago
2 years ago
/**
*
*
* @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());
}
2 years ago
public static void updatePerson(String access_token, String person_id, String person_name) throws ApiException {
2 years ago
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/update");
OapiV2UserUpdateRequest req = new OapiV2UserUpdateRequest();
req.setUserid(person_id);
req.setName(person_name);
OapiV2UserUpdateResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
}
public static List<Kv> personList = new ArrayList();
2 years ago
public static void getDeptPerson(String access_token, long dept_id) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listsimple");
long next_cursor = 0;
while (true) {
OapiUserListsimpleRequest req = new OapiUserListsimpleRequest();
req.setDeptId(dept_id);
req.setCursor(next_cursor);
req.setSize(10L);
req.setOrderField("modify_desc");
req.setContainAccessLimit(false);
req.setLanguage("zh_CN");
OapiUserListsimpleResponse rsp = client.execute(req, access_token);
JSONObject jo = JSONObject.parseObject(rsp.getBody());
JSONArray ja = jo.getJSONObject("result").getJSONArray("list");
if (ja == null) return;
for (int i = 0; i < ja.size(); i++) {
JSONObject j = ja.getJSONObject(i);
Kv kv = Kv.create();
String userid = j.getString("userid");
String name = j.getString("name");
kv.set("userid", userid);
kv.set("name", name);
personList.add(kv);
}
if (jo.getJSONObject("result").getBoolean("has_more"))
next_cursor = jo.getJSONObject("result").getLong("next_cursor");
else break;
}
}
public static void getBureauPerson(String access_token, long dept_id) throws ApiException {
personList.clear();
getDeptPerson(access_token, dept_id);
for (int i = 0; i < deptList.size(); i++) {
getDeptPerson(access_token, deptList.get(i).getInt("dept_id"));
}
}
2 years ago
public static void delBureauPerson(String accessToken, Record rOrg) throws ApiException {
int dingtalk_dept_id = rOrg.getInt("dingtalk_dept_id");
2 years ago
getBureauPerson(accessToken, dingtalk_dept_id);
for (int i = 0; i < personList.size(); i++) {
String userid = personList.get(i).getStr("userid");
String person_name = personList.get(i).getStr("name");
2 years ago
delPerson(accessToken, userid);
2 years ago
CommonUtil.Print("成功删除:" + person_name);
2 years ago
}
}
2 years ago
/**
*
*
* @param accessToken
2 years ago
* @param rOrg
2 years ago
* @throws ApiException
*/
2 years ago
public static void syncPerson(String accessToken, Record rOrg) throws ApiException {
2 years ago
int bureauId = rOrg.getInt("org_id");
2 years ago
//获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下
2 years ago
long DT_BureauId = Model.getDtDeptId(bureauId);
2 years ago
getBureauPerson(accessToken, DT_BureauId);
2 years ago
Set<String> keysOfA = new HashSet<>();
Map<String, JSONObject> mapOfA = new HashMap<>();
Set<String> keysOfB = new HashSet<>();
Map<String, JSONObject> 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:云平台
2 years ago
List<Record> list = Model.getDeptPerson(bureauId);
2 years ago
for (int i = 0; i < list.size(); i++) {
Record r = list.get(i);
int person_id = r.getInt("person_id");
String person_name = r.getStr("person_name");
2 years ago
long dingtalk_dept_id = r.getLong("dingtalk_dept_id");
2 years ago
String tel = r.getStr("tel");
if (StrKit.isBlank(tel) || !PhoneUtil.isMobile(tel)) {
tel = ChineseMobileNumberGenerator.getInstance().generate();//生成一个随机临时测试用的手机号
2 years ago
tel = "14" + tel.substring(2);//以14段开头避开已存在的号码
2 years ago
}
2 years ago
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中都存在
2 years ago
String aName = mapOfA.get(key).getString("person_name");
String bName = mapOfB.get(key).getString("person_name");
if (!aName.equals(bName)) {
updatePerson(accessToken, key, mapOfB.get(key).getString("person_name"));
CommonUtil.Print("发现钉钉与云平台间部门人员姓名不一致,将修改,person_name=" + mapOfB.get(key).getString("person_name"));
} else {
CommonUtil.Print("钉钉与云平台间部门人员姓名一致,无需修改,person_name=" + mapOfB.get(key).getString("person_name"));
}
2 years ago
}
}
//在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");
2 years ago
createPerson(accessToken, String.valueOf(deptId), person_id, person_name, tel, "教师");
2 years ago
CommonUtil.Print("成功加入人员:" + person_name);
}
2 years ago
}
}
2 years ago
/**
*
*
* @param access_token
* @param userId
* @return
*/
public static String getPerson(String access_token, String userId) {
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
OapiV2UserGetRequest req = new OapiV2UserGetRequest();
req.setUserid(userId);
req.setLanguage("zh_CN");
OapiV2UserGetResponse rsp = client.execute(req, access_token);
return rsp.getBody();
} catch (ApiException e) {
e.printStackTrace();
}
return null;
2 years ago
}
2 years ago
//TODO 未完成
public static String getPersonToken(String appKey, String appSecret) throws Exception {
com.aliyun.dingtalkoauth2_1_0.Client client = Common.createClient();
GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()
.setClientId(appKey)
.setClientSecret(appSecret)
//.setCode("abcd")
.setRefreshToken("abcd")
.setGrantType("authorization_code");
try {
GetUserTokenResponse res = client.getUserToken(getUserTokenRequest);
System.out.println(res.getBody().getAccessToken());
System.out.println(res.getBody().getRefreshToken());
} catch (TeaException err) {
System.out.println(err);
} catch (Exception _err) {
System.out.println(_err);
}
return null;
2 years ago
}
2 years ago
}