部分实体类
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 商户id
*/
private Long shopId;
/**
* 用户id
*/
private Long userId;
/**
* 用户图标
*/
@TableField(exist = false)
private String icon;
/**
* 用户姓名
*/
@TableField(exist = false)
private String name;
/**
* 是否点赞过了
*/
@TableField(exist = false)
private Boolean isLike;
逻辑层代码 (没有实现排行榜时的代码.使用Set)
//打开一个博客时调用该方法,可以进行是否高亮展示
@Override
public Result queryBlogById(Long id) {
//查询blog
Blog blog = getById(id);
if(blog==null){
return Result.fail("笔记不存在");
}
queryBlogUser(blog);
//查询blog是否被点赞,返回给前端让前端进行判断是否高亮显示
isBlogLiked(blog);
return Result.ok(blog);
}
//给类中判断是否已经点赞的字段isLike赋值
private void isBlogLiked(Blog blog) {
//1.获取登录用户
Long userId = UserHolder.getUser().getId();
//2.判断当前登录用户是否已经点赞
String key = "blog:liked" + blog.getId();
Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, userId.toString());
blog.setIsLike(BooleanUtil.isTrue(isMember));
}
//通过另外一张表,给类中对应数据库表中不存在的两个字段赋值
private void queryBlogUser(Blog blog) {
//查询blog有关的用户
Long userId = blog.getUserId();
User user = userService.getById(userId);
blog.setName(user.getNickName());
blog.setIcon(user.getIcon());
}
//点击点赞按钮时,执行该方法,进行点赞与取消点赞
@Override
public Result likeBlog(Long id) {
//1.获取登录用户
Long userId = UserHolder.getUser().getId();
//2.判断当前登录用户是否已经点赞
String key = "blog:liked" + id;
Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, userId.toString());
if(BooleanUtil.isFalse(isMember)){
//3.如果未点赞,可以点赞
//3.1 数据库点赞数加1
boolean isSuccess = this.update().setSql("liked = liked + 1").eq("id", id).update();
//3.2 保存用户到Redis的set集合
if(isSuccess){
stringRedisTemplate.opsForSet().add(key, userId.toString());
}
}else {
//4.如果已点赞取消点赞
//4.1数据库点赞数-1
boolean isSuccess = this.update().setSql("liked = liked - 1").eq("id", id).update();
//4.2 把用户从Redis的set集合中移除
if(isSuccess){
stringRedisTemplate.opsForSet().remove(key, userId.toString());
}
}
return Result.ok();
}
逻辑层代码(实现排行榜时的代码 ,使用了SortedSet)
//打开一个博客时调用该方法,可以进行是否高亮展示
@Override
public Result queryBlogById(Long id) {
//查询blog
Blog blog = getById(id);
if(blog==null){
return Result.fail("笔记不存在");
}
queryBlogUser(blog);
//查询blog是否被点赞,返回给前端让前端进行判断是否高亮显示
isBlogLiked(blog);
return Result.ok(blog);
}
//给类中判断是否已经点赞的字段isLike赋值
private void isBlogLiked(Blog blog) {
//1.获取登录用户
UserDTO user = UserHolder.getUser();
if(user==null){
//用户未登录,无需查询是否点赞
return;
}
Long userId = user.getId();
//2.判断当前登录用户是否已经点赞
String key = BLOG_LIKED_KEY + blog.getId();
Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
blog.setIsLike(score != null);
}
//通过另外一张表,给类中对应数据库表中不存在的两个字段赋值
private void queryBlogUser(Blog blog) {
//查询blog有关的用户
Long userId = blog.getUserId();
User user = userService.getById(userId);
blog.setName(user.getNickName());
blog.setIcon(user.getIcon());
}
//点击点赞按钮时,执行该方法,进行点赞与取消点赞
@Override
public Result likeBlog(Long id) {
//1.获取登录用户
Long userId = UserHolder.getUser().getId();
//2.判断当前登录用户是否已经点赞
String key = BLOG_LIKED_KEY + id;
//查询出的score为空,就为未点赞
Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
if(score == null){
//3.如果未点赞,可以点赞
//3.1 数据库点赞数加1
boolean isSuccess = this.update().setSql("liked = liked + 1").eq("id", id).update();
//3.2 保存用户到Redis的SortedSet集合 zadd key value score
if(isSuccess){
stringRedisTemplate.opsForZSet().add(key, userId.toString(), System.currentTimeMillis());
}
}else {
//4.如果已点赞取消点赞
//4.1数据库点赞数-1
boolean isSuccess = this.update().setSql("liked = liked - 1").eq("id", id).update();
//4.2 把用户从Redis的set集合中移除
if(isSuccess){
stringRedisTemplate.opsForZSet().remove(key, userId.toString());
}
}
return Result.ok();
}
@Override
public Result queryBlogByLikes(Long id) {
// 1.查询top5的点赞用户 zrange key 0 4
String key = BLOG_LIKED_KEY + id;
Set<String> top5 = stringRedisTemplate.opsForZSet().range(key, 0, 4);
if(top5 == null || top5.isEmpty()){
return Result.ok(Collections.emptyList());
}
//2.解析出其中的用户id
List<Long> ids = top5.stream().map(Long::valueOf).collect(Collectors.toList());
String idStr = StrUtil.join(",", ids);
//3. 根据用户id查询用户 where id IN ( 5 , 1 ) order by field (id , 5 ,1) 用于指定id先执行哪个
List<UserDTO> userDTOS = userService.query()
.in("id",ids).last("ORDER BY FIELD(id," + idStr + ")").list()
.stream()
.map(user -> BeanUtil.copyProperties(user, UserDTO.class))
.collect(Collectors.toList());
//4.返回
return Result.ok(userDTOS);
}