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

    <resultMap id="BaseResultMap" type="com.xinelu.familydoctor.entity.DeviceBgRecord">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="identity" column="identity" jdbcType="VARCHAR"/>
            <result property="bg" column="bg" jdbcType="DECIMAL"/>
            <result property="bucket" column="bucket" jdbcType="VARCHAR"/>
            <result property="measureTime" column="measure_time" jdbcType="TIMESTAMP"/>
            <result property="uploadType" column="upload_type" jdbcType="VARCHAR"/>
    </resultMap>

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

    <select id="getBgRecord" resultMap="BaseResultMap">
        select <include refid="Base_Column_List" />
        from device_bg_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="getBgCalc" resultType="com.xinelu.familydoctor.vo.BgCalcVO">
        select IFNULL(max(bg),0) "maxVal",
        IFNULL(min(bg),0) "minVal",
        count(case when bg &lt; 4.4 then 1 else null end) "lowerNum",
        count(case when (bucket = '2' and (bg &gt;= 4.4 or bg &lt;= 7.0)) or (bucket != '2' and (bg &gt;= 4.4 or bg &lt;= 10)) then 1 else null end) "normalNum",
        count(case when (bucket = '2' and bg &gt; 7.0) or (bucket != '2' and bg &gt; 10) then 1 else null end) "higherNum"
        from device_bg_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.DeviceBgRecord" useGeneratedKeys="true">
        insert into device_bg_record
        (identity,bg
        ,bucket,measure_time,upload_type
        )
        values (#{identity,jdbcType=VARCHAR},#{bg,jdbcType=DECIMAL}
        ,#{bucket,jdbcType=VARCHAR},#{measureTime,jdbcType=TIMESTAMP},#{uploadType,jdbcType=VARCHAR}
        )
    </insert>
    <update id="update" parameterType="com.xinelu.familydoctor.entity.DeviceBgRecord">
        update device_bg_record
        <set>
            <if test="identity != null">
                identity = #{identity,jdbcType=VARCHAR},
            </if>
            <if test="bg != null">
                bg = #{bg,jdbcType=DECIMAL},
            </if>
            <if test="bucket != null">
                bucket = #{bucket,jdbcType=VARCHAR},
            </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="getLastRecord" resultType="com.xinelu.familydoctor.vo.PhysicalLastRecordVO">
        (select '1' AS "type", measure_time "date", bucket, bg "val" from device_bg_record
        where identity = #{identity} and measure_time &gt;= date_add(now(), interval -30 day) order by id desc limit 1)
        union all
        (select '2' AS "type", measure_time "date", null "bucket", concat(sbp, '/', dbp) "val" from device_bp_record
         where identity = #{identity} and measure_time &gt;= date_add(now(), interval -30 day) order by id desc limit 1)
        union all
        (select '3' AS "type", measure_time "date", null "bucket", concat(tc, '/', tg, '/', hdl, '/', ldl) "val" from device_bf_record
         where identity = #{identity} and measure_time &gt;= date_add(now(), interval -30 day) order by id desc limit 1)
        union all
        (select '4' AS "type", measure_time "date", null "bucket", bmi "val" from device_bmi_record
         where identity = #{identity} and measure_time &gt;= date_add(now(), interval -30 day) order by id desc limit 1)
        union all
        (select '5' AS "type", measure_time "date", null "bucket", spo2 "val" from device_bo_record
         where identity = #{identity} and measure_time &gt;= date_add(now(), interval -30 day) order by id desc limit 1)
        union all
        (select '6' AS "type", measure_time "date", null "bucket", hr "val" from device_hr_record
         where identity = #{identity} and measure_time &gt;= date_add(now(), interval -30 day) order by id desc limit 1)
        union all
        (select '7' AS "type", measure_time "date", null "bucket", temp "val" from device_temp_record
         where identity = #{identity} and measure_time &gt;= date_add(now(), interval -30 day) order by id desc limit 1)
    </select>
	<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_bg_record
		where identity = #{identity}
		order by measure_time desc
	</select>
	<select id="selectById" resultMap="BaseResultMap">
		select <include refid="Base_Column_List" />
		from device_bg_record
		where id = #{id,jdbcType=BIGINT}
	</select>
	<select id="getBgList" resultMap="BaseResultMap">
		select <include refid="Base_Column_List" />
		from device_bg_record
		where identity = #{identity}
		order by measure_time desc
	</select>
</mapper>
