<?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.doctorstation.mapper.DoctorStationElepPrescriptionMapper">
    <select id="selectCatalogDrugInfoByNo" resultType="com.openhis.ybcatalog.domain.CatalogDrugInfo">
        SELECT T1.medical_catalog_code,
               T1.drug_trade_name,
               T1.generic_name_id,
               T1.drug_generic_name,
               T1.chemical_name,
               T1.alias,
               T1.english_name,
               T1.registered_name,
               T1.drug_supervision_code,
               T1.drug_form,
               T1.drug_form_name,
               T1.drug_category,
               T1.drug_category_name,
               T1.drug_specification,
               T1.drug_spec_code,
               T1.registered_form,
               T1.registered_spec,
               T1.registered_spec_code,
               T1.dosage,
               T1.frequency,
               T1.acid_base,
               T1.national_drug_code,
               T1.usage,
               T1.tcm_flag,
               T1.production_area_type,
               T1.production_area_name,
               T1.pricing_unit_type,
               T1.otc_flag,
               T1.otc_flag_name,
               T1.packaging_material,
               T1.packaging_material_name,
               T1.packaging_spec,
               T1.packaging_quantity,
               T1.function_indication,
               T1.administration_route,
               T1.instructions,
               T1.start_date,
               T1.end_date,
               T1.min_use_unit,
               T1.min_sale_unit,
               T1.min_measurement_unit,
               T1.min_package_quantity,
               T1.min_package_unit,
               T1.min_preparation_unit,
               T1.min_package_unit_name,
               T1.min_preparation_unit_name,
               T1.conversion_ratio,
               T1.shelf_life,
               T1.min_pricing_unit,
               T1.wubi_code,
               T1.pinyin_code,
               T1.repackager,
               T1.manufacturer_code,
               T1.manufacturer_name,
               T1.special_price_limit_flag,
               T1.special_drug_flag,
               T1.use_restriction,
               T1.use_restriction_flag,
               T1.registration_cert_no,
               T1.reg_cert_start_date,
               T1.reg_cert_end_date,
               T1.approval_no,
               T1.approval_start_date,
               T1.approval_end_date,
               T1.market_status,
               T1.market_status_name,
               T1.reg_document_archive,
               T1.supp_application_archive,
               T1.national_insurance_notes,
               T1.essential_drug_flag_name,
               T1.essential_drug_flag,
               T1.vat_adjustment_flag,
               T1.vat_adjustment_name,
               T1.listed_drug_flag,
               T1.negotiation_drug_flag,
               T1.negotiation_drug_name,
               T1.nhc_drug_code,
               T1.remarks,
               T1.valid_flag,
               T1.unique_record_id,
               T1.created_at,
               T1.updated_at,
               T1.version,
               T1.version_name,
               T1.pediatric_use,
               T1.company_name,
               T1.generic_evaluation_flag,
               T1.distribution_company,
               T1.distribution_contact,
               T1.distribution_auth_archive,
               T1.insurance_form,
               T1.insurance_class,
               T1.marketing_authorization_holder,
               T1.release_flag,
               T1.transmission_data_id,
               T1.valid_from,
               T1.valid_to
        FROM yb_catalog_drug_info T1
        WHERE T1.medical_catalog_code = #{medicationNo}
          AND T1.version = #{version}
          AND T1.valid_flag = '1'
    </select>
    <select id="selectCatalogDrugInfo" resultType="com.openhis.ybcatalog.domain.CatalogDrugInfo">
        SELECT T3.medical_catalog_code,
               T3.drug_trade_name,
               T3.generic_name_id,
               T3.drug_generic_name,
               T3.chemical_name,
               T3.alias,
               T3.english_name,
               T3.registered_name,
               T3.drug_supervision_code,
               T3.drug_form,
               T3.drug_form_name,
               T3.drug_category,
               T3.drug_category_name,
               T3.drug_specification,
               T3.drug_spec_code,
               T3.registered_form,
               T3.registered_spec,
               T3.registered_spec_code,
               T3.dosage,
               T3.frequency,
               T3.acid_base,
               T3.national_drug_code,
               T3.usage,
               T3.tcm_flag,
               T3.production_area_type,
               T3.production_area_name,
               T3.pricing_unit_type,
               T3.otc_flag,
               T3.otc_flag_name,
               T3.packaging_material,
               T3.packaging_material_name,
               T3.packaging_spec,
               T3.packaging_quantity,
               T3.function_indication,
               T3.administration_route,
               T3.instructions,
               T3.start_date,
               T3.end_date,
               T3.min_use_unit,
               T3.min_sale_unit,
               T3.min_measurement_unit,
               T3.min_package_quantity,
               T3.min_package_unit,
               T3.min_preparation_unit,
               T3.min_package_unit_name,
               T3.min_preparation_unit_name,
               T3.conversion_ratio,
               T3.shelf_life,
               T3.min_pricing_unit,
               T3.wubi_code,
               T3.pinyin_code,
               T3.repackager,
               T3.manufacturer_code,
               T3.manufacturer_name,
               T3.special_price_limit_flag,
               T3.special_drug_flag,
               T3.use_restriction,
               T3.use_restriction_flag,
               T3.registration_cert_no,
               T3.reg_cert_start_date,
               T3.reg_cert_end_date,
               T3.approval_no,
               T3.approval_start_date,
               T3.approval_end_date,
               T3.market_status,
               T3.market_status_name,
               T3.reg_document_archive,
               T3.supp_application_archive,
               T3.national_insurance_notes,
               T3.essential_drug_flag_name,
               T3.essential_drug_flag,
               T3.vat_adjustment_flag,
               T3.vat_adjustment_name,
               T3.listed_drug_flag,
               T3.negotiation_drug_flag,
               T3.negotiation_drug_name,
               T3.nhc_drug_code,
               T3.remarks,
               T3.valid_flag,
               T3.unique_record_id,
               T3.created_at,
               T3.updated_at,
               T3.version,
               T3.version_name,
               T3.pediatric_use,
               T3.company_name,
               T3.generic_evaluation_flag,
               T3.distribution_company,
               T3.distribution_contact,
               T3.distribution_auth_archive,
               T3.insurance_form,
               T3.insurance_class,
               T3.marketing_authorization_holder,
               T3.release_flag,
               T3.transmission_data_id,
               T3.valid_from,
               T3.valid_to,
               T3.tenant_id
        FROM (SELECT T1.medical_catalog_code,
                     T1.drug_trade_name,
                     T1.generic_name_id,
                     T1.drug_generic_name,
                     T1.chemical_name,
                     T1.alias,
                     T1.english_name,
                     T1.registered_name,
                     T1.drug_supervision_code,
                     T1.drug_form,
                     T1.drug_form_name,
                     T1.drug_category,
                     T1.drug_category_name,
                     T1.drug_specification,
                     T1.drug_spec_code,
                     T1.registered_form,
                     T1.registered_spec,
                     T1.registered_spec_code,
                     T1.dosage,
                     T1.frequency,
                     T1.acid_base,
                     T1.national_drug_code,
                     T1.usage,
                     T1.tcm_flag,
                     T1.production_area_type,
                     T1.production_area_name,
                     T1.pricing_unit_type,
                     T1.otc_flag,
                     T1.otc_flag_name,
                     T1.packaging_material,
                     T1.packaging_material_name,
                     T1.packaging_spec,
                     T1.packaging_quantity,
                     T1.function_indication,
                     T1.administration_route,
                     T1.instructions,
                     T1.start_date,
                     T1.end_date,
                     T1.min_use_unit,
                     T1.min_sale_unit,
                     T1.min_measurement_unit,
                     T1.min_package_quantity,
                     T1.min_package_unit,
                     T1.min_preparation_unit,
                     T1.min_package_unit_name,
                     T1.min_preparation_unit_name,
                     T1.conversion_ratio,
                     T1.shelf_life,
                     T1.min_pricing_unit,
                     T1.wubi_code,
                     T1.pinyin_code,
                     T1.repackager,
                     T1.manufacturer_code,
                     T1.manufacturer_name,
                     T1.special_price_limit_flag,
                     T1.special_drug_flag,
                     T1.use_restriction,
                     T1.use_restriction_flag,
                     T1.registration_cert_no,
                     T1.reg_cert_start_date,
                     T1.reg_cert_end_date,
                     T1.approval_no,
                     T1.approval_start_date,
                     T1.approval_end_date,
                     T1.market_status,
                     T1.market_status_name,
                     T1.reg_document_archive,
                     T1.supp_application_archive,
                     T1.national_insurance_notes,
                     T1.essential_drug_flag_name,
                     T1.essential_drug_flag,
                     T1.vat_adjustment_flag,
                     T1.vat_adjustment_name,
                     T1.listed_drug_flag,
                     T1.negotiation_drug_flag,
                     T1.negotiation_drug_name,
                     T1.nhc_drug_code,
                     T1.remarks,
                     T1.valid_flag,
                     T1.unique_record_id,
                     T1.created_at,
                     T1.updated_at,
                     T1.version,
                     T1.version_name,
                     T1.pediatric_use,
                     T1.company_name,
                     T1.generic_evaluation_flag,
                     T1.distribution_company,
                     T1.distribution_contact,
                     T1.distribution_auth_archive,
                     T1.insurance_form,
                     T1.insurance_class,
                     T1.marketing_authorization_holder,
                     T1.release_flag,
                     T1.transmission_data_id,
                     T1.valid_from,
                     T1.valid_to,
                     1 AS tenant_id
              FROM yb_catalog_drug_info T1
                       INNER JOIN (
                  SELECT medical_catalog_code, MAX(version) AS max_version
                  FROM yb_catalog_drug_info
                  WHERE valid_flag = '1'
                  GROUP BY medical_catalog_code
              ) T2
                                  ON T1.medical_catalog_code = T2.medical_catalog_code AND
                                     T1.version = T2.max_version
              WHERE T1.valid_flag = '1') AS T3
            ${ew.customSqlSegment}
    </select>
    <select id="selectElepPrescriptionInfo" resultType="com.openhis.web.doctorstation.dto.ElepPrescriptionInfoDto">
        SELECT tenant_id,
               prescription_no,             --处方号
               ipt_otp_no,                  --门诊号
               department_ward,             --科室病区
               validity_days,               --有效天数
               status_enum,                 --状态
               practitioner_name,           --开方医生名
               prsc_dept_name,              --开单科室
               MIN(prsc_time) AS prsc_time, --处方开立日期
               med_status,                  --取药状态
               extension_reason,            --延长原因
               quash_reason,                --撤销原因
               condition_name,              --诊断名
               rx_type_code                 --处方类别
        FROM (SELECT T1.tenant_id,
                     T1.prescription_no,                 --处方号
                     T1.ipt_otp_no,                      --门诊号
                     T1.department_ward,                 --科室病区
                     T1.validity_days,                   --有效天数
                     T1.status_enum,                     --状态
                     T2.name       AS practitioner_name, --开方医生名
                     T3.name       AS prsc_dept_name,    --开单科室
                     T1.issue_time AS prsc_time,         --处方开立日期
                     CASE
                         WHEN (T4.rx_used_stas_codg IN ('1', '2')) THEN T4.rx_used_stas_name
                         ELSE '未使用'
                         END       AS med_status,        --取药状态
                     T1.extension_reason,                --延长原因
                     T1.quash_reason,                    --撤销原因
                     T6.name       AS condition_name,    --诊断名
                     T1.rx_type_code                     --处方类别
              FROM elep_medication_request AS T1
                       LEFT JOIN adm_practitioner AS T2
                                 ON T1.prescribing_dr_id = T2.id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN adm_organization AS T3
                                 ON T1.org_id = T3.id
                                     AND T3.delete_flag = '0'
                       LEFT JOIN yb_elep_medresult_info AS T4
                                 ON T1.hi_rxno = T4.hi_rxno
                       LEFT JOIN cli_condition AS T5
                                 ON T1.condition_id = T5.id
                                     AND T5.delete_flag = '0'
                       LEFT JOIN cli_condition_definition AS T6
                                 ON T5.definition_id = T6.id
                                     AND T6.delete_flag = '0'
              WHERE T1.patient_id = #{patientId}
                AND T1.delete_flag = '0')
        GROUP BY tenant_id,
                 prescription_no,
                 ipt_otp_no,
                 department_ward,
                 validity_days,
                 status_enum,
                 practitioner_name,
                 prsc_dept_name,
                 med_status,
                 extension_reason,
                 quash_reason,
                 condition_name,
                 rx_type_code
        ORDER BY prsc_time DESC
    </select>
    <select id="selectMedicationInfo" resultType="com.openhis.web.doctorstation.dto.ElepMedicationInfoDto">
        SELECT T1.id,
               T2.medical_catalog_code AS medication_id,   --药品id
               T2.registered_name      AS medication_name, --药品名
               T2.drug_specification,                      --药品规格
               T2.manufacturer_name,                       --生产厂家
               T1.med_dosage,                              --药品剂量
               T1.med_dosage_unit_code,                    --药品剂量单位
               T1.med_frequency,                           --使用频次
               T1.med_route,                               --途径
               T1.quantity,                                --请求数量
               T1.unit_code,                               --请求单位
               T1.support_info,                            --支持用药信息
               T1.effective_dose_start,                    --服药时间(开始)
               T1.effective_dose_end,                      --服药时间(结束)
               T1.dispense_interval,                       --给药间隔
               T1.dispense_per_quantity,                   --单次发药数
               T1.dispense_per_duration,                   --每次发药供应天数
               T1.rx_type_code,                            --处方类别
               T1.rx_item_type_code,                       --处方项目类别
               T1.condition_id,                            --诊断id
               T1.version                                  --药品版本号
        FROM elep_medication_request AS T1
                 LEFT JOIN yb_catalog_drug_info AS T2
                           ON T1.medication_id = T2.medical_catalog_code
                               AND T1.version = T2.version
        WHERE T1.prescription_no = #{prescriptionNo}
          AND T1.delete_flag = '0'
        GROUP BY T1.id,
                 T2.medical_catalog_code,
                 T2.registered_name,
                 T2.drug_specification,
                 T2.manufacturer_name,
                 T1.med_dosage,
                 T1.med_dosage_unit_code,
                 T1.med_frequency,
                 T1.med_route,
                 T1.quantity,
                 T1.unit_code,
                 T1.support_info,
                 T1.effective_dose_start,
                 T1.effective_dose_end,
                 T1.dispense_interval,
                 T1.dispense_per_quantity,
                 T1.dispense_per_duration,
                 T1.rx_type_code,
                 T1.rx_item_type_code,
                 T1.condition_id,
                 T1.version
    </select>
    <select id="selectSaveInfo" resultType="com.openhis.web.doctorstation.dto.ElepPrescriptionInfoParam">
        SELECT T7.id       AS organization_id, --医院id
               T1.bus_no   AS ipt_otp_no,      --门诊/住院病历号
               CASE
                   WHEN (T1.class_enum = '2') THEN T3.name
                   WHEN (T1.class_enum = '1') THEN T5.name
                   ELSE NULL
                   END     AS department_ward, --科室病区
               T6.insutype AS insurance_enum   --医保类型
               --T8.id       AS condition_id     --诊断id
        FROM adm_encounter AS T1
                 --                  LEFT JOIN adm_encounter_diagnosis AS T2
--                            ON T2.encounter_id = T1.id
--                                AND T2.maindise_flag = '1'
--                                AND T2.delete_flag = '0'
--                  LEFT JOIN cli_condition AS T8
--                            ON T2.condition_id = T8.id
--                                AND T8.delete_flag = '0'
                 LEFT JOIN adm_organization AS T3
                           ON T1.organization_id = T3.id
                               AND T3.delete_flag = '0'
                 LEFT JOIN adm_encounter_location AS T4
                           ON T4.encounter_id = T1.id
                               AND T4.status_enum = '2'
                               AND T4.delete_flag = '0'
                 LEFT JOIN adm_location AS T5
                           ON T5.id = T4.location_id
                               AND T5.delete_flag = '0'
                 LEFT JOIN yb_clinc_reg AS T6
                           ON T6.ipt_otp_no = T1.bus_no
                 LEFT JOIN adm_organization AS T7
                           ON LEFT (T3.bus_no, 6) = T7.bus_no
            AND T7.type_enum = '1'
            AND T7.delete_flag = '0'
        WHERE T1.id = #{encounterId}
          AND T1.delete_flag = '0'
        GROUP BY T7.id,
            T1.bus_no,
            T1.class_enum,
            T3.name,
            T5.name,
            T6.insutype
        --  T8.id
    </select>
    <!--    <select id="selectConditionInfo" resultType="com.openhis.web.doctorstation.dto.ElepPrescriptionInfoParam">-->
    <!--        SELECT  T3.id       AS condition_id,     &#45;&#45;诊断id-->
    <!--                T4.name     AS condition_name     &#45;&#45;诊断名-->
    <!--        FROM adm_encounter AS T1-->
    <!--                 LEFT JOIN adm_encounter_diagnosis AS T2-->
    <!--                           ON T2.encounter_id = T1.id-->
    <!--                               AND T2.delete_flag = '0'-->
    <!--                 LEFT JOIN cli_condition AS T3-->
    <!--                           ON T2.condition_id = T3.id-->
    <!--                               AND T3.delete_flag = '0'-->
    <!--                 LEFT JOIN cli_condition_definition AS T4-->
    <!--                           ON T3.definition_id = T4.id-->
    <!--                               AND T3.delete_flag = '0'-->
    <!--        WHERE T1.id = #{encounterId}-->
    <!--          AND T1.delete_flag = '0'-->
    <!--        GROUP BY T3.id,-->
    <!--                 T4.name-->
    <!--    </select>-->
</mapper>
