<?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.NursingRecordAppMapper">
    <!-- 患者信息分页查询-->
    <select id="getPatientPage" parameterType="java.util.Map"
            resultType="com.openhis.web.inpatientmanage.dto.NursingPageDto">
        SELECT T5.tenant_id,
               T5.patient_id,
               T5.patient_name,
               T5.patient_bus_no,
               T5.birth_date,
               T5.gender_enum,
               T5.org_id,
               T5.encounter_id,
               T5.admissionDate,
               T5.ward_location_id,
               T5.bed_location_id
        FROM (SELECT T1.tenant_id,
                     T1.id              AS patient_id,       --患者ID
                     T1.name            AS patient_name,     --患者姓名
                     T1.bus_no          AS patient_bus_no,   --病历号
                     T1.birth_date,                          --患者出生日期
                     T1.gender_enum,                         --患者性别
                     T1.organization_id AS org_id,           --科室ID
                     T2.id              AS encounter_id,     --就诊ID
                     T2.start_time      AS admissionDate,    --入院日期
                     T3.location_id     AS ward_location_id, --病区
                     T4.location_id     AS bed_location_id   --床号
              FROM adm_patient AS T1
                       INNER JOIN adm_encounter AS T2
                                  ON T2.patient_id = T1.id
                                      AND T2.class_enum = #{classEnum}
                                      AND T2.delete_flag = '0'
                       INNER JOIN (SELECT encounter_id,
                                          location_id,
                                          form_enum,
                                          delete_flag
                                   FROM (SELECT encounter_id,
                                                location_id,
                                                form_enum,
                                                delete_flag,
                                                ROW_NUMBER() OVER (PARTITION BY encounter_id
                                                   ORDER BY
                                                   CASE
                                                       WHEN update_time IS NULL THEN create_time
                                                       ELSE update_time
                                                   END DESC) AS rn
                                         FROM adm_encounter_location
                                         WHERE form_enum = #{ward}
                                           AND delete_flag = '0'
                                        ) ranked
                                   WHERE rn = 1) AS T3
                                  ON T3.encounter_id = T2.ID
                       INNER JOIN adm_encounter_location AS T4
                                  ON T4.encounter_id = T2.ID
                                      AND T4.form_enum = #{bed}
                                      AND T4.status_enum = #{active}
                                      AND T4.delete_flag = '0'
              WHERE T1.delete_flag = '0'
              ORDER BY T4.location_id ASC) AS T5
            ${ew.customSqlSegment}
    </select>

    <!-- 患者护理记录单信息分页查询-->
    <select id="getNursingPatientPage" parameterType="java.util.Map"
            resultType="com.openhis.web.inpatientmanage.dto.NursingDetailDto">
        SELECT T6.tenant_id,
               T6.patient_id,
               T6.patient_name,
               T6.birth_date,
               T6.gender_enum,
               T6.org_id,
               T6.encounter_id,
               T6.admissionDate,
               T6.ward_location_id,
               T6.record_id,
               T6.recorder_id,
               T6.recording_time,
               T6.context_json,
               T6.bed_location_id
        FROM (SELECT T1.tenant_id,
                     T1.id              AS patient_id,       --患者ID
                     T1.name            AS patient_name,     --患者姓名
                     T1.birth_date,                          --患者出生日期
                     T1.gender_enum,                         --患者性别
                     T1.organization_id AS org_id,           --科室ID
                     T2.id              AS encounter_id,     --就诊ID
                     T2.start_time      AS admissionDate,    --入院日期
                     T3.location_id     AS ward_location_id, --病区
                     T5.id              AS record_id,        --记录单ID
                     T5.record_id       AS recorder_id,      --记录人
                     T5.record_time     AS recording_time,   --记录时间
                     T5.context_json,                        --病历信息
                     T4.location_id     AS bed_location_id   --床号
              FROM adm_patient AS T1
                       INNER JOIN adm_encounter AS T2
                                  ON T2.patient_id = T1.id
                                      AND T2.class_enum = #{classEnum}
                                      AND T2.delete_flag = '0'
                       INNER JOIN (SELECT encounter_id,
                                          location_id,
                                          form_enum,
                                          delete_flag
                                   FROM (SELECT encounter_id,
                                                location_id,
                                                form_enum,
                                                delete_flag,
                                                ROW_NUMBER() OVER (PARTITION BY encounter_id
                                                   ORDER BY
                                                   CASE
                                                       WHEN update_time IS NULL THEN create_time
                                                       ELSE update_time
                                                   END DESC) AS rn
                                         FROM adm_encounter_location
                                         WHERE form_enum = #{ward}
                                           AND delete_flag = '0'
                                        ) ranked
                                   WHERE rn = 1) AS T3
                                  ON T3.encounter_id = T2.ID
                       INNER JOIN adm_encounter_location AS T4
                                  ON T4.encounter_id = T2.ID
                                      AND T4.form_enum = #{bed}
                                      AND T4.status_enum = #{active}
                                      AND T4.delete_flag = '0'
                       LEFT JOIN doc_emr AS T5
                                 ON T5.patient_id = T1.id
                                     AND T5.encounter_id = T2.ID
                                     AND T5.class_enum = 1 --护理记录单
                                     AND T5.delete_flag = '0'
              WHERE T1.delete_flag = '0'
              ORDER BY T5.record_time DESC) AS T6
            ${ew.customSqlSegment}
    </select>

    <!-- 病历模板分页查询-->
    <select id="getEmrTemplate" parameterType="java.util.Map"
            resultType="com.openhis.web.inpatientmanage.dto.NursingEmrTemplateDto">
        SELECT T2.tenant_id,
               T2.id,
               T2.template_name,
               T2.template_type_code,
               T2.use_scope_code,
               T2.use_scope_code_text,
               T2.user_id,
               T2.context_json
        FROM (SELECT T1.tenant_id,
                     T1.id,
                     T1.template_name,
                     T1.template_type_code,
                     T1.use_scope_code,
                     CASE
                         WHEN T1.use_scope_code = '1' THEN '个人'
                         WHEN T1.use_scope_code = '2' THEN '科室'
                         WHEN T1.use_scope_code = '3' THEN '全院'
                         END AS use_scope_code_text,
                     T1.user_id,
                     T1.context_json
              FROM doc_emr_template AS T1
              WHERE T1.delete_flag = '0'
                AND T1.template_type_code = #{nursing}
              ORDER BY T1.create_time DESC) AS T2
            ${ew.customSqlSegment}
    </select>

</mapper>