Browse Source

优化笔记评论接口

glx_phase2
glx 3 weeks ago
parent
commit
30b8b59860
  1. 10
      src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java
  2. 8
      src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteMapper.java
  3. 4
      src/main/java/com/youlai/boot/mini/model/vo/StrayAnimalNoteCommentVO.java
  4. 137
      src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java
  5. 27
      src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml
  6. 8
      src/main/resources/mapper/mini/MiniStrayAnimalNoteMapper.xml

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

@ -1,8 +1,8 @@
package com.youlai.boot.mini.mapper;
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jakarta.validation.constraints.NotNull;
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment;
import org.apache.ibatis.annotations.Param;
/**
* 流浪动物笔记评论表 Mapper 接口
@ -16,4 +16,10 @@ public interface MiniStrayAnimalNoteCommentMapper extends BaseMapper<MiniStrayAn
int batchCalibrateAllCommentCounts();
Long selectIdByUuid(@Param("uuid") String uuid);
MiniStrayAnimalNoteComment selectParentCommentInfoByUuid(@Param("uuid") String uuid);
String selectUuidById(@Param("id") Long id);
}

8
src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteMapper.java

@ -1,7 +1,8 @@
package com.youlai.boot.mini.mapper;
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNote;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNote;
import org.apache.ibatis.annotations.Param;
/**
* 流浪信息笔记 Mapper 接口
@ -11,4 +12,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/
public interface MiniStrayAnimalNoteMapper extends BaseMapper<MiniStrayAnimalNote> {
/**
* 根据UUID查询笔记ID
*/
Long selectIdByUuid(@Param("uuid") String uuid);
}

4
src/main/java/com/youlai/boot/mini/model/vo/StrayAnimalNoteCommentVO.java

@ -38,7 +38,7 @@ public class StrayAnimalNoteCommentVO {
private String replyToAvatarUrl;
@Schema(description = "被回复用户昵称")
private String replyToNickname = "用户已注销";
private String replyToNickname;
@Schema(description = "点赞数")
private Integer likeCount;
@ -47,7 +47,7 @@ public class StrayAnimalNoteCommentVO {
private Long createdAt;
@Schema(description = "当前用户是否点赞评论,true点赞,false未点赞")
private Boolean isLiked = false;
private Boolean isLiked;
@Schema(description = "所在省")
private String province;

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

@ -1,6 +1,7 @@
package com.youlai.boot.mini.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.common.exception.MsgException;
import com.youlai.boot.common.util.HttpContext;
@ -18,13 +19,10 @@ import com.youlai.boot.system.mapper.UserMapper;
import com.youlai.boot.system.model.entity.SysUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.UUID;
@Slf4j
@Service
@ -35,114 +33,125 @@ public class StrayAnimalNoteCommentServiceImpl extends ServiceImpl<MiniStrayAnim
private final MiniStrayAnimalNoteMapper miniStrayAnimalNoteMapper;
private final UserMapper userMapper;
@Transactional(rollbackFor = Exception.class)
@Override
public StrayAnimalNoteCommentVO addNoteComment(StrayAnimalNoteCommentForm formData) {
// 1. 保存用户评论信息
// 1. 登录校验
Long userId = SecurityUtils.getUserId();
if (userId == null) {
throw new MsgException("请先登录");
}
// 2. 保存用户评论信息
StrayAnimalNoteCommentVO vo = saveCommentInfo(formData, userId);
//2.评论审核
// commentAudit(animalNoteCommentVO.getId(),animalNoteCommentVO.getContent());
return vo;
}
private StrayAnimalNoteCommentVO saveCommentInfo(StrayAnimalNoteCommentForm addCommentForm, Long appUserId) {
// 获取笔记信息
LambdaQueryWrapper<MiniStrayAnimalNote> noteQueryWrapper = new LambdaQueryWrapper<>();
noteQueryWrapper.eq(MiniStrayAnimalNote::getUuid, addCommentForm.getStrayAnimalNoteUuId())
.eq(MiniStrayAnimalNote::getDeleted, 0)
.last("LIMIT 1");
MiniStrayAnimalNote note = miniStrayAnimalNoteMapper.selectOne(noteQueryWrapper);
// 获取父评论信息
Long parentCommentId = null;
// 1. 根据笔记UUID查询ID
Long noteId = miniStrayAnimalNoteMapper.selectIdByUuid(addCommentForm.getStrayAnimalNoteUuId());
if (noteId == null) {
throw new MsgException("笔记不存在或已删除");
}
// 2. 获取父评论信息
Long parentCommentId = 0L;
MiniStrayAnimalNoteComment parentComment = null;
if (!addCommentForm.getParentUuId().equals("0")){
LambdaQueryWrapper<MiniStrayAnimalNoteComment> commentQueryWrapper = new LambdaQueryWrapper<>();
commentQueryWrapper.eq(MiniStrayAnimalNoteComment::getUuid, addCommentForm.getParentUuId())
.eq(MiniStrayAnimalNoteComment::getDeleted, 0)
.last("LIMIT 1");
parentComment = baseMapper.selectOne(commentQueryWrapper);
SysUser replyToUser = null;
if (!"0".equals(addCommentForm.getParentUuId())) {
// 查询父评论
parentComment = baseMapper.selectParentCommentInfoByUuid(addCommentForm.getParentUuId());
if (parentComment == null) {
throw new MsgException("父评论已消失");
}
// 父评论归属校验
if (!parentComment.getNoteId().equals(noteId)) {
throw new MsgException("父评论不属于当前笔记");
}
parentCommentId = parentComment.getId();
} else if(addCommentForm.getParentUuId().equals("0")) {
parentCommentId = 0L;
// 提前查询回复用户信息,避免后续重复查询
replyToUser = userMapper.selectById(parentComment.getMiniUserId());
}
//获取用户ip位置
// 3. 获取用户ip位置
String userIp = IPUtils.getIpAddr(HttpContext.getRequest());
String ipArr = IPUtils.getRegion(userIp);
String provinceShortName = "未知";
try {
String[] parts = ipArr.split("\\|");
String provinceShortName = parts[2].replaceAll("(省|市|自治区|壮族自治区|回族自治区|维吾尔自治区)$", "");
if (parts.length >= 3) {
provinceShortName = parts[2].replaceAll("(省|市|自治区|壮族自治区|回族自治区|维吾尔自治区)$", "");
}
} catch (Exception e) {
log.warn("IP地址解析失败,ip:{}, 错误信息:{}", userIp, e.getMessage());
}
// 4. 构建评论实体
long currentTimeUnix = System.currentTimeMillis();
MiniStrayAnimalNoteComment animalNoteComment = new MiniStrayAnimalNoteComment();
animalNoteComment.setNoteId(note.getId());
animalNoteComment.setUuid(UUID.randomUUID().toString());
animalNoteComment.setNoteId(noteId);
animalNoteComment.setUuid(IdWorker.get32UUID());
animalNoteComment.setMiniUserId(appUserId);
animalNoteComment.setContent(addCommentForm.getContent());
animalNoteComment.setParentId(parentCommentId);
animalNoteComment.setCreateBy(appUserId);
animalNoteComment.setCreateTimestamp(currentTimeUnix);
animalNoteComment.setCreateTime(new Date(currentTimeUnix));
animalNoteComment.setProvince(provinceShortName);
//包装 VO 返回
StrayAnimalNoteCommentVO animalNoteCommentVO = new StrayAnimalNoteCommentVO();
SysUser appUser = userMapper.selectById(appUserId);
animalNoteCommentVO.setNickname(appUser.getNickname());
animalNoteCommentVO.setAvatarUrl(appUser.getAvatar());
// 处理rootId
if (animalNoteComment.getParentId() == 0) {
animalNoteComment.setRootId(0L); //先设置为0L,后面处理
// 一级评论(下面插入后设置rootId=id)
// 5. 处理rootId
if (parentCommentId == 0) {
animalNoteComment.setRootId(0L); // 先设置为0L,插入后回写
} else {
// 二级评论
animalNoteComment.setReplyToUserId(parentComment.getMiniUserId());// 设置被回复的用户ID为父评论的用户ID
animalNoteComment.setReplyToUserId(parentComment.getMiniUserId());
animalNoteComment.setRootId(parentComment.getRootId());
// 设置被回复用户信息
SysUser replyToUser = userMapper.selectById(parentComment.getMiniUserId());
animalNoteCommentVO.setReplyToNickname(replyToUser.getNickname());
animalNoteCommentVO.setReplyToAvatarUrl(replyToUser.getAvatar());
}
// 6. 插入评论
baseMapper.insert(animalNoteComment);
// 如果是顶级评论 仅设置 rootId
if (animalNoteComment.getParentId() == 0) {
// 顶级评论回写rootId为自身ID
if (parentCommentId == 0) {
animalNoteComment.setRootId(animalNoteComment.getId());
baseMapper.updateById(animalNoteComment);
}
//复制到 VO
// BeanUtils.copyProperties(animalNoteComment, animalNoteCommentVO);
animalNoteCommentVO.setNoteUuId(note.getUuid());
// 7. 构建返回VO
StrayAnimalNoteCommentVO animalNoteCommentVO = new StrayAnimalNoteCommentVO();
SysUser appUser = userMapper.selectById(appUserId);
animalNoteCommentVO.setNickname(appUser.getNickname());
animalNoteCommentVO.setAvatarUrl(appUser.getAvatar());
animalNoteCommentVO.setNoteUuId(addCommentForm.getStrayAnimalNoteUuId());
animalNoteCommentVO.setUuid(animalNoteComment.getUuid());
animalNoteCommentVO.setAppUserId(appUser.getUuid());
animalNoteCommentVO.setContent(animalNoteComment.getContent());
animalNoteCommentVO.setParentUuId(addCommentForm.getParentUuId());
LambdaQueryWrapper<MiniStrayAnimalNoteComment> newCommentQueryWrapper = new LambdaQueryWrapper<>();
newCommentQueryWrapper.eq(MiniStrayAnimalNoteComment::getId, animalNoteComment.getRootId())
.eq(MiniStrayAnimalNoteComment::getDeleted, 0)
.last("LIMIT 1");
animalNoteCommentVO.setRootId(baseMapper.selectOne(newCommentQueryWrapper).getUuid());
// 根据rootId查询UUID
String rootUuid = baseMapper.selectUuidById(animalNoteComment.getRootId());
if (rootUuid == null) {
throw new MsgException("评论所属楼层已删除");
}
animalNoteCommentVO.setRootId(rootUuid);
SysUser replyUser = userMapper.selectById(animalNoteComment.getReplyToUserId());
animalNoteCommentVO.setReplyToUserId(replyUser.getUuid());
animalNoteCommentVO.setLikeCount(animalNoteComment.getLikeCount());
animalNoteCommentVO.setCreatedAt(animalNoteComment.getCreateTimestamp());
// animalNoteCommentVO.setIsLiked();
// 设置回复用户信息
if (animalNoteComment.getReplyToUserId() != null && replyToUser != null) {
if (replyToUser.getIsDeleted() == 1) {
animalNoteCommentVO.setReplyToNickname("用户已注销");
} else {
animalNoteCommentVO.setReplyToUserId(replyToUser.getUuid());
animalNoteCommentVO.setReplyToNickname(replyToUser.getNickname());
animalNoteCommentVO.setReplyToAvatarUrl(replyToUser.getAvatar());
}
}
animalNoteCommentVO.setCreatedAt(animalNoteComment.getCreateTimestamp());
animalNoteCommentVO.setIsLiked(false);
animalNoteCommentVO.setLikeCount(0);
animalNoteCommentVO.setProvince(animalNoteComment.getProvince());
// 更新笔记评论数
baseMapper.incrementCommentCount(note.getId());
// 8. 更新笔记评论数
baseMapper.incrementCommentCount(noteId);
return animalNoteCommentVO;
}

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

@ -39,4 +39,31 @@
)
</update>
<!-- 根据UUID查询评论ID -->
<select id="selectIdByUuid" resultType="java.lang.Long">
SELECT id
FROM mini_stray_animal_note_comment
WHERE uuid = #{uuid}
AND deleted = 0
LIMIT 1
</select>
<!-- 根据UUID查询父评论必要字段 -->
<select id="selectParentCommentInfoByUuid" resultType="com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment">
SELECT id, note_id, root_id, mini_user_id
FROM mini_stray_animal_note_comment
WHERE uuid = #{uuid}
AND deleted = 0
LIMIT 1
</select>
<!-- 根据ID查询评论UUID -->
<select id="selectUuidById" resultType="java.lang.String">
SELECT uuid
FROM mini_stray_animal_note_comment
WHERE id = #{id}
AND deleted = 0
LIMIT 1
</select>
</mapper>

8
src/main/resources/mapper/mini/MiniStrayAnimalNoteMapper.xml

@ -5,5 +5,13 @@
<mapper namespace="com.youlai.boot.mini.mapper.MiniStrayAnimalNoteMapper">
<!-- 根据UUID查询笔记ID -->
<select id="selectIdByUuid" resultType="java.lang.Long">
SELECT id
FROM mini_stray_animal_note
WHERE uuid = #{uuid}
AND deleted = 0
LIMIT 1
</select>
</mapper>

Loading…
Cancel
Save