<?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.datadictionary.mapper.DeviceManageMapper">
    <select id="getDevicePage" parameterType="java.util.Map"
            resultType="com.openhis.web.datadictionary.dto.DeviceManageDto">

    SELECT
        T3.id,
        T3.bus_no,
        T3.name,
        T3.py_str,
        T3.wb_str,
        T3.category_code,
        T3.type_code,
        T3.unit_code,
        T3.size,
        T3.part_percent,
        T3.min_unit_code,
        T3.org_id,
        T3.location_id,
        T3.model_number,
        T3.hvcm_flag,
        T3.sales_unit_code,
        T3.approval_number,
        T3.yb_flag,
        T3.yb_no,
        T3.yb_match_flag,
        T3.status_enum,
        T3.manufacturer_id,
        T3.manufacturer_text,
        T3.supply_id,
        T3.description,
        T3.jurisdiction,
        T3.version,
        T3.substance_text,
        T3.allergen_flag,
        T3.tenant_id,
        T3.item_type_code,
        T3.yb_type,
        T3.rx_flag,--处方标志
        T3.chrgitm_lv,--医保等级
        T3.retail_price,
        T3.purchase_price,
        T3.maximum_retail_price
        FROM
        (
        SELECT
            T1.id,
            T1.bus_no,
            T1.name,
            T1.py_str,
            T1.wb_str,
            T1.category_code,
            T1.type_code,
            T1.unit_code,
            T1.size,
            T1.part_percent,
            T1.min_unit_code,
            T1.org_id,
            T1.location_id,
            T1.model_number,
            T1.hvcm_flag,
            T1.sales_unit_code,
            T1.approval_number,
            T1.yb_flag,
            T1.yb_no,
            T1.yb_match_flag,
            T1.status_enum,
            T1.manufacturer_id,
            T1.manufacturer_text,
            T1.supply_id,
            T1.description,
            T1.jurisdiction,
            T1.version,
            T1.substance_text,
            T1.allergen_flag,
            T1.tenant_id,
            T1.rx_flag,--处方标志
            T1.chrgitm_lv,--医保等级
            T2.type_code as item_type_code,
            T2.yb_type,
            T2.price as retail_price,--零售价,
            (   SELECT T4.amount
                FROM adm_charge_item_definition T5
                LEFT JOIN  adm_charge_item_def_detail T4 ON  T4.definition_id = T5.id
                WHERE   T4.condition_code = '2'--2:采购价
                AND T5.instance_id = T1.id
                AND T5.instance_table = 'adm_device_definition'
            ) as purchase_price,--购入价
            (   SELECT T4.amount
                FROM adm_charge_item_definition T5
                LEFT JOIN  adm_charge_item_def_detail T4 ON  T4.definition_id = T5.id
                WHERE   T4.condition_code = '4'--4:限制
                AND T5.instance_id = T1.id
                AND T5.instance_table = 'adm_device_definition'
            ) as maximum_retail_price--最高零售价
        FROM adm_device_definition T1
        LEFT JOIN adm_charge_item_definition T2 ON T1.id = T2.instance_id
        <where>
            T1.delete_flag = '0'
            AND  T2.instance_table = 'adm_device_definition'
        </where>
        ORDER BY T1.bus_no
    ) AS T3
        ${ew.customSqlSegment}

    </select>

    <select id="getOne" resultType="com.openhis.web.datadictionary.dto.DeviceManageDto">
    SELECT
        T1.id,
        T1.bus_no,
        T1.name,
        T1.py_str,
        T1.wb_str,
        T1.category_code,
        T1.type_code,
        T1.unit_code,
        T1.size,
        T1.part_percent,
        T1.min_unit_code,
        T1.org_id,
        T1.location_id,
        T1.model_number,
        T1.hvcm_flag,
        T1.sales_unit_code,
        T1.approval_number,
        T1.yb_flag,
        T1.yb_no,
        T1.yb_match_flag,
        T1.status_enum,
        T1.manufacturer_id,
        T1.manufacturer_text,
        T1.supply_id,
        T1.description,
        T1.jurisdiction,
        T1.version,
        T1.substance_text,
        T1.allergen_flag,
        T1.tenant_id,
        T1.rx_flag,--处方标志
        T1.chrgitm_lv,--医保等级
        T2.type_code as item_type_code,
        T2.yb_type,
        T2.price as retail_price,--零售价,
        (   SELECT T4.amount
        FROM adm_charge_item_definition T5
        LEFT JOIN  adm_charge_item_def_detail T4 ON  T4.definition_id = T5.id
        WHERE   T4.condition_code = '2'--2:采购价
        AND T5.instance_id = T1.id
        AND T5.instance_table = 'adm_device_definition'
        ) as purchase_price,--购入价
        (   SELECT T4.amount
        FROM adm_charge_item_definition T5
        LEFT JOIN  adm_charge_item_def_detail T4 ON  T4.definition_id = T5.id
        WHERE   T4.condition_code = '4'--4:限制
        AND T5.instance_id = T1.id
        AND T5.instance_table = 'adm_device_definition'
        ) as maximum_retail_price--最高零售价
        FROM adm_device_definition T1
        LEFT JOIN adm_charge_item_definition T2 ON T1.id = T2.instance_id
        <where>
            T1.delete_flag = '0'
            AND  T2.instance_table = 'adm_device_definition'

            <if test="id!= null">
                AND T1.id = #{id}
            </if>
            <if test="tenantId!= null">
                AND T1.tenant_id = #{tenantId}
            </if>
        </where>
    </select>

</mapper>