<?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.pharmacymanage.mapper.PendingMedicationDetailsMapper">
    <select id="selectPendingMedicationDetailsPage"
            resultType="com.openhis.web.pharmacymanage.dto.PendingMedicationPageDto">
        SELECT T7.medicine_no,      --药品编码
               T7.medicine_name,    --药品名称
               T7.py_str,    --药品拼音
               T7.dispense_quantity, --待发药数量
               T7.unit_code,        --请求单位
               T7.dispense_enum,    --发药类型
               T7.patient_name,     --患者姓名
               T7.prescription_no,  --处方号
               T7.outpatient_no,    --门诊号
               T7.admission_no,     --住院号
               T7.create_time,      --开单时间
               T7.tenant_id         --租户ID
        FROM (
                 SELECT T1.bus_no                          AS medicine_no,      --药品编码
                        T6."name"                          AS medicine_name,    --药品名称
                        T6.py_str,                                              --药品拼音
                        T1.quantity   AS dispense_quantity,                     --待发药数量
                        T1.unit_code,                                           --请求单位
                        T1.dispense_enum,                                       --发药类型
                        T3."name"                          AS patient_name,     --患者姓名
                        T4.prescription_no,                                     --处方号
                        CASE
                            WHEN T2.class_enum = #{amb}
                                THEN T2.bus_no
                            ELSE '' END                    AS outpatient_no,    --门诊号
                        CASE
                            WHEN T2.class_enum = #{imp}
                                THEN T2.bus_no
                            ELSE '' END                    AS admission_no,     --住院号
                        T1.create_time,                                         --开单时间
                        T1.tenant_id                                            --租户ID
                 FROM med_medication_dispense AS T1
                          LEFT JOIN adm_encounter AS T2
                                     ON T1.encounter_id = T2.id
                                         AND T2.delete_flag = '0'
                          LEFT JOIN adm_patient AS T3
                                     ON T1.patient_id = T3.id
                                         AND T3.delete_flag = '0'
                          INNER JOIN med_medication_request AS T4
                                     ON T1.med_req_id = T4.id
                                         AND T4.delete_flag = '0'
                          LEFT JOIN med_medication_definition AS T6
                                    ON T1.medication_id = T6.id
                                        AND T6.delete_flag = '0'
                          LEFT JOIN med_medication AS T5
                                    ON T5.medication_def_id = T6.id
                                         AND T5.delete_flag = '0'
                 WHERE T1.delete_flag = '0'
                   AND T1.status_enum = #{inProgress}
                 ORDER BY T1.create_time DESC
             ) AS T7
            ${ew.customSqlSegment}
    </select>
</mapper>
