<?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.ybmanage.mapper.YbElepMapper">
    <select id="getVeriPrescriptionInfo" resultType="com.openhis.web.ybmanage.dto.VeriPrescriptionInfoDto">
        SELECT tenant_id,
               prescription_no, --处方号
               ipt_otp_no,      --门诊号
               patient_name,    --姓名
               certno,          --身份证号
               med_status,      --取药状态
               status_enum,     --状态
               prsc_dept_name,  --开单科室
               mdtrt_time,      --挂号日期
               prsc_time,       --处方开立日期
               hi_rxno          --医保处方编号
        FROM (SELECT T1.tenant_id,
                     T1.prescription_no,               --处方号
                     T1.ipt_otp_no,                    --门诊号
                     T2.name        AS patient_name,   --姓名
                     T2.id_card     AS certno,         --身份证号
                     CASE
                         WHEN (T5.rx_used_stas_codg IN ('1', '2')) THEN T5.rx_used_stas_name
                         ELSE '未使用'
                         END        AS med_status,     --取药状态
                     T1.status_enum AS status_enum,    --状态
                     T4.name        AS prsc_dept_name, --开单科室
                     T3.reception_time  AS mdtrt_time,     --挂号日期
                     T1.issue_time  AS prsc_time,      --处方开立日期
                     T1.hi_rxno                        --医保处方编号
              FROM elep_medication_request AS T1
                       LEFT JOIN adm_patient AS T2
                                 ON T1.patient_id = T2.id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN adm_encounter AS T3
                                 ON T1.encounter_id = T3.id
                                     AND T3.delete_flag = '0'
                       LEFT JOIN adm_organization AS T4
                                 ON T1.org_id = T4.id
                                     AND T4.delete_flag = '0'
                       LEFT JOIN yb_elep_medresult_info AS T5
                                 ON T1.hi_rxno = T5.hi_rxno
                       LEFT JOIN adm_account AS T6
                                 ON T1.encounter_id = T6.encounter_id
                                     AND T6.delete_flag = '0'
              WHERE T1.delete_flag = '0') ${ew.customSqlSegment}
        GROUP BY tenant_id,
                 prescription_no,
                 ipt_otp_no,
                 patient_name,
                 certno,
                 med_status,
                 status_enum,
                 prsc_dept_name,
                 mdtrt_time,
                 prsc_time,
                 hi_rxno
        ORDER BY prsc_time DESC
    </select>
    <select id="getVeriPrescriptionDetailInfo" resultType="com.openhis.web.ybmanage.dto.VeriPrescriptionDetailInfoDto">
        SELECT DISTINCT T1.prescription_no,                          --处方号
                        T1.ipt_otp_no,                               --门诊号
                        T1.quantity,                                 --请求数量
                        T1.unit_code,                                --请求单位
                        T1.status_enum       AS status_enum,         --药品请求状态
                        --处方信息
                        T2.drug_generic_name AS medication_name,     --药品名
                        T2.drug_specification,                       --药品规格
                        T1.med_dosage,                               --药品剂量
                        T1.med_dosage_unit_code,                     --药品剂量单位
                        T1.med_frequency,                            --使用频次
                        T1.med_route,                                --途径
                        CASE
                            WHEN (T9.rx_used_stas_codg IN ('1', '2')) THEN T9.rx_used_stas_name
                            ELSE '未使用'
                            END              AS med_status,          --取药状态
                        CASE
                            WHEN (T10.rx_stas_codg IN ('1', '2', '3', '4')) THEN T10.rx_stas_name
                            ELSE '未上传'
                            END              AS prescription_status, --处方状态
                        T1.rx_type_code,                             --处方类别
                        T1.support_info,                             --支持用药信息
                        T1.effective_dose_start,                     -- 服药时间(开始)
                        T1.effective_dose_end,                       -- 服药时间(结束)
                        T1.dispense_interval,                        -- 给药间隔
                        T1.dispense_per_quantity,                    -- 单次发药数
                        T1.dispense_per_duration,                    -- 每次发药供应天数
                        --就诊信息
                        T3.name              AS patn_name,           --患者名
                        T3.id_card           AS certno,              --身份证号
                        T4.name              AS practitioner_name,   --开方医生名
                        T6.name              AS mdtrt_dept_name,     --挂号科室
                        T7.name              AS prsc_dept_name,      --开单科室
                        T5.create_time        AS mdtrt_time,          --挂号日期
                        T1.issue_time        AS prsc_time,           --处方开立日期
                        --诊断信息
                        T8.name              AS condition_name       --诊断名
        FROM elep_medication_request AS T1
                 LEFT JOIN yb_catalog_drug_info AS T2
                           ON T1.medication_id = T2.medical_catalog_code
                 LEFT JOIN adm_patient AS T3
                           ON T1.patient_id = T3.id
                               AND T3.delete_flag = '0'
                 LEFT JOIN adm_practitioner AS T4
                           ON T1.prescribing_dr_id = T4.id
                               AND T4.delete_flag = '0'
                 LEFT JOIN adm_encounter AS T5
                           ON T1.encounter_id = T5.id
                               AND T5.delete_flag = '0'
                 LEFT JOIN adm_organization AS T6
                           ON T5.organization_id = T6.id
                               AND T6.delete_flag = '0'
                 LEFT JOIN adm_organization AS T7
                           ON T1.org_id = T7.id
                               AND T7.delete_flag = '0'
                 LEFT JOIN cli_condition AS T11
                           ON T1.condition_id = T11.id
                               AND T11.delete_flag = '0'
                 LEFT JOIN cli_condition_definition AS T8
                           ON T11.definition_id = T8.id
                               AND T8.delete_flag = '0'
                 LEFT JOIN yb_elep_medresult_info AS T9
                           ON T1.hi_rxno = T9.hi_rxno
                 LEFT JOIN (SELECT hi_rxno, rx_stas_codg, rx_stas_name
                            FROM (SELECT hi_rxno, rx_stas_codg, rx_stas_name, create_time
                                  FROM yb_elep_upload_output
                                  UNION ALL
                                  SELECT hi_rxno, rx_stas_codg, rx_stas_name, create_time
                                  FROM yb_elep_revoke_output
                                  UNION ALL
                                  SELECT hi_rxno, rx_stas_codg, rx_stas_name, create_time
                                  FROM yb_elep_quer_prescription_info
                                  UNION ALL
                                  SELECT hi_rxno, rx_stas_codg, rx_stas_name, create_time
                                  FROM yb_elep_medresult_info) combined_tables
                            ORDER BY create_time DESC LIMIT 1) AS T10
                           ON T1.hi_rxno = T10.hi_rxno
        WHERE T1.prescription_no = #{prescriptionNo}
          AND T1.delete_flag = '0'
    </select>

</mapper>