use of com.jeecg.dingtalk.api.department.vo.Department in project jeecg-boot by jeecgboot.
the class ThirdAppDingtalkServiceImpl method sysUserToDtUser.
/**
* 【同步用户】将SysUser转为【钉钉】的User对象(更新旧用户)
*/
private User sysUserToDtUser(SysUser sysUser, User user, List<Department> allDepartment) {
user.setName(sysUser.getRealname());
user.setMobile(sysUser.getPhone());
user.setTelephone(sysUser.getTelephone());
user.setJob_number(sysUser.getWorkNo());
// 职务翻译
if (oConvertUtils.isNotEmpty(sysUser.getPost())) {
SysPosition position = sysPositionService.getByCode(sysUser.getPost());
if (position != null) {
user.setTitle(position.getName());
}
}
user.setEmail(sysUser.getEmail());
// 查询并同步用户部门关系
List<SysDepart> departList = this.getUserDepart(sysUser);
if (departList != null) {
List<Integer> departmentIdList = new ArrayList<>();
for (SysDepart sysDepart : departList) {
// 企业微信的部门id
Department department = this.getDepartmentByDepartId(sysDepart.getId(), allDepartment);
if (department != null) {
departmentIdList.add(department.getDept_id());
}
}
user.setDept_id_list(departmentIdList.toArray(new Integer[] {}));
user.setDept_order_list(null);
}
if (oConvertUtils.isEmpty(user.getDept_id_list())) {
// 没有找到匹配部门,同步到根部门下
user.setDept_id_list(1);
user.setDept_order_list(null);
}
// sysUser.getStatus()
return user;
}
use of com.jeecg.dingtalk.api.department.vo.Department in project jeecg-boot by jeecgboot.
the class ThirdAppDingtalkServiceImpl method syncLocalUserToThirdApp.
@Override
public SyncInfoVo syncLocalUserToThirdApp(String ids) {
SyncInfoVo syncInfo = new SyncInfoVo();
String accessToken = this.getAccessToken();
if (accessToken == null) {
syncInfo.addFailInfo("accessToken获取失败!");
return syncInfo;
}
List<SysUser> sysUsers;
if (StringUtils.isNotBlank(ids)) {
String[] idList = ids.split(",");
LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysUser::getId, (Object[]) idList);
// 获取本地指定用户
sysUsers = sysUserService.list(queryWrapper);
} else {
// 获取本地所有用户
sysUsers = sysUserService.list();
}
// 查询钉钉所有的部门,用于同步用户和部门的关系
List<Department> allDepartment = JdtDepartmentAPI.listAll(accessToken);
for (SysUser sysUser : sysUsers) {
// 外部模拟登陆临时账号,不同步
if ("_reserve_user_external".equals(sysUser.getUsername())) {
continue;
}
// 钉钉用户信息,不为null代表已同步过
Response<User> dtUserInfo;
/*
* 判断是否同步过的逻辑:
* 1. 查询 sys_third_account(第三方账号表)是否有数据,如果有代表已同步
* 2. 本地表里没有,就先用手机号判断,不通过再用username(用户账号)判断。
*/
SysThirdAccount sysThirdAccount = sysThirdAccountService.getOneBySysUserId(sysUser.getId(), THIRD_TYPE);
if (sysThirdAccount != null && oConvertUtils.isNotEmpty(sysThirdAccount.getThirdUserId())) {
// sys_third_account 表匹配成功,通过第三方userId查询出第三方userInfo
dtUserInfo = JdtUserAPI.getUserById(sysThirdAccount.getThirdUserId(), accessToken);
} else {
// 手机号匹配
Response<String> thirdUserId = JdtUserAPI.getUseridByMobile(sysUser.getPhone(), accessToken);
// 手机号匹配成功
if (thirdUserId.isSuccess() && oConvertUtils.isNotEmpty(thirdUserId.getResult())) {
// 通过查询到的userId查询用户详情
dtUserInfo = JdtUserAPI.getUserById(thirdUserId.getResult(), accessToken);
} else {
// 手机号匹配失败,尝试使用username匹配
dtUserInfo = JdtUserAPI.getUserById(sysUser.getUsername(), accessToken);
}
}
String dtUserId;
// api 接口是否执行成功
boolean apiSuccess;
// 已同步就更新,否则就创建
if (dtUserInfo != null && dtUserInfo.isSuccess() && dtUserInfo.getResult() != null) {
User dtUser = dtUserInfo.getResult();
dtUserId = dtUser.getUserid();
User updateQwUser = this.sysUserToDtUser(sysUser, dtUser, allDepartment);
Response<JSONObject> updateRes = JdtUserAPI.update(updateQwUser, accessToken);
// 收集成功/失败信息
apiSuccess = this.syncUserCollectErrInfo(updateRes, sysUser, syncInfo);
} else {
User newQwUser = this.sysUserToDtUser(sysUser, allDepartment);
Response<String> createRes = JdtUserAPI.create(newQwUser, accessToken);
dtUserId = createRes.getResult();
// 收集成功/失败信息
apiSuccess = this.syncUserCollectErrInfo(createRes, sysUser, syncInfo);
}
// api 接口执行成功,并且 sys_third_account 表匹配失败,就向 sys_third_account 里插入一条数据
if (apiSuccess && (sysThirdAccount == null || oConvertUtils.isEmpty(sysThirdAccount.getThirdUserId()))) {
if (sysThirdAccount == null) {
sysThirdAccount = new SysThirdAccount();
sysThirdAccount.setSysUserId(sysUser.getId());
sysThirdAccount.setStatus(1);
sysThirdAccount.setDelFlag(0);
sysThirdAccount.setThirdType(THIRD_TYPE);
}
// 设置第三方app用户ID
sysThirdAccount.setThirdUserId(dtUserId);
sysThirdAccountService.saveOrUpdate(sysThirdAccount);
}
}
return syncInfo;
}
use of com.jeecg.dingtalk.api.department.vo.Department in project kykms by mahonelau.
the class ThirdAppDingtalkServiceImpl method syncLocalUserToThirdApp.
@Override
public SyncInfoVo syncLocalUserToThirdApp(String ids) {
SyncInfoVo syncInfo = new SyncInfoVo();
String accessToken = this.getAccessToken();
if (accessToken == null) {
syncInfo.addFailInfo("accessToken获取失败!");
return syncInfo;
}
List<SysUser> sysUsers;
if (StringUtils.isNotBlank(ids)) {
String[] idList = ids.split(",");
LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysUser::getId, (Object[]) idList);
// 获取本地指定用户
sysUsers = sysUserService.list(queryWrapper);
} else {
// 获取本地所有用户
sysUsers = sysUserService.list();
}
// 查询钉钉所有的部门,用于同步用户和部门的关系
List<Department> allDepartment = JdtDepartmentAPI.listAll(accessToken);
for (SysUser sysUser : sysUsers) {
// 外部模拟登陆临时账号,不同步
if ("_reserve_user_external".equals(sysUser.getUsername())) {
continue;
}
// 钉钉用户信息,不为null代表已同步过
Response<User> dtUserInfo;
/*
* 判断是否同步过的逻辑:
* 1. 查询 sys_third_account(第三方账号表)是否有数据,如果有代表已同步
* 2. 本地表里没有,就先用手机号判断,不通过再用username判断。
*/
SysThirdAccount sysThirdAccount = sysThirdAccountService.getOneBySysUserId(sysUser.getId(), ThirdAppConfig.DINGTALK.toLowerCase());
if (sysThirdAccount != null && oConvertUtils.isNotEmpty(sysThirdAccount.getThirdUserId())) {
// sys_third_account 表匹配成功,通过第三方userId查询出第三方userInfo
dtUserInfo = JdtUserAPI.getUserById(sysThirdAccount.getThirdUserId(), accessToken);
} else {
// 手机号匹配
Response<String> thirdUserId = JdtUserAPI.getUseridByMobile(sysUser.getPhone(), accessToken);
// 手机号匹配成功
if (thirdUserId.isSuccess() && oConvertUtils.isNotEmpty(thirdUserId.getResult())) {
// 通过查询到的userId查询用户详情
dtUserInfo = JdtUserAPI.getUserById(thirdUserId.getResult(), accessToken);
} else {
// 手机号匹配失败,尝试使用username匹配
dtUserInfo = JdtUserAPI.getUserById(sysUser.getUsername(), accessToken);
}
}
String dtUserId;
// api 接口是否执行成功
boolean apiSuccess;
// 已同步就更新,否则就创建
if (dtUserInfo != null && dtUserInfo.isSuccess() && dtUserInfo.getResult() != null) {
User dtUser = dtUserInfo.getResult();
dtUserId = dtUser.getUserid();
User updateQwUser = this.sysUserToDtUser(sysUser, dtUser, allDepartment);
Response<JSONObject> updateRes = JdtUserAPI.update(updateQwUser, accessToken);
// 收集成功/失败信息
apiSuccess = this.syncUserCollectErrInfo(updateRes, sysUser, syncInfo);
} else {
User newQwUser = this.sysUserToDtUser(sysUser, allDepartment);
Response<String> createRes = JdtUserAPI.create(newQwUser, accessToken);
dtUserId = createRes.getResult();
// 收集成功/失败信息
apiSuccess = this.syncUserCollectErrInfo(createRes, sysUser, syncInfo);
}
// api 接口执行成功,并且 sys_third_account 表匹配失败,就向 sys_third_account 里插入一条数据
if (apiSuccess && (sysThirdAccount == null || oConvertUtils.isEmpty(sysThirdAccount.getThirdUserId()))) {
if (sysThirdAccount == null) {
sysThirdAccount = new SysThirdAccount();
sysThirdAccount.setSysUserId(sysUser.getId());
sysThirdAccount.setStatus(1);
sysThirdAccount.setDelFlag(0);
sysThirdAccount.setThirdType(ThirdAppConfig.DINGTALK.toLowerCase());
}
// 设置第三方app用户ID
sysThirdAccount.setThirdUserId(dtUserId);
sysThirdAccountService.saveOrUpdate(sysThirdAccount);
}
}
return syncInfo;
}
use of com.jeecg.dingtalk.api.department.vo.Department in project kykms by mahonelau.
the class ThirdAppDingtalkServiceImpl method syncLocalDepartmentToThirdApp.
@Override
public boolean syncLocalDepartmentToThirdApp(String ids) {
String accessToken = this.getAccessToken();
if (accessToken == null) {
return false;
}
// 获取【钉钉】所有的部门
List<Department> departments = JdtDepartmentAPI.listAll(accessToken);
// 删除钉钉有但本地没有的部门(以本地部门数据为主)(钉钉不能创建同名部门,只能先删除)
List<SysDepart> sysDepartList = sysDepartService.list();
for1: for (Department department : departments) {
for (SysDepart depart : sysDepartList) {
// id相同,代表已存在,不删除
String sourceIdentifier = department.getSource_identifier();
if (sourceIdentifier != null && sourceIdentifier.equals(depart.getId())) {
continue for1;
}
}
// 循环到此说明本地没有,删除
int deptId = department.getDept_id();
// 钉钉不允许删除带有用户的部门,所以需要判断下,将有用户的部门的用户移动至根部门
Response<List<String>> userIdRes = JdtUserAPI.getUserListIdByDeptId(deptId, accessToken);
if (userIdRes.isSuccess() && userIdRes.getResult().size() > 0) {
for (String userId : userIdRes.getResult()) {
User updateUser = new User();
updateUser.setUserid(userId);
updateUser.setDept_id_list(1);
JdtUserAPI.update(updateUser, accessToken);
}
}
JdtDepartmentAPI.delete(deptId, accessToken);
}
// 获取本地所有部门树结构
List<SysDepartTreeModel> sysDepartsTree = sysDepartService.queryTreeList();
// -- 钉钉不能创建新的顶级部门,所以新的顶级部门的parentId就为1
Department parent = new Department();
parent.setDept_id(1);
// 递归同步部门
departments = JdtDepartmentAPI.listAll(accessToken);
this.syncDepartmentRecursion(sysDepartsTree, departments, parent, accessToken);
return true;
}
use of com.jeecg.dingtalk.api.department.vo.Department in project kykms by mahonelau.
the class ThirdAppDingtalkServiceImpl method syncThirdAppDepartmentToLocal.
@Override
public SyncInfoVo syncThirdAppDepartmentToLocal(String ids) {
SyncInfoVo syncInfo = new SyncInfoVo();
String accessToken = this.getAccessToken();
if (accessToken == null) {
syncInfo.addFailInfo("accessToken获取失败!");
return syncInfo;
}
// 获取【钉钉】所有的部门
List<Department> departments = JdtDepartmentAPI.listAll(accessToken);
String username = JwtUtil.getUserNameByToken(SpringContextUtils.getHttpServletRequest());
List<JdtDepartmentTreeVo> departmentTreeList = JdtDepartmentTreeVo.listToTree(departments);
// 递归同步部门
this.syncDepartmentToLocalRecursion(departmentTreeList, null, username, syncInfo, accessToken);
return syncInfo;
}
Aggregations