<?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.WesternMedicineDispenseMapper">
    <select id="selectEncounterInfoListPage" resultType="com.openhis.web.pharmacymanage.dto.EncounterInfoPageDto">
        SELECT ii.tenant_id,
               ii.encounter_id,
               ii.encounter_no,
               ii.department_name,
               ii.patient_name,
               ii.patient_wb_str,
               ii.patient_py_str,
               ii.gender_enum,
               ii.department_id,
               ii.id_card,
               ii.reception_time,
               ii.status_enum
--                ii.location_id
        FROM (
                 SELECT T1.tenant_id,
                        T1.id AS encounter_id,
                        T1.bus_no AS encounter_no,
                        T1.reception_time,
                        T1.organization_id AS department_id,
                        T2.gender_enum,
                        T2.name AS patient_name,
                        T2.wb_str AS patient_wb_str,
                        T2.py_str AS patient_py_str,
                        T2.id_card,
                        T3.name AS department_name,
                        T4.status_enum
--                         T4.location_id
                 FROM adm_encounter AS T1
                          INNER JOIN adm_patient AS T2
                                     ON T1.patient_id = T2.id
                                         AND T2.delete_flag = '0'
                          LEFT JOIN adm_organization AS T3
                                    ON T1.organization_id = T3.id
                                        AND T3.delete_flag = '0'
                          INNER JOIN med_medication_dispense AS T4
                                     ON T4.encounter_id = T1.id
                                         AND T4.delete_flag = '0'
                 -- 因发药配药合并，前台只能看到待发药，已发药状态，但是后台配药发药状态都查
                 WHERE <if test="statusEnum == null">
                            T4.status_enum IN (#{inProgress},#{completed},#{preparation},#{prepared})
                        </if>
                        <if test="statusEnum == 3">
                            T4.status_enum IN (#{inProgress},#{preparation})
                        </if>
                        <if test="statusEnum == 4">
                            T4.status_enum IN (#{completed},#{prepared})
                        </if>
                 GROUP BY T1.tenant_id,
                          T1.id,
                          T1.bus_no,
                          T1.reception_time,
                          T1.organization_id,
                          T2.gender_enum,
                          T2.name,
                          T2.wb_str,
                          T2.py_str,
                          T2.id_card,
                          T3.name,
                          T4.status_enum
--                           T4.location_id
                 ORDER BY T1.reception_time desc
             ) AS ii
            ${ew.customSqlSegment}
    </select>
    <select id="selectPrescriptionPatientInfo" resultType="com.openhis.web.pharmacymanage.dto.PrescriptionPatientInfoDto">
        SELECT
          T2.name AS patient_name,
          T2.gender_enum,
          T2.birth_date,
          T4.category_enum,
          T2.id_card,
          T5.name AS organization_name,
          TO_CHAR(T1.start_time,'YYYY-MM-DD') AS encounter_date
        FROM adm_encounter AS T1
        INNER JOIN adm_patient AS T2
        ON T1.patient_id = T2.id
        LEFT JOIN med_medication_request AS T3
        ON T1.id = T3.encounter_id
        LEFT JOIN fin_contract AS T4
        ON T3.contract_no = T4.bus_no
        LEFT JOIN adm_organization AS T5
        ON T1.organization_id = T5.id
        WHERE
          T1.id = #{encounterId}
       GROUP BY
          T2.name,
          T2.gender_enum,
          T2.birth_date,
          T4.category_enum,
          T2.id_card,
          T5.name,
          T1.start_time
    </select>
    <select id="selectPrescriptionMedicineInfoList" resultType="com.openhis.web.pharmacymanage.dto.PrescriptionMedicineInfoDto">
        SELECT T8."name" AS department_name,
               T9."name" AS doctor_name,
               T3.category_code AS item_type,
               T7."name" AS condition_name,
               T2.prescription_no,
               T2.lot_number,
               T2.id AS request_id,
               T3."name" AS medicine_name,
               T3.id AS medicine_id,
               T4.total_volume,
               T1.unit_code,
               T1.dose,
               T1.status_enum,
               T2.rate_code,
               T1.location_id,
               T1.method_code,
               T2.dose_unit_code,
               T2.max_dose,
               T2.first_dose,
               T2.first_duration,
               T2.dispense_interval,
               T2.dispense_per_quantity,
               T2.dispense_per_duration,
               T1.quantity,
               T1.id AS dispense_id,
               T5.unit_price,
               T5.total_price,
               T10."name" AS dispense_doctor_name,
               T11."name" AS preparer_doctor_name,
               T13."name" AS location_name,
	       T2.group_id      AS group_id
        FROM med_medication_dispense AS T1
                 LEFT JOIN med_medication_request AS T2
                           ON T1.med_req_id = T2.id
                 LEFT JOIN med_medication_definition AS T3
                           ON T1.medication_id = T3.id
                 LEFT JOIN med_medication AS T4
                           ON T3.id = T4.medication_def_id
                 INNER JOIN adm_charge_item AS T5
                            ON T1.med_req_id = T5.service_id
                 LEFT JOIN adm_encounter AS T6
                           ON T1.encounter_id = T6.id
                 LEFT JOIN cli_condition AS T12
                           ON T2.condition_id = T12.id
                 LEFT JOIN cli_condition_definition AS T7
                           ON T12.definition_id = T7.id
                 LEFT JOIN adm_organization AS T8
                           ON T6.organization_id = T8.id
                 LEFT JOIN adm_practitioner AS T9
                           ON T2.practitioner_id = T9.id
                 LEFT JOIN adm_practitioner AS T10
                           ON T1.practitioner_id = T10.id
                 LEFT JOIN adm_practitioner AS T11
                           ON T1.preparer_id = T11.id
                 LEFT JOIN adm_location AS T13
                           ON T1.location_id = T13.id
        WHERE T1.encounter_id = #{encounterId}
        -- 因发药配药合并，前台只能看到待发药，已发药状态，但是后台配药发药状态都查
          AND
         <if test="dispenseStatus == null">
             T1.status_enum IN (#{inProgress},#{completed},#{preparation},#{prepared})
         </if>
         <if test="dispenseStatus == 3">
             T1.status_enum IN (#{inProgress},#{preparation})
         </if>
         <if test="dispenseStatus == 4">
             T1.status_enum IN (#{completed},#{prepared})
         </if>
        ORDER BY prescription_no
    </select>
    <select id="selectDispenseInventoryInfoByPrescriptionNo" resultType="com.openhis.web.pharmacymanage.dto.DispenseInventoryDto">
        SELECT T1.id AS dispense_id,
               T1.unit_code AS dispense_unit_code,
               T1.quantity AS dispense_quantity,
               T1.status_enum AS dispense_status,
               T1.med_req_id,
               T1.encounter_id,
               T1.dispense_time,
               T1.bus_no AS dispense_no,
               T3.Id AS inventory_id,
               T3.unit_code AS inventory_unit_code,
               T3.quantity AS inventory_quantity,
               T3.lot_number,
               T3.production_date,
               T3.expiration_date,
               T3.trace_no,
               T4.part_percent,
               T4.yb_no,
               T4.bus_no AS medication_no,
               T4.rx_flag,
               T5."name" AS preparer_name,
               T6."name" AS dispense_name,
               T6.phar_prac_cert_no,
               T7."name" AS practitioner_name,
               T8.patient_id,
               T8.bus_no AS encounter_no
        FROM med_medication_dispense AS T1
                 INNER JOIN med_medication_request AS T2
                    ON T1.med_req_id = T2.id
                        AND T2.delete_flag = '0'
                 LEFT JOIN wor_inventory_item AS T3
                    ON T1.medication_id = T3.item_id
                        AND T3.item_table = #{medMedicationDefinition}
                        AND T1.location_id = T3.location_id
                        AND T1.lot_number = T3.lot_number
                        AND T3.delete_flag = '0'
                 LEFT JOIN med_medication_definition AS T4
                    ON T1.medication_id = T4.id
                        AND T4.delete_flag = '0'
                 LEFT JOIN adm_practitioner AS T5
                    ON T1.preparer_id = T5.id
                        AND T5.delete_flag = '0'
                 LEFT JOIN adm_practitioner AS T6
                    ON T1.practitioner_id = T6.id
                        AND T6.delete_flag = '0'
                 LEFT JOIN adm_practitioner AS T7
                    ON T2.practitioner_id = T7.id
                        AND T7.delete_flag = '0'
                 LEFT JOIN adm_encounter AS T8
                    ON T1.encounter_id = T8.id
                        AND T8.delete_flag = '0'
        WHERE T2.prescription_no = #{prescriptionNo}
          AND T1.delete_flag = '0'
    </select>
    <select id="getPreparerDoctorList" resultType="com.openhis.administration.domain.Practitioner">
        SELECT ap.id,
               ap."name"
        FROM adm_practitioner ap
            LEFT JOIN adm_practitioner_role apr
                ON ap.id = apr.practitioner_id
        WHERE apr.role_code = #{pharmacist}
        GROUP BY ap.id,
                 ap."name"
    </select>
</mapper>
