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

推荐列表 fix

parent 16f7e25e
......@@ -77,6 +77,7 @@ import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
......@@ -142,7 +143,7 @@ public class ThemeManager {
FileUtils.forceMkdir(f);
}
}
// 专栏
@Autowired
private FeignClientForCommunity feignClientForCommunity;
......@@ -403,15 +404,19 @@ public class ThemeManager {
*/
// 查询主题列表:推荐/关注/热门/最新
public ThemeListResp queryList(ThemeListReq req, String userId) {
List<String> excludeIds;
List<String> excludeIds = new ArrayList<>();
LocalDateTime firstThemeTime = LocalDateTime.now();
if (req.page.pageNumber > 1) {
String l = redisCache.get("queryThemes_" + req.ident);
excludeIds = StringUtils.isBlank(l) ? new ArrayList<>() : JsonUtil.toBean(l, new TypeReference<List<String>>() {
});
} else {
excludeIds = new ArrayList<>();
if (StringUtils.isBlank(l)) {
excludeIds = JsonUtil.toBean(l, new TypeReference<List<String>>() {
});
firstThemeTime = themeService.queryByThemeIdIgnoreDelete(excludeIds.get(0)).getCreateTime();
}
}
Integer pageStart = (req.page.pageNumber - 1) * req.page.pageSize;
Integer pageSize = req.page.pageSize;
......@@ -422,11 +427,11 @@ public class ThemeManager {
// 需要筛掉用户访问过详情的 & 最近出现在列表页过的.
List<String> visitedIds = visitLogService.queryUserRecentVisited(userId);
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) {
recmdIds.addAll(recommendService.getRecommendThemes(pageStart, pageSize, userId, recmdIds));
recmdIds.addAll(recommendService.getRecommendThemes(pageStart, pageSize, userId, recmdIds, firstThemeTime));
}
themes = themeService.queryByThemeIds(recmdIds);
......
......@@ -6,6 +6,7 @@ import com.tanpu.community.api.beans.resp.PythonResponse;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.util.BizUtils;
import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -13,6 +14,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
......@@ -49,22 +51,25 @@ public class RecommendService {
// 推荐
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
List<String> hotThemeIds = rankService.getHotestThemes().stream()
.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());
hotThemeIds = BizUtils.subList(hotThemeIds, pageStart, pageSize);
hotThemeIds = BizUtils.subList(hotThemeIds, 0, pageSize);
//最新话题,筛掉用户发表的 & 最近看过的 & excludeIds
//最新话题,筛掉用户发表的 & 最近看过的 & excludeIds && 上次查询后发帖的
long margin = TimeUtils.calMinuteTillNow(timeAfter);
List<String> newThemeIds = getNewestThemes().stream()
.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());
newThemeIds = BizUtils.subList(newThemeIds, pageStart, pageSize);
newThemeIds = BizUtils.subList(newThemeIds, 0, pageSize);
//推荐话题
List<String> recThemeIds = getPythonRecommendList(userId).stream()
......
......@@ -44,24 +44,36 @@ public class TimeUtils {
//计算迄今分钟
public static long calMinuteTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now());
return between.toMinutes();
}
//计算迄今毫秒数
public static long calMillisTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now());
return between.toMillis();
}
//计算迄今天数
public static long calDaysTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now());
return between.toDays();
}
//计算迄今小时数
public static long calHoursTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now());
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