<?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.document.mapper.DocRecordAppMapper">
    <!-- 患者信息分页查询-->
    <select id="getRecordPageList" parameterType="java.util.Map"
            resultType="com.openhis.web.document.dto.DocRecordPatientDto">
        SELECT t1.id,
        t1.definition_id,
        t1.definition_bus_no,
        t1.content_json,
        t1.status_enum,
        t1.organization_id,
        t1.encounter_id,
        t1.patient_id,
        t1.record_time,
        t1.tenant_id,
        t1.delete_flag,
        t1.create_by,
        t1.create_time,
        t1.update_by,
        t1.update_time,
        t1.bus_no,
        t1.patient_name,
        t1.gender,
        t1.age,
        t1.id_card,
        t1.phone,
        t1.address,
        t1.birth_date,
        t1.reception_time,
        t1.start_time,
        t1.end_time,
        t1.class_enum,
        t1.primary_menu_enum,
        t1.name,
        t1.tenant_id
        FROM
        (SELECT
        p.bus_no,
        p.name as patient_name,
        p.gender_enum as gender,
        null as age,
        p.birth_date,
        p.address,
        p.phone,
        p.id_card,
        e.start_time,
        e.end_time,
        e.reception_time,
        e.class_enum,
        d.primary_menu_enum,
        d.name ,
        t.id,
        t.definition_id,
        t.definition_bus_no,
        t.content_json,
        t.status_enum,
        t.organization_id,
        t.encounter_id,
        t.patient_id,
        t.record_time,
        t.tenant_id,
        t.delete_flag,
        t.create_by,
        t.create_time,
        t.update_by,
        t.update_time,
        t.tenant_id

        FROM doc_record t
        LEFT JOIN doc_definition d
        on d.id=t.definition_id
        LEFT JOIN adm_encounter e
        on t.encounter_id =e.id
        LEFT JOIN adm_patient p
        on t.patient_id=p.id
        WHERE t.delete_flag='0'

        <if test="primaryMenuEnumList != null and !primaryMenuEnumList.isEmpty()">
            AND t.primary_menu_enum IN
            <foreach collection="primaryMenuEnumList" item="primaryMenuEnum" open="(" separator="," close=")">
                #{primaryMenuEnum}
            </foreach>
        </if>
        order by t.record_time desc
        )t1
        ${ew.customSqlSegment}
    </select>
    <select id="getRecordPageListByEncounterId" parameterType="java.util.Map"
            resultType="com.openhis.web.document.dto.DocRecordDto">
        SELECT t1.id,
               t1.definition_id,
               t1.definition_bus_no,
               t1.content_json,
               t1.status_enum,
               t1.organization_id,
               t1.encounter_id,
               t1.patient_id,
               t1.record_time,
               t1.tenant_id,
               t1.delete_flag,
               t1.create_by,
               t1.create_time,
               t1.update_by,
               t1.update_time,
               t1.name,
               t1.version,
               t1.tenant_id
        FROM
            (SELECT
                 d.name ,
                 d.version,
                 t.id,
                 t.definition_id,
                 t.definition_bus_no,
                 t.content_json,
                 t.status_enum,
                 t.organization_id,
                 t.encounter_id,
                 t.patient_id,
                 t.record_time,
                 t.delete_flag,
                 t.create_by,
                 t.create_time,
                 t.update_by,
                 t.update_time,
                 t.tenant_id
             FROM doc_record t
                      LEFT JOIN doc_definition d
                                on d.id=t.definition_id
             where t.delete_flag='0'
             order by t.record_time desc
            )t1
        ${ew.customSqlSegment}
    </select>

</mapper>