<?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="cn.reghao.oss.store.db.mapper.DataBlockMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into data_block
        (`id`,`deleted`,`create_time`,`update_time`,`content_id`,`index`,`block_id`,`host`,`base_dir`,`relative_dir`,`absolute_path`,`size`,`start`,`end`)
        values
        (#{id},#{deleted},#{createTime},#{updateTime},#{contentId},#{index},#{blockId},#{host},#{baseDir},#{relativeDir},#{absolutePath},#{size},#{start},#{end})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert into data_block
        (`id`,`deleted`,`create_time`,`update_time`,`content_id`,`index`,`block_id`,`host`,`base_dir`,`relative_dir`,`absolute_path`,`size`,`start`,`end`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.id},#{item.deleted},#{item.createTime},#{item.updateTime},#{item.contentId},#{item.index},#{item.blockId},#{item.host},#{item.baseDir},#{item.relativeDir},#{item.absolutePath},#{item.size},#{item.start},#{item.end})
        </foreach>
    </insert>

    <delete id="delete">
        delete from data_block
        where content_id=#{contentId}
    </delete>

    <select id="findDataBlocks" resultType="cn.reghao.oss.store.model.po.DataBlock">
        select *
        from data_block
        where id>#{nextId}
        limit #{pageSize}
    </select>

    <select id="findByContentId" resultType="cn.reghao.oss.store.model.po.DataBlock">
        select *
        from data_block
        where content_id=#{contentId}
    </select>
    <select id="findSubDirCount" resultType="cn.reghao.jutil.jdk.store.SubDirCount">
        select relative_dir,count(*) as total
        from data_block
        group by relative_dir
        order by total asc
    </select>

    <update id="updateParent">
        update data_block
        set relative_dir=#{relativeDir}
        where id=#{id}
    </update>
    <update id="updateBatch">
        update data_block
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="relative_dir =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    when id=#{item.id} then #{item.relativeDir}
                </foreach>
            </trim>
        </trim>
        <where>
            id in
            <foreach collection="list" item="item" separator="," open="(" close=")">
                #{item.id}
            </foreach>
        </where>
    </update>
</mapper>
