<?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.chargemanage.mapper.OutpatientRegistrationAppMapper">

    <select id="getPractitionerMetadataPage" resultType="com.openhis.web.chargemanage.dto.PractitionerMetadata">
        SELECT T3.tenant_id,
               T3.ID,
               T3.NAME,
               T3.gender_enum,
               T3.py_str,
               T3.wb_str
        FROM (
                 SELECT T1.tenant_id,
                        T1.ID,
                        T1.NAME,
                        T1.gender_enum,
                        T1.py_str,
                        T1.wb_str
                 FROM adm_practitioner AS T1
                 WHERE T1.delete_flag = '0'
                   AND EXISTS(SELECT 1
                              FROM adm_practitioner_role AS T2
                              WHERE T2.practitioner_id = T1.ID
                                AND T2.location_id = #{locationId}
                                AND T2.ROLE_code = #{RoleCode})
             ) AS T3
            ${ew.customSqlSegment}
    </select>

    <select id="getNumByPatientIdAndOrganizationId" resultType="Integer">
        SELECT COUNT
                   (1)
        FROM adm_encounter
        WHERE delete_flag = '0'
          AND patient_id = #{patientId}
          AND service_type_id = #{serviceTypeId}
          AND create_time > CURRENT_DATE
    </select>

    <select id="getCurrentDayEncounter" resultType="com.openhis.web.chargemanage.dto.CurrentDayEncounterDto">
        SELECT T9.tenant_id,
               T9.encounter_id,
               T9.organization_id,
               T9.organization_name,
               T9.healthcare_name,
               T9.practitioner_user_id,
               T9.practitioner_name,
               T9.contract_name,
               T9.patient_id,
               T9.patient_name,
               T9.gender_enum,
               T9.id_card,
               T9.status_enum,
               T9.register_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,
                        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,
                        T1.status_enum     AS status_enum,
                        T1.create_time     AS register_time
                 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'
                          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'
                 WHERE T1.delete_flag = '0'
                   AND T1.create_time > CURRENT_DATE) AS T9
            ${ew.customSqlSegment}
    </select>

</mapper>