|
|
|
@ -1,278 +1,41 @@
|
|
|
|
|
package UnitTest;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.aliyun.dingtalkoauth2_1_0.models.*;
|
|
|
|
|
import com.aliyun.tea.TeaException;
|
|
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
|
|
import com.dingtalk.api.DingTalkClient;
|
|
|
|
|
import com.dingtalk.api.request.*;
|
|
|
|
|
import com.dingtalk.api.response.*;
|
|
|
|
|
import com.alibaba.druid.filter.stat.StatFilter;
|
|
|
|
|
import com.dsideal.FengHuang.Util.DingTalkUtil;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import com.jfinal.plugin.redis.Redis;
|
|
|
|
|
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
|
|
|
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.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class TestDingTalk {
|
|
|
|
|
/**
|
|
|
|
|
* 使用 Token 初始化账号Client
|
|
|
|
|
*
|
|
|
|
|
* @return Client
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private static com.aliyun.dingtalkoauth2_1_0.Client createClient() throws Exception {
|
|
|
|
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
|
|
|
|
|
config.protocol = "https";
|
|
|
|
|
config.regionId = "central";
|
|
|
|
|
return new com.aliyun.dingtalkoauth2_1_0.Client(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的AccessToken
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static String getAccessToken(String appKey, String appSecret) throws Exception {
|
|
|
|
|
final String KEY = "DingTalkAccessToken";
|
|
|
|
|
if (!Redis.use().exists(KEY)) {
|
|
|
|
|
String accessToken = null;
|
|
|
|
|
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
|
|
|
|
|
com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
|
|
|
|
|
.setAppKey(appKey)
|
|
|
|
|
.setAppSecret(appSecret);
|
|
|
|
|
try {
|
|
|
|
|
GetAccessTokenResponse res = client.getAccessToken(getAccessTokenRequest);
|
|
|
|
|
accessToken = res.getBody().getAccessToken();
|
|
|
|
|
} catch (TeaException err) {
|
|
|
|
|
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception _err) {
|
|
|
|
|
TeaException err = new TeaException(_err.getMessage(), _err);
|
|
|
|
|
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Redis.use().setex(KEY, 5400, accessToken);//5400:一个半小时过期
|
|
|
|
|
return accessToken;
|
|
|
|
|
} else {
|
|
|
|
|
return Redis.use().get(KEY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的JsApiToken->ticket
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static String getJsApiToken(String access_token) throws Exception {
|
|
|
|
|
final String KEY = "DingTalkJsApiToken";
|
|
|
|
|
if (!Redis.use().exists(KEY)) {
|
|
|
|
|
String jsApiToken = null;
|
|
|
|
|
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
|
|
|
|
|
com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders createJsapiTicketHeaders = new com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders();
|
|
|
|
|
createJsapiTicketHeaders.xAcsDingtalkAccessToken = access_token;
|
|
|
|
|
try {
|
|
|
|
|
CreateJsapiTicketResponse res = client.createJsapiTicketWithOptions(createJsapiTicketHeaders, new com.aliyun.teautil.models.RuntimeOptions());
|
|
|
|
|
jsApiToken = res.getBody().getJsapiTicket();
|
|
|
|
|
} catch (TeaException err) {
|
|
|
|
|
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception _err) {
|
|
|
|
|
TeaException err = new TeaException(_err.getMessage(), _err);
|
|
|
|
|
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Redis.use().setex(KEY, 5400, jsApiToken);//5400:一个半小时过期
|
|
|
|
|
return jsApiToken;
|
|
|
|
|
} else {
|
|
|
|
|
return Redis.use().get(KEY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的SsoToken
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static String getSsoToken(String corpId, String ssoSecret) throws Exception {
|
|
|
|
|
final String KEY = "DingTalkSsoToken";
|
|
|
|
|
if (!Redis.use().exists(KEY)) {
|
|
|
|
|
String ssoToken = null;
|
|
|
|
|
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
|
|
|
|
|
GetSsoAccessTokenRequest getSsoAccessTokenRequest = new GetSsoAccessTokenRequest()
|
|
|
|
|
.setCorpid(corpId)
|
|
|
|
|
.setSsoSecret(ssoSecret);
|
|
|
|
|
try {
|
|
|
|
|
GetSsoAccessTokenResponse res = client.getSsoAccessToken(getSsoAccessTokenRequest);
|
|
|
|
|
ssoToken = res.getBody().getAccessToken();
|
|
|
|
|
} catch (TeaException err) {
|
|
|
|
|
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception _err) {
|
|
|
|
|
TeaException err = new TeaException(_err.getMessage(), _err);
|
|
|
|
|
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Redis.use().setex(KEY, 5400, ssoToken);//5400:一个半小时过期
|
|
|
|
|
return ssoToken;
|
|
|
|
|
} else {
|
|
|
|
|
return Redis.use().get(KEY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取单个人员信息
|
|
|
|
|
*
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getPersonToken(String appKey, String appSecret) throws Exception {
|
|
|
|
|
com.aliyun.dingtalkoauth2_1_0.Client client = 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void createPerson(String access_token) throws ApiException {
|
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/create");
|
|
|
|
|
OapiV2UserCreateRequest req = new OapiV2UserCreateRequest();
|
|
|
|
|
req.setUserid("002");
|
|
|
|
|
req.setName("小钉");
|
|
|
|
|
req.setSeniorMode(false);
|
|
|
|
|
req.setMobile("185xxxxxxxx");
|
|
|
|
|
req.setTitle("教职人员");
|
|
|
|
|
req.setEmail("test@xx.com");
|
|
|
|
|
req.setOrgEmail("test@xxx.com");
|
|
|
|
|
req.setOrgEmailType("profession");
|
|
|
|
|
ArrayList<OapiV2UserCreateRequest.DeptTitle> deptTitles = new ArrayList<>();
|
|
|
|
|
OapiV2UserCreateRequest.DeptTitle deptTitle = new OapiV2UserCreateRequest.DeptTitle();
|
|
|
|
|
//deptTitle.setDeptId(4868821xxL);
|
|
|
|
|
deptTitle.setTitle("测试");
|
|
|
|
|
OapiV2UserCreateRequest.DeptTitle deptTitle1 = new OapiV2UserCreateRequest.DeptTitle();
|
|
|
|
|
// deptTitle1.setDeptId(6099161xxL);
|
|
|
|
|
deptTitle1.setTitle("专员");
|
|
|
|
|
deptTitles.add(deptTitle);
|
|
|
|
|
deptTitles.add(deptTitle1);
|
|
|
|
|
req.setDeptTitleList(deptTitles);
|
|
|
|
|
req.setHideMobile(false);
|
|
|
|
|
req.setTelephone("010-8xxxxx6-2345");
|
|
|
|
|
req.setJobNumber("100828");
|
|
|
|
|
req.setHiredDate(1615219200000L);
|
|
|
|
|
req.setWorkPlace("未来park");
|
|
|
|
|
req.setRemark("备注备注");
|
|
|
|
|
//req.setDeptIdList("4868821xx,6099161xx");
|
|
|
|
|
List<OapiV2UserCreateRequest.DeptOrder> deptOrderList = new ArrayList<OapiV2UserCreateRequest.DeptOrder>();
|
|
|
|
|
OapiV2UserCreateRequest.DeptOrder deptOrder = new OapiV2UserCreateRequest.DeptOrder();
|
|
|
|
|
//deptOrder.setDeptId(4868821xxL);
|
|
|
|
|
// deptOrder.setOrder(1L);
|
|
|
|
|
OapiV2UserCreateRequest.DeptOrder deptOrder1 = new OapiV2UserCreateRequest.DeptOrder();
|
|
|
|
|
//deptOrder1.setDeptId(6099161xxL);
|
|
|
|
|
//deptOrder1.setOrder(1L);
|
|
|
|
|
deptOrderList.add(deptOrder);
|
|
|
|
|
deptOrderList.add(deptOrder1);
|
|
|
|
|
req.setDeptOrderList(deptOrderList);
|
|
|
|
|
req.setExtension("{\"爱好\":\"[爱好](http://test.com?userid=#userid#&corpid=#corpid#)\"}");
|
|
|
|
|
req.setManagerUserid("001");
|
|
|
|
|
req.setLoginEmail("test@xxx.com");
|
|
|
|
|
OapiV2UserCreateResponse rsp = client.execute(req, access_token);
|
|
|
|
|
System.out.println(rsp.getBody());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:创建部门
|
|
|
|
|
*
|
|
|
|
|
* @param access_token
|
|
|
|
|
* @throws ApiException
|
|
|
|
|
*/
|
|
|
|
|
public static long createDept(String access_token, String deptName, long parentId, long orderId) 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);
|
|
|
|
|
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);
|
|
|
|
|
System.out.println(rsp.getBody());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:更新部门信息
|
|
|
|
|
* @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);
|
|
|
|
|
System.out.println(rsp.getBody());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取部门列表
|
|
|
|
|
* @param access_token
|
|
|
|
|
* @throws ApiException
|
|
|
|
|
*/
|
|
|
|
|
public static void getDeptList(String access_token) throws ApiException {
|
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
|
|
|
|
|
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
|
|
|
|
|
req.setDeptId(1L);
|
|
|
|
|
req.setLanguage("zh_CN");
|
|
|
|
|
OapiV2DepartmentListsubResponse rsp = client.execute(req, access_token);
|
|
|
|
|
System.out.println(rsp.getBody());
|
|
|
|
|
public static DruidPlugin createDruidPlugin(String url, String username, String password, String driverClass) {
|
|
|
|
|
DruidPlugin druidPlugin = new DruidPlugin(url, username, password, driverClass);
|
|
|
|
|
//最大连接池数量
|
|
|
|
|
druidPlugin.setMaxActive(20);
|
|
|
|
|
//最小连接池数量
|
|
|
|
|
druidPlugin.setMinIdle(1);
|
|
|
|
|
//初始化时建立物理连接的个数,默认为0
|
|
|
|
|
druidPlugin.setInitialSize(1);
|
|
|
|
|
//获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
|
|
|
|
|
druidPlugin.setMaxWait(60000);
|
|
|
|
|
//如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。
|
|
|
|
|
druidPlugin.setTimeBetweenEvictionRunsMillis(60000);
|
|
|
|
|
//连接保持空闲而不被驱逐的最小时间
|
|
|
|
|
druidPlugin.setMinEvictableIdleTimeMillis(300000);
|
|
|
|
|
//建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
|
|
|
|
|
druidPlugin.setTestWhileIdle(true);
|
|
|
|
|
//申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。默认为true
|
|
|
|
|
druidPlugin.setTestOnBorrow(false);
|
|
|
|
|
//归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。默认为true
|
|
|
|
|
druidPlugin.setTestOnReturn(false);
|
|
|
|
|
//数据监控
|
|
|
|
|
druidPlugin.addFilter(new StatFilter());
|
|
|
|
|
return druidPlugin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args_) throws Exception {
|
|
|
|
@ -283,49 +46,62 @@ public class TestDingTalk {
|
|
|
|
|
final String appKey = PropKit.get("appKey");
|
|
|
|
|
String appSecret = PropKit.get("appSecret");
|
|
|
|
|
|
|
|
|
|
DruidPlugin druid = createDruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim(), PropKit.get("driverClassName"));
|
|
|
|
|
druid.start();
|
|
|
|
|
|
|
|
|
|
ActiveRecordPlugin arp = new ActiveRecordPlugin(druid);
|
|
|
|
|
arp.setDevMode(false);
|
|
|
|
|
arp.setDialect(new MysqlDialect());
|
|
|
|
|
arp.start();
|
|
|
|
|
|
|
|
|
|
// 用于缓存模块的redis服务
|
|
|
|
|
RedisPlugin redis = new RedisPlugin("myRedis", PropKit.get("redis_ip"), PropKit.getInt("redis_port"), 10 * 1000);
|
|
|
|
|
redis.start();
|
|
|
|
|
|
|
|
|
|
String accessToken = getAccessToken(appKey, appSecret);
|
|
|
|
|
System.out.println("accessToken=" + accessToken);
|
|
|
|
|
|
|
|
|
|
String jsApiToken = getJsApiToken(accessToken);
|
|
|
|
|
System.out.println("ticket=" + jsApiToken);
|
|
|
|
|
|
|
|
|
|
String SsoToken = getSsoToken(corpId, ssoSecret);
|
|
|
|
|
System.out.println("ssoToken=" + SsoToken);
|
|
|
|
|
|
|
|
|
|
String res = getPerson(accessToken, "manager3180");
|
|
|
|
|
System.out.println(res);
|
|
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
|
//getPersonToken(appKey, appSecret);
|
|
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
|
//createPerson(accessToken);
|
|
|
|
|
|
|
|
|
|
//创建部门
|
|
|
|
|
long dept_id = createDept(accessToken, "黄海的测试部门A", 1, 1);
|
|
|
|
|
if (dept_id == -1) {
|
|
|
|
|
System.out.println("部门名称已存在,无法创建!");
|
|
|
|
|
}
|
|
|
|
|
// 钉钉开发踩坑:请求的部门 id 不在授权范围内 http://roc.havemail.cn/archives/595.html
|
|
|
|
|
getDeptList(accessToken);
|
|
|
|
|
|
|
|
|
|
//修改部门
|
|
|
|
|
updateDept(accessToken,dept_id,"黄海的测试部门B",2);
|
|
|
|
|
//查询看一下
|
|
|
|
|
getDeptList(accessToken);
|
|
|
|
|
|
|
|
|
|
//删除部门
|
|
|
|
|
delDept(accessToken, dept_id);
|
|
|
|
|
//查询看一下
|
|
|
|
|
getDeptList(accessToken);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//通过云平台查询到的学校、部门、教师、学生、班级等信息,需要写入到钉钉中来,并且获取到钉钉返回的唯一主键,回写到云平台的表中去
|
|
|
|
|
//通过云平台查询到的学校、部门、教师等信息,需要写入到钉钉中来,并且获取到钉钉返回的唯一主键,回写到云平台的表中去
|
|
|
|
|
//这样处理,才能保证下次再来同步数据时,找到相同的唯一主键
|
|
|
|
|
|
|
|
|
|
String accessToken = DingTalkUtil.getAccessToken(appKey, appSecret);
|
|
|
|
|
|
|
|
|
|
//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");
|
|
|
|
|
//单位需要清空一下这个属性
|
|
|
|
|
sql = "update t_base_organization set dingtalk_dept_id=null where org_id=?";
|
|
|
|
|
Db.update(sql, orgId);
|
|
|
|
|
|
|
|
|
|
// 学校及学校下的部门
|
|
|
|
|
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);
|
|
|
|
|
//回写
|
|
|
|
|
sql = "update t_base_organization set dingtalk_dept_id=? where org_id=?";
|
|
|
|
|
Db.update(sql, dt_dept_id, orgId);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("恭喜,所有操作成功完成!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|