<?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.StoreStaffCommentMapper">

    <!-- 评论列表查询结果映射 -->
    <resultMap id="StoreStaffCommentVoResultMap" type="shop.alien.entity.store.vo.StoreStaffCommentVo">
        <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="send_user_name" property="sendUserName" />
        <result column="send_user_avatar" property="sendUserAvatar" />
        <result column="receive_user_name" property="receiveUserName" />
        <result column="receive_user_avatar" property="receiveUserAvatar" />
        <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查询评论列表（包含用户信息） -->
    <select id="getCommentListByReviewId" resultMap="StoreStaffCommentVoResultMap">
        SELECT
            ssc.id,
            ssc.review_id,
            ssc.send_user_id,
            ssc.receive_user_id,
            -- 发送用户名称：根据 sendUserType 分别查询普通用户表和员工配置表
            CASE
                WHEN ssc.send_user_type = 2 THEN ssc_send.name
                WHEN ssc.send_user_type = 1 OR ssc.send_user_type IS NULL THEN lu_send.user_name
                ELSE lu_send.user_name
            END AS send_user_name,
            -- 发送用户头像：根据 sendUserType 分别查询普通用户表和员工配置表
            CASE
                WHEN ssc.send_user_type = 2 THEN ssc_send.staff_image
                WHEN ssc.send_user_type = 1 OR ssc.send_user_type IS NULL THEN lu_send.user_image
                ELSE lu_send.user_image
            END AS send_user_avatar,
            -- 接收用户名称：根据 receiveUserType 分别查询普通用户表和员工配置表
            CASE
                WHEN ssc.receive_user_type = 2 THEN ssc_receive.name
                WHEN ssc.receive_user_type = 1 OR ssc.receive_user_type IS NULL THEN lu_receive.user_name
                ELSE NULL
            END AS receive_user_name,
            -- 接收用户头像：根据 receiveUserType 分别查询普通用户表和员工配置表
            CASE
                WHEN ssc.receive_user_type = 2 THEN ssc_receive.staff_image
                WHEN ssc.receive_user_type = 1 OR ssc.receive_user_type IS NULL THEN lu_receive.user_image
                ELSE NULL
            END AS receive_user_avatar,
            ssc.send_user_type,
            ssc.receive_user_type,
            ssc.comment_content,
            ssc.like_count,
            ssc.reply_count,
            ssc.head_type,
            ssc.head_id,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            ssc.created_time
        FROM store_staff_comment ssc
        -- 发送用户：普通用户表
        LEFT JOIN life_user lu_send ON lu_send.id = ssc.send_user_id 
            AND lu_send.delete_flag = 0
        -- 发送用户：员工配置表
        LEFT JOIN store_staff_config ssc_send ON ssc_send.id = ssc.send_user_id 
            AND ssc_send.delete_flag = 0
        -- 接收用户：普通用户表
        LEFT JOIN life_user lu_receive ON lu_receive.id = ssc.receive_user_id 
            AND lu_receive.delete_flag = 0
        -- 接收用户：员工配置表
        LEFT JOIN store_staff_config ssc_receive ON ssc_receive.id = ssc.receive_user_id 
            AND ssc_receive.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(ssc.id, CHAR)
            AND llr.type = '10' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE ssc.delete_flag = 0
        AND ssc.review_id = #{reviewId}
        AND ssc.head_type = 0
        ORDER BY ssc.created_time DESC
    </select>

    <!-- 根据首评ID查询回复列表（包含用户信息） -->
    <select id="getReplyListByHeadId" resultMap="StoreStaffCommentVoResultMap">
        SELECT
            ssc.id,
            ssc.review_id,
            ssc.send_user_id,
            ssc.receive_user_id,
            -- 发送用户名称：根据 sendUserType 分别查询普通用户表和员工配置表
            CASE
                WHEN ssc.send_user_type = 2 THEN ssc_send.name
                WHEN ssc.send_user_type = 1 OR ssc.send_user_type IS NULL THEN lu_send.user_name
                ELSE lu_send.user_name
            END AS send_user_name,
            -- 发送用户头像：根据 sendUserType 分别查询普通用户表和员工配置表
            CASE
                WHEN ssc.send_user_type = 2 THEN ssc_send.staff_image
                WHEN ssc.send_user_type = 1 OR ssc.send_user_type IS NULL THEN lu_send.user_image
                ELSE lu_send.user_image
            END AS send_user_avatar,
            -- 接收用户名称：根据 receiveUserType 分别查询普通用户表和员工配置表
            CASE
                WHEN ssc.receive_user_type = 2 THEN ssc_receive.name
                WHEN ssc.receive_user_type = 1 OR ssc.receive_user_type IS NULL THEN lu_receive.user_name
                ELSE NULL
            END AS receive_user_name,
            -- 接收用户头像：根据 receiveUserType 分别查询普通用户表和员工配置表
            CASE
                WHEN ssc.receive_user_type = 2 THEN ssc_receive.staff_image
                WHEN ssc.receive_user_type = 1 OR ssc.receive_user_type IS NULL THEN lu_receive.user_image
                ELSE NULL
            END AS receive_user_avatar,
            ssc.send_user_type,
            ssc.receive_user_type,
            ssc.comment_content,
            ssc.like_count,
            ssc.reply_count,
            ssc.head_type,
            ssc.head_id,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            ssc.created_time
        FROM store_staff_comment ssc
        -- 发送用户：普通用户表
        LEFT JOIN life_user lu_send ON lu_send.id = ssc.send_user_id 
            AND lu_send.delete_flag = 0
        -- 发送用户：员工配置表
        LEFT JOIN store_staff_config ssc_send ON ssc_send.id = ssc.send_user_id 
            AND ssc_send.delete_flag = 0
        -- 接收用户：普通用户表
        LEFT JOIN life_user lu_receive ON lu_receive.id = ssc.receive_user_id 
            AND lu_receive.delete_flag = 0
        -- 接收用户：员工配置表
        LEFT JOIN store_staff_config ssc_receive ON ssc_receive.id = ssc.receive_user_id 
            AND ssc_receive.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(ssc.id, CHAR)
            AND llr.type = '10' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE ssc.delete_flag = 0
        AND ssc.head_id = #{headId}
        AND ssc.head_type = 1
        ORDER BY ssc.created_time ASC
    </select>

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

</mapper>

