<?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.inhospitalcharge.mapper.AdvancePaymentManageAppMapper">

    <select id="getAdvancePaymentInfo" resultType="com.openhis.web.inhospitalcharge.dto.AdvancePaymentInfoDto">
        SELECT api.tenant_id,
               api.encounter_id,
               api.bus_no,
               api.patient_id,
               api.patient_name,
               api.gender_enum,
               api.birth_date,
               api.ward_name,
               api.house_name,
               api.bed_name,
               api.inHospital_org_name,
               api.account_id,
               api.total_amount,
               api.balance_amount
        from (SELECT ae.tenant_id,
                     ae.ID                                 AS encounter_id,
                     ae.bus_no                             AS bus_no,
                     ap.id                                 AS patient_id,
                     ap.NAME                               AS patient_name,
                     ap.gender_enum                        AS gender_enum,
                     ap.birth_date                         AS birth_date,
                     alw.name                              AS ward_name,
                     alh.name                              AS house_name,
                     alb.name                              AS bed_name,
                     ao.NAME                               AS inHospital_org_name,
                     aa.id                                 AS account_id,
                     COALESCE(payment.total_amount, 0)     AS total_amount,
                     COALESCE(payment.total_amount, 0) - COALESCE(consumption.consumption_amount, 0) +
                     COALESCE(return_pay.return_amount, 0) AS balance_amount
              FROM adm_encounter AS ae
                       LEFT JOIN adm_patient AS ap ON ap.ID = ae.patient_id
                  AND ap.delete_flag = '0'
                       LEFT JOIN adm_organization AS ao ON ao.ID = ae.organization_id
                  AND ao.delete_flag = '0'
                       LEFT JOIN adm_encounter_location AS aelw
                                 ON aelw.encounter_id = ae.ID AND aelw.delete_flag = '0' AND
                                    aelw.form_enum = #{wardEnum} AND
                                    aelw.status_enum = #{encounterActivityStatus}
                       LEFT JOIN adm_location AS alw ON alw.id = aelw.location_id AND alw.delete_flag = '0'
                       LEFT JOIN adm_encounter_location AS aelh
                                 ON aelh.encounter_id = ae.ID AND aelh.delete_flag = '0' AND
                                    aelh.form_enum = #{houseEnum} AND
                                    aelh.status_enum = #{encounterActivityStatus}
                       LEFT JOIN adm_location AS alh ON alh.id = aelh.location_id AND alh.delete_flag = '0'
                       LEFT JOIN adm_encounter_location AS aelb
                                 ON aelb.encounter_id = ae.ID AND aelb.delete_flag = '0' AND
                                    aelb.form_enum = #{bedEnum} AND
                                    aelb.status_enum = #{encounterActivityStatus}
                       LEFT JOIN adm_location AS alb ON alb.id = aelb.location_id AND alb.delete_flag = '0'
                       LEFT JOIN adm_account AS aa
                                 ON aa.encounter_id = ae.ID AND aa.delete_flag = '0' AND
                                    aa.type_code = #{accountTypeCode}
                       LEFT JOIN LATERAL (
                  SELECT SUM
                             (tendered_amount) AS total_amount
                  FROM fin_payment_reconciliation AS fpr
                  WHERE fpr.delete_flag = '0'
                    AND fpr.kind_enum = #{kindEnum}
                    AND fpr.encounter_id = ae.ID
                      ) AS payment
                                 ON TRUE
                       LEFT JOIN LATERAL (
                  SELECT SUM
                             (total_price) AS consumption_amount
                  FROM adm_charge_item AS aci
                  WHERE aci.delete_flag = '0'
                    AND aci.status_enum IN (#{status1}, #{status2})
                    AND aci.encounter_id = ae.ID
                      ) AS consumption ON TRUE
                       LEFT JOIN LATERAL (
                  SELECT SUM
                             (total_price) AS return_amount
                  FROM adm_charge_item AS aci
                  WHERE aci.delete_flag = '0'
                    AND aci.status_enum IN (#{status3})
                    AND aci.encounter_id = ae.ID
                      ) AS return_pay ON TRUE
              WHERE ae.delete_flag = '0'
                AND ae.class_enum = #{classEnum}) AS api
            ${ew.customSqlSegment}
    </select>

    <select id="getAdvancePaymentFlowRecordList"
            resultType="com.openhis.web.inhospitalcharge.dto.AdvancePaymentFlowRecordDto">
        SELECT fpr.payment_no,
               fpr.tendered_amount,
               fpr.payment_enum,
               fpr.create_time AS operate_time,
               ap.NAME         AS enterer
        FROM fin_payment_reconciliation AS fpr
                 LEFT JOIN adm_practitioner AS ap ON ap.ID = fpr.enterer_id
            AND ap.delete_flag = '0'
        WHERE fpr.delete_flag = '0'
          AND fpr.kind_enum = 0
          AND fpr.encounter_id = #{encounterId}
        ORDER BY fpr.create_time DESC
    </select>

</mapper>