<?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.materialmanage.mapper.MaterialCommonMapper">
    <select id="getDeviceInfo"
            resultType="com.openhis.web.materialmanage.dto.MaterialDeviceInfoDto">
        SELECT T6.id,--物品id
               T6.bus_no,--   药品编号
               T6.name,-- 物品名称
               T6.py_str,-- 拼音码
               T6.total_volume,--规格
               T6.unit_code,-- 包装单位
               T6.min_unit_code,--最小单位
               T6.part_percent,--拆零比
               T6.price,--进货单价
               T6.retailPrice,--零售价
               T6.manufacturer_text,-- 厂家/产地
               T6.approval_number,--批准文号
               T6.item_min_quantity, --库存最小警戒线
               T6.specification_inventory,--规格库存
               T6.batch_inventory,--批次库存
               T6.lot_number,
               T6.production_date,--生产日期
               T6.expiration_date,--失效日期
               <if test="locationId != null">
                   T6.location_id,
               </if>
               T6.supplier_id,
               T6.tenant_id
        FROM (SELECT T1.id,--物品id
                     T1.bus_no,--   药品编号
                     T1.name,-- 物品名称
                     T1.py_str,-- 拼音码
                     T1.size AS total_volume,--规格
                     T1.unit_code,-- 包装单位
                     T1.min_unit_code,--最小单位
                     T1.part_percent,--拆零比
                     CASE
                         WHEN T9.lot_number IS NULL THEN
                             CASE T4.condition_value
                                 WHEN T1.min_unit_code THEN T4.amount * T1.part_percent
                                 ELSE  T4.amount  END
                         ELSE CASE T4.condition_value
                                 WHEN T1.min_unit_code THEN T10.amount * T1.part_percent
                                 ELSE  T10.amount  END
                         END                                                        AS price,--进货单价(大单位)
                     CASE
                         WHEN T9.lot_number IS NULL THEN
                             CASE T5.condition_value
                                 WHEN T1.min_unit_code THEN T5.amount * T1.part_percent
                                 ELSE  T5.amount  END
                         ELSE CASE T5.condition_value
                                 WHEN T1.min_unit_code THEN T11.amount * T1.part_percent
                                 ELSE  T11.amount  END
                         END                                                             AS retailPrice,--零售价(大单位)
                     T1.manufacturer_text,-- 厂家/产地
                     T1.approval_number,--批准文号
                     T1.item_min_quantity * T1.part_percent                              AS item_min_quantity, --库存最小警戒线(小单位)
                     COALESCE(CAST(T8.specification_inventory AS DECIMAL(18, 6)), 0.00) AS specification_inventory,--规格库存(小单位)
                     COALESCE(CAST(T9.batch_inventory AS DECIMAL(18, 6)), 0.00)         AS batch_inventory,--批次库存(小单位)
                     T9.lot_number,
                     T7.production_date,--生产日期
                     T7.expiration_date,--失效日期
                     <if test="locationId != null">
                         T7.location_id,
                     </if>
                     T7.supplier_id,
                     T1.tenant_id
              FROM adm_device_definition AS T1
                       LEFT JOIN adm_device AS T2
                                 ON T1.id = T2.device_def_id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN adm_charge_item_definition T3
                                 ON T3.instance_id = T1.id
                                     AND T3.instance_table = 'adm_device_definition'
                                     AND T3.delete_flag = '0'
                       LEFT JOIN adm_charge_item_def_detail T4
                                 ON T4.definition_id = T3.id
                                     AND T4.condition_code = '2'--2:采购价
                                     AND T4.delete_flag = '0'
                       LEFT JOIN adm_charge_item_def_detail T5
                                 ON T5.definition_id = T3.id
                                     AND T5.condition_code = '3'--2:单位
                                     AND T5.delete_flag = '0'
                       LEFT JOIN wor_inventory_item T7
                                 ON T7.item_table = 'adm_device_definition'
                                     AND T7.item_id = T1.id
                                     AND T7.delete_flag = '0'
                       LEFT JOIN (SELECT i1.item_table,
                                         i1.item_id,
                                         <if test="locationId != null">
                                             i1.location_id,
                                         </if>
                                         COALESCE(CAST(SUM(i1.quantity) AS DECIMAL(18, 6)), 0.00) AS specification_inventory
                                  FROM wor_inventory_item i1
                                           LEFT JOIN adm_device_definition i2 ON i1.item_id = i2.ID
                                      AND i2.delete_flag = '0'
                                  WHERE i1.item_table = 'adm_device_definition'
                                    AND i1.delete_flag = '0'
                                  GROUP BY i1.item_table,
                                           i1.item_id
                                           <if test="locationId != null">
                                               ,i1.location_id
                                           </if>) T8
                                 ON T8.item_table = 'adm_device_definition'
                                     AND T8.item_id = T1.ID
                                     <if test="locationId != null">
                                         AND T8.location_id = T7.location_id
                                     </if>
                       LEFT JOIN (SELECT i3.item_table,
                                         i3.item_id,
                                         i3.lot_number,
                                         <if test="locationId != null">
                                             i3.location_id,
                                         </if>
                                         COALESCE(CAST(SUM(i3.quantity) AS DECIMAL(18, 6)), 0.00) AS batch_inventory
                                  FROM wor_inventory_item i3
                                           LEFT JOIN adm_device_definition i4 ON i3.item_id = i4.ID
                                      AND i4.delete_flag = '0'
                                  WHERE i3.item_table = 'adm_device_definition'
                                    AND i3.delete_flag = '0'
                                  GROUP BY i3.item_table,
                                           i3.item_id,
                                           i3.lot_number
                                           <if test="locationId != null">
                                               ,i3.location_id
                                           </if>) T9
                                 ON T9.item_table = 'adm_device_definition'
                                     AND T9.item_id = T1.ID
                                     AND T9.lot_number = T7.lot_number
                                     <if test="locationId != null">
                                         AND T9.location_id = T7.location_id
                                     </if>
                       LEFT JOIN adm_charge_item_def_detail T10
                                 ON T10.definition_id = T3.id
                                     AND T10.condition_code = '1'--1:批号进价
                                     AND T10.condition_value = T9.lot_number
                                     AND T10.delete_flag = '0'
                       LEFT JOIN adm_charge_item_def_detail T11
                                 ON T11.definition_id = T3.id
                                     AND T11.condition_code = '5'--1:批号售价
                                     AND T11.condition_value = T9.lot_number
                                     AND T11.delete_flag = '0'
              WHERE T1.delete_flag = '0'
                 <if test="locationId != null">
                    AND T7.location_id = #{locationId}
                 </if>
              GROUP BY T1.id,
                       T1.size,
                       T4.condition_value,
                       T5.condition_value,
                       T4.amount,
                       T5.amount,
                       T10.amount,
                       T11.amount,
                       T8.specification_inventory,
                       T9.batch_inventory,
                       T9.lot_number,
                       <if test="locationId != null">
                          T7.location_id,
                       </if>
                       T7.supplier_id,
                       T7.production_date,
                       T7.expiration_date
              <if test="supplierId != null">
                  HAVING
                  T8.specification_inventory   <![CDATA[ <= ]]>  (T1.item_min_quantity * T1.part_percent)
              </if>
              ORDER BY T1.bus_no) AS T6
            ${ew.customSqlSegment}
    </select>
    <select id="getPage"
            resultType="com.openhis.web.materialmanage.dto.MaterialDto">
        SELECT T4.tenant_id,
               T4.supply_bus_no, --单据号
               T4.type_enum,     --类型
               T4.status_enum,   --状态
               T4.supplier_id,   --供应商
               T4.location_type_enum,  --仓库类型
               T4.location_id,   --仓库
               T4.applicant_id,  --开单人
               T4.applicant_name,  --开单人
               T4.approver_id,   --审批人
               T4.apply_time,    -- 制单时间
               T4.approval_time  --审批时间
        FROM (SELECT T1.tenant_id,
                     T1.bus_no              AS supply_bus_no,
                     T1.type_enum,
                     T1.status_enum,
                     T1.supplier_id,
                     <if test="locationFlg != null">
                         T1.source_type_enum AS location_type_enum,
                         T1.source_location_id AS location_id,
                     </if>
                     <if test="locationFlg == null">
                         T1.purpose_type_enum AS location_type_enum,
                         T1.purpose_location_id AS location_id,
                     </if>
                     T1.applicant_id,
                     T2.name  AS applicant_name,
                     T1.approver_id,
                     T1.apply_time,
                     T1.approval_time
              FROM wor_supply_request AS T1
                    LEFT JOIN adm_practitioner AS T2
                          ON T2.id = T1.applicant_id
                            AND T2.delete_flag = '0'
              WHERE  T1.delete_flag = '0'
              <if test="originalBusNoFlg != null">
                  and original_bus_no = ''
              </if>
              GROUP BY T1.tenant_id,
                       T1.bus_no,
                       T1.type_enum,
                       T1.status_enum,
                       T1.supplier_id,
                       <if test="locationFlg != null">
                           T1.source_type_enum,
                           T1.source_location_id,
                       </if>
                       <if test="locationFlg == null">
                           T1.purpose_type_enum,
                           T1.purpose_location_id,
                       </if>
                       T1.applicant_id,
                       T2.name,
                       T1.approver_id,
                       T1.apply_time,
                       T1.approval_time
             ) AS T4
            ${ew.customSqlSegment}
    </select>
    <select id="getDetailPage"
            resultType="com.openhis.web.materialmanage.dto.MaterialDetailDto">
        SELECT T1.id,                             --ID
               T1.bus_no,                         --单据号
               T2.bus_no        AS item_bus_no,   --药品编码
               T1.item_id,                        --药品id
               T2.name          AS item_name,     --名称
               T2.size AS total_volume,                   --规格
               T1.unit_code,                      --选择单位
               T2.unit_code     AS max_unit_code, --常规单位
               T2.min_unit_code AS min_unit_code, --最小单位
               T2.part_percent,                   --拆零比
               T1.item_quantity,                  --数量
               T1.total_quantity,                 --合计数量
               T1.price,                          --采购单价
               T1.total_price,                    --采购总价
               T1.retail_price,                   --零售价
               T1.total_retail_price,             --零价金额
               T2.manufacturer_text,              --生产厂商文本
               T2.approval_number,                --批准文号
               T1.remake,                         --备注
               T1.reason,                         --摘要（理由）
               T1.reason_code,                    --理由类型
               T1.batch_inventory,                --批次库存
               T1.specification_inventory,        --规格库存
               T1.applicant_id,--申请人
               T1.apply_time,--申请时间
               T1.approver_id,--审批人
               T1.approval_time,--审批时间
               T1.supplier_id,--供应商
               T4.phone,--供应商联系人
               T1.status_enum,
               T1.type_enum,
               T1.category_enum,--单据类型
               T1.source_type_enum,             --源仓库类型
               T1.source_location_id,             --源仓库
               T1.purpose_type_enum,             --目的仓库类型
               T1.purpose_location_id,             --目的仓库
               T1.practitioner_id,--仓库员
               T1.invoice_no,--发票号
               T1.lot_number,--产品批号
               T1.start_time,--开始时间
               T1.end_time,--结束时间
               T1.packaging_condition_enum,--包装情况
               T1.acceptance_result_enum,--验收结果
               T1.trace_no,--追溯码
               T1.trace_no_unit_code,--追溯码包装
               T1.occurrence_time,--禁用日期
               T1.tenant_id
        FROM wor_supply_request AS T1
                 LEFT JOIN adm_device_definition AS T2
                           ON T1.item_id = T2.id
                               AND T2.delete_flag = '0'
                 LEFT JOIN adm_device AS T3
                           ON T2.id = T3.device_def_id
                               AND T3.delete_flag = '0'
                 LEFT JOIN adm_supplier AS T4
                           ON T4.id = T1.supplier_id
                               AND T4.delete_flag = '0'
        WHERE T1.bus_no = #{busNo}
          AND T1.delete_flag = '0'
        GROUP BY T1.id,
                 T2.bus_no,
                 T2.name,
                 T2.unit_code,
                 T2.min_unit_code,
                 T2.size,
                 T2.part_percent,
                 T2.manufacturer_text,
                 T2.approval_number,
                 T4.phone
    </select>
</mapper>