<?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.chargemanage.mapper.OutpatientRegistrationAppMapper">

    <select id="getPractitionerMetadataPage" resultType="com.openhis.web.chargemanage.dto.PractitionerMetadata">
        SELECT T3.tenant_id,
               T3.ID,
               T3.NAME,
               T3.gender_enum,
               T3.py_str,
               T3.wb_str
        FROM (
                 SELECT T1.tenant_id,
                        T1.ID,
                        T1.NAME,
                        T1.gender_enum,
                        T1.py_str,
                        T1.wb_str
                 FROM adm_practitioner AS T1
                 WHERE T1.delete_flag = '0'
                   AND EXISTS(SELECT 1
                              FROM adm_practitioner_role AS T2
                              WHERE T2.practitioner_id = T1.ID
                                AND T2.org_id = #{orgId}
                                AND T2.ROLE_code = #{RoleCode})
             ) AS T3
            ${ew.customSqlSegment}
    </select>

    <select id="getNumByPatientIdAndOrganizationId" resultType="Integer">
        SELECT COUNT
                   (1)
        FROM adm_encounter
        WHERE delete_flag = '0'
          AND patient_id = #{patientId}
          AND service_type_id = #{serviceTypeId}
          AND status_enum != #{cancelled}
          AND create_time
            > CURRENT_DATE
    </select>

    <select id="getCurrentDayEncounter" resultType="com.openhis.web.chargemanage.dto.CurrentDayEncounterDto">
        SELECT T9.tenant_id,
               T9.encounter_id,
               T9.organization_id,
               T9.organization_name,
               T9.healthcare_name,
               T9.practitioner_user_id,
               T9.practitioner_name,
               T9.contract_name,
               T9.patient_id,
               T9.patient_name,
               T9.gender_enum,
               T9.id_card,
               T9.status_enum,
               T9.register_time,
               T9.total_price,
               T9.account_name,
               T9.enterer_name,
               T9.charge_item_ids,
               T9.payment_id,
               T9.picture_url,
               T9.birth_date,
               T9.return_date,
               T9.return_reason,
               T9.operator_name,
               T9.operator_id,
               T9.refund_amount,
               T9.contract_no,
               T9.refund_method
        from (
                 SELECT T1.tenant_id       AS tenant_id,
                        T1.id              AS encounter_id,
                        T1.organization_id AS organization_id,
                        T2.NAME            AS organization_name,
                        T3.NAME            AS healthcare_name,
                        T5.user_id         AS practitioner_user_id,
                        T5.NAME            AS practitioner_name,
                        T7.contract_name   AS contract_name,
                        T8.id              AS patient_id,
                        T8.NAME            AS patient_name,
                        T8.gender_enum     AS gender_enum,
                        T8.id_card         AS id_card,
                        T1.status_enum     AS status_enum,
                        T1.create_time     AS register_time,
                        T10.total_price,
                        T11."name"         AS account_name,
                        T12."name"         AS enterer_name,
                        T13.charge_item_ids,
                        T13.id             AS payment_id,
                        ai.picture_url     AS picture_url,
                        T8.birth_date     AS birth_date,
                        -- 退号相关信息
                        T14.bill_date      AS return_date,
                        T14.refund_reason  AS return_reason,
                        T15."name"         AS operator_name,
                        T17.user_name      AS operator_id,
                        ABS(T14.display_amount) AS refund_amount,
                        T6.contract_no     AS contract_no,
                        T16.refund_method  AS refund_method
                 FROM adm_encounter AS T1
                          LEFT JOIN adm_organization AS T2 ON T1.organization_id = T2.ID AND T2.delete_flag = '0'
                          LEFT JOIN adm_healthcare_service AS T3 ON T1.service_type_id = T3.ID AND T3.delete_flag = '0'
                          LEFT JOIN (
                             SELECT *
                             FROM (
                                      SELECT *,
                                             ROW_NUMBER() OVER (PARTITION BY encounter_id ORDER BY create_time ASC) AS rn
                                      FROM adm_encounter_participant
                                      WHERE delete_flag = '0'
                                        AND tenant_id = 1
                                        AND type_code = #{participantType}
                                  ) t
                             WHERE rn = 1
                         ) AS T4 ON T1.ID = T4.encounter_id
                          LEFT JOIN adm_practitioner AS T5 ON T5.ID = T4.practitioner_id AND T5.delete_flag = '0'
                          LEFT JOIN adm_account AS T6
                                    ON T1.ID = T6.encounter_id AND T6.delete_flag = '0' AND T6.encounter_flag = '1'
                          LEFT JOIN fin_contract AS T7 ON T6.contract_no = T7.bus_no AND T7.delete_flag = '0'
                          LEFT JOIN adm_patient AS T8 ON T1.patient_id = T8.ID AND T8.delete_flag = '0'
                          LEFT JOIN adm_charge_item AS T10 ON T1.id = T10.encounter_id AND T10.delete_flag = '0'
                          LEFT JOIN adm_account AS T11 ON T10.account_id = T11.id AND T11.delete_flag = '0'
                          LEFT JOIN adm_practitioner AS T12 ON T12.ID = T10.enterer_id AND T12.delete_flag = '0'
                          LEFT JOIN fin_payment_reconciliation T13
                                    ON T10.id::TEXT = ANY(string_to_array(T13.charge_item_ids,','))
                                        AND T13.delete_flag = '0'
                                        AND T13.status_enum = ${paymentStatus}
                          -- 关联退号记录（当状态为退号时，通过relation_id关联原支付记录）
                          LEFT JOIN fin_payment_reconciliation T14
                                    ON T13.id = T14.relation_id
                                        AND T14.delete_flag = '0'
                                        AND T14.status_enum = 3
                                        AND T14.payment_enum = 1
                          LEFT JOIN adm_practitioner AS T15 ON T15.ID = T14.enterer_id AND T15.delete_flag = '0'
                          LEFT JOIN sys_user AS T17 ON T17.user_id = T15.user_id AND T17.delete_flag = '0'
                          -- 关联退号支付详情，获取退款方式（聚合多个支付方式）
                          LEFT JOIN (
                             SELECT reconciliation_id,
                                    STRING_AGG(
                                        CASE pay_enum
                                            WHEN 220400 THEN '现金'
                                            WHEN 220100 THEN '微信'
                                            WHEN 220200 THEN '支付宝'
                                            WHEN 220300 THEN '银联'
                                        END,
                                        ','
                                        ORDER BY pay_enum
                                    ) AS refund_method
                             FROM fin_payment_rec_detail
                             WHERE delete_flag = '0'
                               AND amount &lt; 0
                               AND pay_enum IN (220400, 220100, 220200, 220300)
                             GROUP BY reconciliation_id
                          ) AS T16 ON T14.id = T16.reconciliation_id
                          LEFT JOIN adm_invoice AS ai
                                    ON ai.reconciliation_id = T13.id AND ai.delete_flag = '0'
                 WHERE T1.delete_flag = '0'
                   AND T1.class_enum = #{classEnum}
--                    AND T1.create_time
--                      > CURRENT_DATE
                   AND T10.context_enum = #{register}
             ) AS T9
            ${ew.customSqlSegment}
        ORDER BY T9.register_time DESC
    </select>

    <select id="getTmpActivityList" resultType="com.openhis.web.personalization.dto.ActivityDeviceDto">
        SELECT dev_act_id,
               quantity
        FROM tmp_activity_device
        WHERE delete_flag = '0'
          AND item_no = #{itemId}
          AND dev_act_table = #{devActable}
    </select>

</mapper>