diff --git a/community-service/src/main/java/com/tanpu/community/manager/ThemeManager.java b/community-service/src/main/java/com/tanpu/community/manager/ThemeManager.java index d418d2f54338b71a68d12f3eb7c39289f8673ab1..f9b905b5e8fb283d0dc7362aa629f36e83360dd5 100644 --- a/community-service/src/main/java/com/tanpu/community/manager/ThemeManager.java +++ b/community-service/src/main/java/com/tanpu/community/manager/ThemeManager.java @@ -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); diff --git a/community-service/src/main/java/com/tanpu/community/service/RecommendService.java b/community-service/src/main/java/com/tanpu/community/service/RecommendService.java index 78f27980f79061ce2430e12e8e326353dbd8d74b..dabc0f43e783ccd054721bf2bc28961ab4a0e4da 100644 --- a/community-service/src/main/java/com/tanpu/community/service/RecommendService.java +++ b/community-service/src/main/java/com/tanpu/community/service/RecommendService.java @@ -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() diff --git a/community-service/src/main/java/com/tanpu/community/util/TimeUtils.java b/community-service/src/main/java/com/tanpu/community/util/TimeUtils.java index e94b50954c6279d2e6c978d0aeea647f75af7fdd..496f128beaf7dc03c3d5cc7b4f2b21678e50375c 100644 --- a/community-service/src/main/java/com/tanpu/community/util/TimeUtils.java +++ b/community-service/src/main/java/com/tanpu/community/util/TimeUtils.java @@ -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(); }