<?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.patientmanage.mapper.PatientManageMapper">
    <!-- 病人信息相关查询-->
    <select id="getPatientPage" resultType="com.openhis.web.patientmanage.dto.PatientInformationDto">
        SELECT
            T1.tenant_id,
            T1.id,
            T1.active_flag,
            T1.temp_flag,
            T1.name,
            T1.name_json,
            T1.bus_no,
            T1.gender_enum,
            T1.birth_date,
            T1.deceased_date,
            T1.marital_status_enum,
            T1.prfs_enum,
            T1.phone,
            T1.address,
            T1.address_province,
            T1.address_city,
            T1.address_district,
            T1.address_street,
            T1.address_json,
            T1.nationality_code,
            T1.id_card,
            T1.py_str,
            T1.wb_str,
            T1.blood_abo,
            T1.blood_rh,
            T1.work_company,
            T1.native_place,
            T1.country_code,
            T1.link_name,
            T1.link_relation_code,
            T1.link_telcom,
            T1.link_jsons,
            T1.organization_id,
            T1.create_time,
            T1.type_code,
            T1.organization_name
        FROM
            (
                SELECT
                    pt.tenant_id,
                    pt.id,
                    pt.active_flag,
                    pt.temp_flag,
                    pt.name,
                    pt.name_json,
                    pt.bus_no,
                    pt.gender_enum,
                    pt.birth_date,
                    pt.deceased_date,
                    pt.marital_status_enum,
                    pt.prfs_enum,
                    pt.phone,
                    pt.address,
                    pt.address_province,
                    pt.address_city,
                    pt.address_district,
                    pt.address_street,
                    pt.address_json,
                    pt.nationality_code,
                    pt.id_card,
                    pt.py_str,
                    pt.wb_str,
                    pt.blood_abo,
                    pt.blood_rh,
                    pt.work_company,
                    pt.native_place,
                    pt.country_code,
                    pt.link_name,
                    pt.link_relation_code,
                    pt.link_telcom,
                    pt.link_jsons,
                    pt.organization_id,
                    pt.create_time,
                    api.type_code,
                    ogt.name as organization_name
                FROM adm_patient pt
                         LEFT JOIN adm_organization ogt ON pt.organization_id = ogt.id
                         LEFT JOIN adm_patient_identifier api ON pt.id = api.patient_id
                ORDER BY pt.bus_no DESC
            ) AS T1
            ${ew.customSqlSegment}
    </select>

    <!-- 查询医生名字列表-->
    <select id="getDoctorNames" resultType ="java.lang.String">
        SELECT MIN(p.name) AS practitioner_name
        FROM adm_encounter_participant ep
                 LEFT JOIN adm_practitioner p ON ep.practitioner_id = p.id
        WHERE ep.type_code = '1'
        GROUP BY ep.practitioner_id
        HAVING MIN(p.name) IS NOT NULL;
    </select>

    <!-- 门诊记录相关查询-->
    <select id="getOutpatientRecord" parameterType="com.openhis.web.patientmanage.dto.OutpatientRecordSearchParam"
            resultType="com.openhis.web.patientmanage.dto.OutpatientRecordDto">

        SELECT
        T1.tenant_id,
        T1.name,
        T1.id_card,
        T1.description,
        T1.patient_bus_no,
        T1.encounter_bus_no,
        T1.gender_enum,
        T1.encounter_time,
        T1.subject_status_enum,
        T1.organization_name,
        T1.doctor_name,
        T1.phone,
        T1.start_time
        FROM
        (
        SELECT
        e.tenant_id,
        pt.name,
        pt.id_card,
        ccd.name AS description,
        pt.bus_no as patient_bus_no,
        e.bus_no as encounter_bus_no,
        pt.gender_enum,
        e.reception_time as encounter_time,
        e.subject_status_enum,
        ogt.name as organization_name,
        pt.phone,
        e.start_time,
        (SELECT pra.name
            FROM adm_encounter_participant ep
            LEFT JOIN adm_practitioner pra ON ep.practitioner_id = pra.id
            WHERE ep.encounter_id = e.id
            AND ep.type_code = #{typeCode} --就诊参与者身份类型是1:接诊医生
            ORDER BY ep.create_time DESC
            LIMIT 1
        ) AS doctor_name--门诊医生
        FROM
        adm_encounter e
        LEFT JOIN
        adm_organization ogt ON e.organization_id = ogt.id
        LEFT JOIN
        adm_patient pt ON e.patient_id = pt.id
        LEFT JOIN adm_encounter_diagnosis ed ON e.id = ed.encounter_id
        LEFT JOIN cli_condition c ON ed.condition_id = c.id
        LEFT JOIN cli_condition_definition ccd ON c.definition_id = ccd.id
        ) AS T1
        ${ew.customSqlSegment}
    </select>

</mapper>