From 541e18749f918bbcb5c3e1749446b36f98fe24c3 Mon Sep 17 00:00:00 2001 From: glx <783262171@qq.com> Date: Wed, 6 May 2026 17:23:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=84=E8=AE=BA=E7=82=B9?= =?UTF-8?q?=E8=B5=9E=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StrayAnimalNoteCommentController.java | 15 +++++ .../MiniStrayAnimalNoteCommentLikeMapper.java | 16 +++++ .../MiniStrayAnimalNoteCommentMapper.java | 15 +++++ .../boot/mini/model/form/CommentLikeForm.java | 19 ++++++ .../StrayAnimalNoteCommentService.java | 4 ++ .../StrayAnimalNoteCommentServiceImpl.java | 59 +++++++++++++++++++ .../MiniStrayAnimalNoteCommentLikeMapper.xml | 33 +++++++++++ .../mini/MiniStrayAnimalNoteCommentMapper.xml | 25 ++++++++ 8 files changed, 186 insertions(+) create mode 100644 src/main/java/com/youlai/boot/mini/model/form/CommentLikeForm.java diff --git a/src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java b/src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java index 99fca9b..e37d6d9 100644 --- a/src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java +++ b/src/main/java/com/youlai/boot/mini/controller/StrayAnimalNoteCommentController.java @@ -7,6 +7,7 @@ import com.youlai.boot.common.enums.ActionTypeEnum; import com.youlai.boot.common.enums.LogModuleEnum; import com.youlai.boot.common.result.Result; import com.youlai.boot.framework.security.util.SecurityUtils; +import com.youlai.boot.mini.model.form.CommentLikeForm; import com.youlai.boot.mini.model.form.DeleteStrayAnimalNoteCommentForm; import com.youlai.boot.mini.model.form.StrayAnimalNoteCommentForm; import com.youlai.boot.mini.model.query.AnimalNoteFirstLevelCommentQueryParam; @@ -14,6 +15,7 @@ import com.youlai.boot.mini.model.query.AnimalNoteSecondLevelCommentQueryParam; 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 java.util.Map; import com.youlai.boot.mini.service.StrayAnimalNoteCommentService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -76,4 +78,17 @@ public class StrayAnimalNoteCommentController { return Result.success(result); } + @Operation(summary = "评论点赞/取消点赞接口") + @PostMapping(value = "like/toggle") + @PreAuthorize("isAuthenticated()") + @RepeatSubmit(expire = 1) + @Log(module = LogModuleEnum.STRAY_ANIMAL_NOTE_COMMENT, value = ActionTypeEnum.UPDATE) + public Result> toggleLike( + @Valid @RequestBody CommentLikeForm form + ) { + Long userId = SecurityUtils.getUserId(); + Map result = strayAnimalNoteCommentService.toggleLike(form, userId); + return Result.success(result); + } + } diff --git a/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentLikeMapper.java b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentLikeMapper.java index 41a1240..11ff567 100644 --- a/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentLikeMapper.java +++ b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentLikeMapper.java @@ -2,6 +2,7 @@ package com.youlai.boot.mini.mapper; import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteCommentLike; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Param; /** * 流浪动物笔记评论点赞表 Mapper 接口 @@ -11,4 +12,19 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; */ public interface MiniStrayAnimalNoteCommentLikeMapper extends BaseMapper { + /** + * 查询用户是否点赞该评论 + */ + Integer selectUserLikeCount(@Param("commentId") Long commentId, @Param("userId") Long userId); + + /** + * 新增或更新点赞记录(原子操作) + */ + int insertOrUpdateLike(MiniStrayAnimalNoteCommentLike like); + + /** + * 逻辑删除点赞记录(取消点赞) + */ + int deleteLike(@Param("commentId") Long commentId, @Param("userId") Long userId, @Param("currentTime") Long currentTime); + } 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 ebdcb6b..360fa3d 100644 --- a/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java +++ b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalNoteCommentMapper.java @@ -37,4 +37,19 @@ public interface MiniStrayAnimalNoteCommentMapper extends BaseMapper batchGetUserCommentLikes(@Param("noteCommentIds") List noteCommentIds, @Param("appUserId") Long appUserId); + /** + * 原子增加评论点赞数 + */ + int incrementLikeCount(@Param("commentId") Long commentId); + + /** + * 原子减少评论点赞数 + */ + int decrementLikeCount(@Param("commentId") Long commentId); + + /** + * 查询评论点赞数 + */ + Long selectLikeCount(@Param("commentId") Long commentId); + } diff --git a/src/main/java/com/youlai/boot/mini/model/form/CommentLikeForm.java b/src/main/java/com/youlai/boot/mini/model/form/CommentLikeForm.java new file mode 100644 index 0000000..1974055 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/model/form/CommentLikeForm.java @@ -0,0 +1,19 @@ +package com.youlai.boot.mini.model.form; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +@Data +@Schema(description = "评论点赞请求参数") +public class CommentLikeForm { + + @NotBlank(message = "评论UUID不能为空") + @Schema(description = "评论UUID", requiredMode = Schema.RequiredMode.REQUIRED, example = "a1b2c3d4e5f6g7h8i9j0") + private String commentUuid; + + @NotNull(message = "操作类型不能为空") + @Schema(description = "操作类型:true=点赞 false=取消点赞", requiredMode = Schema.RequiredMode.REQUIRED, allowableValues = {"true", "false"}) + private Boolean like; +} diff --git a/src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java b/src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java index 192805a..a3d7c5c 100644 --- a/src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java +++ b/src/main/java/com/youlai/boot/mini/service/StrayAnimalNoteCommentService.java @@ -3,6 +3,7 @@ package com.youlai.boot.mini.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment; +import com.youlai.boot.mini.model.form.CommentLikeForm; import com.youlai.boot.mini.model.form.DeleteStrayAnimalNoteCommentForm; import com.youlai.boot.mini.model.form.StrayAnimalNoteCommentForm; import com.youlai.boot.mini.model.query.AnimalNoteFirstLevelCommentQueryParam; @@ -11,6 +12,7 @@ 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 jakarta.validation.Valid; +import java.util.Map; public interface StrayAnimalNoteCommentService extends IService { @@ -22,4 +24,6 @@ public interface StrayAnimalNoteCommentService extends IService getSecondLevelCommentList(AnimalNoteSecondLevelCommentQueryParam queryParam, Long userId); + Map toggleLike(CommentLikeForm form, Long userId); + } 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 6093d84..e1e3268 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 @@ -15,6 +15,7 @@ import com.youlai.boot.mini.mapper.MiniStrayAnimalNoteMapper; import com.youlai.boot.mini.model.entity.MiniStrayAnimalNote; import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment; import com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteCommentLike; +import com.youlai.boot.mini.model.form.CommentLikeForm; import com.youlai.boot.mini.model.form.DeleteStrayAnimalNoteCommentForm; import com.youlai.boot.mini.model.form.StrayAnimalNoteCommentForm; import com.youlai.boot.mini.model.query.AnimalNoteFirstLevelCommentQueryParam; @@ -33,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; @Slf4j @Service @@ -281,4 +283,61 @@ public class StrayAnimalNoteCommentServiceImpl extends ServiceImpl toggleLike(CommentLikeForm form, Long userId) { + // 1. 根据评论UUID查询内部主键ID + Long commentId = baseMapper.selectIdByUuid(form.getCommentUuid()); + if (commentId == null) { + throw new MsgException("评论不存在或已删除"); + } + + long currentTime = System.currentTimeMillis(); + boolean targetLike = form.getLike(); + Boolean currentLiked = null; + + // 2. 查询用户当前点赞状态 + Integer count = miniStrayAnimalNoteCommentLikeMapper.selectUserLikeCount(commentId, userId); + if (count != null && count > 0) { + currentLiked = true; + } + + // 3. 执行点赞/取消点赞操作 + if (targetLike) { + // 点赞操作:不存在则新增,存在则更新为未删除状态 + if (!Boolean.TRUE.equals(currentLiked)) { + MiniStrayAnimalNoteCommentLike like = new MiniStrayAnimalNoteCommentLike(); + like.setUuid(IdWorker.get32UUID()); + like.setNoteCommentId(commentId); + like.setMiniUserId(userId); + like.setCreateBy(userId); + like.setCreateTimestamp(currentTime); + like.setCreateTime(new Date(currentTime)); + like.setUpdateBy(userId); + like.setUpdateTimestamp(currentTime); + like.setUpdateTime(new Date(currentTime)); + like.setDeleted(false); + miniStrayAnimalNoteCommentLikeMapper.insertOrUpdateLike(like); + + // 原子增加点赞数 + baseMapper.incrementLikeCount(commentId); + } + } else { + // 取消点赞操作:逻辑删除 + if (Boolean.TRUE.equals(currentLiked)) { + miniStrayAnimalNoteCommentLikeMapper.deleteLike(commentId, userId, currentTime); + + // 原子减少点赞数 + baseMapper.decrementLikeCount(commentId); + } + } + + // 4. 查询最新点赞数返回 + Long likeCount = baseMapper.selectLikeCount(commentId); + Map result = new HashMap<>(); + result.put("isLiked", targetLike); + result.put("likeCount", likeCount != null ? likeCount : 0); + return result; + } + } diff --git a/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentLikeMapper.xml b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentLikeMapper.xml index 5d3f226..828cb81 100644 --- a/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentLikeMapper.xml +++ b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentLikeMapper.xml @@ -5,5 +5,38 @@ + + + + + + INSERT INTO mini_stray_animal_note_comment_like + (uuid, note_comment_id, mini_user_id, create_time, create_timestamp, create_by, update_time, update_timestamp, update_by, is_deleted) + VALUES + (#{uuid}, #{noteCommentId}, #{miniUserId}, #{createTime}, #{createTimestamp}, #{createBy}, #{updateTime}, #{updateTimestamp}, #{updateBy}, #{deleted}) + ON DUPLICATE KEY UPDATE + is_deleted = 0, + update_time = VALUES(update_time), + update_timestamp = VALUES(update_timestamp), + update_by = VALUES(update_by) + + + + + UPDATE mini_stray_animal_note_comment_like + SET is_deleted = 1, + update_time = NOW(), + update_timestamp = #{currentTime}, + update_by = #{userId} + WHERE note_comment_id = #{commentId} + AND mini_user_id = #{userId} + AND is_deleted = 0 + diff --git a/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml index 11ddb82..b6fa467 100644 --- a/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml +++ b/src/main/resources/mapper/mini/MiniStrayAnimalNoteCommentMapper.xml @@ -163,4 +163,29 @@ AND is_deleted = 0 + + + UPDATE mini_stray_animal_note_comment + SET like_count = like_count + 1 + WHERE id = #{commentId} + AND is_deleted = 0 + + + + + UPDATE mini_stray_animal_note_comment + SET like_count = like_count - 1 + WHERE id = #{commentId} + AND is_deleted = 0 + AND like_count > 0 + + + + +