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

    <!-- 评价列表查询结果映射 -->
    <resultMap id="StoreStaffReviewVoResultMap" type="shop.alien.entity.store.vo.StoreStaffReviewVo">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="user_name" property="userName" />
        <result column="user_avatar" property="userAvatar" />
        <result column="staff_user_id" property="staffUserId" />
        <result column="staff_name" property="staffName" />
        <result column="staff_avatar" property="staffAvatar" />
        <result column="overall_rating" property="overallRating" />
        <result column="service_attitude_rating" property="serviceAttitudeRating" />
        <result column="response_time_rating" property="responseTimeRating" />
        <result column="professional_ability_rating" property="professionalAbilityRating" />
        <result column="review_content" property="reviewContent" />
        <result column="review_images" property="reviewImagesJson" />
        <result column="is_anonymous" property="isAnonymous" />
        <result column="like_count" property="likeCount" />
        <result column="comment_count" property="commentCount" />
        <result column="is_liked" property="isLiked" />
        <result column="created_time" property="createdTime" />
        <result column="appeal_id" property="appealId" />
    </resultMap>

    <!-- 分页查询评价列表（包含用户和员工信息） -->
    <select id="getReviewListWithUser" resultMap="StoreStaffReviewVoResultMap">
        SELECT
            ssr.id,
            ssr.user_id,
            CASE 
                WHEN ssr.is_anonymous = 1 THEN '匿名用户'
                ELSE lu.user_name
            END AS user_name,
            CASE 
                WHEN ssr.is_anonymous = 1 THEN NULL
                ELSE lu.user_image
            END AS user_avatar,
            ssr.staff_user_id,
            sp.personnel_name AS staff_name,
            si.img_url AS staff_avatar,
            ssr.overall_rating,
            ssr.service_attitude_rating,
            ssr.response_time_rating,
            ssr.professional_ability_rating,
            ssr.review_content,
            ssr.is_anonymous,
            ssr.like_count,
            ssr.comment_count,
            ssr.review_images,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            ssr.created_time,
            ssr.appeal_id
        FROM store_staff_review ssr
        LEFT JOIN life_user lu ON lu.id = ssr.user_id AND lu.delete_flag = 0
        LEFT JOIN store_personnel sp ON sp.id = ssr.staff_user_id AND sp.delete_flag = 0
        LEFT JOIN store_img si ON si.id = sp.img_id AND si.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(ssr.id, CHAR)
            AND llr.type = '9' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE ssr.delete_flag = 0
        <if test="staffUserId != null">
            AND ssr.staff_user_id = #{staffUserId}
        </if>
        <if test="userId != null">
            AND ssr.user_id = #{userId}
        </if>
        ORDER BY ssr.created_time DESC
    </select>

    <!-- 根据评价ID查询评价详情（包含用户和员工信息） -->
    <select id="getReviewDetailById" resultMap="StoreStaffReviewVoResultMap">
        SELECT
            ssr.id,
            ssr.user_id,
            CASE 
                WHEN ssr.is_anonymous = 1 THEN '匿名用户'
                ELSE lu.user_name
            END AS user_name,
            CASE 
                WHEN ssr.is_anonymous = 1 THEN NULL
                ELSE lu.user_image
            END AS user_avatar,
            ssr.staff_user_id,
            sp.personnel_name AS staff_name,
            si.img_url AS staff_avatar,
            ssr.overall_rating,
            ssr.service_attitude_rating,
            ssr.response_time_rating,
            ssr.professional_ability_rating,
            ssr.review_content,
            ssr.is_anonymous,
            ssr.like_count,
            ssr.comment_count,
            ssr.review_images,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            ssr.created_time,
            ssr.appeal_id
        FROM store_staff_review ssr
        LEFT JOIN life_user lu ON lu.id = ssr.user_id AND lu.delete_flag = 0
        LEFT JOIN store_personnel sp ON sp.id = ssr.staff_user_id AND sp.delete_flag = 0
        LEFT JOIN store_img si ON si.id = sp.img_id AND si.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(ssr.id, CHAR)
            AND llr.type = '9' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE ssr.delete_flag = 0
        AND ssr.id = #{reviewId}
        LIMIT 1
    </select>

    <!-- 计算员工的平均评分（overallRating的平均值） -->
    <select id="getAverageRatingByStaffUserId" resultType="java.math.BigDecimal">
        SELECT 
            COALESCE(AVG(overall_rating), 0) AS average_rating
        FROM store_staff_review
        WHERE staff_user_id = #{staffUserId}
          AND delete_flag = 0
          AND overall_rating IS NOT NULL
    </select>

    <!-- 统计员工的全部评价数量 -->
    <select id="getTotalReviewCountByStaffUserId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM store_staff_review
        WHERE staff_user_id = #{staffUserId}
        AND delete_flag = 0
    </select>

    <!-- 统计员工的好评数（4.5-5分） -->
    <select id="getGoodReviewCountByStaffUserId" resultType="java.lang.Integer">
        <![CDATA[
        SELECT COUNT(*)
        FROM store_staff_review
        WHERE staff_user_id = #{staffUserId}
        AND delete_flag = 0
        AND overall_rating IS NOT NULL
        AND overall_rating >= 4.5
        AND overall_rating <= 5
        ]]>
    </select>

    <!-- 统计员工的中评数（3-4分） -->
    <select id="getMediumReviewCountByStaffUserId" resultType="java.lang.Integer">
        <![CDATA[
        SELECT COUNT(*)
        FROM store_staff_review
        WHERE staff_user_id = #{staffUserId}
        AND delete_flag = 0
        AND overall_rating IS NOT NULL
        AND overall_rating >= 3
        AND overall_rating < 4.5
        ]]>
    </select>

    <!-- 统计员工的差评数（0-2.5分，包含2.5） -->
    <select id="getBadReviewCountByStaffUserId" resultType="java.lang.Integer">
        <![CDATA[
        SELECT COUNT(*)
        FROM store_staff_review
        WHERE staff_user_id = #{staffUserId}
        AND delete_flag = 0
        AND overall_rating IS NOT NULL
        AND overall_rating >= 0
        AND overall_rating < 3
        ]]>
    </select>

    <!-- 统计员工的有图评价数量 -->
    <select id="getImageReviewCountByStaffUserId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM store_staff_review
        WHERE staff_user_id = #{staffUserId}
        AND delete_flag = 0
        AND review_images IS NOT NULL
        AND review_images != ''
        AND TRIM(review_images) != ''
    </select>

    <!-- 根据员工ID和类型分页查询评价列表（包含用户和员工信息） -->
    <select id="getReviewListByStaffAndType" resultMap="StoreStaffReviewVoResultMap">
        SELECT
            ssr.id,
            ssr.user_id,
            CASE 
                WHEN ssr.is_anonymous = 1 THEN '匿名用户'
                ELSE lu.user_name
            END AS user_name,
            CASE 
                WHEN ssr.is_anonymous = 1 THEN NULL
                ELSE lu.user_image
            END AS user_avatar,
            ssr.staff_user_id,
            sp.personnel_name AS staff_name,
            si.img_url AS staff_avatar,
            ssr.overall_rating,
            ssr.service_attitude_rating,
            ssr.response_time_rating,
            ssr.professional_ability_rating,
            ssr.review_content,
            ssr.is_anonymous,
            COALESCE((
                SELECT COUNT(1)
                FROM life_like_record llr_count
                WHERE CONVERT(llr_count.huifu_id, CHAR) = CONVERT(ssr.id, CHAR)
                    AND llr_count.type = '9'
                    AND llr_count.delete_flag = 0
            ), 0) AS like_count,
            ssr.comment_count,
            ssr.review_images,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            ssr.created_time,
            ssr.appeal_id
        FROM store_staff_review ssr
        LEFT JOIN life_user lu ON lu.id = ssr.user_id AND lu.delete_flag = 0
        LEFT JOIN store_personnel sp ON sp.id = ssr.staff_user_id AND sp.delete_flag = 0
        LEFT JOIN store_img si ON si.id = sp.img_id AND si.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(ssr.id, CHAR)
            AND llr.type = '9' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE ssr.delete_flag = 0
        AND ssr.staff_user_id = #{staffUserId}
        <if test="type != null">
            <choose>
                <when test="type == 1">
                    <![CDATA[
                    AND ssr.overall_rating >= 4.5
                    AND ssr.overall_rating <= 5
                    ]]>
                </when>
                <when test="type == 2">
                    <![CDATA[
                    AND ssr.overall_rating >= 3
                    AND ssr.overall_rating < 4.5
                    ]]>
                </when>
                <when test="type == 3">
                    <![CDATA[
                    AND ssr.overall_rating >= 0
                    AND ssr.overall_rating < 3
                    ]]>
                </when>
                <when test="type == 4">
                    AND ssr.review_images IS NOT NULL
                    AND ssr.review_images != ''
                    AND TRIM(ssr.review_images) != ''
                </when>
            </choose>
        </if>
        ORDER BY ssr.created_time DESC
    </select>

</mapper>

