<?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.outpatientmanage.mapper.OutpatientTreatmentAppMapper">
    <select id="selectEncounterInfoListPage"
            resultType="com.openhis.web.outpatientmanage.dto.OutpatientTreatmentEncounterDto">
        SELECT ii.encounter_id,
               ii.reception_time,
               ii.encounter_no,
               ii.gender_enum,
               ii.patient_name,
               ii.patient_py_str,
               ii.patient_wb_str,
               ii.birth_date,
               ii.status_enum
        FROM (
                 SELECT T1.tenant_id,
                        T1.id     AS encounter_id,
                        T1.reception_time,
                        T1.bus_no AS encounter_no,
                        T2.gender_enum,
                        T2."name" AS patient_name,
                        T2.py_str AS patient_py_str,
                        T2.wb_str AS patient_wb_str,
                        T2.birth_date,
                        T3.status_enum
                 FROM adm_encounter AS T1
                          INNER JOIN adm_patient AS T2
                                     ON T1.patient_id = T2.id
                                         AND T2.delete_flag = '0'
                          INNER JOIN wor_service_request AS T3
                                     ON T1.id = T3.encounter_id
                                         AND T3.refund_service_id IS NULL
                                         AND T3.delete_flag = '0'
                 WHERE T1.class_enum = #{amb}
                   AND T1.delete_flag = '0'
                 GROUP BY T1.tenant_id,
                          T1.id,
                          T1.reception_time,
                          T1.bus_no,
                          T2.gender_enum,
                          T2.name,
                          T2.py_str,
                          T2.wb_str,
                          T2.birth_date,
                          T3.status_enum
                 ORDER BY T1.bus_no DESC
             ) AS ii
            ${ew.customSqlSegment}
    </select>
    <select id="selectTreatmentInfoPage"
            resultType="com.openhis.web.outpatientmanage.dto.OutpatientTreatmentInfoDto">
        SELECT ii.encounter_id,
               ii.request_id,
               ii.service_status,
               ii.dispense_id,
               ii.dispense_status,
               ii.execute_num,
               ii.quantity,
               ii.unit_code,
               ii.bus_no,
               ii.charge_status,
               ii.unit_price,
               ii.total_price,
               ii.request_table,
               ii.service_category,
               ii.device_category,
               ii.med_category,
               ii.item_name,
               ii.py_str,
               ii.wb_str,
               ii.size,
               ii.group_id,
               ii.sort_number,
               ii.method_code,
               ii.dispense_per_duration
        FROM (SELECT ae.id                AS encounter_id,
                     ae.tenant_id,
                     wsr.id               AS request_id,
                     wsr.status_enum      AS service_status,
                     NULL                 AS dispense_id,
                     NULL                 AS dispense_status,
                     wsr.quantity         AS execute_num,
                     NULL                 AS quantity,
                     wsr.unit_code,
                     wsr.bus_no,
                     aci.status_enum      AS charge_status,
                     aci.unit_price,
                     aci.total_price,
                     #{worServiceRequest} AS request_table,
                     wad.category_code    AS service_category,
                     NULL                 AS device_category,
                     NULL                 AS med_category,
                     wad."name"           AS item_name,
                     wad.py_str,
                     wad.wb_str,
                     NULL                 AS "size",
                     wsr.group_id,
                     NULL                 AS sort_number,
                     NULL                 AS method_code,
                     NULL                 AS dispense_per_duration
              FROM adm_encounter ae
                       INNER JOIN wor_service_request wsr
                                  ON wsr.encounter_id = ae.id
                                      AND wsr.delete_flag = '0'
                                      AND wsr.refund_service_id IS NULL
                       LEFT JOIN adm_charge_item aci
                                 ON aci.service_id = wsr.id
                                     AND aci.delete_flag = '0'
                                     AND aci.refund_id IS NULL
                       INNER JOIN wor_activity_definition wad
                                  ON wsr.activity_id = wad.id
                                      AND wad.delete_flag = '0'
              WHERE wsr.status_enum = #{reqCompleted}
                AND ae.delete_flag = '0'
              UNION ALL
              SELECT ae.id                   AS encounter_id,
                     ae.tenant_id,
                     mmr.id                  AS request_id,
                     NULL                    AS service_status,
                     dis.id                  AS dispense_id,
                     dis.status_enum         AS dispense_status,
                     mmr.execute_num,
                     mmr.quantity,
                     mmr.unit_code,
                     mmr.bus_no,
                     aci.status_enum         AS charge_status,
                     aci.unit_price,
                     aci.total_price,
                     #{medMedicationRequest} AS request_table,
                     NULL                    AS service_category,
                     NULL                    AS device_category,
                     mmd.category_code       AS med_category,
                     mmd."name"              AS item_name,
                     mmd.py_str,
                     mmd.wb_str,
                     mm.total_volume         AS "size",
                     mmr.group_id,
                     mmr.sort_number,
                     mmr.method_code,
                     mmr.dispense_per_duration
              FROM adm_encounter ae
                       LEFT JOIN med_medication_request mmr
                                 ON ae.id = mmr.encounter_id
                                     AND mmr.refund_medicine_id IS NULL
                                     AND mmr.delete_flag = '0'
                       LEFT JOIN med_medication_dispense dis
                                 ON dis.med_req_id = mmr.id
                                     AND dis.delete_flag = '0'
                       INNER JOIN med_medication_definition mmd
                                  ON mmr.medication_id = mmd.id
                                      AND (mmd.skin_test_flag = 1 OR mmd.inject_flag = 1)
                                      AND mmd.delete_flag = '0'
                       INNER JOIN med_medication mm
                                  ON mm.medication_def_id = mmd.id
                                      AND mm.delete_flag = '0'
                       LEFT JOIN adm_charge_item aci
                                 ON aci.service_id = mmr.id
                                     AND aci.delete_flag = '0'
                                     AND aci.refund_id IS NULL
              WHERE dis.status_enum != #{draft}
                AND ae.delete_flag = '0'
              UNION ALL
              SELECT ae.id AS encounter_id,
                  ae.tenant_id,
                  wdr.id AS request_id,
                  NULL AS service_status,
                  wdd.id AS dispense_id,
                  wdd.status_enum AS dispense_status,
                  NULL AS execute_num,
                  wdr.quantity,
                  wdr.unit_code,
                  wdr.bus_no,
                  aci.status_enum AS charge_status,
                  aci.unit_price,
                  aci.total_price,
                  #{worDeviceRequest} AS request_table,
                  NULL AS service_category,
                  dev.category_code AS device_category,
                  NULL AS med_category,
                  dev."name" AS item_name,
                  dev.py_str,
                  dev.wb_str,
                  dev.size,
                  NULL AS group_id,
                  NULL AS sort_number,
                  NULL AS method_code,
                  NULL AS dispense_per_duration
              FROM adm_encounter ae
                  INNER JOIN wor_device_request wdr
              ON wdr.encounter_id = ae.id
                  AND wdr.delete_flag = '0'
                  AND wdr.refund_device_id IS NULL
                  LEFT JOIN wor_device_dispense wdd
                  ON wdd.device_req_id = wdr.id
                  AND wdd.delete_flag = '0'
                  LEFT JOIN adm_charge_item aci
                  ON aci.service_id = wdr.id
                  AND aci.delete_flag = '0'
                  AND aci.refund_id IS NULL
                  INNER JOIN adm_device_definition dev
                  ON dev.id = wdr.device_def_id
                  AND dev.delete_flag = '0'
              WHERE wdd.status_enum != #{draft}
                AND ae.delete_flag = '0'
             ) AS ii
            ${ew.customSqlSegment}
        ORDER BY ii.sort_number
    </select>
    <select id="selectPerformRecordList" resultType="com.openhis.web.common.dto.PerformRecordDto">
        SELECT cp.id AS procedure_id,
        cp.status_enum,
        cp.occurrence_time,
        cp.create_time,
        al."name" AS location_name,
        ao."name" AS org_name,
        cp.request_id,
        cp.refund_id,
        cpp.start_time,
        cpp.end_time,
        ap."name" AS practitioner_name
        FROM cli_procedure cp
        LEFT JOIN cli_procedure_performer cpp
        ON cpp.procedure_id = cp.id
        AND cpp.delete_flag = '0'
        LEFT JOIN adm_organization ao
        ON ao.id = cp.org_id
        AND ao.delete_flag = '0'
        LEFT JOIN adm_location al
        ON al.id = cp.location_id
        AND al.delete_flag = '0'
        LEFT JOIN adm_practitioner ap
        ON ap.id = cpp.practitioner_id
        AND ap.delete_flag = '0'
        WHERE cp.delete_flag = '0'
        <if test="reqIds!=null and !reqIds.isEmpty()">
            AND cp.request_id IN
            <foreach collection="reqIds" item="reqId" open="(" separator="," close=")">
                #{reqId}
            </foreach>
        </if>
    </select>

    <select id="getBloodTransfusionPatch" resultType="com.openhis.web.outpatientmanage.dto.BloodTransfusionPatchDto">
        SELECT ap.NAME                      AS patient_name,
               ap.gender_enum,
               ap.birth_date,
               wad.NAME                     AS activity_name,
               SUBSTRING(wad.bus_no FROM 3) AS bus_no,
               aper.name                    AS practitioner_name,
               ao.name                      AS org_name
        FROM wor_service_request AS wsr
                 LEFT JOIN adm_patient AS ap ON ap.ID = wsr.patient_id
                 LEFT JOIN wor_activity_definition AS wad ON wad.ID = wsr.activity_id
                 LEFT JOIN adm_practitioner AS aper ON aper.id = wsr.requester_id AND aper.delete_flag = '0'
                 LEFT JOIN adm_organization AS ao ON ao.id = aper.org_id AND ao.delete_flag = '0'
        WHERE wsr.ID = #{requestId}
    </select>

</mapper>
