<?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.InventoryProductReportMapper">

    <select id="selectProductReportPage"
            resultType="com.openhis.web.reportmanage.dto.InventoryProductReportPageDto">
        SELECT *
        FROM (
               SELECT T8.id,                                                --ID
               T8.bus_no,                                            --药品编码
               T8.name,                                              --药品名称
               T8.lot_number,                                        --批次号
               T8.manufacturer_text,                                 --厂家
               T8.item_table,                                        --药品类型
               T8.item_quantity,                                     --库存数量
               T8.unit_code,                                         --计量单位
               T8.min_package_quantity,                              --小包装库存数
               T8.min_package_unit,                                  --小包装单位
               T8.price,                                             --进价
               T8.sale_price,                                        --售价
               T8.price * T8.part_percent       AS part_price,       --拆零进价
               T8.sale_price * T8.part_percent  AS part_sale_price,  --拆零售价
               T8.price * T8.item_quantity      AS total_price,      --进价金额（总价）
               T8.sale_price * T8.item_quantity AS total_sale_price, --售价金额（总价）
               T8.purpose_type_enum,                                 --仓库类型
               T8.location_name,                                     --仓库名称
               T8.location_store_name,                               --货位名称
               T8.purpose_location_id,                               --仓库ID
               T8.expiration_date,                                   --有效期
               T8.yb_no,                                              --医保编码
               T8.tenant_id,               -- 租户ID
               CAST(T8.item_table AS INTEGER) AS category_type          -- 项目类型（按分类编码，整型）
        FROM (SELECT T1.id,                                                 --ID
                     T2.bus_no,                                             --药品编码
                     T2.name,                                               --药品名称
                     T1.lot_number,                                         --批次号
                     T2.manufacturer_text,                                  --厂家
                     T2.category_code              AS item_table,           --药品类型
                     T1.quantity AS item_quantity,                          --库存数量
                     T2.unit_code,                                          --计量单位
                     T1.quantity                   AS min_package_quantity, --小包装库存数
                     T2.min_unit_code              AS min_package_unit,     --小包装单位
                     T1.price,                                              --进价
                     CASE
                         WHEN T4.condition_code = #{lotNumber}
                             AND T4.condition_value = T1.lot_number
                             THEN T4.amount
                         ELSE T3.price END         AS sale_price,           --售价
                     T2.part_percent,                                       --拆零比
                     T6.form_enum AS purpose_type_enum,                     --仓库类型
                     T6.name                       AS location_name,        --仓库名称
                     T7.name                       AS location_store_name,  --货位名称
                     T1.location_id                AS purpose_location_id,  --仓库ID
                     T1.expiration_date,                                    --有效期
                     T2.yb_no,                                               --医保编码
                     T1.tenant_id                -- 租户ID
              FROM wor_inventory_item AS T1
                       INNER JOIN med_medication_definition AS T2
                                  ON T2.id = T1.item_id
                                      AND T2.delete_flag = '0'
                       INNER JOIN adm_charge_item_definition AS T3
                                  ON T3.instance_id = T2.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'
                       LEFT JOIN adm_location T6
                                 ON T1.location_id = T6.id
                                     AND T6.delete_flag = '0'
                       LEFT JOIN adm_location T7
                                 ON T1.location_store_id = T7.id
                                     AND T7.delete_flag = '0'
              WHERE T1.delete_flag = '0') AS T8
        UNION
        SELECT T10.id,                                                 --ID
               T10.bus_no,                                             --器材编码
               T10.name,                                               --器材名称
               T10.lot_number,                                         --批次号
               T10.manufacturer_text,                                  --厂家
               T10.item_table,                                         --器材类型
               T10.item_quantity,                                      --库存数量
               T10.unit_code,                                          --计量单位
               T10.min_package_quantity,                               --小包装库存数
               T10.min_package_unit,                                   --小包装单位
               T10.price,                                              --进价
               T10.sale_price,                                         --售价
               T10.price * T10.part_percent       AS part_price,       --拆零进价
               T10.sale_price * T10.part_percent  AS part_sale_price,  --拆零售价
               T10.price * T10.item_quantity      AS total_price,      --进价金额（总价）
               T10.sale_price * T10.item_quantity AS total_sale_price, --售价金额（总价）
               T10.purpose_type_enum,                                  --仓库类型
               T10.location_name,                                      --仓库名称
               T10.location_store_name,                                --货位名称
               T10.purpose_location_id,                                --仓库ID
               T10.expiration_date,                                    --有效期
               T10.yb_no,                                               --医保编码
               T10.tenant_id,               -- 租户ID
               CAST(T10.item_table AS INTEGER) AS category_type           -- 项目类型（按分类编码，整型）
        FROM (SELECT T1.id,                                                 --ID
                     T9.bus_no,                                           --药品编码
                     T9.name,                                               --药品名称
                     T1.lot_number,                                         --批次号
                     T9.manufacturer_text,                                  --厂家
                     T9.category_code              AS item_table,           --药品类型
                     T1.quantity AS item_quantity,                         --库存数量
                     T9.unit_code,                                          --计量单位
                     T1.quantity                   AS min_package_quantity, --小包装库存数
                     T9.min_unit_code              AS min_package_unit,     --小包装单位
                     T1.price,                                              --进价
                     CASE
                         WHEN T4.condition_code = #{lotNumber}
                             AND T4.condition_value = T1.lot_number
                             THEN T4.amount
                         ELSE T3.price END         AS sale_price,           --售价
                     T9.part_percent,                                       --拆零比
                     T6.form_enum AS purpose_type_enum,                     --仓库类型
                     T6.name                       AS location_name,        --仓库名称
                     T7.name                       AS location_store_name,  --货位名称
                     T1.location_id                AS purpose_location_id,  --仓库ID
                     T1.expiration_date,                                    --有效期
                     T9.yb_no,                                               --医保编码
                     T1.tenant_id                -- 租户ID
              FROM wor_inventory_item AS T1
                       INNER JOIN adm_device_definition AS T9
                                  ON T9.id = T1.item_id
                                      AND T9.delete_flag = '0'
                       INNER JOIN adm_charge_item_definition AS T3
                                  ON T3.instance_id = T9.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'
                       LEFT JOIN adm_location T6
                                 ON T1.location_id = T6.id
                                     AND T6.delete_flag = '0'
                       LEFT JOIN adm_location T7
                                 ON T1.location_store_id = T7.id
                                     AND T7.delete_flag = '0'
             WHERE T1.delete_flag = '0') AS T10
            ) AS T
            ${ew.customSqlSegment}
    </select>
</mapper>
