<?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.AdviceProcessAppMapper">

    <select id="selectInpatientPage"
            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
        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
                 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'
                 WHERE ae.delete_flag = '0'
                   AND ae.class_enum = #{imp}
                   AND ae.status_enum != #{toBeRegistered}
                   AND ae.status_enum != #{registered}
                 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"
                 ORDER BY ae.bus_no DESC
             ) AS ii
            ${ew.customSqlSegment}
    </select>
    <select id="selectInpatientAdvicePage"
            resultType="com.openhis.web.inhospitalnursestation.dto.InpatientAdviceDto">
    SELECT ii.encounter_id,
           ii.advice_table,
           ii.request_id,
           ii.start_time,
           ii.end_time,
           ii.requester_id,
           ii.request_time,
           ii.skin_test_flag,
           ii.inject_flag,
           ii.group_id,
           ii.advice_name,
           ii.item_id,
           ii.volume,
           ii.lot_number,
           ii.quantity,
           ii.unit_code,
           ii.request_status,
           ii.method_code,
           ii.rate_code,
           ii.dose,
           ii.dose_unit_code,
           ii.position_id,
           ii.position_name,
           ii.dispense_per_duration,
           ii.part_percent,
           ii.condition_definition_name,
           ii.therapy_enum,
           ii.sort_number,
           ii.execute_num,
           ii.day_times,
           ii.bus_no,
           ii."patient_name" AS patient_name,
           ii."bed_name" AS bed_name,
           ii.gender_enum ,
           ii.birth_date ,
           ii.patient_id,
           ii.contract_name ,
           ii.condition_names,
           ii.admitting_doctor_name AS admitting_doctor_name,
           ii.balance_amount AS balance_amount,
           ii.account_id AS account_id,
           ii.performer_check_id
    FROM (( SELECT T1.encounter_id,
                   T1.tenant_id,
                   #{medMedicationRequest} AS advice_table,
                   T1.id AS request_id,
                   T1.effective_dose_start AS start_time,
                   T1.effective_dose_end AS end_time,
                   T1.practitioner_id AS requester_id,
                   T1.create_time AS request_time,
                   T1.skin_test_flag AS skin_test_flag,
                   T1.infusion_flag AS inject_flag,
                   T1.group_id AS group_id,
                   T1.performer_check_id,
                   T2."name" AS advice_name,
                   T2.id AS item_id,
                   T3.total_volume AS volume,
                   T1.lot_number AS lot_number,
                   T1.quantity AS quantity,
                   T1.unit_code AS unit_code,
                   T1.status_enum AS request_status,
                   T1.method_code AS method_code,
                   T1.rate_code AS rate_code,
                   T1.dose AS dose,
                   T1.dose_unit_code AS dose_unit_code,
                   al1.id AS position_id,
                   al1."name" AS position_name,
                   T1.dispense_per_duration AS dispense_per_duration,
                   T2.part_percent AS part_percent,
                   ccd."name" AS condition_definition_name,
                   T1.therapy_enum AS therapy_enum,
                   T1.sort_number AS sort_number,
                   T1.execute_num,
                   af.day_times,
                   ae.bus_no,
                   ap."name" AS patient_name,
                   al2."name" AS bed_name,
                   ap.gender_enum ,
                   ap.birth_date ,
                   ap.id AS patient_id,
                   fc.contract_name ,
                   diagnosis.condition_names,
                   pra."name" AS admitting_doctor_name,
                   personal_account.balance_amount,
                   personal_account.id AS account_id
            FROM med_medication_request AS T1
                     LEFT JOIN med_medication_definition AS T2
                               ON T2.id = T1.medication_id
                                   AND T2.delete_flag = '0'
                     LEFT JOIN med_medication AS T3
                               ON T3.medication_def_id = T2.ID
                                   AND T3.delete_flag = '0'
                     LEFT JOIN adm_location AS al1
                               ON al1.id = T1.perform_location
                                   AND al1.delete_flag = '0'
                     LEFT JOIN cli_condition AS cc
                               ON cc.id = T1.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_frequency af
                               ON af.rate_code = T1.rate_code
                                   AND af.delete_flag = '0'
                     LEFT JOIN adm_encounter ae
                               ON ae.id = T1.encounter_id
                                   AND ae.class_enum = #{imp}
                                   AND ae.delete_flag = '0'
                     LEFT JOIN adm_patient ap
                               ON ae.patient_id = ap.id
                                   AND ap.delete_flag = '0'
                     LEFT JOIN adm_encounter_location ael
                               ON ae.id = ael.encounter_id
                                   AND ael.delete_flag = '0'
                                   AND ael.status_enum = #{active}
                                   AND ael.form_enum = #{bed}
                     LEFT JOIN adm_location al2
                               ON ael.location_id = al2.id
                                   AND al2.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
                    LEFT JOIN adm_encounter_participant aep
                              ON ae.id = aep.encounter_id
                                  AND aep.delete_flag = '0'
                                  AND aep.status_enum = #{active}
                                  AND aep.type_code = #{admittingDoctor}
                    LEFT JOIN adm_practitioner pra
                              ON aep.practitioner_id = pra.id
                                  AND pra.delete_flag = '0'
                    LEFT JOIN ( SELECT aa.id,
                                       aa.encounter_id,
                                       (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.id,
                                         aa.encounter_id,
                                         aa.balance_amount
                           ) AS personal_account
                              ON personal_account.encounter_id = ae.id
                WHERE T1.delete_flag = '0'
                AND T1.refund_medicine_id IS NULL
                AND CASE WHEN T1.status_enum = #{draft}
                    THEN T1.performer_check_id IS NOT NULL
                    ELSE 1=1 END
            ORDER BY T1.status_enum,
                     T1.sort_number,
                     T1.group_id )
          UNION
          ( SELECT T1.encounter_id,
                   T1.tenant_id,
                   #{worServiceRequest} AS advice_table,
                   T1.id AS request_id,
                   T1.occurrence_start_time AS start_time,
                   T1.occurrence_end_time AS end_time,
                   T1.requester_id AS requester_id,
                   T1.create_time AS request_time,
                   NULL AS skin_test_flag,
                   NULL AS inject_flag,
                   NULL AS group_id,
                   T1.performer_check_id,
                   T2."name" AS advice_name,
                   T2.id AS item_id,
                   NULL AS volume,
                   NULL AS lot_number,
                   T1.quantity AS quantity,
                   T1.unit_code AS unit_code,
                   T1.status_enum AS request_status,
                   NULL AS method_code,
                   NULL AS rate_code,
                   NULL AS dose,
                   NULL AS dose_unit_code,
                   ao1.id AS position_id,
                   ao1."name" AS position_name,
                   NULL AS dispense_per_duration,
                   1 AS part_percent,
                   ccd."name" AS condition_definition_name,
                   T1.therapy_enum AS therapy_enum,
                   NULL AS sort_number,
                   T1.quantity AS execute_num,
                   af.day_times,
                   ae.bus_no,
                   ap."name" AS patient_name,
                   al2."name" AS bed_name,
                   ap.gender_enum ,
                   ap.birth_date ,
                   ap.id AS patient_id,
                   fc.contract_name ,
                   diagnosis.condition_names,
                   pra."name" AS admitting_doctor_name,
                   personal_account.balance_amount,
                   personal_account.id AS account_id
            FROM wor_service_request AS T1
                     LEFT JOIN wor_activity_definition AS T2
                               ON T2.id = T1.activity_id
                                   AND T2.delete_flag = '0'
                     LEFT JOIN cli_condition AS cc
                               ON cc.id = T1.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_organization AS ao1
                               ON ao1.id = T1.org_id
                                   AND ao1.delete_flag = '0'
                     LEFT JOIN adm_encounter ae
                               ON ae.id = T1.encounter_id
                                   AND ae.class_enum = #{imp}
                                   AND ae.delete_flag = '0'
                     LEFT JOIN adm_patient ap
                               ON ae.patient_id = ap.id
                                   AND ap.delete_flag = '0'
                     LEFT JOIN adm_encounter_location ael
                               ON ae.id = ael.encounter_id
                                   AND ael.delete_flag = '0'
                                   AND ael.status_enum = #{active}
                                   AND ael.form_enum = #{bed}
                     LEFT JOIN adm_location al
                               ON ael.location_id = al.id
                                   AND al.delete_flag = '0'
                     LEFT JOIN adm_location al2
                               ON ael.location_id = al2.ID
                                   AND al2.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
                     LEFT JOIN adm_encounter_participant aep
                               ON ae.id = aep.encounter_id
                                   AND aep.delete_flag = '0'
                                   AND aep.status_enum = #{active}
                                   AND aep.type_code = #{admittingDoctor}
                     LEFT JOIN adm_practitioner pra
                               ON aep.practitioner_id = pra.id
                                   AND pra.delete_flag = '0'
                     LEFT JOIN ( SELECT aa.id,
                                        aa.encounter_id,
                                        (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.id,
                                           aa.encounter_id,
                                           aa.balance_amount
                        ) AS personal_account
                                ON personal_account.encounter_id = ae.id
                     LEFT JOIN adm_frequency af
                               ON af.rate_code = T1.rate_code
                                   AND af.delete_flag = '0'
            WHERE T1.delete_flag = '0'
                AND CASE WHEN T1.status_enum = #{draft}
                    THEN T1.performer_check_id IS NOT NULL
                    ELSE 1=1 END
                AND T1.refund_service_id IS NULL
            ORDER BY T1.status_enum )
         ) AS ii
    ${ew.customSqlSegment}
    </select>
</mapper>