<?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.ReturnMedicineMapper">
    <select id="selectEncounterInfoListPage" resultType="com.openhis.web.pharmacymanage.dto.EncounterInfoDto">
        SELECT ii.reception_time,
               ii.encounter_id,
               ii.encounter_no,
               ii.tenant_id,
               ii.refund_enum,
               ii.patient_name,
               ii.patient_wb_str,
               ii.patient_py_str,
               ii.id_card,
               ii.gender_enum,
               ii.birth_date,
               ii.department_name
        FROM (
            SELECT ae.reception_time,
                   ae.id           AS encounter_id,
                   ae.bus_no       AS encounter_no,
                   ae.tenant_id,
                   mmr.status_enum AS refund_enum,
                   ap."name"       AS patient_name,
                   ap.wb_str       AS patient_wb_str,
                   ap.py_str       AS patient_py_str,
                   ap.id_card,
                   ap.gender_enum,
                   ap.birth_date,
                   ao."name"       AS department_name
            FROM adm_encounter ae
                 LEFT JOIN med_medication_request mmr
                        ON mmr.encounter_id = ae.id
                 LEFT JOIN adm_patient ap
                        ON ae.patient_id = ap.id
                 LEFT JOIN adm_organization ao
                        ON ao.id = ae.organization_id
            WHERE mmr.refund_medicine_id IS NOT NULL
                AND ae.class_enum = #{amb}
            UNION
            SELECT ae.reception_time,
                   ae.id           AS encounter_id,
                   ae.bus_no       AS encounter_no,
                   ae.tenant_id,
                   wdr.status_enum AS refund_enum,
                   ap."name"       AS patient_name,
                   ap.wb_str       AS patient_wb_str,
                   ap.py_str       AS patient_py_str,
                   ap.id_card,
                   ap.gender_enum,
                   ap.birth_date,
                   ao."name"       AS department_name
            FROM adm_encounter ae
                     LEFT JOIN wor_device_request wdr
                            ON wdr.encounter_id = ae.id
                     LEFT JOIN adm_patient ap
                            ON ae.patient_id = ap.id
                     LEFT JOIN adm_organization ao
                            ON ao.id = ae.organization_id
            WHERE wdr.refund_device_id IS NOT NULL
              AND ae.class_enum = #{amb}
        ) AS ii
        ${ew.customSqlSegment}
        ORDER BY ii.reception_time DESC
    </select>
    <select id="selectInventoryInfoList"
            resultType="com.openhis.web.pharmacymanage.dto.InventoryDto">
        <choose>
        <when test="(medDispenseIdList != null and !medDispenseIdList.isEmpty())
                  or (devDispenseIdList != null and !devDispenseIdList.isEmpty())">
            <if test="medDispenseIdList != null and !medDispenseIdList.isEmpty()">
                SELECT
                T1.id AS dispense_id,
                T1.unit_code AS dispense_unit,
                T1.quantity,
                T1.dispense_quantity,
                T2.id AS inventory_id,
                T2.unit_code AS inventory_unit_code,
                T2.quantity AS inventory_quantity,
                T2.inventory_status_enum,
                T2.expiration_date,
                T3.part_percent,
                T3."name" AS item_name,
                T3.id AS item_id
                FROM med_medication_dispense AS T1
                INNER JOIN wor_inventory_item AS T2
                ON T1.medication_id = T2.item_id
                AND T2.delete_flag = '0'
                AND T2.item_table = #{medMedicationDefinition}
                AND T1.lot_number = T2.lot_number
                AND T1.location_id = T2.location_id
                INNER JOIN med_medication_definition AS T3
                ON T1.medication_id = T3.id
                AND T3.delete_flag = '0'
                WHERE T1.id IN
                <foreach collection="medDispenseIdList" item="medDispenseId" open="(" separator="," close=")">
                    #{medDispenseId}
                </foreach>
                    AND T1.delete_flag = '0'
            </if>
            <if test="(medDispenseIdList != null and !medDispenseIdList.isEmpty())
                    and (devDispenseIdList != null and !devDispenseIdList.isEmpty())">
                UNION
            </if>
            <if test="devDispenseIdList != null and !devDispenseIdList.isEmpty()">
                SELECT
                T4.id AS dispense_id,
                T4.unit_code AS dispense_unit,
                T4.quantity,
                T4.dispense_quantity,
                T2.id AS inventory_id,
                T2.unit_code AS inventory_unit_code,
                T2.quantity AS inventory_quantity,
                T2.inventory_status_enum,
                T2.expiration_date,
                T5.part_percent,
                T5."name" AS item_name,
                T5.id AS item_id
                FROM wor_device_dispense AS T4
                INNER JOIN wor_inventory_item AS T2
                ON T4.device_def_id = T2.item_id
                AND T2.delete_flag = '0'
                AND T2.item_table = #{admDeviceDefinition}
                AND T4.lot_number = T2.lot_number
                AND T4.location_id = T2.location_id
                INNER JOIN adm_device_definition AS T5
                ON T4.device_def_id = T5.id
                AND T5.delete_flag = '0'
                WHERE T4.id IN
                <foreach collection="devDispenseIdList" item="devDispenseId" open="(" separator="," close=")">
                    #{devDispenseId}
                </foreach>
                    AND T4.delete_flag = '0'
            </if>
        </when>
        </choose>
    </select>
    <select id="selectReturnMedicineInfo"
            resultType="com.openhis.web.pharmacymanage.dto.ReturnMedicineInfoDto">
        SELECT mmd.status_enum AS refund_enum,
               mmr.refund_medicine_id,
               mmr.id          AS request_id,
               mmr.quantity,
               mmr.unit_code,
               mmr.lot_number,
               mmr.status_enum AS req_status,
               mmd.dispense_quantity,
               mmd.id AS dispense_id,
               med."name"      AS item_name,
               med.id      AS item_id,
               aci.total_price,
               aci.service_table,
               doc."name"      AS doctor_name,
               mmd.trace_no
        FROM med_medication_request mmr
                 LEFT JOIN med_medication_dispense mmd
                           ON mmr.refund_medicine_id = mmd.med_req_id
                               AND mmd.delete_flag = '0'
                 INNER JOIN med_medication_definition med
                            ON mmr.medication_id = med.id
                                AND med.delete_flag = '0'
                 LEFT JOIN adm_charge_item aci
                           ON aci.service_id = mmr.refund_medicine_id
                               AND aci.delete_flag = '0'
                 LEFT JOIN adm_practitioner doc
                           ON mmr.practitioner_id = doc.id
                               AND doc.delete_flag = '0'
        WHERE mmr.refund_medicine_id IS NOT NULL
          AND mmr.encounter_id = #{encounterId}
          AND aci.service_table =#{medMedicationRequest}
          <if test="refundStatus == null">
              AND mmr.status_enum IN (#{inRefund},#{completed})
          </if>
          <if test="refundStatus == 5">
              AND mmr.status_enum = #{inRefund}
          </if>
          <if test="refundStatus == 3">
              AND mmr.status_enum = #{completed}
          </if>
          AND mmr.delete_flag = '0'
        UNION
        SELECT wdr.status_enum AS refund_enum,
               wdr.refund_device_id,
               wdr.id          AS request_id,
               wdr.quantity,
               wdr.unit_code,
               wdr.lot_number,
               wdr.status_enum AS req_status,
               wdd.dispense_quantity,
               wdd.id AS dispense_id,
               dev."name"      AS item_name,
               dev.id      AS item_id,
               aci.total_price,
               aci.service_table,
               doc."name"      AS doctor_name,
               wdd.trace_no
        FROM wor_device_request wdr
                 LEFT JOIN wor_device_dispense wdd
                           ON wdr.refund_device_id = wdd.device_req_id
                               AND wdd.delete_flag = '0'
                 INNER JOIN adm_device_definition dev
                            ON wdr.device_def_id = dev.id
                                AND dev.delete_flag = '0'
                 LEFT JOIN adm_charge_item aci
                           ON aci.service_id = wdr.refund_device_id
                               AND aci.delete_flag = '0'
                 LEFT JOIN adm_practitioner doc
                           ON wdr.requester_id = doc.id
                               AND doc.delete_flag = '0'
        WHERE wdr.refund_device_id IS NOT NULL
          AND wdr.encounter_id =#{encounterId}
          AND aci.service_table =#{worDeviceRequest}
          <if test="refundStatus == null">
           AND wdr.status_enum IN (#{inRefund},#{completed})
          </if>
          <if test="refundStatus == 5">
            AND wdr.status_enum = #{inRefund}
          </if>
          <if test="refundStatus == 3">
            AND wdr.status_enum = #{completed}
          </if>
          AND wdr.delete_flag = '0'
    </select>
    <select id="selectReturnItemDetail" resultType="com.openhis.web.pharmacymanage.dto.DispenseInventoryDto">
        <choose>
        <when test="(medDispenseIdList != null and !medDispenseIdList.isEmpty())
                  or (devDispenseIdList != null and !devDispenseIdList.isEmpty())">
        <if test="medDispenseIdList != null and !medDispenseIdList.isEmpty()">
        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 AS req_id,
               T1.encounter_id,
               T1.dispense_time,
               T1.bus_no AS dispense_no,
               T1.trace_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,
               T4.part_percent,
               T4.yb_no,
               T4.bus_no AS item_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,
               T9.id_card,
               T9."name" AS patient_name,
               #{medMedicationDefinition} AS item_table
        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'
            LEFT JOIN adm_patient AS T9
                ON T1.patient_id = T9.id
                    AND T9.delete_flag = '0'
        WHERE T1.id IN
            <foreach collection="medDispenseIdList" item="medDispenseId" open="(" separator="," close=")">
                #{medDispenseId}
            </foreach>
          AND T1.delete_flag = '0'
        </if>
        <if test="(medDispenseIdList != null and !medDispenseIdList.isEmpty())
             and (devDispenseIdList != null and !devDispenseIdList.isEmpty())">
            UNION
        </if>
        <if test="devDispenseIdList != null and !devDispenseIdList.isEmpty()">
        SELECT T10.id AS dispense_id,
               T10.unit_code AS dispense_unit_code,
               T10.quantity AS dispense_quantity,
               T10.status_enum AS dispense_status,
               T10.device_req_id AS req_id,
               T10.encounter_id,
               T10.dispense_time,
               T10.bus_no AS dispense_no,
               T10.trace_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,
               T12.part_percent,
               T12.yb_no,
               T12.bus_no AS item_no,
               T12.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,
               T9.id_card,
               T9."name" AS patient_name,
               #{admDeviceDefinition} AS item_table
        FROM wor_device_dispense AS T10
            INNER JOIN wor_device_request AS T11
                ON T10.device_req_id = T11.id
                    AND T11.delete_flag = '0'
            LEFT JOIN wor_inventory_item AS T3
                ON T10.device_def_id = T3.item_id
                    AND T3.item_table = #{admDeviceDefinition}
                    AND T10.location_id = T3.location_id
                    AND T10.lot_number = T3.lot_number
                    AND T3.delete_flag = '0'
            LEFT JOIN adm_device_definition AS T12
                ON T10.device_def_id = T12.id
                    AND T12.delete_flag = '0'
            LEFT JOIN adm_practitioner AS T5
                ON T10.preparer_id = T5.id
                    AND T5.delete_flag = '0'
            LEFT JOIN adm_practitioner AS T6
                ON T10.performer_id = T6.id
                    AND T6.delete_flag = '0'
            LEFT JOIN adm_practitioner AS T7
                ON T11.requester_id = T7.id
                    AND T7.delete_flag = '0'
            LEFT JOIN adm_encounter AS T8
                ON T10.encounter_id = T8.id
                    AND T8.delete_flag = '0'
            LEFT JOIN adm_patient AS T9
                ON T10.patient_id = T9.id
                    AND T9.delete_flag = '0'
        WHERE T10.id IN
            <foreach collection="devDispenseIdList" item="devDispenseId" open="(" separator="," close=")">
                #{devDispenseId}
            </foreach>
            AND T10.delete_flag = '0'
        </if>
        </when>
    </choose>
    </select>
</mapper>
