新增猜你喜欢

This commit is contained in:
苏元皓
2024-09-02 17:42:24 +08:00
parent 5dd7f7193f
commit 5eb573fb9d
10 changed files with 93 additions and 0 deletions

View File

@@ -321,6 +321,24 @@ public class PostController {
return Result.ok(drafts);
}
/**
* 猜你喜欢
*/
@GetMapping("/guessYouLike")
@Operation(summary = "猜你喜欢")
public Result guessYouLike(@RequestParam("latitude") Double latitude,
@RequestParam("longitude") Double longitude) {
String userId = StpUtil.getLoginIdAsString();
String key = "search_history:" + userId;
// 获取用户的搜索历史关键词
List<String> searchHistory = redisTemplate.opsForList().range(key, 0, -1);
// 获取猜你喜欢的帖子列表
List<Post> recommendedPosts = postService.getRecommendedPosts(latitude, longitude, searchHistory);
return Result.ok(recommendedPosts);
}
}

View File

@@ -44,4 +44,6 @@ public class Post implements Serializable {
private Date updatedAt;
private static final long serialVersionUID = 1L;
}

View File

@@ -21,6 +21,9 @@ public interface LikeMapper extends BaseMapper<Likee> {
@Select("SELECT SUM(like_count) FROM comments WHERE user_openid = #{userOpenid}")
Long getCommentLikeCount(@Param("userOpenid") String userOpenid);
// 查询帖子被点赞的总数
Long getLikeCountByPostId(@Param("postId") Long postId);
}

View File

@@ -23,6 +23,12 @@ public interface PostMapper extends BaseMapper<Post> {
List<Post> searchPosts(String keyword);
List<Post> getTopFavoritedPosts(int limit);
List<Post> getPostsBasedOnHistoryAndHotness(Long userId, List<String> searchHistory);
@Select("SELECT * FROM post WHERE ST_Distance_Sphere(point(location_lng, location_lat), point(#{longitude}, #{latitude})) < 50000") // 50km 范围
List<Post> getPostsNearby(Double latitude, Double longitude);
}

View File

@@ -31,4 +31,6 @@ public interface InteractionService {
void unfavoriteOfficialTutorial(String userOpenid, Long tutorialId);
IPage<Tutorials> getFavoriteOfficialTutorials(String userOpenid, int pageNum, int pageSize);
// 新增方法:根据帖子 ID 获取点赞总数
Long getLikeCountByPostId(Long postId);
}

View File

@@ -31,4 +31,8 @@ public interface PostService extends IService<Post> {
List<Post> getTopFavoritedPosts(int limit);
List<Post> getDraftsByUserId(String userId);
List<Post> getRecommendedPosts(Double latitude, Double longitude, List<String> searchHistory);
}

View File

@@ -251,4 +251,10 @@ public class InteractionServiceImpl implements InteractionService {
}
@Override
public Long getLikeCountByPostId(Long postId) {
log.info("Fetching like count for post {}", postId);
return likeMapper.getLikeCountByPostId(postId);
}
}

View File

@@ -5,17 +5,24 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ivmiku.tutorial.entity.BrowingHistory;
import com.ivmiku.tutorial.entity.Post;
import com.ivmiku.tutorial.mapper.CommentMapper;
import com.ivmiku.tutorial.mapper.FavoriteMapper;
import com.ivmiku.tutorial.mapper.LikeMapper;
import com.ivmiku.tutorial.mapper.PostMapper;
import com.ivmiku.tutorial.service.BrowingHistoryService;
import com.ivmiku.tutorial.service.InteractionService;
import com.ivmiku.tutorial.service.PostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
@@ -25,6 +32,10 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements Po
@Autowired
private PostMapper postMapper;
@Lazy
@Autowired
private InteractionService interactionService;
@Autowired
private BrowingHistoryService browingHistoryService;
@@ -197,5 +208,36 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements Po
}
@Override
public List<Post> getRecommendedPosts(Double latitude, Double longitude, List<String> searchHistory) {
// 1. 根据用户当前位置计算距离(例如使用 Haversine 公式)
List<Post> nearbyPosts = postMapper.getPostsNearby(latitude, longitude);
// 2. 根据帖子热度排序
List<Post> sortedPosts = sortPostsByHeat(nearbyPosts);
// 3. 根据用户的搜索历史关键词筛选相关帖子
List<Post> matchingPosts = sortedPosts.stream()
.filter(post -> searchHistory.stream().anyMatch(keyword ->
post.getTitle().contains(keyword) || post.getContent().contains(keyword)))
.collect(Collectors.toList());
return matchingPosts;
}
public List<Post> sortPostsByHeat(List<Post> posts) {
// 根据帖子热度进行排序
return posts.stream()
.sorted((post1, post2) -> {
int post1Heat = calculatePostHeat(post1);
int post2Heat = calculatePostHeat(post2);
return Integer.compare(post2Heat, post1Heat); // 降序排列
})
.collect(Collectors.toList());
}
public int calculatePostHeat(Post post) {
Long postId = post.getPostId();
return interactionService.getLikeCountByPostId(postId).intValue();
}
}

View File

@@ -17,4 +17,8 @@
id,user_openid,post_id,tutorialId,
comment_id,is_deleted
</sql>
<select id="getLikeCountByPostId" resultType="java.lang.Long">
SELECT COUNT(*) FROM likee
WHERE post_id = #{postId} AND is_deleted = 0
</select>
</mapper>

View File

@@ -108,5 +108,11 @@
ORDER BY COUNT(f.id) DESC
LIMIT #{limit}
</select>
<select id="getPostsBasedOnHistoryAndHotness" resultType="com.ivmiku.tutorial.entity.Post">
SELECT *
FROM post
WHERE content LIKE CONCAT('%', #{searchHistory}, '%')
ORDER BY hotness DESC
</select>
</mapper>