<?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.tongran.rocketmq.mapper.InitialCpuInfoMapper">

    <resultMap type="InitialCpuInfo" id="InitialCpuInfoResult">
        <result property="id"    column="id"    />
        <result property="clientId"    column="client_id"    />
        <result property="avg1"    column="avg1"    />
        <result property="avg5"    column="avg5"    />
        <result property="avg15"    column="avg15"    />
        <result property="interrupt"    column="interrupt"    />
        <result property="uti"    column="uti"    />
        <result property="num"    column="num"    />
        <result property="cores"    column="cores"    />
        <result property="normal"    column="normal"    />
        <result property="idle"    column="idle"    />
        <result property="iowait"    column="iowait"    />
        <result property="system"    column="system"    />
        <result property="noresp"    column="noresp"    />
        <result property="user"    column="user"    />
        
        <result property="temperature"    column="temperature"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectInitialCpuInfoVo">
        select
        id,
        client_id,
        avg1,
        avg5,
        avg15,
        interrupt,
        uti,
        num,
        cores,
        normal,
        idle,
        iowait,
        system,
        noresp,
        user,
        temperature,  
        create_by,
        update_by,
        create_time,
        update_time
        from initial_cpu_info
    </sql>

    <select id="selectInitialCpuInfoList" parameterType="InitialCpuInfo" resultMap="InitialCpuInfoResult">
        <include refid="selectInitialCpuInfoVo"/>
        <where>
            <if test="clientId != null  and clientId != ''"> and client_id = #{clientId}</if>
            <if test="temperature != null"> and temperature = #{temperature}</if>
            <if test="startTime != null  and startTime != ''"> and create_time &gt;= #{startTime}</if>
            <if test="endTime != null  and endTime != ''"> and create_time &lt;= #{endTime}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectInitialCpuInfoById" parameterType="Long" resultMap="InitialCpuInfoResult">
        <include refid="selectInitialCpuInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertInitialCpuInfo" parameterType="InitialCpuInfo" useGeneratedKeys="true" keyProperty="id">
        insert IGNORE into initial_cpu_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="clientId != null and clientId != ''">client_id,</if>
            <if test="avg1 != null">avg1,</if>
            <if test="avg5 != null">avg5,</if>
            <if test="avg15 != null">avg15,</if>
            <if test="interrupt != null">interrupt,</if>
            <if test="uti != null">uti,</if>
            <if test="num != null">num,</if>
            <if test="cores != null">cores,</if>
            <if test="normal != null">normal,</if>
            <if test="idle != null">idle,</if>
            <if test="iowait != null">iowait,</if>
            <if test="system != null">system,</if>
            <if test="noresp != null">noresp,</if>
            <if test="user != null">user,</if>
            
            <if test="temperature != null">temperature,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="clientId != null and clientId != ''">#{clientId},</if>
            <if test="avg1 != null">#{avg1},</if>
            <if test="avg5 != null">#{avg5},</if>
            <if test="avg15 != null">#{avg15},</if>
            <if test="interrupt != null">#{interrupt},</if>
            <if test="uti != null">#{uti},</if>
            <if test="num != null">#{num},</if>
            <if test="cores != null">#{cores},</if>
            <if test="normal != null">#{normal},</if>
            <if test="idle != null">#{idle},</if>
            <if test="iowait != null">#{iowait},</if>
            <if test="system != null">#{system},</if>
            <if test="noresp != null">#{noresp},</if>
            <if test="user != null">#{user},</if>
            
            <if test="temperature != null">#{temperature},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateInitialCpuInfo" parameterType="InitialCpuInfo">
        update initial_cpu_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="clientId != null and clientId != ''">client_id = #{clientId},</if>
            <if test="avg1 != null">avg1 = #{avg1},</if>
            <if test="avg5 != null">avg5 = #{avg5},</if>
            <if test="avg15 != null">avg15 = #{avg15},</if>
            <if test="interrupt != null">interrupt = #{interrupt},</if>
            <if test="uti != null">uti = #{uti},</if>
            <if test="num != null">num = #{num},</if>
            <if test="cores != null">cores = #{cores},</if>
            <if test="normal != null">normal = #{normal},</if>
            <if test="idle != null">idle = #{idle},</if>
            <if test="iowait != null">iowait = #{iowait},</if>
            <if test="system != null">system = #{system},</if>
            <if test="noresp != null">noresp = #{noresp},</if>
            <if test="user != null">user = #{user},</if>
            
            <if test="temperature != null">temperature = #{temperature},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInitialCpuInfoById" parameterType="Long">
        delete from initial_cpu_info where id = #{id}
    </delete>

    <delete id="deleteInitialCpuInfoByIds" parameterType="String">
        delete from initial_cpu_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="batchInsertInitialCpuInfo" parameterType="java.util.List">
        INSERT IGNORE INTO initial_cpu_info
        (
        client_id,
        avg1,
        avg5,
        avg15,
        interrupt,
        uti,
        num,
        cores,
        normal,
        idle,
        iowait,
        `system`,
        noresp,
        `user`,
        temperature,  
        create_by,
        update_by,
        create_time,
        update_time
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.clientId},
            #{item.avg1},
            #{item.avg5},
            #{item.avg15},
            #{item.interrupt},
            #{item.uti},
            #{item.num},
            #{item.cores},
            #{item.normal},
            #{item.idle},
            #{item.iowait},
            #{item.system},
            #{item.noresp},
            #{item.user},
            #{item.temperature},  
            #{item.createBy},
            #{item.updateBy},
            <choose>
                <when test="item.createTime != null">
                    #{item.createTime}
                </when>
                <otherwise>
                    NOW()
                </otherwise>
            </choose>,
            <choose>
                <when test="item.updateTime != null">
                    #{item.updateTime}
                </when>
                <otherwise>
                    NOW()
                </otherwise>
            </choose>
            )
        </foreach>
    </insert>

    <select id="getCpuInfoByClientId" parameterType="String" resultMap="InitialCpuInfoResult">
        <include refid="selectInitialCpuInfoVo"/>
        where client_id = #{clientId}
        order by create_time desc
        limit 1
    </select>
</mapper>