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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="shop.alien.entity.store.LawyerUser">
        <id column="id" property="id" />
        <result column="phone" property="phone" />
        <result column="name" property="name" />
        <result column="id_card" property="idCard" />
        <result column="password" property="password" />
        <result column="pay_password" property="payPassword" />
        <result column="money" property="money" />
        <result column="status" property="status" />
        <result column="pass_type" property="passType" />
        <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="logout_flag" property="logoutFlag" />
        <result column="logout_reason" property="logoutReason" />
        <result column="logout_time" property="logoutTime" />
        <result column="logout_code" property="logoutCode" />
        <result column="nick_name" property="nickName" />
        <result column="account_blurb" property="accountBlurb" />
        <result column="head_img" property="headImg" />
        <result column="alipay_account" property="alipayAccount" />
        <result column="lawyer_certificate_no" property="lawyerCertificateNo" />
        <result column="law_firm" property="lawFirm" />
<!--        <result column="law_firm_id" property="lawFirmId" />-->
        <result column="practice_years" property="practiceYears" />
        <result column="practice_start_date" property="practiceStartDate" />
        <result column="specialty_fields" property="specialtyFields" />
        <result column="certification_status" property="certificationStatus" />
        <result column="certification_fail_reason" property="certificationFailReason" />
        <result column="certification_time" property="certificationTime" />
        <result column="certification_reviewer_id" property="certificationReviewerId" />
        <result column="certificate_image" property="certificateImage" />
        <result column="id_card_front_image" property="idCardFrontImage" />
        <result column="id_card_back_image" property="idCardBackImage" />
        <result column="service_score" property="serviceScore" />
        <result column="service_count" property="serviceCount" />
        <result column="good_review_count" property="goodReviewCount" />
        <result column="medium_review_count" property="mediumReviewCount" />
        <result column="bad_review_count" property="badReviewCount" />
        <result column="consultation_fee" property="consultationFee" />
        <result column="agency_fee" property="agencyFee" />
        <result column="province" property="province" />
        <result column="city" property="city" />
        <result column="district" property="district" />
        <result column="address" property="address" />
        <result column="email" property="email" />
        <result column="gender" property="gender" />
        <result column="birthday" property="birthday" />
        <result column="personal_introduction" property="personalIntroduction" />
        <result column="education_background" property="educationBackground" />
        <result column="work_experience" property="workExperience" />
        <result column="expertise_cases" property="expertiseCases" />
        <result column="is_online" property="isOnline" />
        <result column="last_online_time" property="lastOnlineTime" />
        <result column="is_recommended" property="isRecommended" />
        <result column="recommend_sort" property="recommendSort" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, phone, name, id_card, password, pay_password, money, status, pass_type, delete_flag,
        created_time, created_user_id, updated_time, updated_user_id, logout_flag, logout_reason,
        logout_time, logout_code, nick_name, account_blurb, head_img, alipay_account,
        lawyer_certificate_no, law_firm, law_firm_id, practice_years, practice_start_date,
        specialty_fields, certification_status, certification_fail_reason, certification_time,
        certification_reviewer_id, certificate_image, id_card_front_image, id_card_back_image,
        service_score, service_count, good_review_count, medium_review_count, bad_review_count,
        consultation_fee, agency_fee, province, city, district, address, email, gender, birthday,
        personal_introduction, education_background, work_experience, expertise_cases,
        is_online, last_online_time, is_recommended, recommend_sort
    </sql>

    <!-- 律师列表查询结果映射 -->
    <resultMap id="LawyerUserVoResultMap" type="shop.alien.entity.store.vo.LawyerUserVo">
        <id column="id" property="id" />
        <result column="phone" property="phone" />
        <result column="name" property="name" />
        <result column="firm_id" property="firmId" />
        <result column="head_img" property="headImg" />
        <result column="personal_introduction" property="personalIntroduction" />
        <result column="order_receiving_status" property="orderReceivingStatus" />
        <result column="practice_start_date" property="practiceStartDate" />
        <result column="address" property="address" />
        <result column="status" property="status" />
        <result column="delete_flag" property="deleteFlag" />
        <result column="created_time" property="createdTime" />
        <result column="firmName" property="firmName" />
        <result column="paymentNum" property="paymentNum" />
        <result column="scenarioNames" property="scenarioNames" />
        <result column="firstLevelScenario" property="firstLevelScenario" />
        <result column="expertiseAreaInfo" property="expertiseAreaInfo" />
    </resultMap>

    <!-- 查询律师列表（分页） -->
    <select id="selectLawyerUserList" resultMap="LawyerUserVoResultMap">
        SELECT
        user.id,
        user.phone,
        user.`name`,
        user.firm_id,
        user.head_img,
        user.personal_introduction,
        user.order_receiving_status,
        user.practice_start_date,
        user.address,
        user.status,
        user.delete_flag,
        user.created_time,
        user.is_recommended,
        firm.firm_name AS firmName,
        firmTwo.payment_account AS paymentNum,
        firmTwo.address AS address,
        GROUP_CONCAT(DISTINCT a.name SEPARATOR ',') AS scenarioNames,
        (SELECT GROUP_CONCAT(`name` SEPARATOR ',') FROM lawyer_legal_problem_scenario WHERE level = 1 AND delete_flag = 0) AS firstLevelScenario
        FROM
        lawyer_user user
        LEFT JOIN law_firm firm ON firm.id = user.firm_id
        LEFT JOIN law_firm_payment firmTwo ON firmTwo.firm_id = user.firm_id
        LEFT JOIN lawyer_service_area area ON area.lawyer_user_id = user.id AND area.delete_flag = 0 AND area.status = 1
        LEFT JOIN lawyer_legal_problem_scenario a ON a.id = area.problem_scenario_id AND a.delete_flag = 0 AND a.status = 1
        WHERE user.delete_flag = 0
        <if test="name != null and name != ''">AND user.name LIKE CONCAT('%', #{name}, '%')</if>
        <if test="phone != null and phone != ''">AND user.phone LIKE CONCAT('%', #{phone}, '%')</if>
        <if test="firmId != null">AND user.firm_id = #{firmId}</if>
        <if test="startTime != null and startTime != ''">AND user.practice_start_date &gt;= #{startTime}</if>
        <if test="endTime != null and endTime != ''">AND user.practice_start_date &lt;= #{endTime}</if>
        GROUP BY
        user.id
        ORDER BY user.created_time DESC
    </select>

    <!-- 查询律师列表总数 -->
    <select id="selectLawyerUserListCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT user.id)
        FROM
        lawyer_user user
        LEFT JOIN law_firm firm ON firm.id = user.firm_id
        WHERE user.delete_flag = 0
        <if test="name != null and name != ''">AND user.name LIKE CONCAT('%', #{name}, '%')</if>
        <if test="phone != null and phone != ''">AND user.phone LIKE CONCAT('%', #{phone}, '%')</if>
        <if test="firmId != null">AND user.firm_id = #{firmId}</if>
        <if test="createdTimeStart != null and createdTimeStart != ''">AND user.created_time &gt;= #{createdTimeStart}</if>
    </select>

</mapper>

