main
黄海 2 years ago
parent 588f2118b7
commit 4fb12298ae

@ -252,44 +252,6 @@ public class DingTalkUtil {
}
}
/**
*
*
* @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 JSONObject getDeptInfo(String access_token, long deptId) {
JSONObject jo;
try {
@ -403,8 +365,56 @@ public class DingTalkUtil {
}
}
public static List<Kv> personList = new ArrayList();
/**
*
*
* @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, String tel) 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);
req.setMobile(tel);
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");
@ -456,22 +466,79 @@ public class DingTalkUtil {
}
}
/**
*
*
* @param accessToken
* @param orgName
* @throws ApiException
*/
public static void syncPerson(String accessToken, String orgName) throws ApiException {
Record rOrg = getOrgByOrgName(orgName);
int bureauId = rOrg.getInt("org_id");
List<Record> list = DingTalkUtil.getDeptPerson(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 = getDeptPerson(bureauId);
for (int i = 0; i < list.size(); i++) {
Record r = list.get(i);
long deptId = r.getLong("dingtalk_dept_id");
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();//生成一个随机临时测试用的手机号
}
//DingTalkUtil.createPerson(accessToken, String.valueOf(deptId), String.valueOf(person_id), person_name, tel, "教师");
//CommonUtil.Print("成功加入人员:" + person_name);
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中都存在
updatePerson(accessToken, key, mapOfB.get(key).getString("person_name"), mapOfB.get(key).getString("tel"));
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");
DingTalkUtil.createPerson(accessToken, String.valueOf(deptId), person_id, person_name, tel, "教师");
CommonUtil.Print("成功加入人员:" + person_name);
}
}
}

Loading…
Cancel
Save