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

    <resultMap type="AllDiskName" id="AllDiskNameResult">
        <result property="id"    column="id"    />
        <result property="clientId"    column="client_id"    />
        <result property="name"    column="name"    />
        <result property="status"    column="status"    />
        <result property="readIops"    column="read_iops"    />
        <result property="writeIops"    column="write_iops"    />
        <result property="umountFlag"    column="umount_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectAllDiskNameVo">
        select id, client_id, name, status, read_iops, write_iops, umount_flag, create_time, update_time, create_by, update_by from all_disk_name
    </sql>

    <select id="selectAllDiskNameList" parameterType="AllDiskName" resultMap="AllDiskNameResult">
        <include refid="selectAllDiskNameVo"/>
        <where>
            <if test="clientId != null  and clientId != ''"> and client_id = #{clientId}</if>
            <if test="name != null  and name != ''"> and name = #{name}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="readIops != null  and readIops != ''"> and read_iops = #{readIops}</if>
            <if test="writeIops != null  and writeIops != ''"> and write_iops = #{writeIops}</if>
            <if test="umountFlag != null "> and umount_flag = #{umountFlag}</if>
        </where>
    </select>

    <select id="selectAllDiskNameById" parameterType="Long" resultMap="AllDiskNameResult">
        <include refid="selectAllDiskNameVo"/>
        where id = #{id}
    </select>

    <insert id="insertAllDiskName" parameterType="AllDiskName" useGeneratedKeys="true" keyProperty="id">
        insert into all_disk_name
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="clientId != null and clientId != ''">client_id,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="status != null">status,</if>
            <if test="readIops != null">read_iops,</if>
            <if test="writeIops != null">write_iops,</if>
            <if test="umountFlag != null">umount_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="clientId != null and clientId != ''">#{clientId},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="status != null">#{status},</if>
            <if test="readIops != null">#{readIops},</if>
            <if test="writeIops != null">#{writeIops},</if>
            <if test="umountFlag != null">#{umountFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
        on duplicate key update
        <trim suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="status != null">status = #{status},</if>
            <if test="readIops != null">read_iops = #{readIops},</if>
            <if test="writeIops != null">write_iops = #{writeIops},</if>
            <if test="umountFlag != null">umount_flag = #{umountFlag},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
    </insert>

    <update id="updateAllDiskName" parameterType="AllDiskName">
        update all_disk_name
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = #{status},</if>
            <if test="readIops != null">read_iops = #{readIops},</if>
            <if test="writeIops != null">write_iops = #{writeIops},</if>
            <if test="umountFlag != null">umount_flag = #{umountFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        <where>
            <choose>
                <when test="id != null">
                    and id=#{id}
                </when>
                <when test="clientId != null and name != null">
                    and client_id = #{clientId} and name =#{name}
                </when>
                <otherwise>
                    and 1=0
                </otherwise>
            </choose>
        </where>
    </update>

    <delete id="deleteAllDiskNameById" parameterType="Long">
        delete from all_disk_name where id = #{id}
    </delete>

    <delete id="deleteAllDiskNameByIds" parameterType="String">
        delete from all_disk_name where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <insert id="batchInsertAllDistName" parameterType="java.util.List">
        insert IGNORE into all_disk_name
        (client_id, name, status, read_iops, write_iops, umount_flag, create_time, update_time, create_by, update_by)
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.clientId},
            #{item.name},
            #{item.status},
            #{item.readIops},
            #{item.writeIops},
            #{item.umountFlag},
            #{item.createTime},
            #{item.updateTime},
            #{item.createBy},
            #{item.updateBy}
            )
        </foreach>
    </insert>

    <select id="selectDiskInfoList" parameterType="AllDiskName" resultType="AllDiskName">
        SELECT
        a.id,
        a.client_id as clientId,
        a.name,
        a.status,
        a.read_iops as readIops,
        a.write_iops as writeIops,
        a.umount_flag as umountFlag,
        a.create_time as createTime,
        a.update_time as updateTime,
        b.serial,
        TRUNCATE(IFNULL(b.total,0)/(1024 * 1024 * 1024), 0) as total,
        TRUNCATE(IFNULL(b.write_speed,0)/1024, 0) as writeSpeed,
        TRUNCATE(IFNULL(b.read_speed,0)/1024, 0) as readSpeed,
        b.type,
        TRUNCATE(IFNULL(b.used_space,0)/(1024 * 1024 * 1024), 0) as usedSpace
        FROM all_disk_name a
        LEFT JOIN (
        SELECT client_id, name, MAX(create_time) as max_create_time
        FROM ${tableName}
        GROUP BY client_id, name
        ) latest ON a.client_id = latest.client_id AND a.name = latest.name
        LEFT JOIN ${tableName} b
        ON latest.client_id = b.client_id
        AND latest.name = b.name
        AND latest.max_create_time = b.create_time
        <where>
            <if test="clientId != null">
                AND a.client_id = #{clientId}
            </if>
        </where>
    </select>
</mapper>