You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
7.2 KiB

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.youlai.boot.mini.mapper.MiniStrayAnimalNoteCommentMapper">
<update id="incrementCommentCount">
UPDATE mini_stray_animal_note
SET comment_count = comment_count + 1
WHERE id = #{noteId}
</update>
<!-- 一次性校准所有笔记的评论数 -->
<update id="batchCalibrateAllCommentCounts">
UPDATE mini_stray_animal_note n
SET n.comment_count = (
SELECT COUNT(1)
FROM mini_stray_animal_note_comment c
WHERE c.note_id = n.id
AND c.flag = 0
)
WHERE 1 = 1
</update>
<!-- 只校准最近有变动的笔记(24小时内有过评论的) -->
<update id="batchCalibrateRecentNotes">
UPDATE mini_stray_animal_note n
INNER JOIN (
SELECT note_id, MAX(create_time) as last_comment_time
FROM mini_stray_animal_note_comment
WHERE create_time >= DATE_SUB(NOW(), INTERVAL 1 DAY)
GROUP BY note_id
) c ON n.id = c.note_id
SET n.comment_count = (
SELECT COUNT(1)
FROM mini_stray_animal_note_comment c2
WHERE c2.note_id = n.id AND c2.flag = 0
)
</update>
<!-- 根据UUID查询评论ID -->
<select id="selectIdByUuid" resultType="java.lang.Long">
SELECT id
FROM mini_stray_animal_note_comment
WHERE uuid = #{uuid}
AND is_deleted = 0
LIMIT 1
</select>
<!-- 根据UUID查询父评论必要字段 -->
<select id="selectParentCommentInfoByUuid" resultType="com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteComment">
SELECT id, note_id, root_id, mini_user_id
FROM mini_stray_animal_note_comment
WHERE uuid = #{uuid}
AND is_deleted = 0
LIMIT 1
</select>
<!-- 根据ID查询评论UUID -->
<select id="selectUuidById" resultType="java.lang.String">
SELECT uuid
FROM mini_stray_animal_note_comment
WHERE id = #{id}
AND is_deleted = 0
LIMIT 1
</select>
<!-- 带权限校验的原子删除评论 -->
<update id="deleteCommentWithPermissionCheck">
UPDATE mini_stray_animal_note_comment c
INNER JOIN mini_stray_animal_note n ON c.note_id = n.id
SET c.is_deleted = 1,
c.update_by = #{userId},
c.update_time = NOW(),
c.update_timestamp = UNIX_TIMESTAMP(NOW(3)) * 1000
WHERE n.uuid = #{noteUuid}
AND c.uuid = #{commentUuid}
AND c.is_deleted = 0
AND n.is_deleted = 0
AND (c.mini_user_id = #{userId} OR n.mini_user_id = #{userId})
</update>
<!-- 扣减笔记评论数(避免负数) -->
<update id="decrementCommentCount">
UPDATE mini_stray_animal_note
SET comment_count = comment_count - 1
WHERE id = #{noteId}
AND comment_count > 0
</update>
<!-- 分页查询一级评论列表(返回UUID+二级评论数量) -->
<select id="getFirstLevelComment" resultType="com.youlai.boot.mini.model.vo.AnimalNoteFirstLevelCommentVO">
SELECT
sanc.id AS commentId,
sanc.uuid AS uuid,
au.uuid AS appUserId,
sanc.content,
sanc.like_count AS likeCount,
sanc.create_timestamp AS createTimestamp,
sanc.create_time AS createTime,
sanc.province,
au.nickname,
au.avatar AS avatarUrl,
<!-- 统计该一级评论下的有效二级评论数量 -->
(SELECT COUNT(*)
FROM mini_stray_animal_note_comment sc
WHERE sc.root_id = sanc.id
AND sc.parent_id != 0
AND sc.is_deleted = 0
) AS secondLevelCount
FROM mini_stray_animal_note_comment sanc
INNER JOIN mini_stray_animal_note note ON sanc.note_id = note.id AND note.uuid = #{query.noteUuId} AND note.is_deleted = 0
LEFT JOIN sys_user au ON sanc.mini_user_id = au.id AND au.is_deleted = 0
WHERE sanc.parent_id = 0
AND sanc.is_deleted = 0
AND sanc.root_id = sanc.id
ORDER BY sanc.like_count DESC, sanc.create_timestamp DESC
</select>
<!-- 分页查询二级评论列表 -->
<select id="getSecondLevelComment" resultType="com.youlai.boot.mini.model.vo.AnimalNoteSecondLevelCommentVO">
SELECT
note.uuid AS noteUuId,
sanc.id AS commentId,
sanc.uuid AS uuid,
au.uuid AS appUserId,
au.avatar AS avatarUrl,
au.nickname AS nickname,
sanc.content AS content,
parent_comment.uuid AS parentUuId,
sanc.reply_to_user_id AS replyToUserId,
rau.avatar AS replyToAvatarUrl,
rau.nickname AS replyToNickname,
sanc.like_count AS likeCount,
sanc.create_timestamp AS createTimestamp,
sanc.create_time AS createTime,
sanc.province AS province
FROM mini_stray_animal_note_comment sanc
INNER JOIN mini_stray_animal_note note ON sanc.note_id = note.id AND note.uuid = #{query.noteUuId} AND note.is_deleted = 0
LEFT JOIN mini_stray_animal_note_comment parent_comment ON sanc.parent_id = parent_comment.id AND parent_comment.is_deleted = 0
LEFT JOIN sys_user au ON sanc.mini_user_id = au.id AND au.is_deleted = 0
LEFT JOIN sys_user rau ON sanc.reply_to_user_id = rau.id AND rau.is_deleted = 0
WHERE sanc.parent_id != 0
AND sanc.is_deleted = 0
AND sanc.root_id = (SELECT id FROM mini_stray_animal_note_comment WHERE uuid = #{query.rootUuid} AND is_deleted = 0 AND parent_id = 0)
ORDER BY sanc.create_timestamp ASC
</select>
<!-- 批量查询用户对指定评论的点赞状态 -->
<select id="batchGetUserCommentLikes" resultType="com.youlai.boot.mini.model.entity.MiniStrayAnimalNoteCommentLike">
SELECT
note_comment_id,
mini_user_id
FROM
mini_stray_animal_note_comment_like
WHERE
mini_user_id = #{appUserId}
AND note_comment_id IN
<foreach item="noteCommentId" collection="noteCommentIds" open="(" separator="," close=")">
#{noteCommentId}
</foreach>
AND is_deleted = 0
</select>
<!-- 原子增加评论点赞数 -->
<update id="incrementLikeCount">
UPDATE mini_stray_animal_note_comment
SET like_count = like_count + 1
WHERE id = #{commentId}
AND is_deleted = 0
</update>
<!-- 原子减少评论点赞数 -->
<update id="decrementLikeCount">
UPDATE mini_stray_animal_note_comment
SET like_count = like_count - 1
WHERE id = #{commentId}
AND is_deleted = 0
AND like_count > 0
</update>
<!-- 查询评论点赞数 -->
<select id="selectLikeCount" resultType="java.lang.Long">
SELECT like_count
FROM mini_stray_animal_note_comment
WHERE id = #{commentId}
AND is_deleted = 0
</select>
</mapper>