<?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.MedicalDeviceDispenseMapper">
    <select id="selectEncounterInfoListPage" resultType="com.openhis.web.pharmacymanage.dto.EncounterInfoDto">
        SELECT 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.birth_date,
               ii.reception_time,
               ii.status_enum
        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,
                      T2.birth_date,
                      T3.name AS department_name,
                      T4.status_enum
               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'
                   INNER JOIN med_medication_request AS T5
                       ON T4.med_req_id = T5.id
                           AND T5.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},#{prepared})
                   </if>
                   <if test="statusEnum == 4">
                       T4.status_enum = #{completed}
                   </if>
               UNION
               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,
                      T2.birth_date,
                      T3.name AS department_name,
                      T4.status_enum
               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 wor_device_dispense AS T4
                       ON T4.encounter_id = T1.id
                           AND T4.delete_flag = '0'
                   INNER JOIN wor_device_request AS T5
                       ON T4.device_req_id = T5.id
                           AND T5.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},#{prepared})
                   </if>
                   <if test="statusEnum == 4">
                       T4.status_enum = #{completed}
                   </if>
               ) AS ii
        ${ew.customSqlSegment}
        GROUP BY 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.birth_date,
                 ii.reception_time,
                 ii.status_enum
        ORDER BY ii.reception_time DESC
    </select>
    <select id="selectDeviceDispenseOrderPage" resultType="com.openhis.web.pharmacymanage.dto.ItemDispenseOrderDto">
    SELECT ii.department_name,
           ii.doctor_name,
           ii.item_type,
           ii.condition_name,
           ii.lot_number,
           ii.request_id,
           ii.item_name,
           ii.item_id,
           ii.total_volume,
           ii.unit_code,
           ii.encounter_id,
           ii.status_enum,
           ii.rate_code,
           ii.location_id,
           ii.quantity,
           ii.dispense_id,
           ii.unit_price,
           ii.total_price,
           ii.dispense_doctor_name,
           ii.location_name,
           ii.encounter_bus_no,
           ii.req_authored_time,
           ii.tcm_flag,
           ii.trace_no
        FROM ( SELECT T8."name" AS department_name,
                      T9."name" AS doctor_name,
                      T3.category_code AS item_type,
                      T7."name" AS condition_name,
                      T2.lot_number,
                      T2.id AS request_id,
                      T3."name" AS item_name,
                      T3.id AS item_id,
                      T3.size AS total_volume,
                      T1.unit_code,
                      T1.encounter_id,
                      T1.status_enum,
                      T2.rate_code,
                      T1.location_id,
                      T1.quantity,
                      T1.id AS dispense_id,
                      T5.unit_price,
                      T5.total_price,
                      T10."name" AS dispense_doctor_name,
                      T13."name" AS location_name,
                      T6.bus_no AS encounter_bus_no,
                      T2.req_authored_time,
                      T1.tenant_id,
                      0 AS tcm_flag,
                      T1.trace_no
               FROM wor_device_dispense AS T1
                        LEFT JOIN wor_device_request AS T2
                           ON T1.device_req_id = T2.id
                               AND T2.delete_flag = '0'
                        LEFT JOIN adm_device_definition AS T3
                           ON T2.device_def_id = T3.id
                               AND T3.delete_flag = '0'
                        INNER JOIN adm_charge_item AS T5
                           ON T1.device_req_id = T5.service_id
                               AND T5.delete_flag = '0'
                        LEFT JOIN adm_encounter AS T6
                           ON T1.encounter_id = T6.id
                               AND T6.delete_flag = '0'
                        LEFT JOIN cli_condition AS T12
                            ON T2.condition_id = T12.id
                               AND T12.delete_flag = '0'
                        LEFT JOIN cli_condition_definition AS T7
                           ON T12.definition_id = T7.id
                               AND T7.delete_flag = '0'
                        LEFT JOIN adm_organization AS T8
                           ON T6.organization_id = T8.id
                               AND T8.delete_flag = '0'
                        LEFT JOIN adm_practitioner AS T9
                           ON T2.requester_id = T9.id
                               AND T9.delete_flag = '0'
                        LEFT JOIN adm_practitioner AS T10
                           ON T1.performer_id = T10.id
                               AND T10.delete_flag = '0'
                        LEFT JOIN adm_location AS T13
                           ON T1.location_id = T13.id
                               AND T13.delete_flag = '0'
               WHERE T1.delete_flag = '0'
                    -- 因发药配药合并，前台只能看到待发药，已发药状态，但是后台配药发药状态都查
                    AND
                    <if test="dispenseStatus == null">
                        T1.status_enum IN (#{inProgress},#{completed},#{preparation},#{prepared})
                    </if>
                    <if test="dispenseStatus == 3">
                        T1.status_enum IN (#{inProgress},#{preparation},#{prepared})
                    </if>
                    <if test="dispenseStatus == 4">
                        T1.status_enum = #{completed}
                    </if>
                ORDER BY req_authored_time DESC
        ) AS ii
        ${ew.customSqlSegment}
    </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>
