Commit c9d39e6a authored by 张辰's avatar 张辰
parents 76f15fb0 4d81b0e8
......@@ -29,15 +29,19 @@ public class TopicRankQo {
@ApiModelProperty(value = "浏览量调整基数")
private Integer viewCntAdjust;
@ApiModelProperty(value = "话题下的帖子权重")
private Integer themeWeight;
/**
* TODO 热度计算算法
*
* @return
*/
public Integer getRank(){
public Integer getRank() {
//顶置话题
if (isTop>0){
if (isTop > 0) {
return Integer.MAX_VALUE;
}
return this.viewCount+this.disscussCount*3+viewCount;
return this.viewCount + this.disscussCount * 3 + viewCount + themeWeight;
}
}
package com.tanpu.community.manager;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Sets;
import com.tanpu.common.api.CommonResp;
......@@ -216,20 +215,20 @@ public class ThemeManager {
Integer pageStart = (req.page.pageNumber - 1) * req.page.pageSize;
Integer pageSize = req.page.pageSize;
Integer querySize = pageSize * 3;
List<ThemeEntity> themes = new ArrayList<>();
if (ThemeListTypeEnum.RECOMMEND.getCode().equals(req.getType())) {
//推荐
// 推荐
// 需要筛掉用户访问过详情的 & 最近出现在列表页过的.
List<String> visitedIds = visitLogService.queryUserRecentVisited(userId);
List<String> excludes = ListUtils.union(excludeIds, visitedIds);
List<String> recmdIds = recommendService.getRecommendThemes(pageStart, querySize, userId, excludes);
List<String> recmdIds = recommendService.getRecommendThemes(pageStart, pageSize, userId, excludes);
recmdIds = BizUtils.subList(recmdIds, pageStart, pageSize);
themes = themeService.queryByThemeIds(recmdIds);
themes = RankUtils.sortThemeEntityByIds(themes, recmdIds);
themes = RankUtils.sortThemeEntityByIds(themes, recmdIds).stream().limit(pageSize).collect(Collectors.toList());
} else if (ThemeListTypeEnum.FOLLOW.getCode().equals(req.getType())) {
// TODO 临时埋点,接入新埋点后删除
......@@ -257,7 +256,7 @@ public class ThemeManager {
if (StringUtils.isEmpty(req.getTopicId())) {
throw new BizException("TopicId为空");
}
themes = themeService.queryNewestByTopic(req.topicId, pageStart, querySize, excludeIds);
themes = themeService.queryNewestByTopic(req.topicId, pageStart, pageSize, excludeIds);
}
ThemeListResp resp = new ThemeListResp();
......
......@@ -124,6 +124,11 @@ public class RankService {
//讨论数=发布主题贴数+回复总数
Integer commentCount = commentService.getTotalCountByThemeIds(themeIds);
topic.setDisscussCount(themeIds.size() + commentCount);
//帖子权重,求和
double themeSum = getHotestThemes().stream().filter(o -> topic.getTopicId().equals(o.getTopicId()))
.mapToDouble(ThemeAnalysDO::calcScore)
.sum();
topic.setThemeWeight((int)themeSum);
}
Map<TopicRankQo, Integer> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getRank));
List<TopicRankQo> rankList = map.entrySet().stream()
......
......@@ -240,6 +240,7 @@ public class ThemeService {
if (CollectionUtils.isEmpty(userIds)){
return Collections.emptyList();
}
//TODO 索引优化
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getAuthorId, userIds)
.last("limit " + pageStart + ", " + pageSize)
......
......@@ -10,13 +10,22 @@ import java.util.stream.Collectors;
public class RankUtils {
/**
* 根据id排序主题对象
* @param list 主题
* @param ids 主题Id,list中可重复
* @return
*/
public static List<ThemeEntity> sortThemeEntityByIds(List<ThemeEntity> list, List<String> ids){
int count=0;
HashMap<String, Integer> indexMap = new HashMap<>();
for (String id : ids) {
//list中出现重复元素时,优化取第一次出现的顺序
if (!indexMap.containsKey(id)){
indexMap.put(id,count++);
}
}
List<ThemeEntity> collect = list.stream().filter(o -> indexMap.containsKey(o.getThemeId()))
.sorted(Comparator.comparingInt(o -> indexMap.get(o.getThemeId())))
.collect(Collectors.toList());
......
......@@ -29,10 +29,12 @@ public class TimeUtils {
return duration + "分钟前";
} else if (duration < 60 * 24) {
return duration / 60 + "小时前";
} else if (start.getYear() == LocalDateTime.now().getYear()) {
return start.format(DateTimeFormatter.ofPattern("MM-dd HH:mm:ss"));
} else if (duration < 60 * 48) {
return "1天前";
} else if (duration < 60 * 24 * 180) {
return start.format(DateTimeFormatter.ofPattern("MM-dd HH:mm"));
}
return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}
//计算迄今时间
......@@ -43,8 +45,17 @@ public class TimeUtils {
//计算n天前的时间
public static LocalDateTime getDaysBefore(Integer number){
public static LocalDateTime getDaysBefore(Integer number) {
return LocalDateTime.now().minusDays(number);
}
public static void main(String[] args) {
System.out.println(calUpToNowTime(LocalDateTime.now().minusDays(180)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusDays(1)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusDays(18)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusHours(2)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusMinutes(12)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusSeconds(12)));
}
}
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