main
黄海 2 years ago
parent 660c7955ff
commit e957fa33c2

@ -62,14 +62,17 @@ public class TestDingTalk {
//同步钉钉与云平台中部门信息
String orgName = "长春市东光学校";
//删除所有的组织机构
//OrgPerson.delAllDept(accessToken, orgName);
//同步组织机构
OrgPerson.syncOrg(accessToken, orgName);
//删除单位下所有人员(开发测试时使用)
//DingTalkUtil.delBureauPerson(accessToken,orgName);
//OrgPerson.delBureauPerson(accessToken,orgName);
//同步人员
OrgPerson.syncPerson(accessToken,orgName);
//OrgPerson.syncPerson(accessToken,orgName);
CommonUtil.Print("恭喜,所有操作成功完成!");
}

@ -28,12 +28,16 @@ public class OrgPerson {
* @param access_token
* @throws ApiException
*/
public static long createDept(String access_token, String deptName, long parentId, long orderId) throws ApiException {
public static long createDept(String access_token, String deptName, long parentId, long orderId, boolean isRoot) throws ApiException {
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);
if (isRoot) {
req.setOuterDept(true);
req.setOuterDeptOnlySelf(true);
}
OapiV2DepartmentCreateResponse rsp = client.execute(req, access_token);
JSONObject jo = JSONObject.parseObject(rsp.getBody());
if (jo.getLong("errcode") > 0) {
@ -85,6 +89,7 @@ public class OrgPerson {
*/
public static List<Kv> deptList = new ArrayList();
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();
@ -105,6 +110,20 @@ public class OrgPerson {
}
}
public static void delAllDept(String access_token, String orgName) throws ApiException {
Record rOrg = Model.getOrgByOrgName(orgName);
int bureauId = rOrg.getInt("org_id");
//获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下
long DT_BureauId = Model.getDtDeptId(bureauId);
delDept(access_token, DT_BureauId);
deptList.clear();
getDeptList(access_token, DT_BureauId);
for (int i = 0; i < deptList.size(); i++) {
delDept(access_token, deptList.get(i).getLong("dept_id"));
}
}
public static JSONObject getDeptInfo(String access_token, long deptId) {
JSONObject jo;
try {
@ -126,11 +145,10 @@ public class OrgPerson {
int sortId = rOrg.getInt("sort_id");
//获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下
long DT_BureauId = Model.getDtDeptId(bureauId);
//获取部门信息
deptList.clear();
JSONObject jRes = getDeptInfo(accessToken, DT_BureauId);
if (jRes.getLong("errcode") > 0) {
DT_BureauId = createDept(accessToken, orgName, 1, sortId);
DT_BureauId = createDept(accessToken, orgName, 1, sortId, true);
Model.writeDtDeptId(bureauId, String.valueOf(DT_BureauId));//记得要回写
CommonUtil.Print("单位名称:" + orgName + "不存在!已创建!");
} else {
@ -210,7 +228,7 @@ public class OrgPerson {
int org_id = mapOfB.get(key).getInteger("org_id");
//这个部门是在哪个部门下的
long dingTalkParentDeptId = Model.getDtDeptId(parentId);
long dt_dept_id = createDept(accessToken, org_name, dingTalkParentDeptId, sId);
long dt_dept_id = createDept(accessToken, org_name, dingTalkParentDeptId, sId, false);
Model.writeDtDeptId(org_id, String.valueOf(dt_dept_id));
CommonUtil.Print("成功创建部门:" + org_name);
}

Loading…
Cancel
Save