<?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.inpatientmanage.mapper.PatientHomeAppMapper">

    <!-- 患者首页信息分页查询-->
    <select id="getPage" parameterType="java.util.Map"
            resultType="com.openhis.web.inpatientmanage.dto.PatientHomeDto">
        SELECT
            T1.id,
            T1.active_flag,
            T1.temp_flag,
            T1."name" AS patientName, -- 患者姓名
            T1.name_json,
            T1.bus_no AS patientNo, -- 病历号
            T1.gender_enum, -- 患者性别
            T1.birth_date,
            T1.deceased_date,
            T1.marital_status_enum,
            T1.prfs_enum,
            T1.phone, -- 患者手机号
            COALESCE(NULLIF(T1.address, ''), '') AS address, -- 处理空地址
            T1.address_province,
            T1.address_city,
            T1.address_district,
            T1.address_street,
            T1.address_json,
            T1.nationality_code,
            T1.id_card,
            T1.py_str,
            T1.wb_str,
            T1.blood_abo,
            T1.blood_rh,
            T1.work_company,
            T1.native_place,
            T1.country_code,
            T1.link_name,
            T1.link_relation_code,
            T1.link_telcom,
            T1.link_jsons,
            T1.tenant_id,
            T2.bus_no AS hospitalNo, -- 住院号
            T2.id AS encounterId, -- 就诊ID
            T2.priority_enum, -- 护理级别
            T2.status_enum, -- 患者状态
            T2.organization_id,-- 入院科室
            T2.start_time AS admissionDate, -- 入院日期
            T2.end_time AS dischargeDate, -- 出院日期
            T2.class_enum, -- 就诊类别
            Doctor.name AS responsibleDoctor, -- 责任医生
            Nurse.name AS responsibleNurse, -- 责任护士
            T6.type_code, -- 费别
            T7.status_enum AS surgeryStatusEnum, -- 手术状态
            T8.start_time AS surgeryStartTime,  -- 手术开始时间
            T8.end_time AS surgeryEndTime,    -- 手术结束时间
            T9.id AS encounterLocationId, -- 就诊位置ID
            T9.location_id, -- 床位号
            T10.category_code,  -- 过敏原
            T11."name" AS caty, -- 入院科室
            T12."name" AS mainDiagnosis -- 主要诊断
        FROM adm_patient AS T1
            LEFT JOIN adm_encounter T2
                ON T1.id = T2.patient_id  -- 患者ID
                AND T2.delete_flag = '0'
                AND T2.class_enum = '1'   -- 仅选择住院患者
            -- 获取责任医生
            LEFT JOIN (
                SELECT
                    encounter_id,
                    practitioner_id,
                    "name",
                    ROW_NUMBER() OVER (PARTITION BY encounter_id ORDER BY practitioner_id) as rn
                FROM (
                    SELECT
                        T3.encounter_id,
                        T4."name",
                        T3.practitioner_id
                    FROM adm_encounter_participant T3
                        LEFT JOIN adm_practitioner T4
                            ON T3.practitioner_id = T4.id
                            AND T4.delete_flag = '0'
                    WHERE T3.type_code = '9' -- 责任医生类型
                        AND T3.delete_flag = '0'
                    ) sub
            ) Doctor ON T2.id = Doctor.encounter_id AND Doctor.rn = 1
            -- 获取责任护士
            LEFT JOIN (
                SELECT
                    encounter_id,
                    practitioner_id,
                    "name",
                    ROW_NUMBER() OVER (PARTITION BY encounter_id ORDER BY practitioner_id) as rn
                FROM (
                    SELECT
                        T3.encounter_id,
                        T4."name",
                        T3.practitioner_id
                    FROM adm_encounter_participant T3
                        LEFT JOIN adm_practitioner T4
                            ON T3.practitioner_id = T4.id
                            AND T4.delete_flag = '0'
                    WHERE T3.type_code = '2' -- 责任护士类型
                        AND T3.delete_flag = '0'
                ) sub
            ) Nurse ON T2.id = Nurse.encounter_id AND Nurse.rn = 1
            LEFT JOIN adm_encounter_diagnosis T5
                ON T2.id = T5.encounter_id -- 就诊ID
                AND T5.maindise_flag = 1
                AND T5.delete_flag = '0'
            LEFT JOIN adm_account T6
                ON T2.id = T6.encounter_id -- 就诊ID
                AND T6.delete_flag = '0'
            LEFT JOIN cli_procedure T7
                ON T1.id = T7.patient_id -- 患者ID
                AND T7.delete_flag = '0'
            LEFT JOIN cli_procedure_performer T8
                ON T7.id = T8.procedure_id -- 手术ID
                AND T8.delete_flag = '0'
            LEFT JOIN adm_encounter_location T9
                ON T2.id = T9.encounter_id -- 就诊ID
                AND T9.form_enum = 8
                AND T9.delete_flag = '0'
            LEFT JOIN cli_allergy_intolerance T10
                ON T1.id = T10.patient_id -- 患者ID
                AND T10.delete_flag = '0'
            LEFT JOIN adm_organization T11
                ON T2.organization_id = T11.id
                AND T11.type_enum = 2
                AND T11.delete_flag = '0'
            LEFT JOIN cli_condition_definition T12
                ON T5.condition_id = T12.id
                AND T12.delete_flag = '0'
            LEFT JOIN wor_service_request T13
                ON T2.id = T13.encounter_id
                AND T7.base_service_req_id = T13.id
                AND T13.delete_flag = '0'
            LEFT JOIN wor_service_request_detail T14
                ON T13.id = T14.service_req_id
                AND T14.type_code = '3'
                AND T14.delete_flag = '0'
        <where>
            T1.delete_flag = '0'
            -- 患者ID
            <if test="patientId != null">
                AND T1.id = #{patientId}
            </if>

            -- 在科
            <if test="statusEnum == 1">
                AND T9.status_enum = 2
            </if>

            -- 待出院
            <if test="statusEnum == 3">
                AND T2.status_enum = 4
            </if>

            -- 危重
            <if test="statusEnum == 4">
                AND T2.priority_enum = 1
            </if>

            -- 手术
            <if test="statusEnum == 5">
                AND T14.type_code = '3'
            </if>

            -- 欠费
            <if test="statusEnum == 6">
                AND T6.balance_amount &lt; 0
            </if>

            -- 已出院
            <if test="statusEnum == 7">
                AND T2.status_enum = 5
            </if>

            <if test="searchKey != null and searchKey != ''">
                AND (
                    -- 住院号
                    T2.bus_no = #{searchKey}
                    -- 患者姓名
                    OR T1.name like concat('%', #{searchKey}, '%')
                    -- 责任医生
                    OR Doctor.name like concat('%', #{searchKey}, '%')
                    -- 责任护士
                    OR Nurse.name like concat('%', #{searchKey}, '%')
                    )
            </if>
        </where>

        ORDER BY T9.location_id ASC
    </select>

    <!-- 科室空床查询-->
    <select id="getEmptyBedList" parameterType="java.util.List"
            resultType="com.openhis.web.inpatientmanage.dto.PatientHomeEmptyBedDto">
        SELECT id as locationId
        FROM adm_location
        WHERE organization_id = #{organizationId}
          AND operational_enum = 4
          AND form_enum = 8
          AND delete_flag = '0'
        ORDER BY id ASC
    </select>

</mapper>