|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package UnitTest;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.druid.filter.stat.StatFilter;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.dsideal.FengHuang.Util.DingTalkUtil;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
|
|
|
@ -11,7 +12,7 @@ import com.jfinal.plugin.druid.DruidPlugin;
|
|
|
|
|
import com.jfinal.plugin.redis.RedisPlugin;
|
|
|
|
|
import com.taobao.api.ApiException;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class TestDingTalk {
|
|
|
|
|
public static DruidPlugin createDruidPlugin(String url, String username, String password, String driverClass) {
|
|
|
|
@ -63,17 +64,71 @@ public class TestDingTalk {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 云平台->集合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 < 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"));
|
|
|
|
|
long deptId = DingTalkUtil.deptList.get(i).getLong("dept_id");
|
|
|
|
|
String org_name = DingTalkUtil.deptList.get(i).getStr("name");
|
|
|
|
|
keysOfA.add(deptId);
|
|
|
|
|
JSONObject jo = new JSONObject();
|
|
|
|
|
jo.put("org_name", org_name);
|
|
|
|
|
mapOfA.put(deptId, jo);
|
|
|
|
|
}
|
|
|
|
|
//B:云平台
|
|
|
|
|
List<Record> list = DingTalkUtil.getOrgList(orgId);
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
long deptId = list.get(i).getLong("org_id");
|
|
|
|
|
String org_name = list.get(i).getStr("org_name");
|
|
|
|
|
int sortId = list.get(i).getInt("sort_id");
|
|
|
|
|
int parentId = list.get(i).getInt("parent_id");
|
|
|
|
|
keysOfB.add(deptId);
|
|
|
|
|
|
|
|
|
|
JSONObject jo = new JSONObject();
|
|
|
|
|
jo.put("org_name", org_name);
|
|
|
|
|
jo.put("sort_id", sortId);
|
|
|
|
|
jo.put("parent_id", parentId);
|
|
|
|
|
mapOfB.put(deptId, jo);
|
|
|
|
|
}
|
|
|
|
|
//在A不在B
|
|
|
|
|
for (Long key : keysOfA) {
|
|
|
|
|
if (!keysOfB.contains(key)) {
|
|
|
|
|
// key只存在于A中
|
|
|
|
|
// 删除
|
|
|
|
|
DingTalkUtil.delDept(accessToken, key);
|
|
|
|
|
} else {
|
|
|
|
|
// key在A和B中都存在
|
|
|
|
|
DingTalkUtil.updateDept(accessToken,key,mapOfB.get(key).getString("org_name"),mapOfB.get(key).getInteger("sort_id"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//在B不在A
|
|
|
|
|
for (Long key : keysOfB) {
|
|
|
|
|
if (!keysOfA.contains(key)) {
|
|
|
|
|
// key只存在于B中
|
|
|
|
|
// 增加
|
|
|
|
|
String org_name = mapOfB.get(key).getString("org_name");
|
|
|
|
|
int sortId = mapOfB.get(key).getInteger("sort_id");
|
|
|
|
|
int parentId = mapOfB.get(key).getInteger("parent_id");
|
|
|
|
|
if (parentId == -1) parentId = orgId;
|
|
|
|
|
long dingtalk_dept_id = DingTalkUtil.getDtDeptId(parentId);
|
|
|
|
|
DingTalkUtil.createDept(accessToken, org_name, dingtalk_dept_id, sortId);
|
|
|
|
|
System.out.println("成功创建部门:" + org_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.out.println("恭喜,所有操作成功完成!");
|
|
|
|
|
}
|
|
|
|
|