<?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="shop.alien.mapper.ReviewCommentMapper">

    <!-- 评论列表查询结果映射 -->
    <resultMap id="ReviewCommentVoResultMap" type="shop.alien.entity.store.vo.ReviewCommentVo">
        <id column="id" property="id" />
        <result column="review_id" property="reviewId" />
        <result column="send_user_id" property="sendUserId" />
        <result column="receive_user_id" property="receiveUserId" />
        <result column="user_name" property="sendUserName" />
        <result column="user_avatar" property="userAvatar" />
        <result column="receive_user_name" property="receiveUserName" />
        <result column="comment_content" property="commentContent" />
        <result column="like_count" property="likeCount" />
        <result column="reply_count" property="replyCount" />
        <result column="head_type" property="headType" />
        <result column="head_id" property="headId" />
        <result column="is_liked" property="isLiked" />
        <result column="created_time" property="createdTime" />
    </resultMap>

    <!-- 根据评价ID查询评论列表（包含用户信息） -->
    <select id="getCommentListByReviewId" resultMap="ReviewCommentVoResultMap">
        SELECT
            rc.id,
            rc.review_id,
            rc.send_user_id,
            rc.receive_user_id,
            lu.user_name AS user_name,
            lu.user_image AS user_avatar,
            lu2.user_name AS receive_user_name,
            rc.comment_content,
            rc.like_count,
            rc.reply_count,
            rc.head_type,
            rc.head_id,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            rc.created_time
        FROM lawyer_review_comment rc
        LEFT JOIN life_user lu ON lu.id = rc.send_user_id AND lu.delete_flag = 0
        LEFT JOIN life_user lu2 ON lu2.id = rc.receive_user_id AND lu2.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(rc.id, CHAR)
            AND llr.type = '8' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE rc.delete_flag = 0
        AND rc.review_id = #{reviewId}
        AND rc.head_type = 0
        ORDER BY rc.created_time DESC
    </select>

    <!-- 根据首评ID查询回复列表（包含用户信息） -->
    <select id="getReplyListByHeadId" resultMap="ReviewCommentVoResultMap">
        SELECT
            rc.id,
            rc.review_id,
            rc.send_user_id,
            rc.receive_user_id,
            lu.user_name AS user_name,
            lu.user_image AS user_avatar,
            lu2.user_name AS receive_user_name,
            rc.comment_content,
            rc.like_count,
            rc.reply_count,
            rc.head_type,
            rc.head_id,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            rc.created_time
        FROM lawyer_review_comment rc
        LEFT JOIN life_user lu ON lu.id = rc.send_user_id AND lu.delete_flag = 0
        LEFT JOIN life_user lu2 ON lu2.id = rc.receive_user_id AND lu2.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(rc.id, CHAR)
            AND llr.type = '8' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE rc.delete_flag = 0
        AND rc.head_id = #{headId}
        AND rc.head_type = 1
        ORDER BY rc.created_time ASC
    </select>

    <!-- 根据评价ID查询评论数量（只统计首评论） -->
    <select id="getCommentCountByReviewId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM lawyer_review_comment
        WHERE delete_flag = 0
        AND review_id = #{reviewId}
        AND head_type = 0
    </select>

    <!-- 根据评价ID查询总评论数量（包括首评论和子评论） -->
    <select id="getTotalCommentCountByReviewId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM lawyer_review_comment
        WHERE delete_flag = 0
        AND review_id = #{reviewId}
    </select>

</mapper>

