<?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.tnb.content.app.disk.db.mapper.DiskFileMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into my_disk_file
        (`channel_code`,`file_id`,`pid`,`path`,`filename`,`file_type`,`sha256sum`,`size`,`owner`)
        values
        (#{channelCode},#{fileId},#{pid},#{path},#{filename},#{fileType},#{sha256sum},#{size},#{owner})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert into my_disk_file
        (`channel_code`,`file_id`,`pid`,`path`,`filename`,`file_type`,`sha256sum`,`size`,`owner`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.channelCode},#{item.fileId},#{item.pid},#{item.path},#{item.filename},#{item.fileType},#{item.sha256sum},#{item.size},#{item.owner})
        </foreach>
    </insert>

    <update id="updateMoveDiskFile">
        update my_disk_file
        set update_time=now(),pid=#{pid},`path`=#{path}
        where file_id=#{fileId}
    </update>
    <update id="updateDeleteDiskFiles">
        update my_disk_file
        set update_time=now(),deleted=1
        where file_id in
        <foreach collection="collection" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>
    <update id="updateDeleteByPathPrefix">
        update my_disk_file
        set update_time=now(),deleted=1
        where `owner`=#{owner} and `path` like concat(#{pathPrefix},'%')
    </update>

    <delete id="deleteByAlbumIdAndPostId">
        delete from my_disk_file
        where album_id=#{albumId} and post_id=#{postId}
    </delete>

    <select id="countByDiskQuery" resultType="java.lang.Integer">
        select count(*)
        from my_disk_file
        <where>
            deleted=0
            <if test="fileType != null">
                and file_type=#{fileType}
            </if>
            <if test="owner != null">
                and owner=#{owner}
            </if>
            <if test="pid != null">
                and pid=#{pid}
            </if>
            <if test="fileId != null">
                and file_id=#{fileId}
            </if>
            <if test="path != null">
                and path=#{path}
            </if>
            <if test="sha256sum != null">
                and sha256sum=#{sha256sum}
            </if>
        </where>
    </select>
    <select id="findDiskQueryByPage" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskFile">
        select *
        from my_disk_file
        <where>
            deleted=0
            <if test="diskQuery.fileType != null">
                and file_type=#{diskQuery.fileType}
            </if>
            <if test="diskQuery.owner != null">
                and `owner`=#{diskQuery.owner}
            </if>
            <if test="diskQuery.pid != null">
                and pid=#{diskQuery.pid}
            </if>
            <if test="diskQuery.fileId != null">
                and file_id=#{diskQuery.fileId}
            </if>
            <if test="diskQuery.path != null">
                and `path`=#{diskQuery.path}
            </if>
            <if test="diskQuery.sha256sum != null">
                and sha256sum=#{diskQuery.sha256sum}
            </if>
            <if test="diskQuery.pathPrefix != null">
                and `path` like concat(#{diskQuery.pathPrefix},'%')
            </if>
        </where>
        order by file_type,filename
    </select>
    <select id="findFileTypeCountByGroup" resultType="cn.reghao.tnb.content.app.disk.model.vo.DiskFileCount">
        select file,count(*) as total
        from my_disk_file
        group by file_type
        order by total desc
    </select>

    <select id="findByFileIds" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskFile">
        select *
        from my_disk_file
        where deleted=0 and file_id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
    <select id="findByParentIds" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskFile">
        select *
        from my_disk_file
        where deleted=0 and pid in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
    <select id="findRoot" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskFile">
        select *
        from my_disk_file
        where file_id='0'
    </select>
    <select id="findByPathPrefix" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskFile">
        select *
        from my_disk_file
        where deleted=0 and file_id!=#{fileId} and `path` like concat(#{pathPrefix})
    </select>

    <select id="countSha256sumGroupByDiskQuery" resultType="java.lang.String">
        select sha256sum
        from my_disk_file
        where deleted=0 and `owner`=#{userId} and file_type=#{fileType} and sha256sum not in (
            select sha256sum
            from my_disk_album_file A
            where exists (
                          select album_id
                          from my_disk_album B
                          where B.create_by=#{userId} and B.album_id=A.album_id
                      )
        )
        group by sha256sum
    </select>
    <select id="findSha256sumGroupByPage" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskFile">
        select *
        from my_disk_file
        where deleted=0 and `owner`=#{userId} and file_type=#{fileType} and sha256sum not in (
            select sha256sum
            from my_disk_album_file A
            where exists (
                          select album_id
                          from my_disk_album B
                          where B.create_by=#{userId} and B.album_id=A.album_id
                      )
        )
        group by sha256sum
    </select>
    <select id="findSha256sumGroup" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskFile">
        select *
        from my_disk_file
        where deleted=0 and sha256sum in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        group by sha256sum
    </select>
</mapper>
