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

    <!-- 订单结果映射，包含律师信息 -->
    <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="alipay_no" property="alipayNo" />
        <result column="order_str" property="orderStr" />
        <result column="place_id" property="placeId" />
        <!-- 律师信息 -->
        <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" />
    </resultMap>

    <!-- 查询订单列表（分页，关联律师信息）-->
    <select id="getOrderListWithLawyer" 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,
            lco.place_id,
            -- 律师信息
            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
        FROM lawyer_consultation_order lco
        LEFT JOIN lawyer_user lu ON lco.lawyer_user_id = lu.id AND lu.delete_flag = 0
        WHERE lco.delete_flag = 0
        <if test="orderNumber != null and orderNumber != ''">
            AND lco.order_number LIKE CONCAT('%', #{orderNumber}, '%')
        </if>
        <if test="clientUserId != null">
            AND lco.client_user_id = #{clientUserId}
        </if>
        <if test="lawyerUserId != null">
            AND lco.lawyer_user_id = #{lawyerUserId}
        </if>
        <if test="lawyerName != null and lawyerName != ''">
            AND lu.name LIKE CONCAT('%', #{lawyerName}, '%')
        </if>
        <if test="orderStatus != null">
            AND lco.order_status = #{orderStatus}
        </if>
        <if test="paymentStatus != null">
            AND lco.payment_status = #{paymentStatus}
        </if>
        <if test="startTime != null and startTime != ''">
            AND lco.created_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND lco.created_time &lt;= #{endTime}
        </if>
        ORDER BY lco.created_time DESC
    </select>

    <!-- 查询订单列表（不分页，关联律师信息） -->
    <select id="getOrderList" 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,
            lco.place_id,
            -- 律师信息
            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
        FROM lawyer_consultation_order lco
        LEFT JOIN lawyer_user lu ON lco.lawyer_user_id = lu.id AND lu.delete_flag = 0
        WHERE lco.delete_flag = 0
        <if test="orderNumber != null and orderNumber != ''">
            AND lco.order_number LIKE CONCAT('%', #{orderNumber}, '%')
        </if>
        <if test="clientUserId != null">
            AND lco.client_user_id = #{clientUserId}
        </if>
        <if test="lawyerUserId != null">
            AND lco.lawyer_user_id = #{lawyerUserId}
        </if>
        <if test="lawyerName != null and lawyerName != ''">
            AND lu.name LIKE CONCAT('%', #{lawyerName}, '%')
        </if>
        <if test="orderStatus != null">
            AND lco.order_status = #{orderStatus}
        </if>
        <if test="paymentStatus != null">
            AND lco.payment_status = #{paymentStatus}
        </if>
        <if test="startTime != null and startTime != ''">
            AND lco.created_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND lco.created_time &lt;= #{endTime}
        </if>
        ORDER BY lco.created_time DESC
    </select>

</mapper>
