<?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.InitialMemoryInfoMapper">
    
    <resultMap type="InitialMemoryInfo" id="InitialMemoryInfoResult">
        <result property="id"    column="id"    />
        <result property="clientId"    column="client_id"    />
        <result property="swapSizeFree"    column="swap_size_free"    />
        <result property="untilzation"    column="untilzation"    />
        <result property="swapSizePercent"    column="swap_size_percent"    />
        <result property="available"    column="available"    />
        <result property="percent"    column="percent"    />
        <result property="total"    column="total"    />
        <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="selectInitialMemoryInfoVo">
        select id, client_id, swap_size_free, untilzation, swap_size_percent, available, percent, total, create_by, update_by, create_time, update_time from initial_memory_info
    </sql>

    <select id="selectInitialMemoryInfoList" parameterType="InitialMemoryInfo" resultMap="InitialMemoryInfoResult">
        <include refid="selectInitialMemoryInfoVo"/>
        <where>  
            <if test="clientId != null  and clientId != ''"> and client_id = #{clientId}</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="selectInitialMemoryInfoById" parameterType="Long" resultMap="InitialMemoryInfoResult">
        <include refid="selectInitialMemoryInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertInitialMemoryInfo" parameterType="InitialMemoryInfo" useGeneratedKeys="true" keyProperty="id">
        insert IGNORE into initial_memory_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="clientId != null and clientId != ''">client_id,</if>
            <if test="swapSizeFree != null">swap_size_free,</if>
            <if test="untilzation != null">untilzation,</if>
            <if test="swapSizePercent != null">swap_size_percent,</if>
            <if test="available != null">available,</if>
            <if test="percent != null">percent,</if>
            <if test="total != null">total,</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="swapSizeFree != null">#{swapSizeFree},</if>
            <if test="untilzation != null">#{untilzation},</if>
            <if test="swapSizePercent != null">#{swapSizePercent},</if>
            <if test="available != null">#{available},</if>
            <if test="percent != null">#{percent},</if>
            <if test="total != null">#{total},</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="updateInitialMemoryInfo" parameterType="InitialMemoryInfo">
        update initial_memory_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="clientId != null and clientId != ''">client_id = #{clientId},</if>
            <if test="swapSizeFree != null">swap_size_free = #{swapSizeFree},</if>
            <if test="untilzation != null">untilzation = #{untilzation},</if>
            <if test="swapSizePercent != null">swap_size_percent = #{swapSizePercent},</if>
            <if test="available != null">available = #{available},</if>
            <if test="percent != null">percent = #{percent},</if>
            <if test="total != null">total = #{total},</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="deleteInitialMemoryInfoById" parameterType="Long">
        delete from initial_memory_info where id = #{id}
    </delete>

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

    <insert id="batchInsertInitialMemoryInfo" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT IGNORE INTO initial_memory_info
        (
        client_id,
        swap_size_free,
        untilzation,
        swap_size_percent,
        available,
        percent,
        total,
        create_by,
        update_by,
        create_time,
        update_time
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.clientId},
            #{item.swapSizeFree},
            #{item.untilzation},
            #{item.swapSizePercent},
            #{item.available},
            #{item.percent},
            #{item.total},
            #{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="getMemoryInfoByClientId" parameterType="String" resultMap="InitialMemoryInfoResult">
        <include refid="selectInitialMemoryInfoVo"/>
        where client_id = #{clientId}
        limit 1
    </select>
</mapper>