|
|
|
@ -1,15 +1,22 @@
|
|
|
|
|
package com.dsideal.FengHuang.Util;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.aliyun.dingtalkoauth2_1_0.models.*;
|
|
|
|
|
import com.aliyun.tea.TeaException;
|
|
|
|
|
import com.aspose.slides.Collections.ArrayList;
|
|
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
|
|
import com.dingtalk.api.DingTalkClient;
|
|
|
|
|
import com.dingtalk.api.request.*;
|
|
|
|
|
import com.dingtalk.api.response.*;
|
|
|
|
|
import com.jfinal.kit.Kv;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
import com.jfinal.plugin.redis.Redis;
|
|
|
|
|
import com.taobao.api.ApiException;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class DingTalkUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 使用 Token 初始化账号Client
|
|
|
|
@ -219,13 +226,27 @@ public class DingTalkUtil {
|
|
|
|
|
* @param access_token
|
|
|
|
|
* @throws ApiException
|
|
|
|
|
*/
|
|
|
|
|
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();
|
|
|
|
|
req.setDeptId(dept_id);
|
|
|
|
|
req.setLanguage("zh_CN");
|
|
|
|
|
OapiV2DepartmentListsubResponse rsp = client.execute(req, access_token);
|
|
|
|
|
System.out.println(rsp.getBody());
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(rsp.getBody());
|
|
|
|
|
JSONArray ja = jo.getJSONArray("result");
|
|
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
kv.set("parent_id", j.getLongValue("parent_id"));
|
|
|
|
|
deptList.add(kv);
|
|
|
|
|
getDeptList(access_token, childDeptId);//递归
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -265,4 +286,54 @@ public class DingTalkUtil {
|
|
|
|
|
OapiV2UserDeleteResponse rsp = client.execute(req, access_token);
|
|
|
|
|
System.out.println(rsp.getBody());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void writeDtDeptId(int orgId, String value) {
|
|
|
|
|
String sql = "update t_base_organization set dingtalk_dept_id=? where org_id=?";
|
|
|
|
|
Db.update(sql, value, orgId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static long getDtDeptId(int org_id) {
|
|
|
|
|
String sql = "select dingtalk_dept_id from t_base_organization where org_id=?";
|
|
|
|
|
Record r = Db.findFirst(sql, org_id);
|
|
|
|
|
if (r.get("dingtalk_dept_id") == null) return 1;
|
|
|
|
|
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 void importYptOrg(String accessToken, int orgId) throws ApiException {
|
|
|
|
|
//1、读取云平台数据,创建学校
|
|
|
|
|
/*
|
|
|
|
|
需要先手动执行,对表增加一列,记录钉钉的部门ID号:
|
|
|
|
|
ALTER TABLE `dsideal_db`.`t_base_organization`
|
|
|
|
|
ADD COLUMN `dingtalk_dept_id` bigint NULL COMMENT '钉钉中的部门ID' AFTER `zydz`,
|
|
|
|
|
ADD INDEX(`dingtalk_dept_id`);
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//单位需要清空一下这个属性,有枣没枣都打一下子
|
|
|
|
|
writeDtDeptId(orgId, null);
|
|
|
|
|
|
|
|
|
|
// 学校及学校下的部门
|
|
|
|
|
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";
|
|
|
|
|
List<Record> list = Db.find(sql, orgId);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
orgId = list.get(i).getInt("org_id");
|
|
|
|
|
int parentId = list.get(i).getInt("parent_id");
|
|
|
|
|
if (parentId == -1) parentId = orgId;
|
|
|
|
|
String dept_name = list.get(i).getStr("org_name");
|
|
|
|
|
long dingtalk_dept_id = getDtDeptId(parentId);
|
|
|
|
|
long sortId = list.get(i).getLong("sort_id");
|
|
|
|
|
//创建
|
|
|
|
|
long dt_dept_id = DingTalkUtil.createDept(accessToken, dept_name, dingtalk_dept_id, sortId);
|
|
|
|
|
//回写
|
|
|
|
|
writeDtDeptId(orgId, String.valueOf(dt_dept_id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|