Commit 4348dcca authored by 刘基明's avatar 刘基明

排序落库 fix

parent d326f0fe
......@@ -46,7 +46,7 @@ public class TopicRankQo {
*
* @return
*/
public Double getScore() {
public Double getRank() {
double g = 0.3;//时间系数
//顶置话题
if (isTop > 0) {
......
......@@ -175,7 +175,7 @@ public class HomePageManager {
public Page<FollowQo> queryFollow(QueryFollowReq req, String userId) {
//TODO 数据库分页
List<String> userIds = QueryFollowTypeEnum.QUERY_FANS.getCode().equals(req.getQueryType()) ?
followRelService.queryFansByIdolId(req.getUserId()) : followRelService.queryFansByFollowerId(req.getUserId());
followRelService.queryFansByIdolId(req.getUserId()) : followRelService.queryIdolsByFollowerId(req.getUserId());
List<FollowQo> followQos = new ArrayList<>();
if (!CollectionUtils.isEmpty(userIds)) {
List<UserInfoResp> userInfoNews = feignClientForFatools.queryUserListNew(userIds);
......@@ -189,7 +189,7 @@ public class HomePageManager {
//判断返回列表中的用户是否被当前用户关注
public List<FollowQo> judgeFollowed(List<FollowQo> followQos, String followerId) {
Set<String> idolSet = new HashSet<>(followRelService.queryFansByFollowerId(followerId));
Set<String> idolSet = new HashSet<>(followRelService.queryIdolsByFollowerId(followerId));
return followQos.stream().map(o -> {
if (idolSet.contains(o.getUserId())) {
o.setFollowed(true);
......
......@@ -258,7 +258,7 @@ public class ThemeManager {
visitLogService.addPageView(userId, userId, VisitTypeEnum.FOLLOW_THEME_VIEW);
}
// 根据关注列表查询,按时间倒序
List<String> fansList = followRelService.queryFansByFollowerId(userId);
List<String> fansList = followRelService.queryIdolsByFollowerId(userId);
themes = themeService.queryByUserIdsCreateDesc(fansList, pageStart, pageSize);
} else if (ThemeListTypeEnum.TOPIC_HOT.getCode().equals(req.getType())) {
......@@ -301,9 +301,10 @@ public class ThemeManager {
for (ThemeQo themeQO : themeQos) {
// 通用信息
buildThemeQoExtraInfo(themeQO);
// 和用户相关信息
buildThemeExtraInfoByUser(userId, themeQO);
}
// 和用户相关信息
buildThemeExtraInfoByUser(userId,themeQos);
return themeQos;
}
......@@ -324,23 +325,38 @@ public class ThemeManager {
themeQo.setForwardCount(forwardCount);
}
// 组装和当前用户相关信息
// 组装和当前用户相关信息(单个查询)
private void buildThemeExtraInfoByUser(String userId, ThemeQo themeQo) {
//todo 批量查询 IN
String themeId = themeQo.getThemeId();
// 是否关注作者
themeQo.setFollow(followRelService.checkFollow(themeQo.getAuthorId(), userId));
// 是否点赞
CollectionEntity likeEntity = collectionService.getNotDeleteTargetCollection(themeId, userId, CollectionTypeEnum.LIKE_THEME);
CollectionEntity likeEntity = collectionService.getTarget(themeId, userId, CollectionTypeEnum.LIKE_THEME);
themeQo.setHasLiked(likeEntity != null);
// 是否转发
Integer forwardCountByUser = themeService.getForwardCountByUser(themeId, userId);
themeQo.setHasForward(forwardCountByUser > 0);
themeQo.setHasForward(themeService.judgeForwardByUser(themeId, userId));
// 是否收藏
CollectionEntity collectionEntity = collectionService.getNotDeleteTargetCollection(themeId, userId, CollectionTypeEnum.COLLECT_THEME);
CollectionEntity collectionEntity = collectionService.getTarget(themeId, userId, CollectionTypeEnum.COLLECT_THEME);
themeQo.setHasCollect(collectionEntity != null);
}
// 组装和当前用户相关信息(批量查询)
private void buildThemeExtraInfoByUser(String userId, List<ThemeQo> themeQos) {
// 批量查询
List<String> themeIds = themeQos.stream().map(ThemeQo::getThemeId).collect(Collectors.toList());
Set<String> idolSet = new HashSet<>(followRelService.queryIdolsByFollowerId(userId));
Set<String> likeSet = collectionService.getTargets(themeIds, userId, CollectionTypeEnum.LIKE_THEME);
Set<String> bookSet = collectionService.getTargets(themeIds, userId, CollectionTypeEnum.COLLECT_THEME);
Set<String> forwardUsers = themeService.getForwardUsers(themeIds);
// 从set中查找
for (ThemeQo themeQo : themeQos) {
themeQo.setFollow(idolSet.contains(themeQo.getAuthorId()));
themeQo.setHasLiked(likeSet.contains(themeQo.getThemeId()));
themeQo.setHasCollect(bookSet.contains(themeQo.getThemeId()));
themeQo.setHasForward(forwardUsers.contains(userId));
}
}
// 返回用户发布、回复、点赞、收藏的主题列表
public List<ThemeQo> queryThemesByUser(QueryRecordThemeReq req, String userId) {
......@@ -430,7 +446,7 @@ public class ThemeManager {
//关注用户是否有更新
public Integer getFollowUpdateCount(String userId) {
LocalDateTime lastViewTime = visitLogService.queryLatestViewFollow(userId);
List<String> fansList = followRelService.queryFansByFollowerId(userId);
List<String> fansList = followRelService.queryIdolsByFollowerId(userId);
return themeService.queryCountFromLastTime(fansList, lastViewTime);
}
......
......@@ -28,7 +28,11 @@ public class CollectionService {
@Transactional
public void saveOrUpdate(String themeId, String userId, CollectionTypeEnum type) {
// 判断记录是否存在,无论是否删除
CollectionEntity queryCollection = getTargetCollection(themeId, userId, type);
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getUserId, userId)
.eq(CollectionEntity::getTargetId, themeId);
CollectionEntity queryCollection = collectionMapper.selectOne(queryWrapper);
if (queryCollection != null) {
queryCollection.setDeleteTag(DeleteTagEnum.NOT_DELETED.getCode());
queryCollection.setCollectionTime(LocalDateTime.now());
......@@ -47,7 +51,7 @@ public class CollectionService {
//根据用户、主题、类型查询未删除对象
public CollectionEntity getNotDeleteTargetCollection(String targetId, String userId, CollectionTypeEnum type) {
public CollectionEntity getTarget(String targetId, String userId, CollectionTypeEnum type) {
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getUserId, userId)
......@@ -56,15 +60,18 @@ public class CollectionService {
return collectionMapper.selectOne(queryWrapper);
}
//根据用户、主题、类型查询记录,包括已删除对象
public CollectionEntity getTargetCollection(String themeId, String userId, CollectionTypeEnum type) {
//根据用户、主题、类型查询删除对象
public Set<String> getTargets(List<String> targetIds, String userId, CollectionTypeEnum type) {
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getUserId, userId)
.eq(CollectionEntity::getTargetId, themeId);
return collectionMapper.selectOne(queryWrapper);
.in(CollectionEntity::getTargetId, targetIds)
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
return collectionMapper.selectList(queryWrapper).stream().map(CollectionEntity::getTargetId)
.collect(Collectors.toSet());
}
// 根据用户id和行为type获取target_id列表
public Set<String> getSetByUser(String userId, CollectionTypeEnum type) {
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
......@@ -95,7 +102,7 @@ public class CollectionService {
// 统计多个对象(主题、评论)的数量(点赞、收藏)
public Map<String, Integer> getCountMapByType(List<String> targetIds, CollectionTypeEnum type) {
if (CollectionUtils.isEmpty(targetIds)){
if (CollectionUtils.isEmpty(targetIds)) {
return new HashMap<>();
}
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>().eq(CollectionEntity::getCollectionType, 1)
......@@ -116,7 +123,7 @@ public class CollectionService {
//逻辑删除,修改delete_tag
@Transactional
public void delete(String themeId, String userId, CollectionTypeEnum type) {
CollectionEntity queryCollection = getNotDeleteTargetCollection(themeId, userId, type);
CollectionEntity queryCollection = getTarget(themeId, userId, type);
if (queryCollection != null) {
queryCollection.setDeleteTag(DeleteTagEnum.DELETED.getCode());
queryCollection.setUncollectionTime(LocalDateTime.now());
......
......@@ -21,7 +21,7 @@ public class FollowRelService {
@Resource
private FollowRelMapper followRelMapper;
public List<String> queryFansByFollowerId(String followerId) {
public List<String> queryIdolsByFollowerId(String followerId) {
return followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
.eq(FollowRelEntity::getFansId, followerId)
.eq(FollowRelEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()))
......
......@@ -146,7 +146,7 @@ public class RankService {
topic.setFormatViewCount(BizUtils.formatCountNumber(topic.getViewCount()));
topic.setFormatDisscussCount(BizUtils.formatCountNumber(topic.getDisscussCount()));
}
Map<TopicRankQo, Double> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getScore));
Map<TopicRankQo, Double> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getRank));
List<TopicRankQo> rankList = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.map(Map.Entry::getKey)
......
......@@ -7,8 +7,6 @@ import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.util.BizUtils;
import com.tanpu.community.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
......@@ -180,6 +178,7 @@ public class RecommendService {
if (hotStart >= hotIds.size() && newestStart >= newestIds.size() && recmdStart >= recmdIds.size()) {
break;
}
retList.addAll(BizUtils.subList(hotIds, hotStart, hotStart + 6));
retList.addAll(BizUtils.subList(newestIds, newestStart, newestStart + 3));
retList.addAll(BizUtils.subList(recmdIds, recmdStart, recmdStart + 1));
......
......@@ -19,10 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Service
......@@ -256,13 +253,22 @@ public class ThemeService {
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
}
public Integer getForwardCountByUser(String themeId, String userId) {
public boolean judgeForwardByUser(String themeId, String userId) {
return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getFormerThemeId, themeId)
.eq(ThemeEntity::getAuthorId, userId)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))>0;
}
public Set<String> getForwardUsers(List<String> themeIds) {
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getFormerThemeId, themeIds)
.eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))
.stream().map(ThemeEntity::getAuthorId).collect(Collectors.toSet());
}
@Transactional
public void deleteById(String themeId,String userId) {
ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
......
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