<?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.paymentmanage.mapper.EleInvoiceMapper">
    <!-- 付款信息相关查询-->
    <select id="getPaymentInfo" resultType="com.openhis.web.paymentmanage.dto.EleInvoicePaymentInfoDto">
        SELECT T1.status_enum     AS payment_status,-- 付款状态
               T4.id              AS invoice_id, --发票id
               T4.status_enum     AS invoice_status, --发票状态
               T1.id              AS payment_id, --结算id
               T1.payment_no      AS bus_no,--支付的业务标识符(用于显示,支付流水号)
               T3.name            AS payee,--收款员
               T1.tendered_amount AS total_amt,--付款总额
               SUM(CASE
                       WHEN T2.pay_enum = #{selfYbZhPay} THEN T2.amount
                       ELSE 0.00
                   END)           AS zh_pay_amount, --个人医保账户支付
               SUM(CASE
                       WHEN T2.pay_enum = #{selfCashPay} THEN T2.amount
                       ELSE 0.00
                   END)           AS rmb_cash_pay_amount, --个人现金支付金额
               SUM(CASE
                       WHEN T2.pay_enum = #{selfCashVxValue} THEN T2.amount
                       ELSE 0.00
                   END)           AS wx_pay_amount, --个人现金支付金额(微信)
               SUM(CASE
                       WHEN T2.pay_enum = #{selfCashAliValue} THEN T2.amount
                       ELSE 0.00
                   END)           AS ali_pay_amount, --个人现金支付金额(支付宝)
               SUM(CASE
                       WHEN T2.pay_enum = #{selfCashUnionValue} THEN T2.amount
                       ELSE 0.00
                   END)           AS debit_pay_amount, --个人现金支付金额(银联)
               SUM(CASE
                       WHEN T2.pay_enum = #{ybFundPay} THEN T2.amount
                       ELSE 0.00
                   END)           AS yb_fund_pay_amount, --基金支付总额
               SUM(CASE
                       WHEN T2.pay_enum = #{otherPay} THEN T2.amount
                       ELSE 0.00
                   END)           AS other_pay_amount, --其他（如医院负担金额）
               SUM(CASE
                       WHEN T2.pay_enum = #{selfYbZhGjValue} THEN T2.amount
                       ELSE 0.00
                   END)           AS aelf_yb_zh_gj_value, --账户共济支付金额
               T1.charge_item_ids,   --付款账单集合
               T1.bill_date    --结算时间
        FROM fin_payment_reconciliation AS T1
                 LEFT JOIN fin_payment_rec_detail AS T2
                           ON T1.id = T2.reconciliation_id
                               AND T2.delete_flag = '0'
                 LEFT JOIN adm_practitioner AS T3
                           ON T1.enterer_id = T3.id
                               AND T3.delete_flag = '0'
                 LEFT JOIN adm_invoice AS T4
                           ON T4.reconciliation_id = T1.id
                               AND T4.delete_flag = '0'
        WHERE T1.id = #{paymentId}
          AND T1.delete_flag = '0'
        GROUP BY T1.status_enum,
                 T4.id,
                 T4.status_enum,
                 T1.id,
                 T1.payment_no,
                 T3.name,
                 T1.tendered_amount,
                 T1.charge_item_ids,
                 T1.bill_date
    </select>
    <!-- 患者信息相关查询-->
    <select id="getPatientInfo" resultType="com.openhis.web.paymentmanage.dto.EleInvoicePatientInfoDto">
        SELECT DISTINCT T1.id          AS payer_id,              --患者id
                        T1.name        AS payer,                 --患者姓名
                        T1.bus_no      AS case_number,           --患者院内编码/病历号
                        T1.gender_enum,                          --性别
                        T1.birth_date,                           --出生日期
                        T1.phone       AS tel,                   --电话
                        T1.id_card     AS card_no,               --身份证号
                        T2.bus_no      AS encounter_bus_no,      --就这编码
                        T2.create_time AS consultation_date,     --就诊日期
                        T2.start_time  AS in_hospital_date,      --住院日期
                        T2.end_time    AS out_hospital_date,     --出院日期
                        T3.name        AS patient_category,      --就诊科室名称
                        T3.bus_no      AS patient_category_code, --就诊科室编码
                        T11.medins_lv  AS medical_institution,   --医疗机构类型
                        T4.yb_area_no  AS med_care_area_code,    --医保区域编码
                        T9.bus_no      AS category_code,         --入院科室编码
                        T9.name        as category,              --入院科室名称
                        T10.bus_no     AS leave_category_code,   --出院科室编码
                        T10.name       AS leave_category         --出院科室名称
        FROM adm_patient AS T1
                 INNER JOIN adm_encounter AS T2
                            ON T1.id = T2.patient_id
                                AND T2.delete_flag = '0'
                 LEFT JOIN adm_organization AS T3
                           ON T2.organization_id = T3.id
                               AND T3.delete_flag = '0'
                 LEFT JOIN adm_account AS T4
                           ON T4.encounter_id = T2.id
                               AND T4.patient_id = T1.id
                               AND T4.delete_flag = '0'
                 LEFT JOIN (SELECT A1.encounter_id,
                                   A1.location_id,
                                   A1.create_time
                            FROM adm_encounter_location A1
                                     INNER JOIN (SELECT encounter_id,
                                                        MIN(create_time) AS min_time
                                                 FROM adm_encounter_location
                                                 WHERE delete_flag = '0'
                                                 GROUP BY encounter_id) A2
                                                ON A1.encounter_id = A2.encounter_id AND A1.create_time = A2.min_time) AS T5
                           ON T5.encounter_id = T2.id
                 LEFT JOIN (SELECT A3.encounter_id,
                                   A3.location_id,
                                   A3.create_time
                            FROM adm_encounter_location A3
                                     INNER JOIN (SELECT encounter_id,
                                                        MAX(create_time) AS min_time
                                                 FROM adm_encounter_location
                                                 WHERE delete_flag = '0'
                                                 GROUP BY encounter_id) A4
                                                ON A3.encounter_id = A4.encounter_id AND A3.create_time = A4.min_time) AS T6
                           ON T6.encounter_id = T2.id
                 LEFT JOIN adm_location AS T7
                           ON T5.location_id = T7.id
                               AND T7.delete_flag = '0'
                 LEFT JOIN adm_location AS T8
                           ON T6.location_id = T8.id
                               AND T8.delete_flag = '0'
                 LEFT JOIN adm_organization AS T9
                           ON T7.organization_id = T9.id
                               AND T9.delete_flag = '0'
                 LEFT JOIN adm_organization AS T10
                           ON T8.organization_id = T10.id
                               AND T10.delete_flag = '0'
                 LEFT JOIN adm_organization AS T11
                           ON LEFT (T3.bus_no, 6) = T11.bus_no
        WHERE T2.id = #{encounterId}
          AND T2.class_enum = #{encClassEnum}
          AND T3.class_enum = #{orgClassEnum}
          AND T1.delete_flag = '0'
        LIMIT 1
    </select>
    <!-- 收费项目明细相关查询-->
    <select id="getChargeDetail" resultType="com.openhis.web.paymentmanage.dto.EleInvoiceChargeDetailDto">
        SELECT T2.yb_type, --医疗收费项目类别
        SUM(T1.quantity_value) AS number, --数量
        SUM(T1.total_price) AS amt,--金额
        -- AS self_amt --自费金额
        T3.dict_label AS charge_name,--收费项目名称
        T4.code AS charge_code --收费项目代码
        FROM adm_charge_item AS T1
        INNER JOIN adm_charge_item_definition AS T2
        ON T1.definition_id = T2.id
        AND T2.delete_flag = '0'
        LEFT JOIN sys_dict_data AS T3
        ON T3.dict_type = #{chrgitmType}
        AND T3.dict_value = T2.yb_type
        LEFT JOIN org_contrast AS T4
        ON T4.dict_code = T3.dict_code
        AND T4.type_enum = #{contrastType}
        AND T4.delete_flag = '0'
        WHERE T1.id IN
        <foreach collection="chargeItemIds" item="itemId" separator="," open="(" close=")">
            #{itemId}
        </foreach>
        AND T1.encounter_id = #{encounterId}
        AND T2.yb_type IN
        <foreach collection="ybTypeList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        AND T1.delete_flag = '0'
        GROUP BY T2.yb_type,
        T3.dict_label,
        T4.code
        ORDER BY T2.yb_type
    </select>
    <!-- 清单项目明细相关查询-->
    <select id="getListDetail" resultType="com.openhis.web.paymentmanage.dto.EleInvoiceListDetailDto">
        SELECT T1.id AS list_detail_no,--明细流水号
        T2.yb_type, --医疗收费项目类别
        CASE
        WHEN (T1.product_table = 'adm_device_definition') THEN T3.bus_no
        WHEN (T1.product_table = 'med_medication_definition') THEN T4.bus_no
        WHEN (T1.product_table = 'wor_activity_definition') THEN T5.bus_no
        END AS code, --药品编码
        CASE
        WHEN (T1.product_table = 'adm_device_definition') THEN T3.name
        WHEN (T1.product_table = 'med_medication_definition') THEN T4.name
        WHEN (T1.product_table = 'wor_activity_definition') THEN T5.name
        END AS name, --药品名称
        CASE
        WHEN (T1.product_table = 'adm_device_definition') THEN T3.yb_no
        WHEN (T1.product_table = 'med_medication_definition') THEN T4.yb_no
        WHEN (T1.product_table = 'wor_activity_definition') THEN T5.yb_no
        END AS yb_code, --医保项目编码
        T1.quantity_unit AS unit,--计量单位
        T1.unit_price AS std,--单价
        T1.quantity_value AS number, --数量
        T1.total_price AS amt,--金额
        -- AS self_amt, --自费金额
        CASE
        WHEN (T1.product_table = 'adm_device_definition') THEN T3.chrgitm_lv
        WHEN (T1.product_table = 'med_medication_definition') THEN T4.chrgitm_lv
        WHEN (T1.product_table = 'wor_activity_definition') THEN T5.chrgitm_lv
        END AS medical_care_type, --医保药品分类
        T6.dict_label AS charge_name,--收费项目名称
        T7.code AS charge_code --收费项目代码
        FROM adm_charge_item AS T1
        INNER JOIN adm_charge_item_definition AS T2
        ON T1.definition_id = T2.id
        AND T2.delete_flag = '0'
        LEFT JOIN adm_device_definition AS T3
        ON T1.product_id = T3.id
        AND T1.product_table = 'adm_device_definition'
        AND T3.delete_flag = '0'
        LEFT JOIN med_medication_definition AS T4
        ON T1.product_id = T4.id
        AND T1.product_table = 'med_medication_definition'
        AND T4.delete_flag = '0'
        LEFT JOIN wor_activity_definition AS T5
        ON T1.product_id = T5.id
        AND T1.product_table = 'wor_activity_definition'
        AND T5.delete_flag = '0'
        LEFT JOIN sys_dict_data AS T6
        ON T6.dict_type = #{chrgitmType}
        AND T6.dict_value = T2.yb_type
        LEFT JOIN org_contrast AS T7
        ON T7.dict_code = T6.dict_code
        AND T7.type_enum = #{contrastType}
        AND T7.delete_flag = '0'
        WHERE T1.id IN
        <foreach collection="chargeItemIds" item="itemId" separator="," open="(" close=")">
            #{itemId}
        </foreach>
        AND T1.encounter_id = #{encounterId}
        AND T2.yb_type IN
        <foreach collection="ybTypeList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        AND T1.delete_flag = '0'
    </select>
    <!-- 挂号清单项目明细相关查询-->
    <select id="getRegListDetail" resultType="com.openhis.web.paymentmanage.dto.EleInvoiceListDetailDto">
        SELECT T1.id AS list_detail_no,--明细流水号
        T2.id AS code, --药品编码
        T2.charge_name AS name, --药品名称
        T1.unit_price AS std,--单价
        T1.quantity_value AS number, --数量
        T1.total_price AS amt,--金额
        T3.dict_label AS charge_name,--收费项目名称
        T4.code AS charge_code --收费项目代码
        FROM adm_charge_item AS T1
        INNER JOIN adm_charge_item_definition AS T2
        ON T1.definition_id = T2.id
        AND T2.delete_flag = '0'
        LEFT JOIN sys_dict_data AS T3
        ON T3.dict_type = #{chrgitmType}
        AND T3.dict_value = T2.yb_type
        LEFT JOIN org_contrast AS T4
        ON T4.dict_code = T3.dict_code
        AND T4.type_enum = #{contrastType}
        AND T4.delete_flag = '0'
        WHERE T1.service_table = 'adm_healthcare_service'
        AND T1.id IN
        <foreach collection="chargeItemIds" item="itemId" separator="," open="(" close=")">
            #{itemId}
        </foreach>
        AND T1.encounter_id = #{encounterId}
        AND T2.yb_type IN
        <foreach collection="ybTypeList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        AND T1.delete_flag = '0'
    </select>
</mapper>
