cloudroam
2025-06-13 790d073559bfc15e9d1130fb9d51e9d673985cc7
src/main/java/com/mzl/flower/service/film/impl/CommentLikesServiceImpl.java
@@ -1,6 +1,7 @@
package com.mzl.flower.service.film.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.film.CommentLikesDTO;
import com.mzl.flower.dto.request.film.CommentLikesQueryDTO;
@@ -37,16 +38,32 @@
        //查询有没有对应人员的点赞信息
        CommentLikes commentLikes = commentLikesMapper.getCommentLikeByUserIdAndCommentId(SecurityUtils.getUserId(), commentLikesDTO.getCommentId());
        if (commentLikes == null) {
            CommentLikes commentLikes1 = new CommentLikes();
            commentLikes1.setCommentId(commentLikesDTO.getCommentId());
            commentLikes1.setStatus(true);
            commentLikes1.create();
            commentLikesMapper.insert(commentLikes1);
            commentLikes = new CommentLikes();
            commentLikes.setCommentId(commentLikesDTO.getCommentId());
            commentLikes.setStatus(true);
            commentLikes.setCreateBy(SecurityUtils.getUserId());
            commentLikes.create();
            if (commentLikesMapper.insert(commentLikes) <= 0) {
                throw new ValidationException("添加评论点赞失败");
            }
        } else {
            // 状态取反
            commentLikes.setStatus(!commentLikes.getStatus());
            commentLikes.update(SecurityUtils.getUserId());
            commentLikesMapper.updateById(commentLikes);
            // 校验主键是否存在
            if (commentLikes.getId() == null) {
                throw new ValidationException("主键 ID 不能为空");
            }
            int affectedRows = commentLikesMapper.updateStatusById(
                    commentLikes.getId(),         // 确保 id 是 Long 类型
                    commentLikes.getStatus(),
                    SecurityUtils.getUserId()  // 更新人
            );
            if (affectedRows <= 0) {
                throw new ValidationException("更新作品点赞状态失败");
            }
        }
    }