main
黄海 2 years ago
parent d783fdbc89
commit 32dcac913c

@ -9,6 +9,7 @@ import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.taobao.api.ApiException;
import java.util.List;
@ -38,10 +39,6 @@ public class TestDingTalk {
return druidPlugin;
}
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 void main(String[] args_) throws Exception {
PropKit.use("dingtalk.properties");
@ -62,49 +59,21 @@ 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);
//查询钉钉中有哪些部门
int orgId = DingTalkUtil.getOrgIdByOrgName("长春市东光学校");
//导入部门
DingTalkUtil.importYptOrg(accessToken, orgId);
//获取部门信息
DingTalkUtil.deptList.clear();
long dept_id = DingTalkUtil.getDtDeptId(orgId);
DingTalkUtil.getDeptList(accessToken, dept_id);
//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`);
*/
String sql = "select org_id,org_name from t_base_organization where org_name = '长春市东光学校'";
Record record = Db.findFirst(sql);
int orgId = record.getInt("org_id");
//单位需要清空一下这个属性,有枣没枣都打一下子
writeDtDeptId(orgId, null);
// 学校及学校下的部门
sql = "select org_id,org_name,parent_id,sort_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 yptParentId = list.get(i).getInt("parent_id");
if (yptParentId == -1) yptParentId = orgId;
String dept_name = list.get(i).getStr("org_name");
sql = "select dingtalk_dept_id from t_base_organization where org_id=?";
Record r = Db.findFirst(sql, yptParentId);
long dingtalk_dept_id;
if (r.get("dingtalk_dept_id") == null) {
dingtalk_dept_id = 1;
} else {
dingtalk_dept_id = r.getLong("dingtalk_dept_id");
}
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));
for (int i = 0; i < DingTalkUtil.deptList.size(); i++) {
System.out.println(DingTalkUtil.deptList.get(i).getLong("dept_id"));
System.out.println(DingTalkUtil.deptList.get(i).getStr("name"));
System.out.println(DingTalkUtil.deptList.get(i).getLong("parent_id"));
}
System.out.println("恭喜,所有操作成功完成!");
}

@ -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));
}
}
}

Loading…
Cancel
Save