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

    <select id="selectStocktakingReportPage"
            resultType="com.openhis.web.reportmanage.dto.StocktakingReportPageDto">
        SELECT T9.id,                --ID
               T9.bus_no,            --编码
               T9.name,              --名称
               T9.total_volume,      --规格
               T9.manufacturer_text, --厂家
               T9.lot_number,        --产品批号
               T9.location_name,     --目的仓库
               T9.unit_code,         --小单位
               T9.item_quantity,     --数量
               T9.approval_time,   --审批时间
               T9.supply_bus_no,--单据号
               T9.purpose_location_id,-- 目的仓库id
               T9.price,--采购单价
               T9.location_store_name,--仓库货位
               T9.total_price,--报损金额
               T9.name          AS supplier,--供应商
               T9.approver_id,                          --审核人
               T9.occurrence_time,                          --制单日期
               T9.tenant_id          -- 租户ID
        FROM (SELECT T1.id,                             --ID
                     T2.bus_no,                         --编码
                     T2.name,                           --名称
                     T3.total_volume,                   --规格
                     T2.manufacturer_text,              --厂家
                     T1.lot_number,                     --产品批号
                     T5.name          AS location_name, --目的仓库
                     T1.unit_code,                      --小单位
                     T1.item_quantity,                   --盈亏数量
                     T1.approval_time,                --审批时间
                     T1.bus_no        AS  supply_bus_no,--单据号
                     T1.purpose_location_id,-- 目的仓库id
                     T1.price,--采购单价
                     T4.name          AS location_store_name,--仓库货位
                     T1.total_price,--报损金额
                     T9.name          AS supplier,--供应商
                     T1.approver_id,                          --审核人
                     T1.occurrence_time,                          --制单日期
                     T1.tenant_id                       -- 租户ID
              FROM wor_supply_request AS T1
                       INNER JOIN wor_supply_delivery AS T8
                                  ON T8.request_id = T1.id
                                      AND T8.status_enum = #{deliveryStatus}
                                      AND T8.delete_flag = '0'
                       LEFT JOIN med_medication_definition AS T2
                                  ON T1.item_id = T2.id
                                      AND T2.delete_flag = '0'
                       LEFT JOIN med_medication AS T3
                                 ON T2.id = T3.medication_def_id
                                     AND T3.delete_flag = '0'
                       LEFT JOIN adm_location T5
                                 ON T1.purpose_location_id = T5.id
                                     AND T5.delete_flag = '0'
                       LEFT JOIN adm_location AS T4
                                 ON T4.id = T1.purpose_location_store_id
                                     AND T4.delete_flag = '0'
                       LEFT JOIN adm_supplier AS T9
                                 ON T9.id = T1.supplier_id
                                     AND T9.delete_flag = '0'
              WHERE T1.type_enum in (#{stocktakingReport}, #{stocktakingBatchReport})
                AND T1.status_enum = #{supplyStatus}
                AND T1.item_table = #{medicationTableName}
                AND T1.delete_flag = '0'
              UNION
              SELECT T1.id,                                     --ID
                     T6.bus_no,                                 --编码
                     T6.name,                                   --名称
                     T6.size AS total_volume,                 --规格（器材规格）
                     T6.manufacturer_text,                      --厂家
                     T1.lot_number,                             --产品批号
                     T5.name                  AS location_name, --目的仓库
                     T1.unit_code,                      --小单位
                     T1.item_quantity,                   --盈亏数量
                     T1.approval_time,                        --审批时间
                     T1.bus_no        AS  supply_bus_no,--单据号
                     T1.purpose_location_id,-- 目的仓库id
                     T1.price,--采购单价
                     T4.name          AS location_store_name,--仓库货位
                     T1.total_price,--报损金额
                     T9.name          AS supplier,--供应商
                     T1.approver_id,                          --审核人
                     T1.occurrence_time,                          --制单日期
                     T1.tenant_id                               -- 租户ID
              FROM wor_supply_request AS T1
                       INNER JOIN wor_supply_delivery AS T8
                                  ON T8.request_id = T1.id
                                      AND T8.status_enum = #{deliveryStatus}
                                      AND T8.delete_flag = '0'
                       LEFT JOIN adm_device_definition AS T6
                                  ON T1.item_id = T6.id
                                      AND T6.delete_flag = '0'
                       LEFT JOIN adm_location T5
                                 ON T1.purpose_location_id = T5.id
                                     AND T5.delete_flag = '0'
                       LEFT JOIN adm_location AS T4
                                 ON T4.id = T1.purpose_location_store_id
                                     AND T4.delete_flag = '0'
                       LEFT JOIN adm_supplier AS T9
                                 ON T9.id = T1.supplier_id
                                     AND T9.delete_flag = '0'
              WHERE T1.type_enum in (#{stocktakingReport}, #{stocktakingBatchReport})
                AND T1.status_enum = #{supplyStatus}
                AND T1.item_table = #{deviceTableName}
                AND T1.delete_flag = '0') AS T9
            ${ew.customSqlSegment}
        ORDER BY T9.supply_bus_no DESC
    </select>

    <select id="selectAutoStocktakingReportPage"
            resultType="com.openhis.web.reportmanage.dto.StocktakingReportPageDto">
        SELECT T9.id,                --ID
               T9.bus_no,            --编码
               T9.name,              --名称
               T9.total_volume,      --规格
               T9.manufacturer_text, --厂家
               T9.lot_number,        --产品批号
               T9.location_name,     --目的仓库
               T9.unit_code,         --小单位
               T9.item_quantity,     --数量
               T9.approval_time,   --审批时间
               T9.supply_bus_no,--单据号
               T9.purpose_location_id,-- 目的仓库id
               T9.price,--采购单价
               T9.location_store_name,--仓库货位
               T9.total_price,--报损金额
               T9.name          AS supplier,--供应商
               T9.approver_id,                          --审核人
               T9.occurrence_time,                          --制单日期
               T9.tenant_id          -- 租户ID
        FROM (SELECT T1.id,                             --ID
                     T2.bus_no,                         --编码
                     T2.name,                           --名称
                     T3.total_volume,                   --规格
                     T2.manufacturer_text,              --厂家
                     T1.lot_number,                     --产品批号
                     T5.name          AS location_name, --目的仓库
                     T1.unit_code,                      --小单位
                     T1.item_quantity,                   --盈亏数量
                     T1.approval_time,                --审批时间
                     T1.bus_no        AS  supply_bus_no,--单据号
                     T1.purpose_location_id,-- 目的仓库id
                     T1.price,--采购单价
                     T4.name          AS location_store_name,--仓库货位
                     T1.total_price,--报损金额
                     T9.name          AS supplier,--供应商
                     T1.approver_id,                          --审核人
                     T1.occurrence_time,                          --制单日期
                     T1.tenant_id                       -- 租户ID
              FROM wor_aut_supply_request AS T1
                       LEFT JOIN med_medication_definition AS T2
                                 ON T1.item_id = T2.id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN med_medication AS T3
                                 ON T2.id = T3.medication_def_id
                                     AND T3.delete_flag = '0'
                       LEFT JOIN adm_location T5
                                 ON T1.purpose_location_id = T5.id
                                     AND T5.delete_flag = '0'
                       LEFT JOIN adm_location AS T4
                                 ON T4.id = T1.purpose_location_store_id
                                     AND T4.delete_flag = '0'
                       LEFT JOIN adm_supplier AS T9
                                 ON T9.id = T1.supplier_id
                                     AND T9.delete_flag = '0'
              WHERE T1.type_enum = #{stocktakingReport}
                AND T1.status_enum = #{supplyStatus}
                AND T1.item_table = #{medicationTableName}
                AND T1.delete_flag = '0'
                AND TO_CHAR(T1.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') = #{time}
              UNION
              SELECT T1.id,                                     --ID
                     T6.bus_no,                                 --编码
                     T6.name,                                   --名称
                     T6.size AS total_volume,                 --规格（器材规格）
                     T6.manufacturer_text,                      --厂家
                     T1.lot_number,                             --产品批号
                     T5.name                  AS location_name, --目的仓库
                     T1.unit_code,                      --小单位
                     T1.item_quantity,                   --盈亏数量
                     T1.approval_time,                        --审批时间
                     T1.bus_no        AS  supply_bus_no,--单据号
                     T1.purpose_location_id,-- 目的仓库id
                     T1.price,--采购单价
                     T4.name          AS location_store_name,--仓库货位
                     T1.total_price,--报损金额
                     T9.name          AS supplier,--供应商
                     T1.approver_id,                          --审核人
                     T1.occurrence_time,                          --制单日期
                     T1.tenant_id                               -- 租户ID
              FROM wor_aut_supply_request AS T1
                       LEFT JOIN adm_device_definition AS T6
                                 ON T1.item_id = T6.id
                                     AND T6.delete_flag = '0'
                       LEFT JOIN adm_location T5
                                 ON T1.purpose_location_id = T5.id
                                     AND T5.delete_flag = '0'
                       LEFT JOIN adm_location AS T4
                                 ON T4.id = T1.purpose_location_store_id
                                     AND T4.delete_flag = '0'
                       LEFT JOIN adm_supplier AS T9
                                 ON T9.id = T1.supplier_id
                                     AND T9.delete_flag = '0'
              WHERE T1.type_enum = #{stocktakingReport}
                AND T1.status_enum = #{supplyStatus}
                AND T1.item_table = #{deviceTableName}
                AND T1.delete_flag = '0'
                AND TO_CHAR(T1.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') = #{time}) AS T9
        ORDER BY T9.supply_bus_no DESC
    </select>
</mapper>
