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

    <!-- 评价列表查询结果映射 -->
    <resultMap id="OrderReviewVoResultMap" type="shop.alien.entity.store.vo.OrderReviewVo">
        <id column="id" property="id" />
        <result column="order_id" property="orderId" />
        <result column="order_number" property="orderNumber" />
        <result column="user_id" property="userId" />
        <result column="user_name" property="userName" />
        <result column="user_avatar" property="userAvatar" />
        <result column="lawyer_user_id" property="lawyerUserId" />
        <result column="lawyer_name" property="lawyerName" />
        <result column="lawyer_avatar" property="lawyerAvatar" />
        <result column="law_firm_name" property="lawFirmName" />
        <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="is_appealed" property="isAppealed" />
    </resultMap>

    <!-- 分页查询评价列表（包含用户和律师信息） -->
    <select id="getReviewListWithUser" resultMap="OrderReviewVoResultMap">
        SELECT
            orv.id,
            orv.order_id,
            orv.order_number,
            orv.user_id,
            CASE 
                WHEN orv.is_anonymous = 1 THEN '匿名用户'
                ELSE lu.user_name
            END AS user_name,
            CASE 
                WHEN orv.is_anonymous = 1 THEN NULL
                ELSE lu.user_image
            END AS user_avatar,
            orv.lawyer_user_id,
            lu2.name AS lawyer_name,
            lu2.head_img AS lawyer_avatar,
            lf.firm_name AS law_firm_name,
            orv.overall_rating,
            orv.service_attitude_rating,
            orv.response_time_rating,
            orv.professional_ability_rating,
            orv.review_content,
            orv.is_anonymous,
            orv.like_count,
            orv.comment_count,
            orv.review_images,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            orv.created_time
        FROM lawyer_order_review orv
        LEFT JOIN life_user lu ON lu.id = orv.user_id AND lu.delete_flag = 0
        LEFT JOIN lawyer_user lu2 ON lu2.id = orv.lawyer_user_id AND lu2.delete_flag = 0
        LEFT JOIN law_firm lf ON lf.id = lu2.firm_id AND lf.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(orv.id, CHAR)
            AND llr.type = '7' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE orv.delete_flag = 0
        <if test="orderId != null">
            AND orv.order_id = #{orderId}
        </if>
        <if test="lawyerUserId != null">
            AND orv.lawyer_user_id = #{lawyerUserId}
        </if>
        <if test="userId != null">
            AND orv.user_id = #{userId}
        </if>
        ORDER BY orv.created_time DESC
    </select>

    <!-- 根据订单ID查询评价 -->
    <select id="getReviewByOrderId" resultMap="OrderReviewVoResultMap">
        SELECT
            orv.id,
            orv.order_id,
            orv.order_number,
            orv.user_id,
            CASE 
                WHEN orv.is_anonymous = 1 THEN '匿名用户'
                ELSE lu.user_name
            END AS user_name,
            CASE 
                WHEN orv.is_anonymous = 1 THEN NULL
                ELSE lu.user_image
            END AS user_avatar,
            orv.lawyer_user_id,
            lu2.name AS lawyer_name,
            lu2.head_img AS lawyer_avatar,
            lf.firm_name AS law_firm_name,
            orv.overall_rating,
            orv.service_attitude_rating,
            orv.response_time_rating,
            orv.professional_ability_rating,
            orv.review_content,
            orv.is_anonymous,
            orv.like_count,
            orv.comment_count,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            orv.created_time
        FROM lawyer_order_review orv
        LEFT JOIN life_user lu ON lu.id = orv.user_id AND lu.delete_flag = 0
        LEFT JOIN lawyer_user lu2 ON lu2.id = orv.lawyer_user_id AND lu2.delete_flag = 0
        LEFT JOIN law_firm lf ON lf.id = lu2.firm_id AND lf.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(orv.id, CHAR)
            AND llr.type = '7' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE orv.delete_flag = 0
        AND orv.order_id = #{orderId}
        LIMIT 1
    </select>

    <!-- 根据评价ID查询评价详情（包含用户和律师信息） -->
    <select id="getReviewDetailById" resultMap="OrderReviewVoResultMap">
        SELECT
            orv.id,
            orv.order_id,
            orv.order_number,
            orv.user_id,
            CASE 
                WHEN orv.is_anonymous = 1 THEN '匿名用户'
                ELSE lu.user_name
            END AS user_name,
            CASE 
                WHEN orv.is_anonymous = 1 THEN NULL
                ELSE lu.user_image
            END AS user_avatar,
            orv.lawyer_user_id,
            lu2.name AS lawyer_name,
            lu2.head_img AS lawyer_avatar,
            lf.firm_name AS law_firm_name,
            orv.overall_rating,
            orv.service_attitude_rating,
            orv.response_time_rating,
            orv.professional_ability_rating,
            orv.review_content,
            orv.review_images,
            orv.is_anonymous,
            COALESCE((
                SELECT COUNT(1) 
                FROM life_like_record llr_count 
                WHERE CONVERT(llr_count.huifu_id, CHAR) = CONVERT(orv.id, CHAR)
                    AND llr_count.type = '7'
                    AND llr_count.delete_flag = 0
            ), 0) AS like_count,
            orv.comment_count,
            CASE 
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            orv.created_time
        FROM lawyer_order_review orv
        LEFT JOIN life_user lu ON lu.id = orv.user_id AND lu.delete_flag = 0
        LEFT JOIN lawyer_user lu2 ON lu2.id = orv.lawyer_user_id AND lu2.delete_flag = 0
        LEFT JOIN law_firm lf ON lf.id = lu2.firm_id AND lf.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(orv.id, CHAR)
            AND llr.type = '7' 
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE orv.delete_flag = 0
        AND orv.id = #{reviewId}
    </select>

    <!-- 评价列表结果映射 -->
    <resultMap id="PendingReviewVoResultMap" type="shop.alien.entity.store.vo.PendingReviewVo">
        <result column="order_id" property="orderId" />
        <result column="order_number" property="orderNumber" />
        <result column="lawyer_user_id" property="lawyerUserId" />
        <result column="lawyer_name" property="lawyerName" />
        <result column="lawyer_avatar" property="lawyerAvatar" />
        <result column="law_firm_name" property="lawFirmName" />
        <result column="overall_rating" property="overallRating" />
        <result column="review_content" property="reviewContent" />
        <result column="created_time" property="createdTime" />
        <result column="completed_time" property="completedTime" />
    </resultMap>

    <!-- 分页查询待评价列表（查询用户已完成但未评价的订单对应的律师信息） -->
    <select id="getPendingReviewList" resultMap="PendingReviewVoResultMap">
        SELECT
            lco.id AS order_id,
            lco.order_number,
            lco.lawyer_user_id,
            lu.name AS lawyer_name,
            lu.head_img AS lawyer_avatar,
            lf.firm_name AS law_firm_name,
            NULL AS overall_rating,
            NULL AS review_content,
            lco.end_time AS created_time,
            lco.end_time AS completed_time
        FROM lawyer_consultation_order lco
        LEFT JOIN lawyer_user lu ON lu.id = lco.lawyer_user_id AND lu.delete_flag = 0
        LEFT JOIN law_firm lf ON lf.id = lu.firm_id AND lf.delete_flag = 0
        LEFT JOIN lawyer_order_review orv ON orv.order_id = lco.id AND orv.delete_flag = 0
        WHERE lco.delete_flag = 0
        AND lco.order_status = 3
        AND lco.is_appealed = 0
        AND lco.client_user_id = #{userId}
        AND orv.id IS NULL
        ORDER BY lco.end_time DESC
    </select>

    <!-- 计算律师的平均评分（overallRating的平均值） -->
    <select id="getAverageRatingByLawyerUserId" resultType="java.lang.Double">
        SELECT AVG(overall_rating)
        FROM lawyer_order_review
        WHERE lawyer_user_id = #{lawyerUserId}
        AND delete_flag = 0
        AND overall_rating IS NOT NULL
    </select>

    <!-- 统计律师的好评数（4.5-5分） -->
    <select id="getGoodReviewCountByLawyerUserId" resultType="java.lang.Integer">
        <![CDATA[
        SELECT COUNT(*)
        FROM lawyer_order_review
        WHERE lawyer_user_id = #{lawyerUserId}
        AND delete_flag = 0
        AND overall_rating IS NOT NULL
        AND overall_rating >= 4.5
        AND overall_rating <= 5
        ]]>
    </select>

    <!-- 统计律师的中评数（3-4分） -->
    <select id="getMediumReviewCountByLawyerUserId" resultType="java.lang.Integer">
        <![CDATA[
        SELECT COUNT(*)
        FROM lawyer_order_review
        WHERE lawyer_user_id = #{lawyerUserId}
        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="getBadReviewCountByLawyerUserId" resultType="java.lang.Integer">
        <![CDATA[
        SELECT COUNT(*)
        FROM lawyer_order_review
        WHERE lawyer_user_id = #{lawyerUserId}
        AND delete_flag = 0
        AND overall_rating IS NOT NULL
        AND overall_rating >= 0
        AND overall_rating < 3
        ]]>
    </select>

    <!-- 统计律师的全部评价数量 -->
    <select id="getTotalReviewCountByLawyerUserId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM lawyer_order_review
        WHERE lawyer_user_id = #{lawyerUserId}
        AND delete_flag = 0
    </select>

    <!-- 统计律师的有图评价数量 -->
    <select id="getImageReviewCountByLawyerUserId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM lawyer_order_review
        WHERE lawyer_user_id = #{lawyerUserId}
        AND delete_flag = 0
        AND review_images IS NOT NULL
        AND review_images != ''
        AND TRIM(review_images) != ''
    </select>

    <!-- 根据律师ID和类型分页查询评价列表（包含用户和律师信息） -->
    <select id="getReviewListByLawyerAndType" resultMap="OrderReviewVoResultMap">
        SELECT
            orv.id,
            orv.order_id,
            orv.order_number,
            orv.user_id,
            CASE
                WHEN orv.is_anonymous = 1 THEN '匿名用户'
                ELSE lu.user_name
            END AS user_name,
            CASE
                WHEN orv.is_anonymous = 1 THEN NULL
                ELSE lu.user_image
            END AS user_avatar,
            orv.lawyer_user_id,
            lu2.name AS lawyer_name,
            lu2.head_img AS lawyer_avatar,
            lf.firm_name AS law_firm_name,
            orv.overall_rating,
            orv.service_attitude_rating,
            orv.response_time_rating,
            orv.professional_ability_rating,
            orv.review_content,
            orv.is_anonymous,
            COALESCE((
                SELECT COUNT(1)
                FROM life_like_record llr_count
                WHERE CONVERT(llr_count.huifu_id, CHAR) = CONVERT(orv.id, CHAR)
                    AND llr_count.type = '7'
                    AND llr_count.delete_flag = 0
            ), 0) AS like_count,
            orv.comment_count,
            orv.review_images,
            CASE
                WHEN #{currentUserId} IS NOT NULL AND llr.id IS NOT NULL THEN 1
                ELSE 0
            END AS is_liked,
            orv.created_time
        FROM lawyer_order_review orv
        LEFT JOIN life_user lu ON lu.id = orv.user_id AND lu.delete_flag = 0
        LEFT JOIN lawyer_user lu2 ON lu2.id = orv.lawyer_user_id AND lu2.delete_flag = 0
        LEFT JOIN law_firm lf ON lf.id = lu2.firm_id AND lf.delete_flag = 0
        LEFT JOIN life_like_record llr ON CONVERT(llr.huifu_id, CHAR) = CONVERT(orv.id, CHAR)
            AND llr.type = '7'
            AND CONVERT(llr.dianzan_id, CHAR) = CONVERT(#{currentUserId}, CHAR)
            AND llr.delete_flag = 0
        WHERE orv.delete_flag = 0
        AND orv.lawyer_user_id = #{lawyerUserId}
        <if test="type != null">
            <choose>
                <when test="type == 1">
                    <![CDATA[
                    AND orv.overall_rating >= 4.5
                    AND orv.overall_rating <= 5
                    ]]>
                </when>
                <when test="type == 2">
                    <![CDATA[
                    AND orv.overall_rating >= 3
                    AND orv.overall_rating < 4.5
                    ]]>
                </when>
                <when test="type == 3">
                    <![CDATA[
                    AND orv.overall_rating >= 0
                    AND orv.overall_rating < 3
                    ]]>
                </when>
                <when test="type == 4">
                    AND orv.review_images IS NOT NULL
                    AND orv.review_images != ''
                    AND TRIM(orv.review_images) != ''
                </when>
            </choose>
        </if>
        ORDER BY orv.created_time DESC
    </select>

    <!-- 根据订单ID查询评价 -->
    <select id="getOrderEvaluation" resultMap="OrderReviewVoResultMap">
        SELECT
            orv.id,
            orv.order_id,
            orv.order_number,
            orv.user_id,
            orv.lawyer_user_id,
            orv.overall_rating,
            orv.service_attitude_rating,
            orv.response_time_rating,
            orv.professional_ability_rating,
            orv.review_content,
            orv.review_images,
            orv.is_anonymous,
            orv.like_count,
            orv.comment_count,
            orv.created_time,
            lco.is_appealed,
            lu.user_name
        FROM lawyer_order_review orv
        INNER JOIN lawyer_consultation_order lco ON lco.id = orv.order_id AND lco.delete_flag = 0
        LEFT JOIN life_user lu ON lu.id = orv.user_id AND lu.delete_flag = 0
        WHERE orv.delete_flag = 0
        AND orv.order_id = #{orderId}
        LIMIT 1
    </select>

</mapper>

