use of com.jeecg.dingtalk.api.department.vo.Department in project kms by mahonelau.
the class ThirdAppDingtalkServiceImpl method syncThirdAppUserToLocal.
@Override
public SyncInfoVo syncThirdAppUserToLocal() {
SyncInfoVo syncInfo = new SyncInfoVo();
String accessToken = this.getAccessToken();
if (accessToken == null) {
syncInfo.addFailInfo("accessToken获取失败!");
return syncInfo;
}
// 获取本地用户
List<SysUser> sysUsersList = sysUserService.list();
// 查询钉钉所有的部门,用于同步用户和部门的关系
List<Department> allDepartment = JdtDepartmentAPI.listAll(accessToken);
// 根据钉钉部门查询所有钉钉用户,用于反向同步到本地
List<User> ddUserList = this.getDtAllUserByDepartment(allDepartment, accessToken);
for (User dtUserInfo : ddUserList) {
SysThirdAccount sysThirdAccount = sysThirdAccountService.getOneByThirdUserId(dtUserInfo.getUserid(), ThirdAppConfig.DINGTALK.toLowerCase());
List<SysUser> collect = sysUsersList.stream().filter(user -> (dtUserInfo.getMobile().equals(user.getPhone()) || dtUserInfo.getUserid().equals(user.getUsername()))).collect(Collectors.toList());
if (collect != null && collect.size() > 0) {
SysUser sysUserTemp = collect.get(0);
// 循环到此说明用户匹配成功,进行更新操作
SysUser updateSysUser = this.dtUserToSysUser(dtUserInfo, sysUserTemp);
try {
sysUserService.updateById(updateSysUser);
String str = String.format("用户 %s(%s) 更新成功!", updateSysUser.getRealname(), updateSysUser.getUsername());
syncInfo.addSuccessInfo(str);
} catch (Exception e) {
this.syncUserCollectErrInfo(e, dtUserInfo, syncInfo);
}
// 第三方账号关系表
this.thirdAccountSaveOrUpdate(sysThirdAccount, updateSysUser.getId(), dtUserInfo.getUserid());
} else {
// 如果没有匹配到用户,则走创建逻辑
SysUser newSysUser = this.dtUserToSysUser(dtUserInfo);
try {
sysUserService.save(newSysUser);
String str = String.format("用户 %s(%s) 创建成功!", newSysUser.getRealname(), newSysUser.getUsername());
syncInfo.addSuccessInfo(str);
} catch (Exception e) {
this.syncUserCollectErrInfo(e, dtUserInfo, syncInfo);
}
// 第三方账号关系表
this.thirdAccountSaveOrUpdate(null, newSysUser.getId(), dtUserInfo.getUserid());
}
}
return syncInfo;
}
use of com.jeecg.dingtalk.api.department.vo.Department in project kms by mahonelau.
the class ThirdAppDingtalkServiceImpl method syncDepartmentToLocalRecursion.
public void syncDepartmentToLocalRecursion(List<JdtDepartmentTreeVo> departmentTreeList, String sysParentId, String username, SyncInfoVo syncInfo, String accessToken) {
if (departmentTreeList != null && departmentTreeList.size() != 0) {
for (JdtDepartmentTreeVo departmentTree : departmentTreeList) {
LambdaQueryWrapper<SysDepart> queryWrapper = new LambdaQueryWrapper<>();
// 根据 source_identifier 字段查询
queryWrapper.eq(SysDepart::getId, departmentTree.getSource_identifier());
SysDepart sysDepart = sysDepartService.getOne(queryWrapper);
if (sysDepart != null) {
// 执行更新操作
SysDepart updateSysDepart = this.dtDepartmentToSysDepart(departmentTree, sysDepart);
if (sysParentId != null) {
updateSysDepart.setParentId(sysParentId);
}
try {
sysDepartService.updateDepartDataById(updateSysDepart, username);
String str = String.format("部门 %s 更新成功!", updateSysDepart.getDepartName());
syncInfo.addSuccessInfo(str);
} catch (Exception e) {
this.syncDepartCollectErrInfo(e, departmentTree, syncInfo);
}
if (departmentTree.hasChildren()) {
// 紧接着同步子级
this.syncDepartmentToLocalRecursion(departmentTree.getChildren(), updateSysDepart.getId(), username, syncInfo, accessToken);
}
} else {
// 执行新增操作
SysDepart newSysDepart = this.dtDepartmentToSysDepart(departmentTree, null);
if (sysParentId != null) {
newSysDepart.setParentId(sysParentId);
}
try {
sysDepartService.saveDepartData(newSysDepart, username);
// 更新钉钉 source_identifier
Department updateDtDepart = new Department();
updateDtDepart.setDept_id(departmentTree.getDept_id());
updateDtDepart.setSource_identifier(newSysDepart.getId());
Response response = JdtDepartmentAPI.update(updateDtDepart, accessToken);
if (!response.isSuccess()) {
throw new RuntimeException(response.getErrmsg());
}
String str = String.format("部门 %s 创建成功!", newSysDepart.getDepartName());
syncInfo.addSuccessInfo(str);
} catch (Exception e) {
this.syncDepartCollectErrInfo(e, departmentTree, syncInfo);
}
// 紧接着同步子级
if (departmentTree.hasChildren()) {
this.syncDepartmentToLocalRecursion(departmentTree.getChildren(), newSysDepart.getId(), username, syncInfo, accessToken);
}
}
}
}
}
use of com.jeecg.dingtalk.api.department.vo.Department in project kms 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 jeecg-boot by jeecgboot.
the class ThirdAppDingtalkServiceImpl method syncLocalDepartmentToThirdApp.
// update:2022-1-21,updateBy:sunjianlei; for 【JTC-704】【钉钉】部门同步成功,实际没成,后台提示ip白名单
@Override
public SyncInfoVo syncLocalDepartmentToThirdApp(String ids) {
SyncInfoVo syncInfo = new SyncInfoVo();
String accessToken = this.getAccessToken();
if (accessToken == null) {
syncInfo.addFailInfo("accessToken获取失败!");
return syncInfo;
}
// 获取【钉钉】所有的部门
List<Response<Department>> departments = JdtDepartmentAPI.listAllResponse(accessToken);
// 删除钉钉有但本地没有的部门(以本地部门数据为主)(钉钉不能创建同名部门,只能先删除)
List<SysDepart> sysDepartList = sysDepartService.list();
for1: for (Response<Department> departmentRes : departments) {
// 判断部门是否查询成功
if (!departmentRes.isSuccess()) {
syncInfo.addFailInfo(departmentRes.getErrmsg());
// 88 是 ip 不在白名单的错误码,如果遇到此错误码,后面的操作都可以不用进行了,因为肯定都是失败的
if (new Integer(88).equals(departmentRes.getErrcode())) {
return syncInfo;
}
continue;
}
Department department = departmentRes.getResult();
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.listAllResponse(accessToken);
this.syncDepartmentRecursion(sysDepartsTree, departments, parent, accessToken, syncInfo);
return syncInfo;
}
use of com.jeecg.dingtalk.api.department.vo.Department in project jeecg-boot by jeecgboot.
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