|
|
@@ -203,35 +203,80 @@ public class LifeStoreService {
|
|
|
return mutualAttention;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取首页信息(优化版)
|
|
|
+ *
|
|
|
+ * @param phoneId 用户标识(格式:user_手机号 或 store_手机号)
|
|
|
+ * @return 首页信息
|
|
|
+ */
|
|
|
public LifeFansVo getHomePageInfo(String phoneId) {
|
|
|
+ // 参数校验
|
|
|
+ if (StringUtils.isEmpty(phoneId)) {
|
|
|
+ log.warn("getHomePageInfo: phoneId为空");
|
|
|
+ return new LifeFansVo();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证 phoneId 格式
|
|
|
+ String[] phoneIdParts = phoneId.split("_");
|
|
|
+ if (phoneIdParts.length < 2) {
|
|
|
+ log.warn("getHomePageInfo: phoneId格式错误,phoneId={}", phoneId);
|
|
|
+ return new LifeFansVo();
|
|
|
+ }
|
|
|
+
|
|
|
QueryWrapper<LifeFansVo> wrapper = new QueryWrapper<>();
|
|
|
wrapper.groupBy("foll.phoneId");
|
|
|
|
|
|
// 判断自己的拉黑type
|
|
|
- String blockerType = "";
|
|
|
- String blockerId = "";
|
|
|
- if ("user".equals(phoneId.split("_")[0])) {
|
|
|
- String myselfUserPhone = phoneId.split("_")[1];
|
|
|
- blockerType = "2";
|
|
|
- LifeUser myLifeUser = lifeUserService.getUserByPhone(myselfUserPhone);
|
|
|
- blockerId = String.valueOf(myLifeUser.getId());
|
|
|
- } else {
|
|
|
- String myselfStorePhone = phoneId.split("_")[1];
|
|
|
- blockerType = "1";
|
|
|
- StoreUser myStoreUser = storeUserService.getUserByPhone(myselfStorePhone);
|
|
|
- blockerId = String.valueOf(myStoreUser.getId());
|
|
|
+ final String blockerType;
|
|
|
+ final String blockerId;
|
|
|
+ try {
|
|
|
+ if ("user".equals(phoneIdParts[0])) {
|
|
|
+ String myselfUserPhone = phoneIdParts[1];
|
|
|
+ blockerType = "2";
|
|
|
+ LifeUser myLifeUser = lifeUserService.getUserByPhone(myselfUserPhone);
|
|
|
+ if (myLifeUser == null || myLifeUser.getId() == null) {
|
|
|
+ log.warn("getHomePageInfo: 用户不存在,phoneId={}", phoneId);
|
|
|
+ return new LifeFansVo();
|
|
|
+ }
|
|
|
+ blockerId = String.valueOf(myLifeUser.getId());
|
|
|
+ } else {
|
|
|
+ String myselfStorePhone = phoneIdParts[1];
|
|
|
+ blockerType = "1";
|
|
|
+ StoreUser myStoreUser = storeUserService.getUserByPhone(myselfStorePhone);
|
|
|
+ if (myStoreUser == null || myStoreUser.getId() == null) {
|
|
|
+ log.warn("getHomePageInfo: 商户不存在,phoneId={}", phoneId);
|
|
|
+ return new LifeFansVo();
|
|
|
+ }
|
|
|
+ blockerId = String.valueOf(myStoreUser.getId());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("getHomePageInfo: 获取用户信息异常,phoneId={}", phoneId, e);
|
|
|
+ return new LifeFansVo();
|
|
|
}
|
|
|
|
|
|
- IPage<LifeFansVo> myFollowed = lifeFansMapper.getMyFollowed(new Page<>(1, Integer.MAX_VALUE), phoneId, blockerType, blockerId, wrapper);
|
|
|
- int followedNum = myFollowed.getRecords().stream().filter(x -> null == x.getBlackListid()).collect(Collectors.toList()).size();
|
|
|
+ // 优化:使用分页查询统计数量,避免使用 Integer.MAX_VALUE 导致内存溢出
|
|
|
+ // 使用合理的分页大小,如果数据量很大则循环分页统计
|
|
|
+ int pageSize = 1000; // 设置合理的分页大小
|
|
|
+
|
|
|
+ // 统计关注数量(过滤被拉黑的)
|
|
|
+ int followedNum = countFilteredRecords(phoneId, blockerType, blockerId, wrapper, pageSize,
|
|
|
+ (page, w) -> lifeFansMapper.getMyFollowed(page, phoneId, blockerType, blockerId, w));
|
|
|
|
|
|
- IPage<LifeFansVo> myFans = lifeFansMapper.getMyFans(new Page<>(1, Integer.MAX_VALUE), phoneId, blockerType, blockerId, wrapper);
|
|
|
- int fansNum = myFans.getRecords().stream().filter(x -> null == x.getBlackListid()).collect(Collectors.toList()).size();
|
|
|
+ // 统计粉丝数量(过滤被拉黑的)
|
|
|
+ int fansNum = countFilteredRecords(phoneId, blockerType, blockerId, wrapper, pageSize,
|
|
|
+ (page, w) -> lifeFansMapper.getMyFans(page, phoneId, blockerType, blockerId, w));
|
|
|
|
|
|
- IPage<LifeFansVo> mutualAttention = lifeFansMapper.getMutualAttention(new Page<>(1, Integer.MAX_VALUE), phoneId, blockerType, blockerId, wrapper);
|
|
|
- int friendNum = mutualAttention.getRecords().stream().filter(x -> null == x.getBlackListid()).collect(Collectors.toList()).size();
|
|
|
+ // 统计好友数量(过滤被拉黑的)
|
|
|
+ int friendNum = countFilteredRecords(phoneId, blockerType, blockerId, wrapper, pageSize,
|
|
|
+ (page, w) -> lifeFansMapper.getMutualAttention(page, phoneId, blockerType, blockerId, w));
|
|
|
|
|
|
+ // 获取基础信息(包含dynamicsNum)
|
|
|
LifeFansVo lifeFansVo = lifeFansMapper.getHomePageInfo(phoneId);
|
|
|
+ if (lifeFansVo == null) {
|
|
|
+ lifeFansVo = new LifeFansVo();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置统计数量
|
|
|
lifeFansVo.setFollowNum(String.valueOf(followedNum));
|
|
|
lifeFansVo.setFansNum(String.valueOf(fansNum));
|
|
|
lifeFansVo.setFriendNum(String.valueOf(friendNum));
|
|
|
@@ -398,6 +443,48 @@ public class LifeStoreService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 统计过滤后的记录数量(分页查询,避免内存溢出)
|
|
|
+ *
|
|
|
+ * @param phoneId 用户标识
|
|
|
+ * @param blockerType 拉黑类型
|
|
|
+ * @param blockerId 拉黑者ID
|
|
|
+ * @param wrapper 查询条件
|
|
|
+ * @param pageSize 分页大小
|
|
|
+ * @param queryFunction 查询函数
|
|
|
+ * @return 过滤后的记录数量
|
|
|
+ */
|
|
|
+ private int countFilteredRecords(String phoneId, String blockerType, String blockerId,
|
|
|
+ QueryWrapper<LifeFansVo> wrapper, int pageSize,
|
|
|
+ java.util.function.BiFunction<Page<LifeFansVo>, QueryWrapper<LifeFansVo>, IPage<LifeFansVo>> queryFunction) {
|
|
|
+ int totalCount = 0;
|
|
|
+ int currentPage = 1;
|
|
|
+ int maxPages = 100; // 限制最大页数,防止无限循环
|
|
|
+
|
|
|
+ while (currentPage <= maxPages) {
|
|
|
+ IPage<LifeFansVo> pageResult = queryFunction.apply(new Page<>(currentPage, pageSize), wrapper);
|
|
|
+
|
|
|
+ if (pageResult == null || CollectionUtils.isEmpty(pageResult.getRecords())) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 统计当前页过滤后的数量
|
|
|
+ long currentPageCount = pageResult.getRecords().stream()
|
|
|
+ .filter(x -> null == x.getBlackListid())
|
|
|
+ .count();
|
|
|
+ totalCount += (int) currentPageCount;
|
|
|
+
|
|
|
+ // 如果当前页数据少于分页大小,说明已经是最后一页
|
|
|
+ if (pageResult.getRecords().size() < pageSize) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ currentPage++;
|
|
|
+ }
|
|
|
+
|
|
|
+ return totalCount;
|
|
|
+ }
|
|
|
+
|
|
|
private void filterBlocked(String fansId, IPage<LifeFansVo> myFollowed) {
|
|
|
|
|
|
// 判断自己的拉黑type
|