<?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.PatientPHRHistoryMapper">
	<insert id="insertPHRHistory" parameterType="PatientPHRHistoryDTO" useGeneratedKeys="true">
	<![CDATA[
	    INSERT INTO patient_phr_history (patient_idx, phr_type, phr_value
	]]>
		<if test='phrValue2 != null and phrValue2 != ""'>
            <![CDATA[
	    	, phr_value_2
	    	]]>
        </if>
   		<if test='recordedById != null and recordedById != ""'>
            <![CDATA[
	    	, recorded_by_id
	    	]]>
        </if>
	<![CDATA[    	
			, recorded_by_name
	    	, create_date)
       	VALUE (#{patientIdx}, #{phrType}, #{phrValue}
    ]]>
    	<if test='phrValue2 != null and phrValue2 != ""'>
            <![CDATA[
	    	, #{phrValue2}
	    	]]>
        </if>
   		<if test='recordedById != null and recordedById != ""'>
            <![CDATA[
	    	, #{recordedById}
	    	]]>
        </if>
	<![CDATA[
			, #{recordedByName}
       		, NOW())
	]]>
	</insert>
    <select id="selectPHRHistoryCount" parameterType="PatientPHRHistoryDTO" resultType="int">
	<![CDATA[
	    SELECT count(*) AS total
		FROM patient_phr_history
		WHERE 
			patient_idx = #{patientIdx}
		AND
			phr_type = #{phrType}
	]]>
    </select>
    <select id="selectPHRHistoryList" parameterType="PatientPHRHistoryDTO" resultType="PatientPHRHistoryDTO">
    <![CDATA[
	    SELECT	create_date			AS createDate,
	    		phr_type			AS phrType,
	    		phr_value			AS phrValue,
	]]>
    	<if test='phrType == "bloodPressure"'>
    	 <![CDATA[
    	 		phr_value_2			AS phrValue2,
    	 ]]>
        </if>
	<![CDATA[
	    		recorded_by_name	AS recordedByName
		FROM patient_phr_history
		WHERE 
			patient_idx = #{patientIdx}
		AND
			phr_type = #{phrType}
		ORDER BY create_date ASC
	]]>
    </select>
</mapper>