<?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" />
        <!-- send_user_name: 发送用户名称（根据 send_user_id 和 send_user_type 查询） -->
        <result column="send_user_name" property="sendUserName" />
        <result column="user_avatar" property="userAvatar" />
        <!-- receive_user_name: 接收用户名称（根据 receive_user_id 和 receive_user_type 查询） -->
        <result column="receive_user_name" property="receiveUserName" />
        <result column="send_user_type" property="sendUserType" />
        <result column="receive_user_type" property="receiveUserType" />
        <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查询评论列表（包含用户信息） -->
    <!-- 评论列表中的 sendUserName 和 receiveUserName 根据 sendUserType 和 receiveUserType 分别从对应的表中查询 -->
    <select id="getCommentListByReviewId" resultMap="ReviewCommentVoResultMap">
        SELECT
            rc.id,
            rc.review_id,
            rc.send_user_id,
            rc.receive_user_id,
            -- 发送用户名称：根据 sendUserType 分别查询普通用户表和律师表
            -- sendUserType = 2（律师）：优先从 lawyer_user 表查询，如果没有则从 life_user 表查询
            -- sendUserType = 1 或 NULL（普通用户）：优先从 life_user 表查询，如果没有则从 lawyer_user 表查询
            CASE
                WHEN rc.send_user_type = 2 THEN COALESCE(lu_lawyer.name, lu_user.user_name)
                WHEN rc.send_user_type = 1 OR rc.send_user_type IS NULL THEN COALESCE(lu_user.user_name, lu_lawyer.name)
                ELSE COALESCE(lu_user.user_name, lu_lawyer.name)
            END AS send_user_name,
            -- 发送用户头像：根据 sendUserType 分别查询普通用户表和律师表
            CASE
                WHEN rc.send_user_type = 2 THEN COALESCE(lu_lawyer.head_img, lu_user.user_image)
                WHEN rc.send_user_type = 1 OR rc.send_user_type IS NULL THEN COALESCE(lu_user.user_image, lu_lawyer.head_img)
                ELSE COALESCE(lu_user.user_image, lu_lawyer.head_img)
            END AS user_avatar,
            -- 接收用户名称：根据 receiveUserType 严格从对应的表中查询
            -- receiveUserType = 2（律师）：只从 lawyer_user 表查询
            -- receiveUserType = 1 或 NULL（普通用户）：只从 life_user 表查询
            CASE
                WHEN rc.receive_user_type = 2 THEN lu_lawyer2.name
                WHEN rc.receive_user_type = 1 OR rc.receive_user_type IS NULL THEN lu_user2.user_name
                ELSE NULL
            END AS receive_user_name,
            rc.send_user_type,
            rc.receive_user_type,
            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
        -- 发送用户：普通用户表（先 JOIN，不限制类型）
        LEFT JOIN life_user lu_user ON lu_user.id = rc.send_user_id 
            AND lu_user.delete_flag = 0
        -- 发送用户：律师表（先 JOIN，不限制类型）
        LEFT JOIN lawyer_user lu_lawyer ON lu_lawyer.id = rc.send_user_id 
            AND lu_lawyer.delete_flag = 0
        -- 接收用户：普通用户表（先 JOIN，不限制类型）
        LEFT JOIN life_user lu_user2 ON lu_user2.id = rc.receive_user_id 
            AND lu_user2.delete_flag = 0
        -- 接收用户：律师表（先 JOIN，不限制类型）
        LEFT JOIN lawyer_user lu_lawyer2 ON lu_lawyer2.id = rc.receive_user_id 
            AND lu_lawyer2.delete_flag = 0
        -- 如果 receive_user_id 在用户表中查询不到，尝试从评论表查询（处理 receive_user_id 可能是评论 ID 的情况）
        LEFT JOIN lawyer_review_comment rc_comment ON rc_comment.id = rc.receive_user_id 
            AND rc_comment.delete_flag = 0
        LEFT JOIN life_user lu_user_comment ON lu_user_comment.id = rc_comment.send_user_id 
            AND lu_user_comment.delete_flag = 0
        LEFT JOIN lawyer_user lu_lawyer_comment ON lu_lawyer_comment.id = rc_comment.send_user_id 
            AND lu_lawyer_comment.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查询回复列表（包含用户信息） -->
    <!-- 回复列表中的 sendUserName 和 receiveUserName 根据 sendUserType 和 receiveUserType 分别从对应的表中查询 -->
    <select id="getReplyListByHeadId" resultMap="ReviewCommentVoResultMap">
        SELECT
            rc.id,
            rc.review_id,
            rc.send_user_id,
            rc.receive_user_id,
            -- 发送用户名称：根据 sendUserType 分别查询普通用户表和律师表
            -- sendUserType = 2（律师）：优先从 lawyer_user 表查询，如果没有则从 life_user 表查询
            -- sendUserType = 1 或 NULL（普通用户）：优先从 life_user 表查询，如果没有则从 lawyer_user 表查询
            CASE
                WHEN rc.send_user_type = 2 THEN COALESCE(lu_lawyer.name, lu_user.user_name)
                WHEN rc.send_user_type = 1 OR rc.send_user_type IS NULL THEN COALESCE(lu_user.user_name, lu_lawyer.name)
                ELSE COALESCE(lu_user.user_name, lu_lawyer.name)
            END AS send_user_name,
            -- 发送用户头像：根据 sendUserType 分别查询普通用户表和律师表
            CASE
                WHEN rc.send_user_type = 2 THEN COALESCE(lu_lawyer.head_img, lu_user.user_image)
                WHEN rc.send_user_type = 1 OR rc.send_user_type IS NULL THEN COALESCE(lu_user.user_image, lu_lawyer.head_img)
                ELSE COALESCE(lu_user.user_image, lu_lawyer.head_img)
                END AS user_avatar,
            -- 接收用户名称：根据 receiveUserType 严格从对应的表中查询
            -- receiveUserType = 2（律师）：只从 lawyer_user 表查询
            -- receiveUserType = 1 或 NULL（普通用户）：只从 life_user 表查询
            CASE
                WHEN rc.receive_user_type = 2 THEN lu_lawyer2.name
                WHEN rc.receive_user_type = 1 OR rc.receive_user_type IS NULL THEN lu_user2.user_name
                ELSE NULL
            END AS receive_user_name,
            rc.send_user_type,
            rc.receive_user_type,
            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
        -- 发送用户：普通用户表（先 JOIN，不限制类型）
        LEFT JOIN life_user lu_user ON lu_user.id = rc.send_user_id 
            AND lu_user.delete_flag = 0
        -- 发送用户：律师表（先 JOIN，不限制类型）
        LEFT JOIN lawyer_user lu_lawyer ON lu_lawyer.id = rc.send_user_id 
            AND lu_lawyer.delete_flag = 0
        -- 接收用户：普通用户表（先 JOIN，不限制类型）
        LEFT JOIN life_user lu_user2 ON lu_user2.id = rc.receive_user_id 
            AND lu_user2.delete_flag = 0
        -- 接收用户：律师表（先 JOIN，不限制类型）
        LEFT JOIN lawyer_user lu_lawyer2 ON lu_lawyer2.id = rc.receive_user_id 
            AND lu_lawyer2.delete_flag = 0
        -- 如果 receive_user_id 在用户表中查询不到，尝试从评论表查询（处理 receive_user_id 可能是评论 ID 的情况）
        LEFT JOIN lawyer_review_comment rc_comment ON rc_comment.id = rc.receive_user_id 
            AND rc_comment.delete_flag = 0
        LEFT JOIN life_user lu_user_comment ON lu_user_comment.id = rc_comment.send_user_id 
            AND lu_user_comment.delete_flag = 0
        LEFT JOIN lawyer_user lu_lawyer_comment ON lu_lawyer_comment.id = rc_comment.send_user_id 
            AND lu_lawyer_comment.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>

