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

    <resultMap id="BaseResultMap" type="com.xinelu.familydoctor.entity.DeviceBpRecord">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="identity" column="identity" jdbcType="VARCHAR"/>
            <result property="sbp" column="sbp" jdbcType="INTEGER"/>
            <result property="dbp" column="dbp" jdbcType="INTEGER"/>
            <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,sbp,
        dbp,hr,measure_time,
        upload_type
    </sql>

    <select id="getBpRecord" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from device_bp_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>

    <sql id="maxSbpSql">
        select concat(sbp, '/', dbp) "maxSbpVal" from device_bp_record
        where sbp = (select max(sbp) from device_bp_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>)
        and 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 id desc limit 1
    </sql>

    <sql id="maxDbpSql">
        select concat(sbp, '/', dbp) "maxDbpVal" from device_bp_record
        where dbp = (select max(dbp) from device_bp_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>)
        and 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 id desc limit 1
    </sql>

    <sql id="minSbpSql">
        select concat(sbp, '/', dbp) "minSbpVal" from device_bp_record
        where sbp = (select min(sbp) from device_bp_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>)
        and 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 id desc limit 1
    </sql>

    <sql id="minDbpSql">
        select concat(sbp, '/', dbp) "minDbpVal" from device_bp_record
        where dbp = (select min(dbp) from device_bp_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>)
        and 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 id desc limit 1
    </sql>

    <sql id="avgSql">
        select concat(cast(avg(sbp) as signed), '/', cast(avg(dbp) as signed)) "avgVal",
               count(case when sbp &gt;= 90 and sbp &lt;= 140 and dbp &gt;= 60 and dbp &lt;= 90 then 1 else null end) "normalNum",
               count(case when sbp &lt; 90 or sbp &gt; 140 or dbp &lt; 60 or dbp &gt; 90 then 1 else null end) "overNum"
        from device_bp_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>
    </sql>

    <select id="getBpCalc" resultType="com.xinelu.familydoctor.vo.BpCalcVO">
        select * from
        (<include refid="maxSbpSql" />) a,
        (<include refid="maxDbpSql" />) b,
        (<include refid="minSbpSql" />) c,
        (<include refid="minDbpSql" />) d,
        (<include refid="avgSql" />) e
    </select>

    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.xinelu.familydoctor.entity.DeviceBpRecord" useGeneratedKeys="true">
        insert into device_bp_record
        (identity,sbp
        ,dbp,hr,measure_time
        ,upload_type)
        values (#{identity,jdbcType=VARCHAR},#{sbp,jdbcType=INTEGER}
        ,#{dbp,jdbcType=INTEGER},#{hr,jdbcType=INTEGER},#{measureTime,jdbcType=TIMESTAMP}
        ,#{uploadType,jdbcType=VARCHAR})
    </insert>
    <update id="update" parameterType="com.xinelu.familydoctor.entity.DeviceBpRecord">
        update device_bp_record
        <set>
            <if test="identity != null">
                identity = #{identity,jdbcType=VARCHAR},
            </if>
            <if test="sbp != null">
                sbp = #{sbp,jdbcType=INTEGER},
            </if>
            <if test="dbp != null">
                dbp = #{dbp,jdbcType=INTEGER},
            </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>
</mapper>
