|
|
|
@ -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; |
|
|
|
} |
|
|
|
|