package com.bruce.sams.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.bruce.sams.domain.ams.Comment; import com.bruce.sams.mapper.CommentMapper; import com.bruce.sams.service.CommentService; import org.springframework.stereotype.Service; import java.util.Date; import java.util.List; /** * 活动评论服务实现类 * @author bruce * @description 针对表【ams_comment(活动评论表)】的数据库操作Service实现 * @createDate 2025-02-20 15:08:30 */ @Service public class CommentServiceImpl extends ServiceImpl implements CommentService { /** * 用户发表评论 * @param userId 用户ID * @param actId 活动ID * @param content 评论内容 * @param parentCommentId 父评论ID(如果是回复) */ @Override public void addComment(Long userId, Long actId, String content, Long parentCommentId) { Comment comment = new Comment(); comment.setUserId(userId); comment.setActId(actId); comment.setContent(content); comment.setParentCommentId(parentCommentId); comment.setCreatedAt(new Date(System.currentTimeMillis())); this.save(comment); } /** * 删除评论 * @param commentId 评论ID * @param userId 用户ID(用于权限判断) * @param isAdmin 是否为管理员操作 */ @Override public void deleteComment(Long commentId, Long userId, boolean isAdmin) { Comment comment = this.getById(commentId); if (comment != null) { if (isAdmin || comment.getUserId().equals(userId)) { this.removeById(commentId); } else { throw new RuntimeException("无权限删除该评论"); } } } /** * 获取活动的所有评论 * @param actId 活动ID * @return 评论列表 */ @Override public List getCommentsForActivity(Long actId) { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(Comment::getActId, actId).isNull(Comment::getParentCommentId); return this.list(query); } /** * 获取某条评论的所有回复 * @param parentCommentId 父评论ID * @return 回复列表 */ @Override public List getRepliesForComment(Long parentCommentId) { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(Comment::getParentCommentId, parentCommentId); return this.list(query); } }