From 30b8b59860b76e1fc9c7773b0b3fb932de309bd5 Mon Sep 17 00:00:00 2001 From: glx <783262171@qq.com> Date: Wed, 6 May 2026 11:26:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=AC=94=E8=AE=B0=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MiniStrayAnimalNoteCommentMapper.java | 10 +- .../mapper/MiniStrayAnimalNoteMapper.java | 8 +- .../model/vo/StrayAnimalNoteCommentVO.java | 4 +- .../StrayAnimalNoteCommentServiceImpl.java | 141 ++++++++++-------- .../mini/MiniStrayAnimalNoteCommentMapper.xml | 27 ++++ .../mapper/mini/MiniStrayAnimalNoteMapper.xml | 8 + 6 files changed, 127 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java index 32a6184..a06dc22 100644 --- a/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java +++ b/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 { + /** + * 根据UUID查询笔记ID + */ + Long selectIdByUuid(@Param("uuid") String uuid); + } diff --git a/src/main/java/com/youlai/boot/mini/model/vo/StrayAnimalNoteCommentVO.java b/src/main/java/com/youlai/boot/mini/model/vo/StrayAnimalNoteCommentVO.java index 2e0284e..639f0b1 100644 --- a/src/main/java/com/youlai/boot/mini/model/vo/StrayAnimalNoteCommentVO.java +++ b/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; diff --git a/src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java b/src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java index 75cc0a2..da950ed 100644 --- a/src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalNoteCommentServiceImpl.java +++ b/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 noteQueryWrapper = new LambdaQueryWrapper<>(); - noteQueryWrapper.eq(MiniStrayAnimalNote::getUuid, addCommentForm.getStrayAnimalNoteUuId()) - .eq(MiniStrayAnimalNote::getDeleted, 0) - .last("LIMIT 1"); - MiniStrayAnimalNote note = miniStrayAnimalNoteMapper.selectOne(noteQueryWrapper); - - // 获取父评论信息 - Long parentCommentId = null; - MiniStrayAnimalNoteComment parentComment=null; - if (!addCommentForm.getParentUuId().equals("0")){ - LambdaQueryWrapper commentQueryWrapper = new LambdaQueryWrapper<>(); - commentQueryWrapper.eq(MiniStrayAnimalNoteComment::getUuid, addCommentForm.getParentUuId()) - .eq(MiniStrayAnimalNoteComment::getDeleted, 0) - .last("LIMIT 1"); - parentComment = baseMapper.selectOne(commentQueryWrapper); + // 1. 根据笔记UUID查询ID + Long noteId = miniStrayAnimalNoteMapper.selectIdByUuid(addCommentForm.getStrayAnimalNoteUuId()); + if (noteId == null) { + throw new MsgException("笔记不存在或已删除"); + } + + // 2. 获取父评论信息 + Long parentCommentId = 0L; + MiniStrayAnimalNoteComment parentComment = null; + 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[] parts = ipArr.split("\\|"); - String provinceShortName = parts[2].replaceAll("(省|市|自治区|壮族自治区|回族自治区|维吾尔自治区)$", ""); + String provinceShortName = "未知"; + try { + String[] parts = ipArr.split("\\|"); + 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 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); + + // 设置回复用户信息 + 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()); + } + } - SysUser replyUser = userMapper.selectById(animalNoteComment.getReplyToUserId()); - animalNoteCommentVO.setReplyToUserId(replyUser.getUuid()); - animalNoteCommentVO.setLikeCount(animalNoteComment.getLikeCount()); animalNoteCommentVO.setCreatedAt(animalNoteComment.getCreateTimestamp()); -// animalNoteCommentVO.setIsLiked(); - + animalNoteCommentVO.setIsLiked(false); + animalNoteCommentVO.setLikeCount(0); + animalNoteCommentVO.setProvince(animalNoteComment.getProvince()); - // 更新笔记评论数 - baseMapper.incrementCommentCount(note.getId()); + // 8. 更新笔记评论数 + baseMapper.incrementCommentCount(noteId); return animalNoteCommentVO; } diff --git a/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml index dca00b1..b634a69 100644 --- a/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml +++ b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml @@ -39,4 +39,31 @@ ) + + + + + + + + + diff --git a/src/main/resources/mapper/mini/MiniStrayAnimalNoteMapper.xml b/src/main/resources/mapper/mini/MiniStrayAnimalNoteMapper.xml index 1c0a5fd..936fd8f 100644 --- a/src/main/resources/mapper/mini/MiniStrayAnimalNoteMapper.xml +++ b/src/main/resources/mapper/mini/MiniStrayAnimalNoteMapper.xml @@ -5,5 +5,13 @@ + +