main
黄海 2 years ago
parent 00bb91a508
commit 79c17ce7a9

@ -1,116 +0,0 @@
package UnitTest;
import com.alibaba.druid.filter.stat.StatFilter;
import com.dsideal.FengHuang.DingTalk.Common;
import com.dsideal.FengHuang.DingTalk.Model;
import com.dsideal.FengHuang.DingTalk.OrgPerson;
import com.dsideal.FengHuang.DingTalk.RolePerson;
import com.dsideal.FengHuang.Util.CommonUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
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 java.util.List;
public class TestDingTalk {
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 {
PropKit.use("dingtalk.properties");
String corpId = PropKit.get("corpId");
long agentId = PropKit.getLong("agentId");
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();
//accessToken
String accessToken = Common.getAccessToken(appKey, appSecret);
//同步钉钉与云平台中部门信息
String orgName = "长春市东光学校";
Record rOrg = Model.getOrgByOrgName(orgName);
//同步组织机构
OrgPerson.syncOrg(accessToken, rOrg);
//从数据库中获取最新的部门列表(已与钉钉匹配完毕)
Model.fillDeptListByDataBase(rOrg);
//删除所有的组织机构
//OrgPerson.delAllDept(accessToken);
//同步人员
OrgPerson.syncPerson(accessToken, rOrg);
//删除单位下所有人员(开发测试时使用)
// OrgPerson.delBureauPerson(accessToken, rOrg);
//创建角色组【执行一次即可】
//RolePerson.createRoleGroup(accessToken, "义务教育阶段角色组"); ---> 3779920123
//long groupId = 3779920123L;
//获取指定角色组下有哪些角色
//List<Record> list = RolePerson.getRoleList(accessToken, groupId);
//System.out.println(list);
//删除指定角色组下的角色
//RolePerson.delGroupRole(accessToken,groupId);
//同步角色
//RolePerson.syncRole(accessToken, groupId);
//获取钉钉侧指定角色下人员信息
// RolePerson.getRolePersonList(accessToken, 3780868223L, 0);
//
// for (int i = 0; i < RolePerson.rolePersonList.size(); i++) {
// System.out.println(RolePerson.rolePersonList.get(i));
// }
// //获取云平台侧指定角色下人员信息
// List<Record> list2 = Model.getRolePersonReleation();
//尝试同步两个人员信息
CommonUtil.Print("恭喜,所有操作成功完成!");
}
}

@ -0,0 +1,26 @@
package UnitTest;
// 引入需要的类
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import com.aspose.words.*;
import java.io.File;
public class UpdateImage {
public static void main(String[] args) throws Exception {
String MyDir="C:\\Users\\Administrator\\Desktop\\";
Document doc = new Document(MyDir + "看图写话分类别.docx");
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
for (Shape shape : (Iterable<Shape>) shapes) {
if (shape.hasImage()) {
// Resize the shape to the desired dimensions
shape.setWidth(180);
shape.setHeight(120);
// Set the layout of the image to "in-line with text" (i.e. "inline")
shape.setWrapType(WrapType.INLINE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
}
}
doc.save("c:\\2.docx");
}
}

@ -0,0 +1,83 @@
package com.dsideal.FengHuang.DingTalk;
import com.alibaba.druid.filter.stat.StatFilter;
import com.dsideal.FengHuang.DingTalk.Util.Common;
import com.dsideal.FengHuang.DingTalk.Util.Model;
import com.dsideal.FengHuang.DingTalk.Util.OrgPerson;
import com.dsideal.FengHuang.Util.CommonUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
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;
public class Init {
public static void main(String[] args_) throws Exception {
PropKit.use("dingtalk.properties");
final String appKey = PropKit.get("appKey");
String appSecret = PropKit.get("appSecret");
DruidPlugin druid = Common.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();
//accessToken
String accessToken = Common.getAccessToken(appKey, appSecret);
//同步钉钉与云平台中部门信息
String orgName = "长春市东光学校";
Record rOrg = Model.getOrgByOrgName(orgName);
//开发测试时专用,批量删除人员与组织机构
// Model.fillDeptListByDataBase(rOrg);//从数据库中获取最新的部门列表(已与钉钉匹配完毕)
// OrgPerson.delBureauPerson(accessToken,rOrg);
// OrgPerson.delAllDept(accessToken);
//初始化组织机构【需提前手动删除此学校及下属部门】
//OrgPerson.InitOrg(accessToken, rOrg);
//同步人员
Model.fillDeptListByDataBase(rOrg);//从数据库中获取最新的部门列表(已与钉钉匹配完毕)
OrgPerson.initPerson(accessToken, rOrg);
//创建角色组【执行一次即可】
//RolePerson.createRoleGroup(accessToken, "义务教育阶段角色组"); ---> 3779920123
//long groupId = 3779920123L;
//获取指定角色组下有哪些角色
//List<Record> list = RolePerson.getRoleList(accessToken, groupId);
//System.out.println(list);
//删除指定角色组下的角色
//RolePerson.delGroupRole(accessToken,groupId);
//同步角色
//RolePerson.syncRole(accessToken, groupId);
//获取钉钉侧指定角色下人员信息
// RolePerson.getRolePersonList(accessToken, 3780868223L, 0);
//
// for (int i = 0; i < RolePerson.rolePersonList.size(); i++) {
// System.out.println(RolePerson.rolePersonList.get(i));
// }
// //获取云平台侧指定角色下人员信息
// List<Record> list2 = Model.getRolePersonReleation();
CommonUtil.Print("恭喜,所有操作成功完成!");
}
}

@ -1,458 +0,0 @@
package com.dsideal.FengHuang.DingTalk;
import cn.binarywang.tools.generator.ChineseMobileNumberGenerator;
import cn.hutool.core.util.PhoneUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
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.dsideal.FengHuang.Util.CommonUtil;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.taobao.api.ApiException;
import java.util.*;
public class OrgPerson {
/**
*
*
* @param access_token
* @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) {
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 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);
JSONObject jo = JSONObject.parseObject(rsp.getBody());
JSONArray ja = jo.getJSONArray("result");
if (ja == null) return;
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"));
deptList.add(kv);
getDeptList(access_token, childDeptId);//递归
}
}
public static void delAllDept(String access_token) throws ApiException {
for (int i = 0; i < deptList.size(); i++) {
delDept(access_token, deptList.get(i).getLong("dept_id"));
CommonUtil.Print("成功删除部门:"+deptList.get(i).getStr("name"));
}
}
public static JSONObject getDeptInfo(String access_token, long deptId) {
JSONObject jo;
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
req.setDeptId(deptId);
req.setLanguage("zh_CN");
OapiV2DepartmentGetResponse rsp = client.execute(req, access_token);
jo = JSONObject.parseObject(rsp.getBody());
} catch (Exception err) {
jo = JSONObject.parseObject(err.toString());
}
return jo;
}
public static void syncOrg(String accessToken, Record rOrg) throws ApiException {
String orgName = rOrg.getStr("org_name");
int bureau_id = rOrg.getInt("org_id");
int sort_id = rOrg.getInt("sort_id");
//获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下
long dingtalk_dept_id = Model.getDtDeptId(bureau_id);
JSONObject jRes = getDeptInfo(accessToken, dingtalk_dept_id);
if (jRes.getLong("errcode") > 0) {
dingtalk_dept_id = createDept(accessToken, orgName, 1, sort_id, true);
Model.writeDtDeptId(bureau_id, String.valueOf(dingtalk_dept_id));//记得要回写
CommonUtil.Print("单位名称:" + orgName + "不存在!已创建!");
} else {
CommonUtil.Print("单位名称:" + orgName + "已存在,保留!");
//获取下属部门的列表
getDeptList(accessToken, dingtalk_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 < deptList.size(); i++) {
long deptId = deptList.get(i).getLong("dept_id");
String org_name = 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 = Model.getOrgList(bureau_id);
for (int i = 0; i < list.size(); i++) {
if (list.get(i).get("dingtalk_dept_id") != null) {
long deptId = list.get(i).getLong("dingtalk_dept_id");
String org_name = list.get(i).getStr("org_name");
sort_id = list.get(i).getInt("sort_id");
int org_id = list.get(i).getInt("org_id");
int parentId = list.get(i).getInt("parent_id");
keysOfB.add(deptId);
JSONObject jo = new JSONObject();
jo.put("org_id", org_id);
jo.put("org_name", org_name);
jo.put("sort_id", sort_id);
jo.put("parent_id", parentId);
mapOfB.put(deptId, jo);
}
}
//在A不在B
for (Long key : keysOfA) {
if (!keysOfB.contains(key)) {
// key只存在于A中
// 删除
CommonUtil.Print("发现钉钉中多出的dept_id:" + key + ",将删除掉...");
delDept(accessToken, key);
} else {
// key在A和B中都存在
String aName = mapOfA.get(key).getString("org_name");
String bName = mapOfB.get(key).getString("org_name");
int sId = mapOfB.get(key).getInteger("sort_id");
if (aName.equals(bName)) {
CommonUtil.Print("发现钉钉与云平台间部门名称一致,不需要修改,aName=" + aName + ",bName=" + bName);
continue;
}
CommonUtil.Print("发现钉钉与云平台间部门名称不一致,将修改,aName=" + aName + ",bName=" + bName);
updateDept(accessToken, key, bName, sId);
}
}
//在B不在A
for (Long key : keysOfB) {
if (!keysOfA.contains(key)) {
// key只存在于B中
// 增加
String org_name = mapOfB.get(key).getString("org_name");
System.out.println(org_name);
int sId = mapOfB.get(key).getInteger("sort_id");
int parentId = mapOfB.get(key).getInteger("parent_id");
int org_id = mapOfB.get(key).getInteger("org_id");
//这个部门是在哪个部门下的
long dingTalkParentDeptId = Model.getDtDeptId(parentId);
long dt_dept_id = createDept(accessToken, org_name, dingTalkParentDeptId, sId, false);
Model.writeDtDeptId(org_id, String.valueOf(dt_dept_id));
CommonUtil.Print("成功创建部门:" + org_name);
}
}
//重新获取下属部门的列表,使得后续的代码不再获取部门列表
getDeptList(accessToken, dingtalk_dept_id);
}
/**
*
*
* @param access_token
* @param deptId
* @param userId
* @param personName
* @param tel
* @param zhiWei
* @throws ApiException
*/
public static void createPerson(String access_token, String deptId, String userId, String personName, String tel, String zhiWei) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/create");
OapiV2UserCreateRequest req = new OapiV2UserCreateRequest();
req.setUserid(userId);
req.setName(personName);
req.setMobile(tel);
req.setTitle(zhiWei);
req.setDeptIdList(deptId);
OapiV2UserCreateResponse rsp = client.execute(req, access_token);
//System.out.println(rsp.getBody());
}
/**
*
*
* @param access_token
* @param userId
* @throws ApiException
*/
public static void delPerson(String access_token, String userId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/delete");
OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest();
req.setUserid(userId);
OapiV2UserDeleteResponse rsp = client.execute(req, access_token);
//System.out.println(rsp.getBody());
}
public static void updatePerson(String access_token, String person_id, String person_name) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/update");
OapiV2UserUpdateRequest req = new OapiV2UserUpdateRequest();
req.setUserid(person_id);
req.setName(person_name);
OapiV2UserUpdateResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
}
public static List<Kv> personList = new ArrayList();
public static void getDeptPerson(String access_token, long dept_id) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listsimple");
long next_cursor = 0;
while (true) {
OapiUserListsimpleRequest req = new OapiUserListsimpleRequest();
req.setDeptId(dept_id);
req.setCursor(next_cursor);
req.setSize(10L);
req.setOrderField("modify_desc");
req.setContainAccessLimit(false);
req.setLanguage("zh_CN");
OapiUserListsimpleResponse rsp = client.execute(req, access_token);
JSONObject jo = JSONObject.parseObject(rsp.getBody());
JSONArray ja = jo.getJSONObject("result").getJSONArray("list");
if (ja == null) return;
for (int i = 0; i < ja.size(); i++) {
JSONObject j = ja.getJSONObject(i);
Kv kv = Kv.create();
String userid = j.getString("userid");
String name = j.getString("name");
kv.set("userid", userid);
kv.set("name", name);
personList.add(kv);
}
if (jo.getJSONObject("result").getBoolean("has_more"))
next_cursor = jo.getJSONObject("result").getLong("next_cursor");
else break;
}
}
public static void getBureauPerson(String access_token, long dept_id) throws ApiException {
personList.clear();
getDeptPerson(access_token, dept_id);
for (int i = 0; i < deptList.size(); i++) {
getDeptPerson(access_token, deptList.get(i).getInt("dept_id"));
}
}
public static void delBureauPerson(String accessToken, Record rOrg) throws ApiException {
int dingtalk_dept_id = rOrg.getInt("dingtalk_dept_id");
getBureauPerson(accessToken, dingtalk_dept_id);
for (int i = 0; i < personList.size(); i++) {
String userid = personList.get(i).getStr("userid");
String person_name = personList.get(i).getStr("name");
delPerson(accessToken, userid);
CommonUtil.Print("成功删除:" + person_name);
}
}
/**
*
*
* @param accessToken
* @param rOrg
* @throws ApiException
*/
public static void syncPerson(String accessToken, Record rOrg) throws ApiException {
int bureauId = rOrg.getInt("org_id");
//获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下
long DT_BureauId = Model.getDtDeptId(bureauId);
getBureauPerson(accessToken, DT_BureauId);
Set<String> keysOfA = new HashSet<>();
Map<String, JSONObject> mapOfA = new HashMap<>();
Set<String> keysOfB = new HashSet<>();
Map<String, JSONObject> mapOfB = new HashMap<>();
//A:钉钉
for (int i = 0; i < personList.size(); i++) {
String userid = personList.get(i).getStr("userid");
String name = personList.get(i).getStr("name");
keysOfA.add(userid);
JSONObject jo = new JSONObject();
jo.put("person_id", userid);
jo.put("person_name", name);
mapOfA.put(userid, jo);
}
//B:云平台
List<Record> list = Model.getDeptPerson(bureauId);
for (int i = 0; i < list.size(); i++) {
Record r = list.get(i);
int person_id = r.getInt("person_id");
String person_name = r.getStr("person_name");
long dingtalk_dept_id = r.getLong("dingtalk_dept_id");
String tel = r.getStr("tel");
if (StrKit.isBlank(tel) || !PhoneUtil.isMobile(tel)) {
tel = ChineseMobileNumberGenerator.getInstance().generate();//生成一个随机临时测试用的手机号
tel = "14" + tel.substring(2);//以14段开头避开已存在的号码
}
keysOfB.add(String.valueOf(person_id));
JSONObject jo = new JSONObject();
jo.put("person_id", person_id);
jo.put("person_name", person_name);
jo.put("dingtalk_dept_id", dingtalk_dept_id);
jo.put("tel", tel);
mapOfB.put(String.valueOf(person_id), jo);
}
//在A不在B
for (String key : keysOfA) {
if (!keysOfB.contains(key)) {
// key只存在于A中
// 删除
CommonUtil.Print("发现钉钉中多出的user_id:" + key + ",将删除掉...");
delPerson(accessToken, key);
} else {
// key在A和B中都存在
String aName = mapOfA.get(key).getString("person_name");
String bName = mapOfB.get(key).getString("person_name");
if (!aName.equals(bName)) {
updatePerson(accessToken, key, mapOfB.get(key).getString("person_name"));
CommonUtil.Print("发现钉钉与云平台间部门人员姓名不一致,将修改,person_name=" + mapOfB.get(key).getString("person_name"));
} else {
CommonUtil.Print("钉钉与云平台间部门人员姓名一致,无需修改,person_name=" + mapOfB.get(key).getString("person_name"));
}
}
}
//在B不在A
for (String key : keysOfB) {
if (!keysOfA.contains(key)) {
// key只存在于B中
// 增加
String person_id = mapOfB.get(key).getString("person_id");
String person_name = mapOfB.get(key).getString("person_name");
String tel = mapOfB.get(key).getString("tel");
long deptId = mapOfB.get(key).getLong("dingtalk_dept_id");
createPerson(accessToken, String.valueOf(deptId), person_id, person_name, tel, "教师");
CommonUtil.Print("成功加入人员:" + person_name);
}
}
}
/**
*
*
* @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;
}
//TODO 未完成
public static String getPersonToken(String appKey, String appSecret) throws Exception {
com.aliyun.dingtalkoauth2_1_0.Client client = Common.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;
}
}

@ -1,13 +1,39 @@
package com.dsideal.FengHuang.DingTalk;
package com.dsideal.FengHuang.DingTalk.Util;
import com.alibaba.druid.filter.stat.StatFilter;
import com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetSsoAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetSsoAccessTokenResponse;
import com.aliyun.tea.TeaException;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.Redis;
public class Common {
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;
}
/**
* 使 Token Client
*
@ -112,4 +138,8 @@ public class Common {
return Redis.use().get(KEY);
}
}
public static void WriteActionTimes(){
}
}

@ -0,0 +1,33 @@
package com.dsideal.FengHuang.DingTalk.Util;
import com.dsideal.FengHuang.Util.CommonUtil;
import com.jfinal.kit.PropKit;
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;
public class FillRemainCount {
public static void main(String[] args) {
PropKit.use("dingtalk.properties");
DruidPlugin druid = Common.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();
for(int year=2023;year<=2053;year++){
for(int month=1;month<=12;month++){
Record record =new Record();
record.set("year",year);
record.set("month",month);
record.set("remain_count",500000);//初始操作次数为50W
Db.save("t_dingtalk_actioncount","year,month",record);
}
}
CommonUtil.Print("恭喜,数据填充成功!");
}
}

@ -1,4 +1,4 @@
package com.dsideal.FengHuang.DingTalk;
package com.dsideal.FengHuang.DingTalk.Util;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
@ -9,7 +9,7 @@ import java.util.List;
public class Model {
public static List<Record> getOrgList(int orgId) {
// 学校及学校下的部门
String sql = "select org_id,org_name,parent_id,sort_id,dingtalk_dept_id from t_base_organization where bureau_id=? and org_id<>bureau_id order by org_id";
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);
return list;
}

@ -0,0 +1,283 @@
package com.dsideal.FengHuang.DingTalk.Util;
import cn.binarywang.tools.generator.ChineseMobileNumberGenerator;
import cn.hutool.core.util.PhoneUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.dsideal.FengHuang.Util.CommonUtil;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.taobao.api.ApiException;
import java.util.*;
public class OrgPerson {
/**
*
*
* @param access_token
* @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) {
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 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);
JSONObject jo = JSONObject.parseObject(rsp.getBody());
JSONArray ja = jo.getJSONArray("result");
if (ja == null) return;
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"));
deptList.add(kv);
getDeptList(access_token, childDeptId);//递归
}
}
public static JSONObject getDeptInfo(String access_token, long deptId) {
JSONObject jo;
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
req.setDeptId(deptId);
req.setLanguage("zh_CN");
OapiV2DepartmentGetResponse rsp = client.execute(req, access_token);
jo = JSONObject.parseObject(rsp.getBody());
} catch (Exception err) {
jo = JSONObject.parseObject(err.toString());
}
return jo;
}
public static void InitOrg(String accessToken, Record rOrg) throws ApiException {
int bureau_id = rOrg.getInt("org_id");
//云平台
List<Record> list = Model.getOrgList(bureau_id);
//在B不在A
for (Record record : list) {
// 增加
String org_name = record.getStr("org_name");
int sId = record.getInt("sort_id");
int parentId = record.getInt("parent_id");
int org_id = record.getInt("org_id");
//这个部门是在哪个部门下的
long dingTalkParentDeptId = 1;
if (parentId > 0) dingTalkParentDeptId = Model.getDtDeptId(parentId);
long dt_dept_id = createDept(accessToken, org_name, dingTalkParentDeptId, sId, false);
Model.writeDtDeptId(org_id, String.valueOf(dt_dept_id));
CommonUtil.Print("成功创建部门:" + org_name);
}
}
/**
*
*
* @param access_token
* @param deptId
* @param userId
* @param personName
* @param tel
* @param zhiWei
* @throws ApiException
*/
public static void createPerson(String access_token, String deptId, String userId, String personName, String tel, String zhiWei) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/create");
OapiV2UserCreateRequest req = new OapiV2UserCreateRequest();
req.setUserid(userId);
req.setName(personName);
req.setMobile(tel);
req.setTitle(zhiWei);
req.setDeptIdList(deptId);
OapiV2UserCreateResponse rsp = client.execute(req, access_token);
//System.out.println(rsp.getBody());
}
/**
*
*
* @param access_token
* @param userId
* @throws ApiException
*/
public static void delPerson(String access_token, String userId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/delete");
OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest();
req.setUserid(userId);
OapiV2UserDeleteResponse rsp = client.execute(req, access_token);
//System.out.println(rsp.getBody());
}
public static void updatePerson(String access_token, String person_id, String person_name) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/update");
OapiV2UserUpdateRequest req = new OapiV2UserUpdateRequest();
req.setUserid(person_id);
req.setName(person_name);
OapiV2UserUpdateResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
}
public static List<Kv> personList = new ArrayList();
public static void getDeptPerson(String access_token, long dept_id) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listsimple");
long next_cursor = 0;
while (true) {
OapiUserListsimpleRequest req = new OapiUserListsimpleRequest();
req.setDeptId(dept_id);
req.setCursor(next_cursor);
req.setSize(10L);
req.setOrderField("modify_desc");
req.setContainAccessLimit(false);
req.setLanguage("zh_CN");
OapiUserListsimpleResponse rsp = client.execute(req, access_token);
JSONObject jo = JSONObject.parseObject(rsp.getBody());
JSONArray ja = jo.getJSONObject("result").getJSONArray("list");
if (ja == null) return;
for (int i = 0; i < ja.size(); i++) {
JSONObject j = ja.getJSONObject(i);
Kv kv = Kv.create();
String userid = j.getString("userid");
String name = j.getString("name");
kv.set("userid", userid);
kv.set("name", name);
personList.add(kv);
}
if (jo.getJSONObject("result").getBoolean("has_more"))
next_cursor = jo.getJSONObject("result").getLong("next_cursor");
else break;
}
}
public static void getBureauPerson(String access_token, long dept_id) throws ApiException {
personList.clear();
getDeptPerson(access_token, dept_id);
for (int i = 0; i < deptList.size(); i++) {
getDeptPerson(access_token, deptList.get(i).getInt("dept_id"));
}
}
/**
*
*
* @param accessToken
* @param rOrg
* @throws ApiException
*/
public static void initPerson(String accessToken, Record rOrg) throws ApiException {
int bureauId = rOrg.getInt("org_id");
//云平台
List<Record> list = Model.getDeptPerson(bureauId);
int cnt = 0;
for (Record record : list) {
//控制一下人员数量,防止钉钉的版本问题造成人员添加失败
cnt++;
if (cnt > 10) break;
// 增加
String person_id = record.getStr("person_id");
String person_name = record.getStr("person_name");
String tel = record.getStr("tel");
//模拟处理一下手机号
if (StrKit.isBlank(tel) || !PhoneUtil.isMobile(tel)) {
tel = ChineseMobileNumberGenerator.getInstance().generate();//生成一个随机临时测试用的手机号
tel = "14" + tel.substring(2);//以14段开头避开已存在的号码
}
long deptId = record.getLong("dingtalk_dept_id");
createPerson(accessToken, String.valueOf(deptId), person_id, person_name, tel, "教师");
CommonUtil.Print("成功加入人员:" + person_name);
}
}
public static void delAllDept(String access_token) throws ApiException {
for (int i = 0; i < deptList.size(); i++) {
delDept(access_token, deptList.get(i).getLong("dept_id"));
CommonUtil.Print("成功删除部门:" + deptList.get(i).getStr("name"));
}
}
public static void delBureauPerson(String accessToken, Record rOrg) throws ApiException {
int dingtalk_dept_id = rOrg.getInt("dingtalk_dept_id");
getBureauPerson(accessToken, dingtalk_dept_id);
for (int i = 0; i < personList.size(); i++) {
String userid = personList.get(i).getStr("userid");
String person_name = personList.get(i).getStr("name");
delPerson(accessToken, userid);
CommonUtil.Print("成功删除:" + person_name);
}
}
}

@ -1,4 +1,4 @@
package com.dsideal.FengHuang.DingTalk;
package com.dsideal.FengHuang.DingTalk.Util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
Loading…
Cancel
Save