<?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 AS definition_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.tcm_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 AS definition_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 tcm_flag = 0
                         AND definition_id IS NOT NULL
                         AND recorder_id = #{practitionerId}
                       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 T2.ID,
               T2.definition_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 id, 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 T2.ID,
        T2.definition_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 id, 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>

    <select id="getEncounterDiagnosis" resultType="com.openhis.web.doctorstation.dto.DiagnosisQueryDto">
        SELECT T1.id AS encounter_diagnosis_id,
               T1.condition_id,
               T2.definition_id,
               T3.source_enum,
               T3.NAME,
               T1.maindise_flag,
               T1.diag_srt_no,
               T1.med_type_code,
               T1.diagnosis_desc,
               T2.verification_status_enum,
               T2.yb_no
        FROM adm_encounter_diagnosis AS T1
                 LEFT JOIN cli_condition AS T2 ON T2.ID = T1.condition_id
            AND T2.delete_flag = '0' AND T2.tcm_flag = 0
                 LEFT JOIN cli_condition_definition AS T3 ON t3.ID = T2.definition_id
            AND T3.delete_flag = '0'
        WHERE T1.delete_flag = '0'
          AND T1.tcm_flag = 0
          AND T1.encounter_id = #{encounterId}
    </select>

    <select id="getAdviceNumByConditionId" resultType="Integer">
        SELECT SUM
                   (count_result) AS total_count
        FROM (
                 SELECT COUNT
                            (1) AS count_result
                 FROM med_medication_request
                 WHERE delete_flag = '0'
                   AND condition_id = #{conditionId}
                 UNION ALL
                 SELECT COUNT
                            (1)
                 FROM wor_device_request
                 WHERE delete_flag = '0'
                   AND condition_id = #{conditionId}
                 UNION ALL
                 SELECT COUNT
                            (1)
                 FROM wor_service_request
                 WHERE delete_flag = '0'
                   AND condition_id = #{conditionId}
             ) AS counts
    </select>

</mapper>