<?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.inventorymanage.mapper.ReceiptApprovalMapper">

    <select id="selectSupplyDetail" resultType="com.openhis.web.inventorymanage.dto.SupplyItemDetailDto">
        SELECT T1.id AS supply_id,
               T1.item_table,
               T1.item_id,
               T1.item_quantity,
               T1.unit_code AS item_unit,
               T1.price,
               T1.total_price,
               T1.approver_id,
               T1.source_location_id,
               T1.source_location_store_id,
               T1.purpose_location_id,
               T1.purpose_location_store_id,
               T1.supplier_id,
               T1.lot_number,
               T1.trace_no,
               T1.invoice_no,
               T1.bus_no,
               T1.start_time,
               T1.end_time,
               T1.id AS service_id,
               T2."name",
               T2.bus_no AS item_bus_no,
               T2.py_str,
               T2.wb_str,
               T2.unit_code,
               T2.min_unit_code,
               T2.part_percent,
               T2.category_code AS item_category,
               T2.yb_no,
               T2.rx_flag,
               T2.approval_number,
               T2.manufacturer_text,
               T6."name" AS supplier_name,
               T7."name" AS practitioner_name
        FROM wor_supply_request AS T1
            LEFT JOIN wor_supply_delivery AS T5
                ON T1.id = T5.request_id
                    AND T5.delete_flag = '0'
            INNER JOIN med_medication_definition AS T2
                ON T2.id = T1.item_id
                    AND T2.delete_flag = '0'
            LEFT JOIN adm_supplier AS T6
                ON T1.supplier_id = T6.id
                    AND T6.delete_flag = '0'
            LEFT JOIN adm_practitioner AS T7
                ON T1.practitioner_id = T7.id
                    AND T7.delete_flag = '0'
        WHERE T1.bus_no = #{busNo}
            AND T5.status_enum = #{completed}
            AND T1.delete_flag = '0'
        UNION
        SELECT T1.id AS supply_id,
               T1.item_table,
               T1.item_id,
               T1.item_quantity,
               T1.unit_code AS item_unit,
               T1.price,
               T1.total_price,
               T1.approver_id,
               T1.source_location_id,
               T1.source_location_store_id,
               T1.purpose_location_id,
               T1.purpose_location_store_id,
               T1.supplier_id,
               T1.lot_number,
               T1.trace_no,
               T1.invoice_no,
               T1.bus_no,
               T1.start_time,
               T1.end_time,
               T1.id AS service_id,
               T3."name",
               T3.bus_no AS item_bus_no,
               T3.py_str,
               T3.wb_str,
               T3.unit_code,
               T3.min_unit_code,
               T3.part_percent,
               T3.category_code AS item_category,
               T3.yb_no,
               T3.rx_flag,
               T3.approval_number,
               T3.manufacturer_text,
               T6."name" AS supplier_name,
               T7."name" AS practitioner_name
        FROM wor_supply_request AS T1
           LEFT JOIN wor_supply_delivery AS T5
                ON T1.id = T5.request_id
           INNER JOIN adm_device_definition AS T3
                ON T3.id = T1.item_id
           LEFT JOIN adm_supplier AS T6
                ON T1.supplier_id = T6.id
                    AND T6.delete_flag = '0'
           LEFT JOIN adm_practitioner AS T7
                ON T1.practitioner_id = T7.id
                    AND T7.delete_flag = '0'
        WHERE T1.bus_no = #{busNo}
          AND T5.status_enum = #{completed}
          AND T1.delete_flag = '0'
    </select>

    <select id="selectChargeDetail" resultType="com.openhis.web.inventorymanage.dto.ItemChargeDetailDto">
        SELECT T1.id AS definition_id,
               T1.instance_id,
               T1.charge_name,
               T1.price AS sell_price,
               T2.condition_code,
               T2.condition_value,
               T2.amount AS unit_price,
               T3.unit_code,
               T3.min_unit_code
        FROM adm_charge_item_definition AS T1
            LEFT JOIN adm_charge_item_def_detail AS T2
                ON T1.id = T2.definition_id
            LEFT JOIN med_medication_definition AS T3
                ON T1.instance_id = T3.id
        WHERE T1.instance_id IN
            <foreach collection="itemIdList" item="itemId" separator="," open="(" close=")">
                #{itemId}
            </foreach>
    </select>
    <select id="selectReceiptPage" resultType="com.openhis.web.inventorymanage.dto.ReceiptPageDto">
        SELECT T2.supply_bus_no,
               T2.status_enum,
               T2.type_enum,
               T2.supplier_id,
               T2.approver_id,
               T2.approval_time,
               T2.applicant_id,
               T2.apply_time,
               T2.practitioner_id,
               T2.trace_no,
               T2.tenant_id
        FROM (SELECT T1.bus_no AS supply_bus_no,
                     T1.status_enum,
                     T1.type_enum,
                     T1.supplier_id,
                     T1.approver_id,
                     T1.approval_time,
                     T1.applicant_id,
                     T1.apply_time,
                     T1.practitioner_id,
                     T1.trace_no,
                     T1.tenant_id
              FROM wor_supply_request AS T1
              WHERE T1.status_enum IN (#{approval},#{agree},#{reject})
                    AND T1.delete_flag != '1'
              GROUP BY T1.bus_no,
                       T1.status_enum,
                       T1.type_enum,
                       T1.supplier_id,
                       T1.approver_id,
                       T1.approval_time,
                       T1.applicant_id,
                       T1.apply_time,
                       T1.practitioner_id,
                       T1.trace_no,
                       T1.tenant_id
              ORDER BY T1.apply_time desc
             ) AS T2
            ${ew.customSqlSegment}
    </select>
</mapper>
