<?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.externalintegration.mapper.FoodborneAcquisitionAppMapper">

    <select id="selectEncounterDiagnosisConditionNameList" resultType="String">
        SELECT T3."name"
        FROM adm_encounter_diagnosis T1
                 INNER JOIN cli_condition T2
                            ON T2.id = T1.condition_id
                 INNER JOIN cli_condition_definition T3
                            ON T3.id = T2.definition_id
        WHERE T1.encounter_id = #{encounterId}
    </select>

    <select id="selectSimplediseaseAddNopwParam"
            resultType="com.openhis.web.externalintegration.dto.FaSimplediseaseAddNopwParam">
        SELECT to_char(COALESCE(T3.onset_datetime, T1.create_time), 'YYYY-MM-DD HH24:MI') AS diseaseDate
             , to_char(T1.create_time, 'YYYY-MM-DD HH24:MI')                              AS diseaseTreattime
             , T1.bus_no                                                                  AS outPatientNumber
             , T4."name"                                                                  AS patientName
             , CASE
                   WHEN T4.gender_enum = #{genderEnumMale} THEN '1'
                   WHEN T4.gender_enum = #{genderEnumFemale} THEN '0'
                   ELSE '1' END                                                           AS diseaseSex
             , ''                                                                         AS guarderName
             , CASE
                   WHEN T1.first_enum = #{firstEnumFollowUp} THEN '1'
                   ELSE '0' END                                                           AS diseaseIsreexam
             , CASE
                   WHEN T1.class_enum = #{encounterClassEnumImp} THEN '1'
                   ELSE '0' END                                                           AS diseaseIspaint
             , CASE
                   WHEN T1.class_enum = #{encounterClassEnumImp} THEN T1.bus_no
                   ELSE '' END                                                            AS diseaseHospitalno
             , T4.id_card                                                                 AS identityCard
             , to_char(T4.birth_date, 'YYYY-MM-DD')                                       AS diseaseBirthday
             , T4.phone                                                                   AS phoneNumber
             , T4.work_company                                                            AS workUnit
             , to_char(T4.deceased_date, 'YYYY-MM-DD HH24:MI')                            AS deathDate
             , T6."name"                                                                  AS fillingDoctorName
             , T4.address_province                                                        AS diseaseProvince
             , T4.address_city                                                            AS diseaseCity
             , T4.address_district                                                        AS diseaseDistrict
             , T4.address                                                                 AS diseaseAddress
             , T4.prfs_enum                                                               AS diseaseOccupation
        FROM adm_encounter T1
                 INNER JOIN (
            SELECT condition_id
                 , encounter_id
            FROM adm_encounter_diagnosis
            WHERE encounter_id = #{encounterId}
              AND delete_flag = '0'
              AND maindise_flag = 1
            ORDER BY id DESC LIMIT 1
        ) T2 ON T2.encounter_id = T1.id
                 INNER JOIN cli_condition T3
                            ON T3.id = T2.condition_id
                 INNER JOIN adm_patient T4
                            ON T4.id = T1.patient_id
                 LEFT JOIN (
            SELECT practitioner_id
                 , encounter_id
            FROM adm_encounter_participant
            WHERE encounter_id = #{encounterId}
              AND delete_flag = '0'
              AND type_code = #{participantTypeAdmitter}
            ORDER BY id DESC LIMIT 1
        ) T5 ON T5.encounter_id = T1.id
                 LEFT JOIN adm_practitioner T6
                           ON T6.id = T5.practitioner_id
        WHERE T1.id = #{encounterId}
    </select>

</mapper>