<?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.administration.mapper.ChargeItemMapper">

    <select id="getChargeItemBaseInfoByIds" resultType="com.openhis.administration.dto.ChargeItemBaseInfoDto">
        SELECT
        a.id ,
        a.status_enum ,
        a.bus_no ,
        a.patient_id ,
        a.context_enum ,
        a.encounter_id ,
        a.occurrence_time ,
        a.performer_id ,
        a.performing_org_id ,
        a.requesting_org_id ,
        a.cost_org_id ,
        a.quantity_value ,
        a.quantity_unit ,
        a.unit_price ,
        a.total_price ,
        a.definition_id ,
        a.def_detail_id ,
        a.base_amount ,
        a.discount_amount ,
        a.surcharge_amount ,
        a.override_reason_code ,
        a.override_reason_text ,
        a.enterer_id ,
        a.entered_date ,
        a.service_table ,
        a.service_id ,
        a.claim_state_enum ,
        a.account_id ,
        a.org_id ,
        a.tenant_id ,
        a.delete_flag ,
        a.create_by ,
        a.create_time ,
        a.update_by ,
        a.update_time ,
        a.print_count ,
        a.product_table ,
        a.product_id ,
        a.refund_id ,
        a.prescription_no ,
        a.generate_source_enum ,
        a.children_json ,
        a.condition_id ,
        a.encounter_diagnosis_id ,
        a.tcm_flag ,
        COALESCE(b1.bus_no, b2.bus_no, b3.bus_no) AS request_bus_no,
        COALESCE(b6.bus_no, b7.bus_no, b8.bus_no) AS base_bus_no,
        COALESCE(b6.name, b7.name, b8.name) AS "name",
        COALESCE(b6.category_code, b7.category_code, b8.category_code) AS category_code,
        COALESCE(b6.approval_number, b7.approval_number) AS approval_number,
        COALESCE(b6.yb_no, b7.yb_no, b8.yb_no) AS yb_no,
        CASE WHEN b6.children_flag is NOT NULL THEN b6.children_flag ELSE '0' END AS children_flag,
        COALESCE(b6.chrgitm_lv, b7.chrgitm_lv, b8.chrgitm_lv) AS chrgitm_lv,
        CASE WHEN b6.rx_flag is NOT NULL THEN b6.rx_flag ELSE 0 END AS children_flag,
        COALESCE(b1.yb_class_enum, b2.yb_class_enum, b3.yb_class_enum) AS yb_class_enum,
        COALESCE(b1.trace_no, b2.trace_no) AS trace_no,
        CASE WHEN b1.suffering_flag is NOT NULL THEN b1.suffering_flag ELSE 0 END AS suffering_flag,
        b5.yb_type,
        org."name" as dept_name,
        org.yb_no as dept_yb_no,
        pra."name" as doct_name,
        pra.yb_no as doct_yb_no,
        contract.bus_no as contract_no,
        T2.charge_name,
        T2.title,
        T2.status_enum,
        T2.org_id,
        T2.description,
        T2.instance_table,
        T2.instance_id,
        T2.effective_start,
        T2.effective_end,
        T2.type_code,
        T2.yb_type,
        T2.condition_flag,
        T2.price
        -- 添加更多需要的字段
        FROM
        adm_charge_item a
        LEFT JOIN
        adm_charge_item_definition T2 ON a.definition_id = T2.ID AND T2.delete_flag = '0'
        LEFT JOIN
        med_medication_request b1 ON a.service_id = b1.id AND a.service_table = 'med_medication_request'
        LEFT JOIN
        wor_device_request b2 ON a.service_id = b2.id AND a.service_table = 'wor_device_request'
        LEFT JOIN
        wor_service_request b3 ON a.service_id = b3.id AND a.service_table = 'wor_service_request'
        LEFT JOIN
        adm_healthcare_service b4 ON a.service_id = b4.id AND a.service_table = 'adm_healthcare_service'
        LEFT JOIN
        med_medication_definition b6 ON a.product_id = b6.id AND a.product_table = 'med_medication_definition'
        LEFT JOIN
        adm_device_definition b7 ON a.product_id = b7.id AND a.product_table = 'adm_device_definition'
        LEFT JOIN
        wor_activity_definition b8 ON a.product_id = b8.id AND a.product_table = 'wor_activity_definition'
        LEFT JOIN
        adm_charge_item_definition b5 ON a.definition_id = b5.id
        LEFT JOIN
        adm_organization org ON a.requesting_org_id = org.id
        LEFT JOIN
        adm_practitioner pra ON a.enterer_id = pra.id
        LEFT JOIN
        adm_account acc ON a.account_id = acc.id
        LEFT JOIN
        fin_contract contract ON acc.contract_no = contract.bus_no
        WHERE
        a.delete_flag = '0'
        AND
        a.id IN
        <foreach collection="chargeItemIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="getChargeItemDefInfoByIds" resultType="com.openhis.administration.dto.ChargeItemDefInfo">
        SELECT
        T1.id,
        T1.status_enum,
        T1.generate_source_enum,
        T1.bus_no,
        T1.prescription_no,
        T1.patient_id,
        T1.context_enum,
        T1.encounter_id,
        T1.occurrence_time,
        T1.performer_id,
        T1.performing_org_id,
        T1.requesting_org_id,
        T1.cost_org_id,
        T1.quantity_value,
        T1.quantity_unit,
        T1.unit_price,
        T1.total_price,
        T1.definition_id,
        T1.def_detail_id,
        T1.base_amount,
        T1.discount_amount,
        T1.surcharge_amount,
        T1.override_reason_code,
        T1.override_reason_text,
        T1.enterer_id,
        T1.entered_date,
        T1.service_table,
        T1.service_id,
        T1.product_table,
        T1.product_id,
        T1.claim_state_enum,
        T1.print_count,
        T1.account_id,
        T1.org_id,
        T1.refund_id,
        T1.children_json,
        T1.condition_id,
        T1.encounter_diagnosis_id,
        T1.tcm_flag,
        T1.create_time,
        T1.create_by,
        T1.update_time,
        T1.update_by,
        T2.charge_name,
        T2.title,
        T2.status_enum,
        T2.org_id,
        T2.description,
        T2.instance_table,
        T2.instance_id,
        T2.effective_start,
        T2.effective_end,
        T2.type_code,
        T2.yb_type,
        T2.condition_flag,
        T2.price
        FROM
        adm_charge_item T1
        LEFT JOIN adm_charge_item_definition T2 ON T1.definition_id = T2.ID
        AND T2.delete_flag = '0'
        WHERE
        T1.id IN
        <foreach collection="chargeItemIds" item="itemId" open="(" separator="," close=")">
            #{itemId}
        </foreach>
        AND T1.delete_flag = '0'
    </select>
</mapper>