use of com.jeecg.dingtalk.api.department.vo.Department in project kykms 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;
}
Aggregations