refactor(QywxScheduleJob): 优化用户同步逻辑避免重复添加

将多个import语句合并为使用java.util.*
使用stream过滤避免重复添加微信用户
使用Set优化用户ID存在性检查
This commit is contained in:
dzq 2025-06-11 17:16:18 +08:00
parent 74cc7f3201
commit d7f85485db
1 changed files with 8 additions and 8 deletions

View File

@ -26,11 +26,7 @@ import com.agileboot.domain.qywx.template.command.UpdateTemplateCommand;
import com.agileboot.domain.qywx.template.db.QyTemplateEntity;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -435,7 +431,9 @@ public class QywxScheduleJob {
List<UserListResponse.UserInfo> wxUsersList = userList.getUserlist();
if (wxUsersList != null) {
wxUsers.addAll(wxUsersList);
wxUsers.addAll(wxUsersList.stream()
.filter(u -> wxUsers.stream().noneMatch(wu -> wu.getUserid().equals(u.getUserid())))
.collect(Collectors.toList()));
}
}
@ -451,9 +449,11 @@ public class QywxScheduleJob {
.collect(Collectors.toList());
// 识别需要新增的用户
List<QyUserEntity> finalQyUserList = qyUserList;
Set<String> existingUserIds = qyUserList.stream()
.map(QyUserEntity::getUserid)
.collect(Collectors.toSet());
List<UserListResponse.UserInfo> toAdd = wxUsers.stream()
.filter(wxUser -> finalQyUserList.stream().noneMatch(u -> u.getUserid().equals(wxUser.getUserid())))
.filter(wxUser -> !existingUserIds.contains(wxUser.getUserid()))
.collect(Collectors.toList());
log.info("syncUserInfo toAdd: {}", JSONUtil.toJsonStr(toAdd));