fix:关键词查询用户
This commit is contained in:
@@ -116,8 +116,10 @@ public class UserNameCacheService {
|
||||
|
||||
// 3. 根据关键词过滤
|
||||
String lowerKeyword = keyword.trim().toLowerCase();
|
||||
List<String> keywordList = splitKeyword(lowerKeyword);
|
||||
|
||||
List<CIDUserResp> result = allUsers.parallelStream()
|
||||
.filter(user -> matchesKeyword(user, lowerKeyword))
|
||||
.filter(user -> matchesKeyword(user, keywordList))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
log.info("搜索完成,keyword:{},结果数:{},耗时:{}ms",
|
||||
@@ -131,6 +133,24 @@ public class UserNameCacheService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分割关键词(处理逗号分隔,过滤空字符串)
|
||||
*/
|
||||
private List<String> splitKeyword(String lowerKeyword) {
|
||||
List<String> keywordList = new ArrayList<>();
|
||||
if (StringUtils.isNotEmpty(lowerKeyword)) {
|
||||
// 按逗号分割,兼容逗号前后有空格的情况
|
||||
String[] splitArray = lowerKeyword.split(",\\s*");
|
||||
for (String word : splitArray) {
|
||||
// 过滤分割后可能的空字符串
|
||||
if (StringUtils.isNotEmpty(word)) {
|
||||
keywordList.add(word);
|
||||
}
|
||||
}
|
||||
}
|
||||
return keywordList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动清除所有缓存
|
||||
*/
|
||||
@@ -139,17 +159,37 @@ public class UserNameCacheService {
|
||||
log.info("手动清除所有缓存");
|
||||
}
|
||||
|
||||
private boolean matchesKeyword(CIDUserResp user, String lowerKeyword) {
|
||||
if (user == null) return false;
|
||||
|
||||
if (user.getUsername() != null && user.getUsername().toLowerCase().contains(lowerKeyword)) {
|
||||
return true;
|
||||
/**
|
||||
* 扩展匹配逻辑:支持userId、username、nickname匹配
|
||||
*/
|
||||
private boolean matchesKeyword(CIDUserResp user, List<String> keywordList) {
|
||||
if (user == null || keywordList.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (user.getNickname() != null && user.getNickname().toLowerCase().contains(lowerKeyword)) {
|
||||
return true;
|
||||
}
|
||||
// 遍历所有分割后的关键词,只要匹配一个就返回true
|
||||
for (String keyword : keywordList) {
|
||||
// 场景1:关键词是数字,尝试匹配userId(精确匹配)
|
||||
if (keyword.matches("\\d+")) {
|
||||
try {
|
||||
Long userId = Long.parseLong(keyword);
|
||||
if (Objects.equals(user.getUserId(), userId)) {
|
||||
return true;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
// 理论上不会走到这里,因为已经通过数字正则校验
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 场景2:关键词是非数字,匹配username/nickname
|
||||
if (user.getUsername() != null && user.getUsername().toLowerCase().contains(keyword)) {
|
||||
return true;
|
||||
}
|
||||
if (user.getNickname() != null && user.getNickname().toLowerCase().contains(keyword)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,9 @@ public class SysUserController implements ISysUserFeignClient {
|
||||
*/
|
||||
@Operation(summary = "通过关键字查询用户列表", description = "通过关键字查询用户列表")
|
||||
@GetMapping("/listUserWithKeyWord")
|
||||
public SdmResponse<List<CIDUserResp>> listUserWithKeyWord(@Parameter(description = "通过关键字查询用户列表") @RequestParam String keyword) {
|
||||
public SdmResponse<List<CIDUserResp>> listUserWithKeyWord(@Parameter(description = "通过关键字查询用户列表")
|
||||
@RequestParam(value = "keyword", required = false) String keyword
|
||||
, @RequestParam(value = "userIds", required = false) String userIds) {
|
||||
return SdmResponse.success(userNameCacheService.searchUsers(keyword));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user