refactor(QywxScheduleJob): 优化用户信息同步逻辑

移除冗余的用户列表查询和删除用户逻辑,简化代码结构并提高执行效率
This commit is contained in:
dzq 2025-04-25 15:21:47 +08:00
parent 1945cb1787
commit ec7c3e5aa5
1 changed files with 14 additions and 18 deletions

View File

@ -411,13 +411,6 @@ public class QywxScheduleJob {
.filter(d -> Objects.equals(d.getCorpid(), authCorpInfo.getCorpid())) .filter(d -> Objects.equals(d.getCorpid(), authCorpInfo.getCorpid()))
.collect(Collectors.toList()); .collect(Collectors.toList());
List<QyUserEntity> qyUserList = qyUserApplicationService.selectAll();
if (null == qyUserList) {
qyUserList = new ArrayList<>();
}
qyUserList = qyUserList.stream()
.filter(u -> u.getCorpid().equals(authCorpInfo.getCorpid()))
.collect(Collectors.toList());
for (QyDepartmentEntity department : departmentList) { for (QyDepartmentEntity department : departmentList) {
// 获取部门用户列表 // 获取部门用户列表
@ -440,26 +433,29 @@ public class QywxScheduleJob {
Map<String, UserListResponse.UserInfo> wxUserMap = wxUsers.stream() Map<String, UserListResponse.UserInfo> wxUserMap = wxUsers.stream()
.collect(Collectors.toMap(UserListResponse.UserInfo::getUserid, Function.identity())); .collect(Collectors.toMap(UserListResponse.UserInfo::getUserid, Function.identity()));
List<QyUserEntity> localUsers = qyUserList.stream() List<QyUserEntity> qyUserList = qyUserApplicationService.selectAll();
.filter(u -> u.getAppid().equals(appid)) if (null == qyUserList) {
.collect(Collectors.toList()); qyUserList = new ArrayList<>();
Map<String, QyUserEntity> localUserMap = localUsers.stream() }
.collect(Collectors.toMap(QyUserEntity::getUserid, Function.identity())); qyUserList = qyUserList.stream()
.filter(u -> u.getCorpid().equals(authCorpInfo.getCorpid()) && u.getAppid().equals(appid))
.collect(Collectors.toList());
// 识别需要新增的用户 // 识别需要新增的用户
List<QyUserEntity> finalQyUserList = qyUserList;
List<UserListResponse.UserInfo> toAdd = wxUsers.stream() List<UserListResponse.UserInfo> toAdd = wxUsers.stream()
.filter(wxUser -> !localUserMap.containsKey(wxUser.getUserid())) .filter(wxUser -> finalQyUserList.stream().noneMatch(u -> u.getUserid().equals(wxUser.getUserid())))
.collect(Collectors.toList()); .collect(Collectors.toList());
log.info("syncUserInfo toAdd: {}", JSONUtil.toJsonStr(toAdd)); log.info("syncUserInfo toAdd: {}", JSONUtil.toJsonStr(toAdd));
// 识别需要删除的用户 // 识别需要删除的用户
List<QyUserEntity> toRemove = localUsers.stream() /* List<QyUserEntity> toRemove = qyUserList.stream()
.filter(localUser -> !wxUserMap.containsKey(localUser.getUserid())) .filter(localUser -> !wxUserMap.containsKey(localUser.getUserid()))
.collect(Collectors.toList()); .collect(Collectors.toList());
log.info("syncUserInfo toRemove: {}", JSONUtil.toJsonStr(toRemove)); log.info("syncUserInfo toRemove: {}", JSONUtil.toJsonStr(toRemove));*/
// 识别需要更新的用户 // 识别需要更新的用户
List<UpdateQyUserCommand> toUpdate = localUsers.stream() List<UpdateQyUserCommand> toUpdate = qyUserList.stream()
.filter(localUser -> wxUserMap.containsKey(localUser.getUserid())) .filter(localUser -> wxUserMap.containsKey(localUser.getUserid()))
.filter(localUser -> { .filter(localUser -> {
UserListResponse.UserInfo wxUser = wxUserMap.get(localUser.getUserid()); UserListResponse.UserInfo wxUser = wxUserMap.get(localUser.getUserid());
@ -557,12 +553,12 @@ public class QywxScheduleJob {
} }
// 删除用户 // 删除用户
if (!toRemove.isEmpty()) { /*if (!toRemove.isEmpty()) {
BulkOperationCommand<Integer> command = new BulkOperationCommand<>( BulkOperationCommand<Integer> command = new BulkOperationCommand<>(
toRemove.stream().map(QyUserEntity::getId).collect(Collectors.toList()) toRemove.stream().map(QyUserEntity::getId).collect(Collectors.toList())
); );
qyUserApplicationService.deleteUser(command); qyUserApplicationService.deleteUser(command);
} }*/
} }
} catch (Exception e) { } catch (Exception e) {
log.error("syncUserInfo error", e); log.error("syncUserInfo error", e);