Browse Source

增加二级评论查询接口

glx_phase2
glx 3 weeks ago
parent
commit
f9313761c3
  1. 13
      src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java
  2. 3
      src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java
  3. 6
      src/main/java/com/youlai/boot/mini/model/query/AnimalNoteFirstLevelCommentQueryParam.java
  4. 21
      src/main/java/com/youlai/boot/mini/model/query/AnimalNoteSecondLevelCommentQueryParam.java
  5. 18
      src/main/java/com/youlai/boot/mini/model/vo/AnimalNoteSecondLevelCommentVO.java
  6. 4
      src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java
  7. 42
      src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java
  8. 31
      src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml

13
src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java

@ -10,7 +10,9 @@ import com.youlai.boot.framework.security.util.SecurityUtils;
import com.youlai.boot.mini.model.form.DeleteStrayAnimalNoteCommentForm;
import com.youlai.boot.mini.model.form.StrayAnimalNoteCommentForm;
import com.youlai.boot.mini.model.query.AnimalNoteFirstLevelCommentQueryParam;
import com.youlai.boot.mini.model.query.AnimalNoteSecondLevelCommentQueryParam;
import com.youlai.boot.mini.model.vo.AnimalNoteFirstLevelCommentVO;
import com.youlai.boot.mini.model.vo.AnimalNoteSecondLevelCommentVO;
import com.youlai.boot.mini.model.vo.StrayAnimalNoteCommentVO;
import com.youlai.boot.mini.service.StrayAnimalNoteCommentService;
import io.swagger.v3.oas.annotations.Operation;
@ -63,4 +65,15 @@ public class StrayAnimalNoteCommentController {
return Result.success(result);
}
@Operation(summary = "分页查询笔记二级评论列表接口")
@PostMapping(value = "secondLevel/list")
@Log(module = LogModuleEnum.STRAY_ANIMAL_NOTE_COMMENT, value = ActionTypeEnum.LIST)
public Result<IPage<AnimalNoteSecondLevelCommentVO>> getSecondLevelCommentList(
@Valid @RequestBody AnimalNoteSecondLevelCommentQueryParam queryParam
) {
Long userId = SecurityUtils.getUserId();
IPage<AnimalNoteSecondLevelCommentVO> result = strayAnimalNoteCommentService.getSecondLevelCommentList(queryParam, userId);
return Result.success(result);
}
}

3
src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment;
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteCommentLike;
import com.youlai.boot.mini.model.query.AnimalNoteFirstLevelCommentQueryParam;
import com.youlai.boot.mini.model.query.AnimalNoteSecondLevelCommentQueryParam;
import com.youlai.boot.mini.model.vo.AnimalNoteFirstLevelCommentVO;
import com.youlai.boot.mini.model.vo.AnimalNoteSecondLevelCommentVO;
import org.apache.ibatis.annotations.Param;
@ -32,6 +33,8 @@ public interface MiniStrayAnimalNoteCommentMapper extends BaseMapper<MiniStrayAn
IPage<AnimalNoteFirstLevelCommentVO> getFirstLevelComment(IPage<AnimalNoteFirstLevelCommentVO> page, @Param("query") AnimalNoteFirstLevelCommentQueryParam query);
IPage<AnimalNoteSecondLevelCommentVO> getSecondLevelComment(IPage<AnimalNoteSecondLevelCommentVO> page, @Param("query") AnimalNoteSecondLevelCommentQueryParam query);
List<MiniStrayAnimalNoteCommentLike> batchGetUserCommentLikes(@Param("noteCommentIds") List<Long> noteCommentIds, @Param("appUserId") Long appUserId);
}

6
src/main/java/com/youlai/boot/mini/model/query/AnimalNoteFirstLevelCommentQueryParam.java

@ -12,7 +12,7 @@ import lombok.EqualsAndHashCode;
@Schema(description = "动物笔记一级评论分页查询参数")
public class AnimalNoteFirstLevelCommentQueryParam extends BaseQuery {
@NotBlank(message = "笔记UUID不能为空")
@Schema(description = "动物笔记UUID", requiredMode = Schema.RequiredMode.REQUIRED, example = "0677d62d63ec693bf1bd6dab8a877dc1")
private String noteUUId;
@NotBlank(message = "笔记uuid不能为空")
@Schema(description = "动物笔记uuid", requiredMode = Schema.RequiredMode.REQUIRED, example = "0677d62d63ec693bf1bd6dab8a877dc1")
private String noteUuId;
}

21
src/main/java/com/youlai/boot/mini/model/query/AnimalNoteSecondLevelCommentQueryParam.java

@ -0,0 +1,21 @@
package com.youlai.boot.mini.model.query;
import com.youlai.boot.common.base.BaseQuery;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "动物笔记二级评论分页查询参数")
public class AnimalNoteSecondLevelCommentQueryParam extends BaseQuery {
@NotBlank(message = "笔记uuid不能为空")
@Schema(description = "动物笔记uuid", requiredMode = Schema.RequiredMode.REQUIRED, example = "0677d62d63ec693bf1bd6dab8a877dc1")
private String noteUuId;
@NotBlank(message = "一级评论uuid不能为空")
@Schema(description = "一级评论uuid", requiredMode = Schema.RequiredMode.REQUIRED, example = "a1b2c3d4e5f6g7h8i9j0")
private String rootUuid;
}

18
src/main/java/com/youlai/boot/mini/model/vo/AnimalNoteSecondLevelCommentVO.java

@ -1,6 +1,7 @@
package com.youlai.boot.mini.model.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@ -17,8 +18,12 @@ public class AnimalNoteSecondLevelCommentVO {
@Schema(description = "动物笔记评论uuid")
private String uuid;
@Schema(description = "评论用户uuid")
private String miniUserUuId;
@JsonIgnore
@Schema(hidden = true)
private Long commentId;
@Schema(description = "评论用户UUID")
private String appUserId;
@Schema(description = "评论用户头像")
private String avatarUrl;
@ -29,11 +34,8 @@ public class AnimalNoteSecondLevelCommentVO {
@Schema(description = "评论内容")
private String content;
@Schema(description = "父评论ID,0为一级评论")
private Long parentId;
@Schema(description = "根评论ID,一级评论为自身ID,二级及以上为所属一级评论ID")
private Long rootId;
@Schema(description = "父评论UUID")
private String parentUuId;
@Schema(description = "被回复的用户ID")
private Long replyToUserId;
@ -45,7 +47,7 @@ public class AnimalNoteSecondLevelCommentVO {
private String replyToNickname;
@Schema(description = "点赞数")
private Integer likeCount;
private Long likeCount;
@Schema(description = "创建时间戳")
private Long createTimestamp;

4
src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java

@ -6,7 +6,9 @@ import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment;
import com.youlai.boot.mini.model.form.DeleteStrayAnimalNoteCommentForm;
import com.youlai.boot.mini.model.form.StrayAnimalNoteCommentForm;
import com.youlai.boot.mini.model.query.AnimalNoteFirstLevelCommentQueryParam;
import com.youlai.boot.mini.model.query.AnimalNoteSecondLevelCommentQueryParam;
import com.youlai.boot.mini.model.vo.AnimalNoteFirstLevelCommentVO;
import com.youlai.boot.mini.model.vo.AnimalNoteSecondLevelCommentVO;
import com.youlai.boot.mini.model.vo.StrayAnimalNoteCommentVO;
import jakarta.validation.Valid;
@ -18,4 +20,6 @@ public interface StrayAnimalNoteCommentService extends IService<MiniStrayAnimalN
IPage<AnimalNoteFirstLevelCommentVO> getFirstLevelCommentList(AnimalNoteFirstLevelCommentQueryParam queryParam, Long userId);
IPage<AnimalNoteSecondLevelCommentVO> getSecondLevelCommentList(AnimalNoteSecondLevelCommentQueryParam queryParam, Long userId);
}

42
src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java

@ -18,6 +18,7 @@ import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteCommentLike;
import com.youlai.boot.mini.model.form.DeleteStrayAnimalNoteCommentForm;
import com.youlai.boot.mini.model.form.StrayAnimalNoteCommentForm;
import com.youlai.boot.mini.model.query.AnimalNoteFirstLevelCommentQueryParam;
import com.youlai.boot.mini.model.query.AnimalNoteSecondLevelCommentQueryParam;
import com.youlai.boot.mini.model.vo.AnimalNoteFirstLevelCommentVO;
import com.youlai.boot.mini.model.vo.AnimalNoteSecondLevelCommentVO;
import com.youlai.boot.mini.model.vo.StrayAnimalNoteCommentVO;
@ -239,4 +240,45 @@ public class StrayAnimalNoteCommentServiceImpl extends ServiceImpl<MiniStrayAnim
return result;
}
@Override
@Transactional(readOnly = true, rollbackFor = Exception.class)
public IPage<AnimalNoteSecondLevelCommentVO> getSecondLevelCommentList(AnimalNoteSecondLevelCommentQueryParam queryParam, Long userId) {
// 1. 构造分页参数,默认值兼容
Page<AnimalNoteSecondLevelCommentVO> page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize());
// 2. 查询二级评论分页
IPage<AnimalNoteSecondLevelCommentVO> result = baseMapper.getSecondLevelComment(page, queryParam);
List<AnimalNoteSecondLevelCommentVO> commentList = result.getRecords();
if (commentList.isEmpty() || userId == null) {
return result;
}
// 3. 提取二级评论内部主键ID(用于查询点赞状态)
List<Long> secondLevelCommentIds = commentList.stream()
.map(AnimalNoteSecondLevelCommentVO::getCommentId)
.collect(Collectors.toList());
// 4. 批量查询登录用户的点赞状态
Map<Long, Boolean> likeStatusMap = Collections.emptyMap();
try {
List<MiniStrayAnimalNoteCommentLike> likeList = baseMapper.batchGetUserCommentLikes(secondLevelCommentIds, userId);
likeStatusMap = likeList.stream()
.collect(Collectors.toMap(
MiniStrayAnimalNoteCommentLike::getNoteCommentId,
like -> Boolean.TRUE,
(v1, v2) -> v1
));
} catch (Exception e) {
log.error("批量查询二级评论点赞状态失败,用户ID:{}", userId, e);
}
// 5. 给每个评论设置点赞状态
for (AnimalNoteSecondLevelCommentVO comment : commentList) {
Boolean isLiked = likeStatusMap.get(comment.getCommentId());
comment.setIsLiked(Boolean.TRUE.equals(isLiked));
}
return result;
}
}

31
src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml

@ -110,7 +110,7 @@
AND sc.is_deleted = 0
) AS secondLevelCount
FROM mini_stray_animal_note_comment sanc
INNER JOIN mini_stray_animal_note note ON sanc.note_id = note.id AND note.uuid = #{query.noteUUId} AND note.is_deleted = 0
INNER JOIN mini_stray_animal_note note ON sanc.note_id = note.id AND note.uuid = #{query.noteUuId} AND note.is_deleted = 0
LEFT JOIN sys_user au ON sanc.mini_user_id = au.id AND au.is_deleted = 0
WHERE sanc.parent_id = 0
AND sanc.is_deleted = 0
@ -118,6 +118,35 @@
ORDER BY sanc.like_count DESC, sanc.create_timestamp DESC
</select>
<!-- 分页查询二级评论列表 -->
<select id="getSecondLevelComment" resultType="com.youlai.boot.mini.model.vo.AnimalNoteSecondLevelCommentVO">
SELECT
note.uuid AS noteUuId,
sanc.id AS commentId,
sanc.uuid AS uuid,
au.uuid AS appUserId,
au.avatar AS avatarUrl,
au.nickname AS nickname,
sanc.content AS content,
parent_comment.uuid AS parentUuId,
sanc.reply_to_user_id AS replyToUserId,
rau.avatar AS replyToAvatarUrl,
rau.nickname AS replyToNickname,
sanc.like_count AS likeCount,
sanc.create_timestamp AS createTimestamp,
sanc.create_time AS createTime,
sanc.province AS province
FROM mini_stray_animal_note_comment sanc
INNER JOIN mini_stray_animal_note note ON sanc.note_id = note.id AND note.uuid = #{query.noteUuId} AND note.is_deleted = 0
LEFT JOIN mini_stray_animal_note_comment parent_comment ON sanc.parent_id = parent_comment.id AND parent_comment.is_deleted = 0
LEFT JOIN sys_user au ON sanc.mini_user_id = au.id AND au.is_deleted = 0
LEFT JOIN sys_user rau ON sanc.reply_to_user_id = rau.id AND rau.is_deleted = 0
WHERE sanc.parent_id != 0
AND sanc.is_deleted = 0
AND sanc.root_id = (SELECT id FROM mini_stray_animal_note_comment WHERE uuid = #{query.rootUuid} AND is_deleted = 0 AND parent_id = 0)
ORDER BY sanc.create_timestamp ASC
</select>
<!-- 批量查询用户对指定评论的点赞状态 -->
<select id="batchGetUserCommentLikes" resultType="com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteCommentLike">
SELECT

Loading…
Cancel
Save