Commit 611177be authored by 刘基明's avatar 刘基明

推荐列表 fix

parent 16f7e25e
...@@ -77,6 +77,7 @@ import javax.annotation.PostConstruct; ...@@ -77,6 +77,7 @@ import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -403,15 +404,19 @@ public class ThemeManager { ...@@ -403,15 +404,19 @@ public class ThemeManager {
*/ */
// 查询主题列表:推荐/关注/热门/最新 // 查询主题列表:推荐/关注/热门/最新
public ThemeListResp queryList(ThemeListReq req, String userId) { public ThemeListResp queryList(ThemeListReq req, String userId) {
List<String> excludeIds; List<String> excludeIds = new ArrayList<>();
LocalDateTime firstThemeTime = LocalDateTime.now();
if (req.page.pageNumber > 1) { if (req.page.pageNumber > 1) {
String l = redisCache.get("queryThemes_" + req.ident); String l = redisCache.get("queryThemes_" + req.ident);
excludeIds = StringUtils.isBlank(l) ? new ArrayList<>() : JsonUtil.toBean(l, new TypeReference<List<String>>() { if (StringUtils.isBlank(l)) {
excludeIds = JsonUtil.toBean(l, new TypeReference<List<String>>() {
}); });
} else { firstThemeTime = themeService.queryByThemeIdIgnoreDelete(excludeIds.get(0)).getCreateTime();
excludeIds = new ArrayList<>();
} }
}
Integer pageStart = (req.page.pageNumber - 1) * req.page.pageSize; Integer pageStart = (req.page.pageNumber - 1) * req.page.pageSize;
Integer pageSize = req.page.pageSize; Integer pageSize = req.page.pageSize;
...@@ -422,11 +427,11 @@ public class ThemeManager { ...@@ -422,11 +427,11 @@ public class ThemeManager {
// 需要筛掉用户访问过详情的 & 最近出现在列表页过的. // 需要筛掉用户访问过详情的 & 最近出现在列表页过的.
List<String> visitedIds = visitLogService.queryUserRecentVisited(userId); List<String> visitedIds = visitLogService.queryUserRecentVisited(userId);
List<String> excludes = ListUtils.union(excludeIds, visitedIds); List<String> excludes = ListUtils.union(excludeIds, visitedIds);
List<String> recmdIds = recommendService.getRecommendThemes(pageStart, pageSize, userId, excludes); List<String> recmdIds = recommendService.getRecommendThemes(pageStart, pageSize, userId, excludes, firstThemeTime);
// 加载第一页时,为防止首页显示空列表,从推荐池中再捞出已看过帖子 // 加载第一页时,为防止首页显示空列表,从推荐池中再捞出已看过帖子
if (req.page.pageNumber == 1 && recmdIds.size() < pageSize) { if (req.page.pageNumber == 1 && recmdIds.size() < pageSize) {
recmdIds.addAll(recommendService.getRecommendThemes(pageStart, pageSize, userId, recmdIds)); recmdIds.addAll(recommendService.getRecommendThemes(pageStart, pageSize, userId, recmdIds, firstThemeTime));
} }
themes = themeService.queryByThemeIds(recmdIds); themes = themeService.queryByThemeIds(recmdIds);
......
...@@ -6,6 +6,7 @@ import com.tanpu.community.api.beans.resp.PythonResponse; ...@@ -6,6 +6,7 @@ import com.tanpu.community.api.beans.resp.PythonResponse;
import com.tanpu.community.dao.entity.community.ThemeEntity; import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.util.BizUtils; import com.tanpu.community.util.BizUtils;
import com.tanpu.community.util.ConvertUtil; import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.TimeUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -13,6 +14,7 @@ import org.springframework.stereotype.Service; ...@@ -13,6 +14,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -49,22 +51,25 @@ public class RecommendService { ...@@ -49,22 +51,25 @@ public class RecommendService {
// 推荐 // 推荐
private Map<String, List<String>> recommondList = new HashMap<>(); private Map<String, List<String>> recommondList = new HashMap<>();
public List<String> getRecommendThemes(Integer pageStart, Integer pageSize, String userId, List<String> excludeIds) { public List<String> getRecommendThemes(Integer pageStart, Integer pageSize, String userId, List<String> excludeIds, LocalDateTime timeAfter) {
// 最热话题,筛掉用户发表的 & 最近看过的 & excludeIds // 最热话题,筛掉用户发表的 & 最近看过的 & excludeIds
List<String> hotThemeIds = rankService.getHotestThemes().stream() List<String> hotThemeIds = rankService.getHotestThemes().stream()
.filter(theme -> { .filter(theme -> {
return !excludeIds.contains(theme.getThemeId()) && !userId.equals(theme.getAuthorId()); // 暂时不过滤用户自己发的 !userId.equals(theme.getAuthorId());
return !excludeIds.contains(theme.getThemeId());
}) })
.map(ThemeAnalysDO::getThemeId).collect(Collectors.toList()); .map(ThemeAnalysDO::getThemeId).collect(Collectors.toList());
hotThemeIds = BizUtils.subList(hotThemeIds, pageStart, pageSize); hotThemeIds = BizUtils.subList(hotThemeIds, 0, pageSize);
//最新话题,筛掉用户发表的 & 最近看过的 & excludeIds //最新话题,筛掉用户发表的 & 最近看过的 & excludeIds && 上次查询后发帖的
long margin = TimeUtils.calMinuteTillNow(timeAfter);
List<String> newThemeIds = getNewestThemes().stream() List<String> newThemeIds = getNewestThemes().stream()
.filter(theme -> { .filter(theme -> {
return !excludeIds.contains(theme.getThemeId()) && !userId.equals(theme.getAuthorId()); // 暂时不过滤用户自己发的 !userId.equals(theme.getAuthorId());
return !excludeIds.contains(theme.getThemeId()) && theme.getMinutesTillNow() > margin;
}) })
.map(ThemeAnalysDO::getThemeId).collect(Collectors.toList()); .map(ThemeAnalysDO::getThemeId).collect(Collectors.toList());
newThemeIds = BizUtils.subList(newThemeIds, pageStart, pageSize); newThemeIds = BizUtils.subList(newThemeIds, 0, pageSize);
//推荐话题 //推荐话题
List<String> recThemeIds = getPythonRecommendList(userId).stream() List<String> recThemeIds = getPythonRecommendList(userId).stream()
......
...@@ -44,24 +44,36 @@ public class TimeUtils { ...@@ -44,24 +44,36 @@ public class TimeUtils {
//计算迄今分钟 //计算迄今分钟
public static long calMinuteTillNow(LocalDateTime start) { public static long calMinuteTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now()); Duration between = Duration.between(start, LocalDateTime.now());
return between.toMinutes(); return between.toMinutes();
} }
//计算迄今毫秒数 //计算迄今毫秒数
public static long calMillisTillNow(LocalDateTime start) { public static long calMillisTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now()); Duration between = Duration.between(start, LocalDateTime.now());
return between.toMillis(); return between.toMillis();
} }
//计算迄今天数 //计算迄今天数
public static long calDaysTillNow(LocalDateTime start) { public static long calDaysTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now()); Duration between = Duration.between(start, LocalDateTime.now());
return between.toDays(); return between.toDays();
} }
//计算迄今小时数 //计算迄今小时数
public static long calHoursTillNow(LocalDateTime start) { public static long calHoursTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now()); Duration between = Duration.between(start, LocalDateTime.now());
return between.toHours(); return between.toHours();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment