<?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.lemon.lifecenter.mapper.PatientPHRLatestMapper">
	<insert id="insertPHRLatest" parameterType="PatientPHRLatestDTO" useGeneratedKeys="true">
	<![CDATA[
	    INSERT INTO patient_phr_latest (
	    	patient_idx
    ]]>
	    <if test='temperature != null and temperature != ""'>
	    <![CDATA[
   			, temperature, temperature_create_date
	    ]]>
	    </if>
	    <if test='oxygenSaturation != null and oxygenSaturation != ""'>
	    <![CDATA[
   			, oxygen_saturation, oxygen_saturation_create_date
	    ]]>
	    </if>
	    <if test='pulseRate != null and pulseRate != ""'>
	    <![CDATA[
   			, pulse_rate, pulse_rate_create_date
	    ]]>
	    </if>
	    <if test='systolicBloodPressure != null and systolicBloodPressure != ""'>
	    <![CDATA[
   			, systolic_blood_pressure, systolic_blood_pressure_create_date
	    ]]>
	    </if>
	    <if test='diastolicBloodPressure != null and diastolicBloodPressure != ""'>
	    <![CDATA[
   			, diastolic_blood_pressure, diastolic_blood_pressure_create_date
	    ]]>
	    </if>
	    <if test='bloodSugar != null and bloodSugar != ""'>
	    <![CDATA[
   			, blood_sugar, blood_sugar_create_date
	    ]]>
	    </if>
    <![CDATA[
		    , create_date
	    ) VALUE (
	    	#{patientIdx}
   	]]>
   		<if test='temperature != null and temperature != ""'>
	    <![CDATA[
   			, #{temperature}, NOW()
	    ]]>
	    </if>
	    <if test='oxygenSaturation != null and oxygenSaturation != ""'>
	    <![CDATA[
   			, #{oxygenSaturation}, NOW()
	    ]]>
	    </if>
	    <if test='pulseRate != null and pulseRate != ""'>
	    <![CDATA[
   			, #{pulseRate}, NOW()
	    ]]>
	    </if>
	    <if test='systolicBloodPressure != null and systolicBloodPressure != ""'>
	    <![CDATA[
   			, #{systolicBloodPressure}, NOW()
	    ]]>
	    </if>
	    <if test='diastolicBloodPressure != null and diastolicBloodPressure != ""'>
	    <![CDATA[
   			, #{diastolicBloodPressure}, NOW()
	    ]]>
	    </if>
	    <if test='bloodSugar != null and bloodSugar != ""'>
	    <![CDATA[
   			, #{bloodSugar}, NOW()
	    ]]>
	    </if>
    <![CDATA[
    		, NOW()
	    )
	    ON DUPLICATE KEY UPDATE update_date = NOW()
    ]]>
	    <if test='temperature != null and temperature != ""'>
	    <![CDATA[
   			, temperature = #{temperature}, temperature_create_date = NOW()
	    ]]>
	    </if>
	    <if test='oxygenSaturation != null and oxygenSaturation != ""'>
	    <![CDATA[
   			, oxygen_saturation = #{oxygenSaturation}, oxygen_saturation_create_date = NOW()
	    ]]>
	    </if>
	    <if test='pulseRate != null and pulseRate != ""'>
	    <![CDATA[
   			, pulse_rate = #{pulseRate}, pulse_rate_create_date = NOW()
	    ]]>
	    </if>
	    <if test='systolicBloodPressure != null and systolicBloodPressure != ""'>
	    <![CDATA[
   			, systolic_blood_pressure = #{systolicBloodPressure}, systolic_blood_pressure_create_date = NOW()
	    ]]>
	    </if>
	    <if test='diastolicBloodPressure != null and diastolicBloodPressure != ""'>
	    <![CDATA[
   			, diastolic_blood_pressure = #{diastolicBloodPressure}, diastolic_blood_pressure_create_date = NOW()
	    ]]>
	    </if>
	    <if test='bloodSugar != null and bloodSugar != ""'>
	    <![CDATA[
   			, blood_suger = #{bloodSugar}, blood_suger_create_date = NOW()
	    ]]>
	    </if>
	</insert>
    <select id="selectPHRLatestCount" parameterType="PatientPHRLatestDTO" resultType="int">
	<![CDATA[
	    SELECT count(*) AS total
		FROM patient_care c LEFT JOIN patient_phr_latest p ON p.patient_idx = c.patient_idx
		WHERE c.center_code = #{centerCode} AND c.state = 'H'
	]]>
		<if test='searchText != null and searchText != ""'>
            <![CDATA[
               AND (c.patient_name LIKE CONCAT('%', #{searchText}, '%') OR c.ward_number LIKE CONCAT('%', #{searchText}, '%'))
            ]]>
        </if>
    </select>
    <select id="selectPHRLatestList" parameterType="PatientPHRLatestDTO" resultType="PatientPHRLatestDTO">
    <![CDATA[
   		SELECT	
   				c.patient_idx							AS patientIdx,
   				c.patient_name							AS patientName,
	    		c.ward_number							AS wardNumber,
	    		c.room_number							AS roomNumber,
	    		p.temperature							AS temperature,
	    		p.temperature_create_date				AS temperatureCreateDate,
	    		p.oxygen_saturation						AS oxygenSaturation,
	    		p.oxygen_saturation_create_date 		AS oxygenSaturationCreateDate,
	    		p.pulse_rate							AS pulseRate,
	    		p.pulse_rate_create_date				AS pulseRateCreateDate,
	    		p.systolic_blood_pressure				AS systolicBloodPressure,
	    		p.systolic_blood_pressure_create_date	AS systolicBloodPressureCreateDate,	    		
   				p.diastolic_blood_pressure				AS diastolicBloodPressure,
    			p.diastolic_blood_pressure_create_date	AS diastolicBloodPressureCreateDate,
    			p.blood_sugar							AS bloodSugar,
	    		p.blood_sugar_create_date				AS bloodSugarCreateDate,
	    		(SELECT count(*) FROM patient_memo m WHERE m.patient_idx = c.patient_idx) AS memoCount,
	    		(SELECT s.create_date FROM patient_symptom_sim s WHERE s.patient_idx = c.patient_idx AND DATEDIFF(DATE(s.create_date), CURDATE()) = 0 ORDER BY s.create_date DESC LIMIT 1) AS symptomLastDate
		FROM patient_care c LEFT JOIN patient_phr_latest p ON p.patient_idx = c.patient_idx
		WHERE c.center_code = #{centerCode} AND c.state = 'H'
	]]>
		<if test='searchText != null and searchText != ""'>
            <![CDATA[
               AND (c.patient_name LIKE CONCAT('%', #{searchText}, '%') OR c.ward_number LIKE CONCAT('%', #{searchText}, '%'))
            ]]>
        </if>
    <![CDATA[
		ORDER BY c.room_number ASC
		LIMIT #{limit}, #{limitMax}
	]]>
    </select>
</mapper>