<?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.FileMetaMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into file_meta
        (`id`,`deleted`,`create_time`,`update_time`,`object_name`,`object_id`,`content_id`,`pid`,`filename`,`size`,`file_type`,`content_type`,`sha256sum`,`upload_by`,`scope`)
        values
        (#{id},#{deleted},#{createTime},#{updateTime},#{objectName},#{objectId},#{contentId},#{pid},#{filename},#{size},#{fileType},#{contentType},#{sha256sum},#{uploadBy},#{scope})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert into file_meta
        (`id`,`deleted`,`create_time`,`update_time`,`object_name`,`object_id`,`content_id`,`pid`,`filename`,`size`,`file_type`,`content_type`,`sha256sum`,`upload_by`,`scope`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.id},#{item.deleted},#{item.createTime},#{item.updateTime},#{item.objectName},#{item.objectId},#{item.contentId},#{item.pid},#{item.filename},#{item.size},#{item.fileType},#{item.contentType},#{item.sha256sum},#{item.uploadBy},#{item.scope})
        </foreach>
    </insert>

    <delete id="delete">
        delete from file_meta
        where object_id=#{objectId}
    </delete>

    <update id="update">
        update file_meta
        set object_name=#{objectName}
        where object_id=#{objectId}
    </update>
    <update id="updateScopeByObjectName">
        update file_meta
        set scope=#{scope}
        where object_name=#{objectName}
    </update>
    <update id="updateScopeByObjectNames">
        update file_meta
        set scope=#{scope}
        where object_name in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>
    <update id="updateBatch">
        update file_meta
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="content_id =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    when object_id=#{item.objectId} then #{item.contentId}
                </foreach>
            </trim>
        </trim>
        <where>
            object_id in
            <foreach collection="list" item="item" separator="," open="(" close=")">
                #{item.objectId}
            </foreach>
        </where>
    </update>

    <select id="count" resultType="java.lang.Integer">
        select count(*) from file_meta
    </select>
    <select id="findAll" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select * from file_meta
    </select>
    <select id="findAll0" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select file_meta.* from file_meta
        inner join bucket
        on bucket.name=#{bucket}
        and object_name regexp #{regex}
        limit #{max}
    </select>
    <!-- TODO ${prefix} 和 #{prefix} 的区别 -->
    <select id="findAll1" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select file_meta.* from file_meta
        inner join bucket
        on bucket.name=#{bucket}
        and object_name regexp concat_ws('', '^', '${prefix}', '([^/])+/?$')
        limit #{max}
    </select>
    <select id="findAll2" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select file_meta.* from file_meta
        inner join bucket
        on bucket.name=#{bucket}
        and object_name regexp concat_ws('', '^', '${prefix}', '([^/])+/?$')
        and id > (select id from file_meta where object_name=#{start})
        limit #{max}
    </select>

    <select id="findBySha256sum" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select * from file_meta
        where sha256sum=#{sha256sum}
        order by create_time asc
        limit 1
    </select>
    <select id="findByObjectName" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select *
        from file_meta
        where object_name=#{objectName}
    </select>
    <select id="findByObjectNames" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select *
        from file_meta
        where object_name in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="findByObjectId" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select *
        from file_meta
        where object_id=#{objectId}
    </select>
    <select id="findByContentId" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select *
        from file_meta
        where content_id=#{contentId}
    </select>
    <select id="findObjectMeta" resultType="cn.reghao.oss.store.api.dto.ObjectMeta">
        select file_meta.size,file_meta.content_type,file_meta.object_name,file_meta.object_id,file_meta.scope as scope,file_meta.upload_by,
        data_block.absolute_path
        from file_meta
        inner join data_block
        on file_meta.content_id=data_block.content_id
        and file_meta.object_name=#{objectName}
    </select>
    <select id="findObjectMetaById" resultType="cn.reghao.oss.store.api.dto.ObjectMeta">
        select file_meta.size,file_meta.content_type,file_meta.object_name,file_meta.object_id,file_meta.scope as scope,file_meta.upload_by,
        data_block.absolute_path
        from file_meta
        inner join data_block
        on file_meta.content_id=data_block.content_id
        and file_meta.object_id=#{objectId}
    </select>
    <select id="findObjectNames" resultType="java.lang.String">
        select object_name
        from file_meta
        where object_id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <!--*************************************************************************************************************-->
    <update id="updateFilename">
        update file_meta
        set update_time=now() and filename=#{filename}
        where object_id=#{objectId}
    </update>
    <update id="updateParent">
        update file_meta
        set update_time=now() and pid=#{pid}
        where object_id=#{objectId}
    </update>

    <select id="countByPid" resultType="java.lang.Integer">
        select count(*)
        from file_meta
        where pid=#{pid}
    </select>
    <select id="findFileInfoByPage" resultType="cn.reghao.oss.store.api.dto.FileInfo">
        select file_meta.object_id as file_id,file_meta.filename,file_meta.file_type as type,file_meta.size,file_meta.update_time,file_type.icon
        from file_meta
        inner join file_type
        on file_meta.deleted=0 and file_meta.file_type=file_type.id and file_meta.pid=#{pid}
        order by file_type asc
    </select>
    <select id="findFileInfo1ByPage" resultType="cn.reghao.oss.store.api.dto.FileInfo">
        select file_meta.object_id as file_id,file_meta.filename,file_meta.file_type as type,file_meta.size,file_meta.update_time,file_type.icon_large as icon
        from file_meta
        inner join file_type
        on file_meta.deleted=0 and file_meta.file_type=file_type.id and file_meta.pid=#{pid}
        order by file_type asc
    </select>

    <select id="countDeletedFiles" resultType="java.lang.Integer">
        select count(*)
        from file_meta
        where deleted=1
    </select>
    <select id="findDeletedFileByPage" resultType="cn.reghao.oss.store.api.dto.DeleteFile">
        select file_meta.object_id as file_id,file_meta.filename,file_meta.file_type as type,file_meta.size,file_meta.update_time,file_type.icon
        from file_meta
        inner join file_type
        on file_meta.deleted=1 and file_meta.file_type=file_type.id
        order by file_type asc
    </select>

    <select id="findDirectories" resultType="cn.reghao.oss.store.api.dto.FileInfo">
        select file_meta.object_id as file_id,file_meta.filename,file_meta.file_type as type,file_meta.size,file_meta.update_time,file_type.icon
        from file_meta
        inner join file_type
        on file_meta.deleted=0 and file_meta.file_type=file_type.id and file_meta.file_type=1000 and file_meta.pid=#{pid}
        order by file_type asc
    </select>
    <select id="findByFilename" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select *
        from file_meta
        where pid=#{pid} and filename like concat(#{filename},'%')
    </select>
    <select id="findFileInfos" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select *
        from file_meta
        where object_id in
        <foreach collection="objectIds" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
    <select id="findByPid" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select *
        from file_meta
        where pid=#{pid}
    </select>

    <select id="countByKeyword" resultType="java.lang.Integer">
        select count(*)
        from file_meta
        where filename like concat('%',#{keyword},'%')
    </select>
    <select id="findKeywordByPage" resultType="cn.reghao.oss.store.api.dto.FileInfo">
        select file_meta.object_id as file_id,file_meta.filename,file_meta.file_type as type,file_meta.size,file_meta.update_time,file_type.icon
        from file_meta
        inner join file_type
        on file_meta.deleted=0 and file_meta.file_type=file_type.id and file_meta.filename like concat('%',#{keyword},'%')
        order by file_type asc
    </select>

    <select id="findFileMetaByPage" resultType="cn.reghao.oss.store.model.po.FileMeta">
        select * from file_meta
        where file_type!=1000
    </select>
    <select id="findObjectPrefix" resultType="java.lang.String">
        select * from file_meta
        where file_type==1000
    </select>
</mapper>
