<?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.inhospitalcharge.mapper.InHospitalRegisterAppMapper">

    <select id="getInHospitalRegisterInfo" resultType="com.openhis.web.inhospitalcharge.dto.InHospitalRegisterQueryDto">
        SELECT ihri.tenant_id,
        ihri.encounter_id,
        ihri.amb_encounter_id,
        ihri.patient_id,
        ihri.request_time,
        ihri.registrar,
        ihri.source_name,
        ihri.patient_name,
        ihri.gender_enum,
        ihri.birth_date,
        ihri.ward_name
        from (SELECT ae.tenant_id,
        ae.ID AS encounter_id,
        ae.amb_encounter_id AS amb_encounter_id,
        ae.patient_id AS patient_id,
        ae.create_time AS request_time,
        aper.NAME AS registrar,
        ao.NAME AS source_name,
        ap.NAME AS patient_name,
        ap.gender_enum AS gender_enum,
        ap.birth_date AS birth_date,
        al.NAME AS ward_name
        FROM adm_encounter AS ae
        LEFT JOIN adm_encounter AS ambae ON ae.amb_encounter_id = ambae.
        ID
        LEFT JOIN adm_organization AS ao ON ao.ID = ambae.organization_id
        AND ao.delete_flag = '0'
        LEFT JOIN adm_patient AS ap ON ap.ID = ae.patient_id
        AND ap.delete_flag = '0'
        LEFT JOIN adm_encounter_location AS ael ON ael.encounter_id = ae.ID
        AND ael.delete_flag = '0'
        AND ael.form_enum = #{formEnum}
        LEFT JOIN adm_location AS al ON al.ID = ael.location_id
        AND al.delete_flag = '0'
        LEFT JOIN adm_practitioner AS aper ON aper.ID = ae.registrar_id
        AND aper.delete_flag = '0'
        WHERE ae.delete_flag = '0'
        AND ae.class_enum = #{encounterClass}
        <if test="registeredFlag == '0'.toString()">
            AND ae.status_enum = #{encounterStatus}
        </if>
        <if test="registeredFlag == '1'.toString()">
            AND ae.status_enum != #{encounterStatus}
        </if>
        ) AS ihri
        ${ew.customSqlSegment}
    </select>

    <select id="getPatientInfo" resultType="com.openhis.web.inhospitalcharge.dto.InHospitalPatientInfoDto">
        SELECT ap.id   AS patient_id,
               ap.bus_no,
               ap.NAME AS patient_name,
               ap.id_card,
               ap.birth_date,
               ap.gender_enum,
               ap.nationality_code,
               ap.country_code,
               ap.marital_status_enum,
               ap.prfs_enum,
               ap.phone,
               ap.address,
               ap.native_place,
               ap.work_company
        FROM adm_patient AS ap
        WHERE ap.delete_flag = '0'
          AND ap.ID = #{patientId}
    </select>

    <select id="getInHospitalInfo" resultType="com.openhis.web.inhospitalcharge.dto.InHospitalInfoDto">
        SELECT ae.ID                                  AS encounter_id,
               ae.bus_no                              AS bus_no,
               (SELECT COUNT(1)
                FROM adm_encounter
                WHERE delete_flag = '0'
                  AND class_enum = 1
                  AND patient_id = ae.patient_id) - 1 AS in_hospital_count,
               ae.organization_id                     AS in_hospital_org_id,
               ae.amb_encounter_id                    AS amb_encounter_id,
               ae.priority_enum                       AS priority_enum,
               ae.admit_source_code                   AS admit_source_code,
               ae.in_way_code                         AS in_way_code,
               ae.start_time                          AS start_time,
               ao.NAME                                AS in_hospital_org_name,
               al.ID                                  AS ward_location_id,
               al.NAME                                AS ward_name,
               al.bus_no                              AS ward_bus_no,
               ap.ID                                  AS amb_doctor_practitioner_id,
               ap.NAME                                AS amb_doctor_practitioner_name,
               ccd.NAME                               AS amb_diagnosis_name,
               COALESCE(aa.balance_amount, 0)         AS balance_amount
        FROM adm_encounter AS ae
                 LEFT JOIN adm_organization AS ao ON ao.ID = ae.organization_id
            AND ao.delete_flag = '0'
                 LEFT JOIN adm_encounter_location AS ael ON ael.encounter_id = ae.ID
            AND ael.delete_flag = '0'
            AND ael.form_enum = #{formEnum}
                 LEFT JOIN adm_location AS al ON al.ID = ael.location_id
            AND al.delete_flag = '0'
                 LEFT JOIN adm_encounter_participant AS aep ON aep.encounter_id = ae.amb_encounter_id
            AND aep.delete_flag = '0'
            AND aep.type_code = #{participantType}
                 LEFT JOIN adm_practitioner AS ap ON ap.ID = aep.practitioner_id
            AND ap.delete_flag = '0'
                 LEFT JOIN adm_encounter_diagnosis AS aed ON aed.encounter_id = ae.amb_encounter_id
            AND aed.delete_flag = '0'
            AND aed.maindise_flag = #{maindiseFlag}
                 LEFT JOIN cli_condition AS cc ON cc.ID = aed.condition_id
            AND cc.delete_flag = '0'
                 LEFT JOIN cli_condition_definition AS ccd ON ccd.ID = cc.definition_id
            AND ccd.delete_flag = '0'
                 LEFT JOIN adm_account AS aa ON aa.encounter_id = ae.ID
            AND aa.delete_flag = '0'
            AND aa.type_code = #{accountType}
        WHERE ae.ID = #{encounterId}
    </select>

    <select id="getTotalBedsNum" resultType="integer">
        SELECT COUNT
                   (1)
        FROM adm_location
        WHERE delete_flag = '0'
          AND form_enum = #{formEnum}
          AND status_enum != #{status}
          AND bus_no LIKE concat('%'
            , #{wardBusNo}
            , '%')
    </select>

    <select id="getIdleBedsNum" resultType="integer">
        SELECT COUNT
                   (1)
        FROM adm_location
        WHERE delete_flag = '0'
          AND form_enum = #{formEnum}
          AND status_enum = #{status}
          AND bus_no LIKE concat('%'
            , #{wardBusNo}
            , '%')
    </select>

</mapper>