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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="shop.alien.entity.store.CommentAppeal">
        <id column="id" property="id" />
        <result column="comment_id" property="commentId" />
        <result column="comment_info" property="commentInfo" />
        <result column="appeal_reason" property="appealReason" />
        <result column="status" property="status" />
        <result column="delete_flag" property="deleteFlag" />
        <result column="created_time" property="createdTime" />
        <result column="created_user_id" property="createdUserId" />
        <result column="updated_time" property="updatedTime" />
        <result column="updated_user_id" property="updatedUserId" />
        <result column="appeal_time" property="appealTime" />
        <result column="order_id" property="orderId" />
    </resultMap>

    <!-- 申诉历史列表查询结果映射 -->
    <resultMap id="CommentAppealVoResultMap" type="shop.alien.entity.store.vo.CommentAppealVo">
        <id column="id" property="id" />
        <result column="comment_id" property="commentId" />
        <result column="comment_info" property="commentInfo" />
        <result column="appeal_reason" property="appealReason" />
        <result column="status" property="status" />
        <result column="appeal_time" property="appealTime" />
        <result column="appeal_review_time" property="appealReviewTime" />
        <result column="review_reasons" property="reviewReasons" />
        <result column="img_url" property="imgUrl" />
        <result column="created_time" property="createdTime" />
        <result column="created_user_id" property="createdUserId" />
        <result column="updated_time" property="updatedTime" />
        <result column="updated_user_id" property="updatedUserId" />
        <result column="audit_user_id" property="auditUserId" />
        <result column="appeal_number" property="appealNumber" />
        <!-- 评价相关信息 -->
        <result column="review_user_id" property="reviewUserId" />
        <result column="review_user_name" property="reviewUserName" />
        <result column="user_name" property="userName" />
        <result column="review_user_avatar" property="reviewUserAvatar" />
        <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_time" property="reviewTime" />
        <result column="review_images" property="reviewImages" />
        <!-- 中台列表展示字段 -->
        <result column="order_number" property="orderNumber" />
        <result column="user_phone" property="userPhone" />
        <result column="lawyer_user_id" property="lawyerUserId" />
        <result column="lawyer_name" property="lawyerName" />
        <result column="lawyer_phone" property="lawyerPhone" />
        <result column="order_id" property="orderId" />
    </resultMap>

    <!-- 查询申诉历史列表（包含评价和用户信息） -->
    <select id="getAppealHistoryList" resultMap="CommentAppealVoResultMap">
        SELECT
            ca.id,
            ca.comment_id,
            ca.comment_info,
            ca.appeal_reason,
            ca.status,
            ca.appeal_time,
            ca.appeal_review_time,
            ca.review_reasons,
            ca.img_url,
            ca.created_time,
            ca.created_user_id,
            ca.updated_time,
            ca.updated_user_id,
            ca.updated_user_id AS audit_user_id,
            ca.order_id,
            orv.user_id AS review_user_id,
            lu.user_name,
            CASE
                WHEN orv.is_anonymous = 1 THEN NULL
                ELSE lu.user_image
            END AS review_user_avatar,
            orv.overall_rating,
            orv.service_attitude_rating,
            orv.response_time_rating,
            orv.professional_ability_rating,
            orv.review_content,
            orv.created_time AS review_time,
            orv.review_images
        FROM comment_appeals ca
        LEFT JOIN lawyer_order_review orv ON orv.id = ca.comment_id
        LEFT JOIN life_user lu ON lu.id = orv.user_id AND lu.delete_flag = 0
        WHERE ca.delete_flag = 0
        <if test="status != null">
            AND ca.status = #{status}
        </if>
        <if test="lawyerUserId != null">
            AND orv.lawyer_user_id = #{lawyerUserId}
        </if>
        ORDER BY ca.appeal_time DESC
    </select>

    <!-- 根据ID查询申诉详情（包含评价和用户信息） -->
    <select id="getAppealDetailById" resultMap="CommentAppealVoResultMap">
        SELECT
            ca.id,
            ca.comment_id,
            ca.comment_info,
            ca.appeal_reason,
            ca.status,
            ca.appeal_time,
            ca.appeal_review_time,
            ca.review_reasons,
            ca.img_url,
            ca.created_time,
            ca.created_user_id,
            ca.updated_time,
            ca.updated_user_id,
            ca.updated_user_id AS audit_user_id,
            ca.appeal_number,
            ca.order_id,
            orv.user_id AS review_user_id,
            lu.user_name AS review_user_name,
            CASE
                WHEN orv.is_anonymous = 1 THEN NULL
                ELSE lu.user_image
            END AS review_user_avatar,
            orv.overall_rating,
            orv.service_attitude_rating,
            orv.response_time_rating,
            orv.professional_ability_rating,
            orv.review_content,
            orv.created_time AS review_time,
            orv.review_images
        FROM comment_appeals ca
        LEFT JOIN lawyer_order_review orv ON orv.id = ca.comment_id
        LEFT JOIN life_user lu ON lu.id = orv.user_id AND lu.delete_flag = 0
        WHERE ca.delete_flag = 0
        AND ca.id = #{id}
        LIMIT 1
    </select>

    <!-- 分页查询申诉列表（中台使用，支持多条件筛选） -->
    <select id="getAppealPageWithFilters" resultMap="CommentAppealVoResultMap">
        SELECT
            ca.id,
            ca.comment_id,
            ca.comment_info,
            ca.appeal_reason,
            ca.status,
            ca.appeal_time,
            ca.appeal_review_time,
            ca.review_reasons,
            ca.img_url,
            ca.created_time,
            ca.created_user_id,
            ca.updated_time,
            ca.updated_user_id,
            ca.updated_user_id AS audit_user_id,
            ca.order_id,
            orv.order_number,
            orv.user_id AS review_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 review_user_avatar,
            lu.user_phone AS user_phone,
            orv.lawyer_user_id AS lawyer_user_id,
            lu2.name AS lawyer_name,
            lu2.phone AS lawyer_phone,
            orv.overall_rating,
            orv.service_attitude_rating,
            orv.response_time_rating,
            orv.professional_ability_rating,
            orv.review_content,
            orv.created_time AS review_time,
            orv.review_images
        FROM comment_appeals ca
        LEFT JOIN lawyer_order_review orv ON orv.id = ca.comment_id
        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
        WHERE ca.delete_flag = 0
        <if test="status != null">
            AND ca.status = #{status}
        </if>
        <if test="orderNumber != null and orderNumber != ''">
            AND orv.order_number LIKE CONCAT('%', #{orderNumber}, '%')
        </if>
        <if test="userName != null and userName != ''">
            AND lu.user_name LIKE CONCAT('%', #{userName}, '%')
        </if>
        <if test="userPhone != null and userPhone != ''">
            AND lu.user_phone LIKE CONCAT('%', #{userPhone}, '%')
        </if>
        <if test="lawyerName != null and lawyerName != ''">
            AND lu2.name LIKE CONCAT('%', #{lawyerName}, '%')
        </if>
        <if test="lawyerPhone != null and lawyerPhone != ''">
            AND lu2.phone LIKE CONCAT('%', #{lawyerPhone}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
            AND ca.appeal_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND ca.appeal_time &lt;= #{endTime}
        </if>
        ORDER BY ca.appeal_time DESC
    </select>

</mapper>


