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

    <resultMap id="BaseResultMap" type="com.xinelu.familydoctor.entity.DeviceBmiRecord">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="identity" column="identity" jdbcType="VARCHAR"/>
            <result property="height" column="height" jdbcType="DECIMAL"/>
            <result property="weight" column="weight" jdbcType="DECIMAL"/>
            <result property="bmi" column="bmi" jdbcType="DECIMAL"/>
            <result property="measureTime" column="measure_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,identity,height,
        weight,bmi,measure_time
    </sql>

    <select id="getBmiRecord" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from device_bmi_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="getBmiCalc" resultType="com.xinelu.familydoctor.vo.BmiCalcVO">
        select ifnull(max(bmi), 0) "maxVal",
        ifnull(min(bmi), 0) "minVal",
        ifnull(avg(bmi), 0) "avgVal",
        count(case when bmi &gt;= 18.5 and bmi &lt;= 23.9 then 1 else null end) "normalNum",
        count(case when bmi &lt; 18.5 or bmi >= 24 then 1 else null end) "overNum"
        from device_bmi_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.DeviceBmiRecord" useGeneratedKeys="true">
        insert into device_bmi_record
        (identity,height
        ,weight,bmi,measure_time
        )
        values (#{identity,jdbcType=VARCHAR},#{height,jdbcType=DECIMAL}
        ,#{weight,jdbcType=DECIMAL},#{bmi,jdbcType=DECIMAL},#{measureTime,jdbcType=TIMESTAMP}
        )
    </insert>
    <update id="update" parameterType="com.xinelu.familydoctor.entity.DeviceBmiRecord">
        update device_bmi_record
        <set>
                <if test="identity != null">
                    identity = #{identity,jdbcType=VARCHAR},
                </if>
                <if test="height != null">
                    height = #{height,jdbcType=DECIMAL},
                </if>
                <if test="weight != null">
                    weight = #{weight,jdbcType=DECIMAL},
                </if>
                <if test="bmi != null">
                    bmi = #{bmi,jdbcType=DECIMAL},
                </if>
                <if test="measureTime != null">
                    measure_time = #{measureTime,jdbcType=TIMESTAMP},
                </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_bmi_record
		where identity = #{identity}
		order by measure_time desc
	</select>
	<select id="selectById" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from device_bmi_record
		where id = #{id,jdbcType=BIGINT}
	</select>
</mapper>
