<?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.doctorstation.mapper.DoctorStationDiagnosisAppMapper">

    <select id="getDiagnosisBelongBindingPage" resultType="com.openhis.web.doctorstation.dto.DiagnosisBelongBindingDto">
        SELECT T5.tenant_id,
               T5.ID,
               T5.object_id,
               T5.definition_id,
               T5.binding_enum,
               T5.definition_name,
               T5.object_name
        from (
                 SELECT T1.tenant_id,
                        T1.ID,
                        T1.object_id,
                        T1.definition_id,
                        T1.binding_enum,
                        T2.NAME AS definition_name,
                        CASE
                            WHEN T1.binding_enum = #{bindingType1} THEN
                                T3.user_name
                            WHEN T1.binding_enum = #{bindingType2} THEN
                                T4.NAME
                            END AS object_name
                 FROM cli_diagnosis_belong_binding AS T1
                          LEFT JOIN cli_condition_definition AS T2 ON T1.definition_id = T2.ID
                     AND T2.delete_flag = '0'
                          LEFT JOIN sys_user AS T3 ON T1.binding_enum = #{bindingType1}
                     AND T1.object_id = T3.user_id
                          LEFT JOIN adm_organization AS T4 ON T1.binding_enum = #{bindingType2}
                     AND T1.object_id = T4.ID
                 WHERE T1.delete_flag = '0'
                 ORDER BY object_name) AS T5
            ${ew.customSqlSegment}
    </select>

    <select id="getPatientHistoryList" resultType="com.openhis.web.doctorstation.dto.ConditionDefinitionMetadata">
        SELECT T1.ID,
               T1.source_enum,
               T1.condition_code,
               T1.NAME,
               T1.yb_flag,
               T1.yb_no,
               T1.yb_match_flag
        FROM cli_condition_definition AS T1
        WHERE T1.delete_flag = '0'
          AND T1.status_enum = #{statusEnum}
          AND EXISTS(
                SELECT 1
                FROM cli_condition AS T2
                WHERE T2.delete_flag = '0'
                  AND T2.definition_id IS NOT NULL
                  AND T2.patient_id = #{patientId}
                  AND T2.definition_id = T1.ID
            )
    </select>

    <select id="getDoctorCommonUseList" resultType="com.openhis.web.doctorstation.dto.ConditionDefinitionMetadata">
        SELECT T1.ID,
               T1.source_enum,
               T1.condition_code,
               T1.NAME,
               T1.yb_flag,
               T1.yb_no,
               T1.yb_match_flag
        FROM cli_condition_definition AS T1
                 JOIN (SELECT definition_id
                       FROM cli_condition
                       WHERE delete_flag = '0'
                         AND definition_id IS NOT NULL
                         AND recorder_id = #{userId}
                       GROUP BY definition_id LIMIT 10) AS T2 ON T1.ID = T2.definition_id
        WHERE T1.delete_flag = '0'
          AND T1.status_enum = #{statusEnum}
    </select>

    <select id="getUserPersonalList" resultType="com.openhis.web.doctorstation.dto.ConditionDefinitionMetadata">
        SELECT T1.ID,
               T1.source_enum,
               T1.condition_code,
               T1.NAME,
               T1.yb_flag,
               T1.yb_no,
               T1.yb_match_flag
        FROM cli_condition_definition AS T1
                 JOIN (SELECT definition_id
                       FROM cli_diagnosis_belong_binding
                       WHERE delete_flag = '0'
                         AND binding_enum = #{bindingEnum}
                         AND object_id = #{userId}) AS T2 ON T1.ID = T2.definition_id
        WHERE T1.delete_flag = '0'
          AND T1.status_enum = #{statusEnum}
    </select>

    <select id="getOrganizationList" resultType="com.openhis.web.doctorstation.dto.ConditionDefinitionMetadata">
        SELECT T1.ID,
        T1.source_enum,
        T1.condition_code,
        T1.NAME,
        T1.yb_flag,
        T1.yb_no,
        T1.yb_match_flag
        FROM cli_condition_definition AS T1
        JOIN (SELECT definition_id
        FROM cli_diagnosis_belong_binding
        WHERE delete_flag = '0'
        AND binding_enum = #{bindingEnum}
        <if test="currentUserOrganizationId != null">
            AND object_id = #{currentUserOrganizationId}
        </if>
        ) AS T2 ON T1.ID = T2.definition_id
        WHERE T1.delete_flag = '0'
        AND T1.status_enum = #{statusEnum}
    </select>

</mapper>