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()))
.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) {
// 获取部门用户列表
@ -440,26 +433,29 @@ public class QywxScheduleJob {
Map<String, UserListResponse.UserInfo> wxUserMap = wxUsers.stream()
.collect(Collectors.toMap(UserListResponse.UserInfo::getUserid, Function.identity()));
List<QyUserEntity> localUsers = qyUserList.stream()
.filter(u -> u.getAppid().equals(appid))
List<QyUserEntity> qyUserList = qyUserApplicationService.selectAll();
if (null == qyUserList) {
qyUserList = new ArrayList<>();
}
qyUserList = qyUserList.stream()
.filter(u -> u.getCorpid().equals(authCorpInfo.getCorpid()) && u.getAppid().equals(appid))
.collect(Collectors.toList());
Map<String, QyUserEntity> localUserMap = localUsers.stream()
.collect(Collectors.toMap(QyUserEntity::getUserid, Function.identity()));
// 识别需要新增的用户
List<QyUserEntity> finalQyUserList = qyUserList;
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());
log.info("syncUserInfo toAdd: {}", JSONUtil.toJsonStr(toAdd));
// 识别需要删除的用户
List<QyUserEntity> toRemove = localUsers.stream()
/* List<QyUserEntity> toRemove = qyUserList.stream()
.filter(localUser -> !wxUserMap.containsKey(localUser.getUserid()))
.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 -> {
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<>(
toRemove.stream().map(QyUserEntity::getId).collect(Collectors.toList())
);
qyUserApplicationService.deleteUser(command);
}
}*/
}
} catch (Exception e) {
log.error("syncUserInfo error", e);