<?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="com.openhis.web.doctorstation.mapper.DoctorStationMainAppMapper">

    <select id="getPatientInfo" resultType="com.openhis.web.doctorstation.dto.PatientInfoDto">
        SELECT
        T10.encounter_id,
        T10.patient_id,
        T10.patient_name,
        T10.gender_enum,
        T10.id_card,
        T10.phone,
        T10.birth_date,
        T10.status_enum,
        T10.allergy_history_flag,
        T10.account_id,
        T10.type_code,
        T10.contract_name,
        T10.org_id,
        T10.register_time,
        T10.reception_time
        from
        (
        SELECT T1.tenant_id AS tenant_id,
        T1.ID AS encounter_id,
        T1.organization_id AS organization_id,
        T2.NAME AS organization_name,
        T3.NAME AS healthcare_name,
        T5.user_id AS practitioner_user_id,
        T5.NAME AS practitioner_name,
        T6.id AS account_id,
        T6.type_code AS type_code,
        T7.contract_name AS contract_name,
        T8.ID AS patient_id,
        T8.NAME AS patient_name,
        T8.gender_enum AS gender_enum,
        T8.id_card AS id_card,
        T8.phone AS phone,
        T8.birth_date AS birth_date,
        T1.status_enum AS status_enum,
        T1.create_time AS register_time,
        T1.reception_time AS reception_time,
        T1.organization_id AS org_id,
        CASE
        WHEN T9.patient_id IS NOT NULL THEN 1
        ELSE 0
        END AS allergy_history_flag
        FROM adm_encounter AS T1
        LEFT JOIN adm_organization AS T2 ON T1.organization_id = T2.ID AND T2.delete_flag = '0'
        LEFT JOIN adm_healthcare_service AS T3 ON T1.service_type_id = T3.ID AND T3.delete_flag = '0'
        LEFT JOIN adm_encounter_participant AS T4
        ON T1.ID = T4.encounter_id AND T4.type_code = #{participantType} AND T4.delete_flag = '0'
        LEFT JOIN adm_practitioner AS T5 ON T5.ID = T4.practitioner_id AND T5.delete_flag = '0'
        LEFT JOIN adm_account AS T6 ON T1.ID = T6.encounter_id AND T6.delete_flag = '0' and T6.encounter_flag = '1'
        LEFT JOIN fin_contract AS T7 ON T6.contract_no = T7.bus_no AND T7.delete_flag = '0'
        LEFT JOIN adm_patient AS T8 ON T1.patient_id = T8.ID AND T8.delete_flag = '0'
        LEFT JOIN cli_allergy_intolerance AS T9
        ON T1.patient_id = T9.patient_id AND T9.clinical_status_enum != #{ClinicalStatus} AND T9.delete_flag = '0'
        WHERE
        T1.delete_flag = '0'
        <!-- 当前登录账号ID 和 当前登录账号所属的科室ID 用于控制数据权限 -->
        <if test="userId != null and currentUserOrganizationId != null">
            AND ( T5.user_id = #{userId}
            OR (T1.organization_id = #{currentUserOrganizationId} AND T5.user_id != #{userId}) )
        </if>
        <if test="pricingFlag ==1">
            AND T1.status_enum != #{encounterStatus}
        </if>
        ORDER BY
        T1.create_time ) AS T10
        ${ew.customSqlSegment}
    </select>

</mapper>