<?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.reportmanage.mapper.InboundReportMapper">

    <select id="selectInboundReportPage"
            resultType="com.openhis.web.reportmanage.dto.InboundReportPageDto">
        SELECT T10.supply_bus_no,                                 --单据号
               T10.name,                                          --药品名称
               T10.bus_no,                                        --药品编码
               T10.lot_number,                                    --批次号
               T10.purpose_location_id,                           --目的仓库ID
               T10.location_name,                                 --仓库名称
               T10.location_store_name,                           --货位名称
               T10.unit_code,                                     --计量单位
               T10.quantity,                                      --采购数量
               T10.price,                                         --采购单价
               T10.total_price               AS total_price,      --采购金额（总价）
               T10.sale_price,                                    --售价
               T10.sale_price * T10.quantity AS total_sale_price, --售价金额（总价）
               T10.supplier_id,                                   --供应商id
               T10.supplier,                                      --供应商
               T10.approver_id,                                   --审核人
               T10.occurrence_time,                                   --制单日期
               T10.approval_time,                                 --审核日期
               T10.tenant_id                                      -- 租户ID
        FROM (SELECT T1.bus_no        AS supply_bus_no,       --单据号
                     T2.name,                                 --药品名称
                     T2.bus_no,                               --药品编码
                     T1.lot_number,                           --批次号
                     T1.purpose_location_id,                  -- 目的仓库ID
                     T7.name          AS location_name,       --仓库名称
                     T8.name          AS location_store_name, --货位名称
                     T1.unit_code,                            --计量单位
                     T1.item_quantity AS quantity,            --采购数量
                     T4.amount        AS price,               --采购单价
                     T1.total_price,                          --采购金额(总价)
                     T4.amount        AS sale_price,          --售价
                     T2.part_percent,                         --拆零比
                     T1.supplier_id,                            --供应商id
                     T9.name          AS supplier,            --供应商
                     T1.approver_id,                          --审核人
                     T1.occurrence_time,                          --制单日期
                     T1.approval_time,                        --审核日期
                     T1.tenant_id                             -- 租户ID
              FROM wor_supply_request AS T1
                       INNER JOIN wor_supply_delivery AS T14
                                  ON T14.request_id = T1.id
                                      AND T14.status_enum = #{deliveryStatus}
                                      AND T14.delete_flag = '0'
                       LEFT JOIN med_medication_definition AS T2
                                 ON T2.id = T1.item_id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN adm_charge_item_definition AS T3
                                 ON T3.instance_id = T1.item_id
                                     AND T3.delete_flag = '0'
                       LEFT JOIN adm_charge_item_def_detail AS T4
                                 ON T4.definition_id = T3.id
                                     AND T4.delete_flag = '0'
                                     AND T4.condition_value = T1.lot_number
                                     AND T4.condition_code = '1'--1:批号进价
                       LEFT JOIN med_medication AS T5
                                 ON T5.medication_def_id = T2.id
                                     AND T5.delete_flag = '0'
                       LEFT JOIN adm_location AS T7
                                 ON T7.id = T1.purpose_location_id
                                     AND T7.delete_flag = '0'
                       LEFT JOIN adm_location AS T8
                                 ON T8.id = T1.purpose_location_store_id
                                     AND T8.delete_flag = '0'
                       LEFT JOIN adm_supplier AS T9
                                 ON T9.id = T1.supplier_id
                                     AND T9.delete_flag = '0'
              WHERE T1.delete_flag = '0'
                AND T1.type_enum = #{purchaseInventory}
                AND T1.status_enum = #{supplyStatus}
                AND T1.item_table = #{medicationTableName}
              UNION
              SELECT T1.bus_no                           AS supply_bus_no,       --单据号
                     T11.name,                                                   --器材名称
                     T11.bus_no,                                                 --器材编码
                     T1.lot_number,                                              --批次号
                     T1.purpose_location_id,                                     -- 目的仓库ID
                     T7.name                             AS location_name,       --仓库名称
                     T8.name                             AS location_store_name, --货位名称
                     T1.unit_code,                                               --计量单位
                     T1.item_quantity AS quantity,                                --采购数量
                     T4.amount        AS price,                                  --采购单价
                     T1.total_price,                                             --采购金额(总价)
                     T4.amount        AS sale_price,                             --售价
                     T11.part_percent,                                           --拆零比
                     T1.supplier_id,                            --供应商id
                     T9.name                             AS supplier,            --供应商
                     T1.approver_id,                                             --审核人
                     T1.occurrence_time,                                             --制单日期
                     T1.approval_time,                                           --审核日期
                     T1.tenant_id                                                -- 租户ID
              FROM wor_supply_request AS T1
                       INNER JOIN wor_supply_delivery AS T14
                                  ON T14.request_id = T1.id
                                      AND T14.status_enum = #{deliveryStatus}
                                      AND T14.delete_flag = '0'
                       LEFT JOIN adm_device_definition AS T11
                                 ON T11.id = T1.item_id
                                     AND T11.delete_flag = '0'
                       LEFT JOIN adm_charge_item_definition AS T3
                                 ON T3.instance_id = T1.item_id
                                     AND T3.delete_flag = '0'
                       LEFT JOIN adm_charge_item_def_detail AS T4
                                 ON T4.definition_id = T3.id
                                     AND T4.delete_flag = '0'
                                     AND T4.condition_value = T1.lot_number
                                     AND T4.condition_code = '1'--1:批号进价
                       LEFT JOIN adm_location AS T7
                                 ON T7.id = T1.purpose_location_id
                                     AND T7.delete_flag = '0'
                       LEFT JOIN adm_location AS T8
                                 ON T8.id = T1.purpose_location_store_id
                                     AND T8.delete_flag = '0'
                       LEFT JOIN adm_supplier AS T9
                                 ON T9.id = T1.supplier_id
                                     AND T9.delete_flag = '0'
              WHERE T1.delete_flag = '0'
                AND T1.type_enum = #{purchaseInventory}
                AND T1.status_enum = #{supplyStatus}
                AND T1.item_table = #{deviceTableName}) AS T10
            ${ew.customSqlSegment}
        ORDER BY T10.supply_bus_no DESC
    </select>
</mapper>
