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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="shop.alien.entity.store.LawyerConsultationOrder">
        <id column="id" property="id" />
        <result column="order_number" property="orderNumber" />
        <result column="client_user_id" property="clientUserId" />
        <result column="lawyer_user_id" property="lawyerUserId" />
        <result column="problem_scenario_id" property="problemScenarioId" />
        <result column="problem_description" property="problemDescription" />
        <result column="order_amount" property="orderAmount" />
        <result column="consultation_fee" property="consultationFee" />
        <result column="start_time" property="startTime" />
        <result column="end_time" property="endTime" />
        <result column="order_status" property="orderStatus" />
        <result column="payment_status" property="paymentStatus" />
        <result column="order_time" property="orderTime" />
        <result column="payment_time" property="paymentTime" />
        <result column="validity_period" property="validityPeriod" />
        <result column="rating" property="rating" />
        <result column="comment" property="comment" />
        <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="alipay_no" property="alipayNo" />
        <result column="order_str" property="orderStr" />
        <result column="place_id" property="placeId" />
        <result column="lawyer_earnings" property="lawyerEarnings" />
        <result column="accept_orders_time" property="acceptOrdersTime" />
        <result column="reason_order_refusal" property="reasonOrderRefusal" />
        <result column="accept_orders_status" property="acceptOrdersStatus" />
        <result column="apply_refund_status" property="applyRefundStatus" />
        <result column="apply_refund_time" property="applyRefundTime" />
        <result column="apply_refund_process_time" property="applyRefundProcessTime" />
        <result column="apply_refund_reason" property="applyRefundReason" />
        <result column="reject_refund_reason" property="rejectRefundReason" />
        <result column="is_appealed" property="isAppealed" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, order_number, client_user_id, lawyer_user_id, problem_scenario_id, problem_description,
        order_amount, consultation_fee, start_time, end_time, order_status, payment_status,
        order_time,payment_time, validity_period, rating, comment, delete_flag, created_time,
        created_user_id, updated_time, updated_user_id, alipay_no, order_str, place_id
    </sql>

    <!-- 訂單VO結果映射，包含律師信息 -->
    <resultMap id="OrderVOWithLawyerResultMap" type="shop.alien.entity.store.vo.LawyerConsultationOrderVO">
        <!-- 訂單基本信息 -->
        <id column="id" property="id" />
        <result column="order_number" property="orderNumber" />
        <result column="client_user_id" property="clientUserId" />
        <result column="lawyer_user_id" property="lawyerUserId" />
        <result column="problem_scenario_id" property="problemScenarioId" />
        <result column="problem_description" property="problemDescription" />
        <result column="order_amount" property="orderAmount" />
        <result column="consultation_fee" property="consultationFee" />
        <result column="start_time" property="startTime" />
        <result column="end_time" property="endTime" />
        <result column="order_status" property="orderStatus" />
        <result column="payment_status" property="paymentStatus" />
        <result column="order_time" property="orderTime" />
        <result column="payment_time" property="paymentTime" />
        <result column="validity_period" property="validityPeriod" />
        <result column="rating" property="rating" />
        <result column="comment" property="comment" />
        <result column="created_time" property="createdTime" />
        <!-- 律師信息 -->
        <result column="lawyer_name" property="lawyerName" />
        <result column="lawyer_phone" property="lawyerPhone" />
        <result column="lawyer_email" property="lawyerEmail" />
        <result column="lawyer_certificate_no" property="lawyerCertificateNo" />
        <result column="law_firm" property="lawFirm" />
        <result column="practice_years" property="practiceYears" />
        <result column="specialty_fields" property="specialtyFields" />
        <result column="certification_status" property="certificationStatus" />
        <result column="service_score" property="serviceScore" />
        <result column="service_count" property="serviceCount" />
        <result column="lawyer_consultation_fee" property="lawyerConsultationFee" />
        <result column="province" property="province" />
        <result column="city" property="city" />
        <result column="district" property="district" />
        <result column="address" property="address" />
        <result column="head_img" property="headImg" />
        <result column="nick_name" property="nickName" />
        <result column="personal_introduction" property="personalIntroduction" />
        <result column="expertise_area_info" property="expertiseAreaInfo" />
        <result column="alipay_no" property="alipayNo" />
        <result column="order_str" property="orderStr" />
    </resultMap>

    <!-- 分頁查詢訂單列表，聯查律師信息 -->
    <select id="getConsultationOrderListWithLawyer" resultMap="OrderVOWithLawyerResultMap">
        SELECT
            o.id,
            o.order_number,
            o.client_user_id,
            o.lawyer_user_id,
            o.problem_scenario_id,
            o.problem_description,
            o.order_amount,
            o.consultation_fee,
            o.start_time,
            o.end_time,
            o.order_status,
            o.payment_status,
            o.order_time,
            o.payment_time,
            o.validity_period,
            o.rating,
            o.comment,
            o.created_time,
            -- 律師信息
            l.name AS lawyer_name,
            l.phone AS lawyer_phone,
            l.email AS lawyer_email,
            l.lawyer_certificate_no,
            l.law_firm,
            l.practice_years,
            l.specialty_fields,
            l.certification_status,
            l.service_score,
            l.service_count,
            l.consultation_fee AS lawyer_consultation_fee,
            l.province,
            l.city,
            l.district,
            l.address,
            l.head_img,
            l.nick_name,
            l.personal_introduction
        FROM lawyer_consultation_order o
        LEFT JOIN lawyer_user l ON o.lawyer_user_id = l.id AND l.delete_flag = 0
        WHERE o.delete_flag = 0
        <if test="orderNumber != null and orderNumber != ''">
            AND o.order_number = #{orderNumber}
        </if>
        <if test="clientUserId != null">
            AND o.client_user_id = #{clientUserId}
        </if>
        <if test="lawyerUserId != null">
            AND o.lawyer_user_id = #{lawyerUserId}
        </if>
        <if test="lawyerUserIds != null and lawyerUserIds.size() > 0">
            AND o.lawyer_user_id IN
            <foreach collection="lawyerUserIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="lawyerName != null and lawyerName != '' and (lawyerUserIds == null or lawyerUserIds.size() == 0)">
            AND l.name LIKE CONCAT('%', #{lawyerName}, '%')
        </if>
        <if test="orderStatus != null">
            AND o.order_status = #{orderStatus}
        </if>
        ORDER BY o.created_time DESC
    </select>


    <!-- 根据用户ID查询订单列表 -->
    <select id="getConsultationOrderListById" resultMap="OrderVOWithLawyerResultMap">
        SELECT
        lco.id,
        lco.order_number,
        lco.client_user_id,
        lco.lawyer_user_id,
        lco.problem_scenario_id,
        lco.problem_description,
        lco.order_amount,
        lco.consultation_fee,
        lco.start_time,
        lco.end_time,
        lco.order_status,
        lco.payment_status,
        lco.order_time,
        lco.payment_time,
        lco.validity_period,
        lco.rating,
        lco.comment,
        lco.created_time,
        lco.alipay_no,
        lco.order_str,
        lu.name AS lawyer_name,
        lu.phone AS lawyer_phone,
        lu.email AS lawyer_email,
        lu.lawyer_certificate_no,
        lu.law_firm,
        lu.practice_years,
        lu.specialty_fields,
        lu.certification_status,
        lu.service_score,
        lu.service_count,
        lu.consultation_fee AS lawyer_consultation_fee,
        lu.province,
        lu.city,
        lu.district,
        lu.address,
        lu.head_img,
        lu.nick_name,
        lu.personal_introduction,
        lea.expertise_area_info
        FROM lawyer_consultation_order lco
        LEFT JOIN lawyer_user lu ON lco.lawyer_user_id = lu.id AND lu.delete_flag = 0
        left join lawyer_expertise_area lea on lea.id = lu.lawyer_expertise_area_id and lea.delete_flag = 0
        WHERE lco.delete_flag = 0
        <if test="userId != null and userId !=''">
            AND lco.client_user_id = #{userId}
        </if>
        <if test="lawyerUserIds != null and lawyerUserIds.size() > 0">
            AND lco.lawyer_user_id IN
            <foreach collection="lawyerUserIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="orderStatus != null and orderStatus !=''">
            AND lco.order_status = #{orderStatus}
        </if>
        ORDER BY lco.created_time DESC
    </select>
</mapper>

