Commit 43719a2f authored by 刘基明's avatar 刘基明

热度算法加入用户质量

parent 4769e67a
......@@ -46,10 +46,13 @@ public class ThemeAnalysDO {
@ApiModelProperty(value = "距今时间")
private Long minuteTillNow;
@ApiModelProperty(value = "用户质量")
private Double userWeight = 0.0;
public Double getScore() {
double w = (double) (viewCount*0.1 + forwardCount*3 + commentCount*2 + likeCount*1 + collectCount*3);
//质量=帖子质量+用户质量
double w = (double) (viewCount * 0.1 + forwardCount * 3 + commentCount * 2 + likeCount * 1 + collectCount * 3) + userWeight;
double i = 1;//初试权重
double t = Double.valueOf(minuteTillNow) / 60;
double g = 0.1;//时间系数
......
......@@ -3,10 +3,7 @@ package com.tanpu.community.dao.entity.community;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
......@@ -64,12 +61,10 @@ public class CommentEntity implements Serializable {
@ApiModelProperty(value = "举报状态 0:未被举报 1:被举报 2:已处理")
private Integer reportStatus;
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private LocalDateTime createTime;
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private LocalDateTime updateTime;
private Integer deleteTag;
......
......@@ -211,7 +211,7 @@ public class ThemeManager {
return convertEntityToQo(themeEntities, userId);
}
//主题Entity转QO
//主题Entity转QO,组装所有信息
private List<ThemeQo> convertEntityToQo(List<ThemeEntity> themeEntities, String userId) {
//Entity转Qo
List<ThemeQo> themeQos = ConvertUtil.themeEntitiesToDTOs(themeEntities);
......@@ -219,7 +219,9 @@ public class ThemeManager {
batchFeignCallService.getAttachDetailByBatch(themeQos);
//其他信息
for (ThemeQo themeQO : themeQos) {
//通用信息
buildThemeQoExtraInfo(themeQO);
//和用户相关信息
buildThemeExtraInfoByUser(userId, themeQO);
}
......
package com.tanpu.community.service;
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.qo.ThemeAnalysDO;
import com.tanpu.community.api.beans.qo.TopicRankQo;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoNew;
import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.api.enums.TopicStatusEnum;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.feign.fatools.FeignClientForFatools;
import com.tanpu.community.util.ConvertUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import static com.tanpu.community.api.constants.RedisKeyConstant.CACGE_FEIGN_USER_INFO;
@Service
public class RankService {
@Autowired
......@@ -27,6 +36,12 @@ public class RankService {
@Autowired
private VisitSummaryService visitSummaryService;
@Autowired
private RedisCache redisCache;
@Resource
private FeignClientForFatools feignClientForFatools;
//最热
private List<ThemeAnalysDO> rankThemeList = new ArrayList<>();
......@@ -54,6 +69,17 @@ public class RankService {
theme.setForwardCount(forwardCount);
theme.setCollectCount(bookCount);
theme.setViewCount(viewCount);
//查询用户质量
String authorId = theme.getAuthorId();
UserInfoNew authorInfo = redisCache.getObject(StringUtils.joinWith(CACGE_FEIGN_USER_INFO, authorId),
60 * 10, () -> this.getUserInfo(authorId), UserInfoNew.class);
if (authorInfo == null || authorInfo.getLevelGrade() == null) {
theme.setUserWeight(0.0);
} else {
theme.setUserWeight(authorInfo.getLevelGrade() * 1.0);
}
}
//打分
Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getScore));
......@@ -62,6 +88,14 @@ public class RankService {
}
private UserInfoNew getUserInfo(String authorId) {
CommonResp<UserInfoNew> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(authorId);
if (userInfoNewCommonResp.isNotSuccess()) {
throw new BizException("内部接口调用失败");
}
return userInfoNewCommonResp.getData();
}
/**
* 计算话题热度
......@@ -76,7 +110,7 @@ public class RankService {
}
for (TopicRankQo topic : topicRankQos) {
List<String> themeIds = themeService.queryThemeIdsByTopic(topic.getTopicId());
if (CollectionUtils.isEmpty(themeIds)){
if (CollectionUtils.isEmpty(themeIds)) {
topic.setViewCount(0);
topic.setDisscussCount(0);
continue;
......@@ -100,13 +134,14 @@ public class RankService {
/**
* 最新和热门主题集合
*
* @param hotCount
* @param newCount
* @return
*/
public List<String> getHotAndNewThemes(Integer hotCount, Integer newCount,String userId) {
public List<String> getHotAndNewThemes(Integer hotCount, Integer newCount, String userId) {
Set<String> hotThemeIds = this.rankThemeList.stream().limit(hotCount)
.filter(o->!userId.equals(o.getAuthorId()))
.filter(o -> !userId.equals(o.getAuthorId()))
.map(ThemeAnalysDO::getThemeId)
.collect(Collectors.toSet());
Set<String> newThemeIds = themeService.selectExcludeUser(null, null, newCount)
......@@ -117,11 +152,12 @@ public class RankService {
/**
* 话题详情
*
* @param topicId 话题Id
* @return
*/
public TopicRankQo getTopicDetail(String topicId){
if (this.rankTopicList.size()==0){
public TopicRankQo getTopicDetail(String topicId) {
if (this.rankTopicList.size() == 0) {
rankTopics();
}
List<TopicRankQo> matchTopic = this.rankTopicList.stream().filter(o -> topicId.equals(o.getTopicId())).limit(1).collect(Collectors.toList());
......@@ -130,34 +166,32 @@ public class RankService {
}
public List<TopicRankQo> getRankTopicList() {
if (this.rankTopicList.size()==0){
if (this.rankTopicList.size() == 0) {
this.rankTopics();
}
return rankTopicList;
}
public List<TopicRankQo> getRankTopicListTop4() {
if (this.rankTopicList.size()==0){
if (this.rankTopicList.size() == 0) {
this.rankTopics();
}
return rankTopicListTop4;
}
public List<ThemeAnalysDO> getRankThemeList() {
if (this.rankThemeList.size()==0){
if (this.rankThemeList.size() == 0) {
rankThemes();
}
return rankThemeList;
}
public List<String> getRankThemeListByTopic(String topicId) {
if (this.rankThemeList.size()==0){
if (this.rankThemeList.size() == 0) {
this.rankThemes();
}
return rankThemeList.stream().filter(o->topicId.equals(o.getTopicId()))
return rankThemeList.stream().filter(o -> topicId.equals(o.getTopicId()))
.map(ThemeAnalysDO::getThemeId)
.collect(Collectors.toList());
}
......
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