| | |
| | | R<List<TUserTag>> tagResult = userTagClient.getAllUserTag(); |
| | | List<TUserTag> tags = tagResult == null || tagResult.getData() == null |
| | | ? Collections.emptyList() : tagResult.getData(); |
| | | // 单次批量拉取全部用户标签关系(t_app_user_tag 分片键 app_user_id 精确路由), |
| | | // 避免原先 标签×用户 双层循环逐个远程调用导致接口 40s+。 |
| | | R<List<TAppUserTag>> relationResult = appUserTagClient.listByUserIds(new ArrayList<>(userIds)); |
| | | List<TAppUserTag> relations = relationResult == null || relationResult.getData() == null |
| | | ? Collections.emptyList() : relationResult.getData(); |
| | | Map<Integer, Set<Long>> usersByTag = relations.stream() |
| | | .filter(relation -> relation.getUserTagId() != null && relation.getAppUserId() != null) |
| | | .collect(Collectors.groupingBy(TAppUserTag::getUserTagId, |
| | | Collectors.mapping(TAppUserTag::getAppUserId, Collectors.toSet()))); |
| | | Map<String, Long> counts = new LinkedHashMap<>(); |
| | | for (TUserTag tag : tags) { |
| | | if (tag == null || tag.getId() == null || StringUtils.isBlank(tag.getName())) { |
| | | continue; |
| | | } |
| | | long count = 0; |
| | | for (Long userId : userIds) { |
| | | TAppUserTag query = new TAppUserTag(); |
| | | query.setAppUserId(userId); |
| | | query.setUserTagId(tag.getId()); |
| | | R<TAppUserTag> userTagResult = appUserTagClient.getUserTag(query); |
| | | if (userTagResult != null && userTagResult.getData() != null) { |
| | | count++; |
| | | } |
| | | } |
| | | if (count > 0) { |
| | | counts.put(tag.getName(), count); |
| | | Set<Long> taggedUsers = usersByTag.get(tag.getId()); |
| | | if (taggedUsers != null && !taggedUsers.isEmpty()) { |
| | | counts.put(tag.getName(), (long) taggedUsers.size()); |
| | | } |
| | | } |
| | | return counts.entrySet().stream() |