<?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.YbMapper">

    <select id="yb3101SelectEncounterPatientInfo"
            resultType="com.openhis.yb.dto.Yb3101AdapterEncounterPatientInfo">
        SELECT MAX(T5.psn_no)                               AS patnId
             , MAX(T5.psn_name)                             AS patnName
             , MAX(T5.gend)                                 AS gend
             , MAX(T5.brdy)                                 AS brdy
             , MAX(T5.insuplc_admdvs)                       AS poolarea
             , MAX(T8.mdtrt_id)                             AS mdtrtId
             , MAX(T4.id)                                   AS contractId
             , MAX(T4.fixmedins_code)                       AS medinsId
             , MAX(T4.fixmedins_name)                       AS medinsName
             , MAX(T4.adm_vs)                               AS medinsAdmdvs
             , MAX(COALESCE(T1.start_time, T1.create_time)) AS admDate
             , MAX(T1.end_time)                             AS dscgDate
             , MAX(T6.practitioner_id)                      AS drCodg
             , MAX(COALESCE(T12.bus_no, T7.bus_no))         AS admDeptCodg
             , MAX(COALESCE(T12.name, T7.name))             AS admDeptName
             , MAX(T7.bus_no)                               AS dscgDeptCodg
             , MAX(T7.name)                                 AS dscgDeptName
             , MAX(T1.yb_class_enum)                        AS medType
             , SUM(T9.fulamt_ownpay_amt)                    AS ownpayAmt
             , SUM(T9.preselfpay_amt)                       AS selfpayAmt
             , COUNT(T9.id)                                 AS setlTotlnum
             , MAX(T5.insutype)                             AS insutype
        FROM adm_encounter T1
                 INNER JOIN adm_patient T2
                            ON T2.id = T1.patient_id
                 INNER JOIN adm_account T3
                            ON T3.encounter_id = T1.id
                                AND T3.encounter_flag = #{encounterFlag}
                 INNER JOIN fin_contract T4
                            ON T4.bus_no = T3.contract_no
                 INNER JOIN LATERAL ( SELECT psn_no
                                           , psn_name
                                           , gend
                                           , brdy
                                           , insuplc_admdvs
                                           , insutype
                                      FROM yb_pub_perinfo
                                      WHERE certno = T2.id_card
                                      ORDER BY create_time DESC LIMIT 1) T5
        ON T2.id_card IS NOT NULL
            INNER JOIN adm_encounter_participant T6
            ON T6.encounter_id = T1.id
            AND T6.delete_flag = #{deleteFlag}
            AND T6.type_code = #{participantType}
            INNER JOIN adm_organization T7
            ON T7.id = T1.organization_id
            INNER JOIN yb_clinc_reg T8
            ON T8.ipt_otp_no = T1.bus_no
            INNER JOIN yb_clinc_settle T9
            ON T9.mdtrt_id = T8.mdtrt_id
            LEFT JOIN LATERAL ( SELECT location_id
            FROM adm_encounter_location
            WHERE encounter_id = T1.id
            ORDER BY create_time LIMIT 1) T10
            ON TRUE
            LEFT JOIN adm_location T11
            ON T11.id = T10.location_id
            LEFT JOIN adm_organization T12
            ON T12.id = T11.organization_id
        WHERE T1.id = #{encounterId}
    </select>

    <select id="yb3101SelectEncounterDiagnoseInfo"
            resultType="com.openhis.yb.dto.Yb3101AdapterEncounterDiagnoseInfo">
        SELECT T1.id                 AS diseId
             , T1.ipt_dise_type_code AS iptDiseTypeCode
             , T1.maindise_flag      AS maindiseFlag
             , T1.diag_srt_no        AS diasSrtNo
             , T2.yb_no              AS diseCodg
             , T3."name"             AS diseName
             , T1.create_time        AS diseDate
        FROM adm_encounter_diagnosis T1
                 INNER JOIN cli_condition T2
                            ON T2.id = T1.condition_id
                 INNER JOIN cli_condition_definition T3
                            ON T3.id = T2.definition_id
        WHERE T1.delete_flag = #{deleteFlag}
          AND T1.encounter_id = #{encounterId}
    </select>

    <select id="yb3101SelectOrderInfo" resultType="com.openhis.yb.dto.Yb3101AdapterOrderInfo">
        SELECT T3.bus_no                                                  AS rxId
             , T3.prescription_no                                         AS rxno
             , T3.group_id                                                AS grpno
             , T3.therapy_enum                                            AS therapyEnum
             , T2.yb_type                                                 AS ybType
             , COALESCE(T5.medical_catalog_code, T6.medical_catalog_code) AS hilistCode
             , COALESCE(T5.registered_name, T6.single_drug_name)          AS hilistName
             , COALESCE(T5.drug_form, '')                                 AS hilistDosform
             , T4.chrgitm_lv                                              AS hilistLv
             , T1.quantity_value                                          AS cnt
             , T1.unit_price                                              AS pric
             , T1.total_price                                             AS sumamt
             , COALESCE(T5.registered_spec, '')                           AS spec
             , COALESCE(T5.min_package_unit, '')                          AS specUnt
             , T1.entered_date                                            AS drordBegnDate
             , T7.bus_no                                                  AS drordDeptCodg
             , T7."name"                                                  AS drordDeptName
             , T8.yb_no                                                   AS drordDrCodg
             , T8."name"                                                  AS drordDrName
             , T8.dr_profttl_code                                         AS drordDrProfttl
        FROM adm_charge_item T1
                 INNER JOIN adm_charge_item_definition T2
                            ON T2.id = T1.definition_id
                 INNER JOIN med_medication_request T3
                            ON T3.id = T1.service_id
                                AND T1.service_table = 'med_medication_request'
                 INNER JOIN med_medication_definition T4
                            ON T4.id = T1.product_id
                 LEFT JOIN LATERAL (SELECT medical_catalog_code
                                         , registered_name
                                         , drug_form
                                         , registered_spec
                                         , min_package_unit
                                    FROM yb_catalog_drug_info
                                    WHERE medical_catalog_code = T4.yb_no
                                        AND T2.yb_type = #{ybMedChrgItmTypeWest}
                                       OR T2.yb_type = #{ybMedChrgItmTypeChinese}
                                    ORDER BY created_at DESC LIMIT 1) T5
        ON true
            LEFT JOIN LATERAL (SELECT medical_catalog_code
            , single_drug_name
            FROM yb_catalog_medical_herb_info
            WHERE medical_catalog_code = T4.yb_no
            AND T2.yb_type = #{ybMedChrgItmTypeChineseSlices}
            ORDER BY create_time DESC LIMIT 1 ) T6 ON true
            INNER JOIN adm_organization T7
            ON T7.id = T1.requesting_org_id
            INNER JOIN adm_practitioner T8
            ON T8.id = T1.enterer_id
        WHERE T1.delete_flag = #{deleteFlag}
          AND T1.encounter_id = #{encounterId}
    </select>

    <select id="yb4101aSelectSettleInfo" resultType="com.openhis.yb.dto.Yb4101AAdapterSettleInfo">
        SELECT T4.psn_no          AS psnNo
             , T6.mdtrt_id        AS mdtrtId
             , T2.setl_id         AS setlId
             , T3.country_code    AS ntly
             , T3.prfs_enum       AS prfs
             , T7.caty            AS dscgCaty
             , T8.bill_batch_code AS billCode
             , T8.bill_no         AS billNo
             , T1.payment_no      AS bizSn
             , T2.setl_time       AS setlDate
        FROM fin_payment_reconciliation T1
                 INNER JOIN yb_inpatient_settle T2
                            ON T1.yb_settle_ids LIKE CONCAT('%', T2.setl_id, '%')
                 INNER JOIN adm_patient T3
                            ON T3.id = T1.patient_id
                 INNER JOIN LATERAL ( SELECT psn_no
                                      FROM yb_pub_perinfo
                                      WHERE certno = T3.id_card
                                      ORDER BY create_time DESC LIMIT 1) T4
        ON T3.id_card IS NOT NULL
            INNER JOIN adm_encounter T5
            ON T5.id = T1.encounter_id
            INNER JOIN yb_clinc_reg T6
            ON T6.ipt_otp_no = T5.bus_no
            INNER JOIN adm_organization T7
            ON T7.id = T5.organization_id
            INNER JOIN adm_invoice T8
            ON T8.reconciliation_id = T1.id
        WHERE T1.id = #{paymentId}
    </select>

    <select id="yb4101aSelectOpspdiseinfo" resultType="com.openhis.yb.dto.Yb4101AInputOpspdiseinfo">
        SELECT T5."name" AS diagName
             , T5.yb_no  AS diagCode
        FROM fin_payment_reconciliation T1
                 INNER JOIN adm_charge_item T2
                            ON T1.charge_item_ids LIKE CONCAT('%', T2.id, '%')
                 INNER JOIN adm_encounter_diagnosis T3
                            ON T3.id = T2.encounter_diagnosis_id
                 INNER JOIN cli_condition T4
                            ON T4.id = T3.condition_id
                 INNER JOIN cli_condition_definition T5
                            ON T5.id = T4.definition_id
        WHERE T1.id = #{paymentId}
        GROUP BY T5."name", T5.yb_no
    </select>
</mapper>