<?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.DeviceBoRecordMapper">

    <resultMap id="BaseResultMap" type="com.xinelu.familydoctor.entity.DeviceBoRecord">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="identity" column="identity" jdbcType="VARCHAR"/>
            <result property="spo2" column="spo2" jdbcType="INTEGER"/>
            <result property="pulse" column="pulse" jdbcType="INTEGER"/>
            <result property="measureTime" column="measure_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,identity,spo2,
        pulse,measure_time
    </sql>

    <select id="getBoRecord" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from device_bo_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="getBoCalc" resultType="com.xinelu.familydoctor.vo.BoCalcVO">
        select ifnull(max(spo2), 0) "maxVal",
        ifnull(min(spo2), 0) "minVal",
        ifnull(avg(spo2), 0) "avgVal",
        count(case when spo2 &gt;= 95 then 1 else null end) "normalNum",
        count(case when spo2 &lt; 95 then 1 else null end) "overNum"
        from device_bo_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.DeviceBoRecord" useGeneratedKeys="true">
        insert into device_bo_record
        (identity,spo2
        ,pulse,measure_time)
        values (#{identity,jdbcType=VARCHAR},#{spo2,jdbcType=INTEGER}
        ,#{pulse,jdbcType=INTEGER},#{measureTime,jdbcType=TIMESTAMP})
    </insert>
    <update id="update" parameterType="com.xinelu.familydoctor.entity.DeviceBoRecord">
        update device_bo_record
        <set>
            <if test="identity != null">
                identity = #{identity,jdbcType=VARCHAR},
            </if>
            <if test="spo2 != null">
                spo2 = #{spo2,jdbcType=INTEGER},
            </if>
            <if test="pulse != null">
                pulse = #{pulse,jdbcType=INTEGER},
            </if>
            <if test="measureTime != null">
                measure_time = #{measureTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where   id = #{id,jdbcType=BIGINT}
    </update>
</mapper>
