|
|
@ -1,7 +1,9 @@ |
|
|
package com.youlai.boot.mini.service.impl; |
|
|
package com.youlai.boot.mini.service.impl; |
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker; |
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker; |
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.youlai.boot.common.exception.MsgException; |
|
|
import com.youlai.boot.common.exception.MsgException; |
|
|
import com.youlai.boot.common.util.HttpContext; |
|
|
import com.youlai.boot.common.util.HttpContext; |
|
|
@ -12,7 +14,12 @@ import com.youlai.boot.mini.mapper.MiniStrayAnimalNoteCommentMapper; |
|
|
import com.youlai.boot.mini.mapper.MiniStrayAnimalNoteMapper; |
|
|
import com.youlai.boot.mini.mapper.MiniStrayAnimalNoteMapper; |
|
|
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNote; |
|
|
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNote; |
|
|
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment; |
|
|
import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment; |
|
|
|
|
|
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.form.StrayAnimalNoteCommentForm; |
|
|
|
|
|
import com.youlai.boot.mini.model.query.AnimalNoteFirstLevelCommentQueryParam; |
|
|
|
|
|
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.model.vo.StrayAnimalNoteCommentVO; |
|
|
import com.youlai.boot.mini.service.StrayAnimalNoteCommentService; |
|
|
import com.youlai.boot.mini.service.StrayAnimalNoteCommentService; |
|
|
import com.youlai.boot.system.mapper.UserMapper; |
|
|
import com.youlai.boot.system.mapper.UserMapper; |
|
|
@ -22,7 +29,9 @@ import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
import java.util.Date; |
|
|
import java.util.*; |
|
|
|
|
|
import java.util.function.Function; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@Service |
|
|
@Service |
|
|
@ -47,6 +56,39 @@ public class StrayAnimalNoteCommentServiceImpl extends ServiceImpl<MiniStrayAnim |
|
|
return vo; |
|
|
return vo; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
|
public void deleteNoteComment(DeleteStrayAnimalNoteCommentForm formData, Long userId) { |
|
|
|
|
|
// 1. 执行原子删除(带权限校验、存在性校验)
|
|
|
|
|
|
int affectRows = baseMapper.deleteCommentWithPermissionCheck( |
|
|
|
|
|
formData.getNoteUuid(), |
|
|
|
|
|
formData.getCommentUuid(), |
|
|
|
|
|
userId |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// 2. 删除成功,扣减评论数
|
|
|
|
|
|
if (affectRows == 1) { |
|
|
|
|
|
Long noteId = miniStrayAnimalNoteMapper.selectIdByUuid(formData.getNoteUuid()); |
|
|
|
|
|
baseMapper.decrementCommentCount(noteId); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 3. 删除失败,查询具体错误原因(仅失败场景执行)
|
|
|
|
|
|
Long noteId = miniStrayAnimalNoteMapper.selectIdByUuid(formData.getNoteUuid()); |
|
|
|
|
|
if (noteId == null) { |
|
|
|
|
|
throw new MsgException("笔记不存在或已删除"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 查询评论是否存在且属于该笔记
|
|
|
|
|
|
MiniStrayAnimalNoteComment comment = baseMapper.selectParentCommentInfoByUuid(formData.getCommentUuid()); |
|
|
|
|
|
if (comment == null || !comment.getNoteId().equals(noteId)) { |
|
|
|
|
|
throw new MsgException("评论不存在或已删除"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 剩余情况:权限不足
|
|
|
|
|
|
throw new MsgException("无权限删除该评论"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private StrayAnimalNoteCommentVO saveCommentInfo(StrayAnimalNoteCommentForm addCommentForm, Long appUserId) { |
|
|
private StrayAnimalNoteCommentVO saveCommentInfo(StrayAnimalNoteCommentForm addCommentForm, Long appUserId) { |
|
|
// 1. 根据笔记UUID查询ID
|
|
|
// 1. 根据笔记UUID查询ID
|
|
|
Long noteId = miniStrayAnimalNoteMapper.selectIdByUuid(addCommentForm.getStrayAnimalNoteUuId()); |
|
|
Long noteId = miniStrayAnimalNoteMapper.selectIdByUuid(addCommentForm.getStrayAnimalNoteUuId()); |
|
|
@ -156,4 +198,45 @@ public class StrayAnimalNoteCommentServiceImpl extends ServiceImpl<MiniStrayAnim |
|
|
return animalNoteCommentVO; |
|
|
return animalNoteCommentVO; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Transactional(readOnly = true, rollbackFor = Exception.class) |
|
|
|
|
|
public IPage<AnimalNoteFirstLevelCommentVO> getFirstLevelCommentList(AnimalNoteFirstLevelCommentQueryParam queryParam, Long userId) { |
|
|
|
|
|
// 1. 构造分页参数,默认值兼容
|
|
|
|
|
|
Page<AnimalNoteFirstLevelCommentVO> page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize()); |
|
|
|
|
|
|
|
|
|
|
|
// 2. 查询一级评论分页(SQL已自动统计二级评论数量)
|
|
|
|
|
|
IPage<AnimalNoteFirstLevelCommentVO> result = baseMapper.getFirstLevelComment(page, queryParam); |
|
|
|
|
|
List<AnimalNoteFirstLevelCommentVO> commentList = result.getRecords(); |
|
|
|
|
|
if (commentList.isEmpty() || userId == null) { |
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 3. 提取一级评论内部主键ID(用于查询点赞状态)
|
|
|
|
|
|
List<Long> firstLevelCommentIds = commentList.stream() |
|
|
|
|
|
.map(AnimalNoteFirstLevelCommentVO::getCommentId) |
|
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
|
|
// 4. 批量查询登录用户的点赞状态
|
|
|
|
|
|
Map<Long, Boolean> likeStatusMap = Collections.emptyMap(); |
|
|
|
|
|
try { |
|
|
|
|
|
List<MiniStrayAnimalNoteCommentLike> likeList = baseMapper.batchGetUserCommentLikes(firstLevelCommentIds, 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 (AnimalNoteFirstLevelCommentVO comment : commentList) { |
|
|
|
|
|
Boolean isLiked = likeStatusMap.get(comment.getCommentId()); |
|
|
|
|
|
comment.setIsLiked(Boolean.TRUE.equals(isLiked)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|