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

    <resultMap id="BaseResultMap" type="com.xinelu.familydoctor.entity.DeviceBfRecord">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="identity" column="identity" jdbcType="VARCHAR"/>
            <result property="tc" column="tc" jdbcType="DECIMAL"/>
            <result property="tg" column="tg" jdbcType="DECIMAL"/>
            <result property="hdl" column="hdl" jdbcType="DECIMAL"/>
            <result property="ldl" column="ldl" jdbcType="DECIMAL"/>
            <result property="measureTime" column="measure_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,identity,tc,
        tg,hdl,ldl,
        measure_time
    </sql>

    <select id="getBfRecord" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from device_bf_record
        where  identity = #{identity,jdbcType=VARCHAR}
        <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>

    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.xinelu.familydoctor.entity.DeviceBfRecord" useGeneratedKeys="true">
        insert into device_bf_record
        (identity,tc
        ,tg,hdl,ldl
        ,measure_time)
        values (#{identity,jdbcType=VARCHAR},#{tc,jdbcType=DECIMAL}
        ,#{tg,jdbcType=DECIMAL},#{hdl,jdbcType=DECIMAL},#{ldl,jdbcType=DECIMAL}
        ,#{measureTime,jdbcType=TIMESTAMP})
    </insert>
    <update id="update" parameterType="com.xinelu.familydoctor.entity.DeviceBfRecord">
        update device_bf_record
        <set>
            <if test="identity != null">
                identity = #{identity,jdbcType=VARCHAR},
            </if>
            <if test="tc != null">
                tc = #{tc,jdbcType=DECIMAL},
            </if>
            <if test="tg != null">
                tg = #{tg,jdbcType=DECIMAL},
            </if>
            <if test="hdl != null">
                hdl = #{hdl,jdbcType=DECIMAL},
            </if>
            <if test="ldl != null">
                ldl = #{ldl,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_bf_record
		where identity = #{identity}
		order by measure_time desc
	</select>
	<select id="selectById" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from device_bf_record
		where  id = #{id,jdbcType=BIGINT}
	</select>
</mapper>
