<?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.inhospitalnursestation.mapper.ATDManageAppMapper">
    <select id="selectAdmissionPatientPage"
            resultType="com.openhis.web.inhospitalnursestation.dto.AdmissionPatientPageDto">
    SELECT ii.encounter_id,
           ii.bus_no,
           ii.encounter_status,
           ii.start_time,
           ii.end_time,
           ii.admit_source_code,
           ii.priority_enum,
           ii.organization_id,
           ii.patient_name,
           ii.patient_py_str,
           ii.patient_wb_str,
           ii.gender_enum,
           ii.birth_date,
           ii.ward_id,
           ii.ward_name,
           ii.house_id,
           ii.house_name,
           ii.bed_id,
           ii.bed_name,
           ii.organization_name,
           ii.contract_name
    FROM (
            WITH locations AS (
            SELECT ael.encounter_id,
                   al.form_enum,
                   al.id AS location_id,
                   al."name" AS location_name
            FROM adm_encounter_location ael
                LEFT JOIN adm_location al
                    ON ael.location_id = al.id
                        AND al.delete_flag = '0'
            WHERE ael.status_enum = #{active}
              AND ael.delete_flag = '0'
    )
           SELECT ae.tenant_id,
                  ae.id           AS encounter_id,
                  ae.bus_no,
                  ae.status_enum  AS encounter_status,
                  ae.start_time,
                  ae.end_time,
                  ae.admit_source_code,
                  ae.priority_enum,
                  ae.organization_id,
                  ap."name"       AS patient_name,
                  ap.py_str       AS patient_py_str,
                  ap.wb_str       AS patient_wb_str,
                  ap.gender_enum,
                  ap.birth_date,
                  alw.location_id AS ward_id,
                  alw.location_name AS ward_name,
                  alh.location_id AS house_id,
                  alh.location_name AS house_name,
                  alb.location_id AS bed_id,
                  alb.location_name AS bed_name,
                  ao."name"       AS organization_name,
                  fc.contract_name
           FROM adm_encounter ae
                    LEFT JOIN adm_patient ap
                        ON ae.patient_id = ap.id
                            AND ap.delete_flag = '0'
                    LEFT JOIN locations alw
                        ON alw.encounter_id = ae.id
                            AND alw.form_enum = #{ward}
                    LEFT JOIN locations alh
                        ON alh.encounter_id = ae.id
                            AND alh.form_enum = #{house}
                    LEFT JOIN locations alb
                        ON alb.encounter_id = ae.id
                            AND alb.form_enum = #{bed}
                    LEFT JOIN adm_organization ao
                        ON ao.id = ae.organization_id
                            AND ao.delete_flag = '0'
                    LEFT JOIN adm_account aa
                        ON ae.id = aa.encounter_id
                            AND aa.encounter_flag = 1
                            AND aa.delete_flag = '0'
                    LEFT JOIN fin_contract fc
                        ON aa.contract_no = fc.bus_no
                            AND fc.delete_flag = '0'
           WHERE ae.delete_flag = '0'
             AND ae.class_enum = #{imp}
             AND ae.status_enum != #{toBeRegistered}
           GROUP BY ae.id,
                    ae.bus_no,
                    ae.status_enum,
                    ae.start_time,
                    ae.end_time,
                    ae.admit_source_code,
                    ae.priority_enum,
                    ae.organization_id,
                    ap."name",
                    ap.py_str,
                    ap.wb_str,
                    ap.gender_enum,
                    ap.birth_date,
                    alw.location_id,
                    alw.location_name,
                    alh.location_id,
                    alh.location_name,
                    alb.location_id,
                    alb.location_name,
                    ao."name",
                    fc.contract_name
           ORDER BY ae.bus_no DESC
        ) AS ii
    ${ew.customSqlSegment}
    </select>
    <select id="selectAdmissionBadPage"
            resultType="com.openhis.web.inhospitalnursestation.dto.AdmissionBedPageDto">
    SELECT ii.bed_id,
           ii.bed_name,
           ii.bed_status,
           ii.house_id,
           ii.house_name,
           ii.ward_id,
           ii.ward_name,
           ii.encounter_id ,
           ii.organization_id ,
           ii.bus_no ,
           ii.encounter_status,
           ii.patient_name,
           ii.patient_py_str,
           ii.patient_wb_str,
           ii.gender_enum ,
           ii.birth_date ,
           ii.total_amount,
           ii.balance_amount
    FROM ( SELECT bed.id AS bed_id,
                  bed.tenant_id,
                  bed."name" AS bed_name,
                  bed.status_enum AS bed_status,
                  house.id AS house_id,
                  house."name" AS house_name,
                  ward.id AS ward_id,
                  ward."name" AS ward_name,
                  ae.id AS encounter_id ,
                  ae.organization_id ,
                  ae.bus_no ,
                  ae.status_enum AS encounter_status,
                  ap."name" AS patient_name,
                  ap.py_str AS patient_py_str,
                  ap.wb_str AS patient_wb_str,
                  ap.gender_enum ,
                  ap.birth_date ,
                  personal_account.total_amount,
                  personal_account.balance_amount
           FROM adm_location bed
               LEFT JOIN adm_location house
                   ON house.bus_no = SPLIT_PART(bed.bus_no,'.',1) || '.' || SPLIT_PART(bed.bus_no,'.',2)
                       AND house.form_enum = #{house}
                       AND house.delete_flag = '0'
               LEFT JOIN adm_location ward
                    ON ward.bus_no = SPLIT_PART(bed.bus_no,'.',1)
                        AND ward.form_enum = #{ward}
                        AND ward.delete_flag = '0'
               LEFT JOIN adm_encounter_location aelb
                    ON bed.id = aelb.location_id
                        AND aelb.delete_flag = '0'
                        AND aelb.status_enum = #{active}
                        AND bed.form_enum = #{bed}
               LEFT JOIN adm_encounter ae
                    ON aelb.encounter_id = ae.id
                        AND ae.delete_flag = '0'
               LEFT JOIN adm_patient ap
                    ON ap.id = ae.patient_id
                        AND ap.delete_flag = '0'
               LEFT JOIN ( SELECT aa.encounter_id,
                                  aa.balance_amount AS total_amount,
                                  (aa.balance_amount -
                                   COALESCE(SUM(CASE WHEN aci.status_enum IN (#{billed}, #{billable})
                                       THEN aci.total_price ELSE 0 END), 0) +
                                   COALESCE(SUM(CASE WHEN aci.status_enum = #{refunded}
                                       THEN aci.total_price ELSE 0 END), 0)) AS balance_amount
                           FROM adm_account aa
                                    LEFT JOIN adm_charge_item aci
                                              ON aa.encounter_id = aci.encounter_id
                                                  AND aa.delete_flag = '0'
                           WHERE aa.type_code = #{personalCashAccount}
                             AND aa.delete_flag = '0'
                           GROUP BY aa.encounter_id,
                                    aa.balance_amount
           ) AS personal_account
                         ON personal_account.encounter_id = ae.id
           WHERE bed.form_enum = #{bed}
             AND bed.delete_flag = '0'
             AND bed.status_enum != #{inactive}
           GROUP BY bed.id,
                    bed.tenant_id,
                    bed."name",
                    bed.status_enum,
                    house.id,
                    house."name",
                    ward.id,
                    ward."name",
                    ae.id,
                    ae.organization_id ,
                    ae.bus_no ,
                    ae.status_enum,
                    ap."name",
                    ap.py_str,
                    ap.wb_str,
                    ap.gender_enum ,
                    ap.birth_date,
                    personal_account.total_amount,
                    personal_account.balance_amount
           ORDER BY bed.bus_no
        ) AS ii
    ${ew.customSqlSegment}
    </select>
    <select id="selectAdmissionPatientInfo"
            resultType="com.openhis.web.inhospitalnursestation.dto.AdmissionPatientInfoDto">
        WITH locations AS (
            SELECT ael.encounter_id,
                   ael.start_time,
                   al.form_enum,
                   al.id AS location_id,
                   al."name" AS location_name
            FROM adm_encounter_location ael
                LEFT JOIN adm_location al
                    ON ael.location_id = al.id
                        AND al.delete_flag = '0'
            WHERE ael.status_enum = #{active}
              AND ael.delete_flag = '0'
        ) , practitioners AS (
            SELECT aep.encounter_id ,
                   aep.type_code ,
                   pra.id AS practitioner_id,
                   pra."name" AS practitioner_name
            FROM adm_encounter_participant aep
                LEFT JOIN adm_practitioner pra
                    ON aep.practitioner_id = pra.id
                        AND pra.delete_flag = '0'
            WHERE aep.status_enum = #{active}
              AND aep.delete_flag = '0'
        )
        SELECT ae.id AS encounter_id,
               ae.bus_no ,
               ae.priority_enum ,
               ae.organization_id ,
               ae.start_time AS in_hos_time,
               bed.start_time,
               bed.location_id AS bed_id,
               bed.location_name AS bed_name,
               house.location_id AS house_id,
               house.location_name AS house_name,
               ward.location_id AS ward_id,
               ward.location_name AS ward_name,
               primaryNurse.practitioner_id AS primary_nurse_id,
               primaryNurse.practitioner_name AS primary_nurse_name,
               attendingDoctor.practitioner_id AS attending_doctor_id,
               attendingDoctor.practitioner_name AS attending_doctor_name,
               admittingDoctor.practitioner_id AS admitting_doctor_id,
               admittingDoctor.practitioner_name AS admitting_doctor_name,
               chiefDoctor.practitioner_id AS chief_doctor_id,
               chiefDoctor.practitioner_name AS chief_doctor_name,
               ao."name" AS organization_name ,
               ap."name" AS patient_name,
               ap.gender_enum ,
               ap.birth_date ,
               ap.phone ,
               fc.contract_name ,
               diagnosis.condition_names
        FROM adm_encounter ae
            LEFT JOIN locations AS bed
                ON bed.encounter_id = ae.id
                    AND bed.form_enum = #{bed}
            LEFT JOIN locations AS house
                ON house.encounter_id = ae.id
                    AND house.form_enum = #{house}
            LEFT JOIN locations AS ward
                ON ward.encounter_id = ae.id
                    AND ward.form_enum = #{ward}
            LEFT JOIN practitioners AS primaryNurse
                ON primaryNurse.encounter_id = ae.id
                    AND primaryNurse.type_code = #{primaryNurse}
            LEFT JOIN practitioners AS attendingDoctor
                ON attendingDoctor.encounter_id = ae.id
                    AND attendingDoctor.type_code = #{attendingDoctor}
            LEFT JOIN practitioners AS admittingDoctor
                ON admittingDoctor.encounter_id = ae.id
                    AND admittingDoctor.type_code = #{admittingDoctor}
            LEFT JOIN practitioners AS chiefDoctor
                ON chiefDoctor.encounter_id = ae.id
                    AND chiefDoctor.type_code = #{chiefDoctor}
            LEFT JOIN adm_organization ao
                ON ao.id = ae.organization_id
                    AND ao.delete_flag = '0'
            LEFT JOIN adm_patient ap
                ON ae.patient_id = ap.id
                    AND ap.delete_flag = '0'
            LEFT JOIN adm_account aa
                ON ae.id = aa.encounter_id
                    AND aa.encounter_flag = 1
                    AND aa.delete_flag = '0'
            LEFT JOIN fin_contract fc
                ON aa.contract_no = fc.bus_no
                     AND fc.delete_flag = '0'
            LEFT JOIN (
                SELECT aed.encounter_id,
                       STRING_AGG(ccd.name, ', ') AS condition_names
                FROM adm_encounter_diagnosis aed
                    INNER JOIN cli_condition cc
                        ON cc.id = aed.condition_id
                            AND cc.delete_flag = '0'
                    INNER JOIN cli_condition_definition ccd
                        ON ccd.id = cc.definition_id
                            AND ccd.delete_flag = '0'
                WHERE aed.delete_flag = '0'
                GROUP BY aed.encounter_id
                        ) AS diagnosis
                ON ae.id = diagnosis.encounter_id
        WHERE ae.id = #{encounterId}
            AND ae.delete_flag = '0'
        GROUP BY ae.id,
                 ae.bus_no ,
                 ae.priority_enum ,
                 ae.organization_id ,
                 ae.start_time,
                 bed.start_time,
                 bed.location_id,
                 bed.location_name,
                 house.location_id,
                 house.location_name,
                 ward.location_id,
                 ward.location_name,
                 primaryNurse.practitioner_id,
                 primaryNurse.practitioner_name,
                 attendingDoctor.practitioner_id,
                 attendingDoctor.practitioner_name,
                 admittingDoctor.practitioner_id,
                 admittingDoctor.practitioner_name,
                 chiefDoctor.practitioner_id,
                 chiefDoctor.practitioner_name,
                 ao."name",
                 ap."name",
                 ap.gender_enum ,
                 ap.birth_date ,
                 ap.phone ,
                 fc.contract_name ,
                 diagnosis.condition_names
    </select>
</mapper>