<?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.xinelu.familydoctor.mapper.DeviceHrRecordMapper">

    <resultMap id="BaseResultMap" type="com.xinelu.familydoctor.entity.DeviceHrRecord">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="identity" column="identity" jdbcType="VARCHAR"/>
            <result property="hr" column="hr" jdbcType="INTEGER"/>
            <result property="measureTime" column="measure_time" jdbcType="TIMESTAMP"/>
            <result property="uploadType" column="upload_type" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,identity,hr,
        measure_time,upload_type
    </sql>

    <select id="getHrRecord" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from device_hr_record
        where identity = #{identity}
        <choose>
            <when test="type == '0'">

            </when>
            <!-- 按周统计 -->
            <when test="type == '1'">
                and measure_time between date_add(now(), interval -7 day) and now()
            </when>
            <!-- 按月统计 -->
            <when test="type == '2'">
                and measure_time between date_add(now(), interval -1 month) and now()
            </when>
            <!-- 按年统计 -->
            <when test="type == '3'">
                and measure_time between date_add(now(), interval -1 year) and now()
            </when>
            <otherwise>

            </otherwise>
        </choose>
        order by measure_time
    </select>

    <select id="getHrCalc" resultType="com.xinelu.familydoctor.vo.HrCalcVO">
        select ifnull(max(hr), 0) "maxVal",
               ifnull(min(hr), 0) "minVal",
               ifnull(avg(hr), 0) "avgVal",
               count(case when hr &gt;= 60 and hr &lt;= 100 then 1 else null end) "normalNum",
               count(case when hr &lt; 60 or hr &gt; 100 then 1 else null end) "overNum"
        from device_hr_record
        where identity = #{identity}
        <choose>
            <when test="type == '0'">

            </when>
            <!-- 按周统计 -->
            <when test="type == '1'">
                and measure_time between date_add(now(), interval -7 day) and now()
            </when>
            <!-- 按月统计 -->
            <when test="type == '2'">
                and measure_time between date_add(now(), interval -1 month) and now()
            </when>
            <!-- 按年统计 -->
            <when test="type == '3'">
                and measure_time between date_add(now(), interval -1 year) and now()
            </when>
            <otherwise>

            </otherwise>
        </choose>
    </select>

    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.xinelu.familydoctor.entity.DeviceHrRecord" useGeneratedKeys="true">
        insert into device_hr_record
        (identity,hr
        ,measure_time,upload_type)
        values (#{identity,jdbcType=VARCHAR},#{hr,jdbcType=INTEGER}
        ,#{measureTime,jdbcType=TIMESTAMP},#{uploadType,jdbcType=VARCHAR})
    </insert>
    <update id="update" parameterType="com.xinelu.familydoctor.entity.DeviceHrRecord">
        update device_hr_record
        <set>
            <if test="identity != null">
                identity = #{identity,jdbcType=VARCHAR},
            </if>
            <if test="hr != null">
                hr = #{hr,jdbcType=INTEGER},
            </if>
            <if test="measureTime != null">
                measure_time = #{measureTime,jdbcType=TIMESTAMP},
            </if>
            <if test="uploadType != null">
                upload_type = #{uploadType,jdbcType=VARCHAR},
            </if>
        </set>
        where   id = #{id,jdbcType=BIGINT}
    </update>
	<select id="selectTimeLineList" parameterType="java.lang.String" resultType="com.xinelu.common.core.domain.TimelineVo">
		select
			id, date_format(measure_time,'%Y-%m-%d') as createTime
		from device_hr_record
		where identity = #{identity}
		order by measure_time desc
	</select>
	<select id="selectById" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from device_hr_record
		where id = #{id,jdbcType=BIGINT}
	</select>
</mapper>
