<?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.DiskAlbumMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into my_disk_album
        (`album_id`,`album_name`,`file_type`,`cover_file_id`,`num`,`create_at`,`create_by`)
        values
        (#{albumId},#{albumName},#{fileType},#{coverFileId},#{num},#{createAt},#{createBy})
    </insert>
    <insert id="saveAlbumFiles" useGeneratedKeys="true" keyProperty="id">
        insert into my_disk_album_file
        (`album_id`,`sha256sum`,`pos`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.albumId},#{item.sha256sum},#{item.pos})
        </foreach>
    </insert>

    <update id="updateDiskAlbum">
        update my_disk_album
        set update_time=now(),cover_file_id=#{coverFileId},num=#{num}
        where album_id=#{albumId}
    </update>
    <update id="updateDiskAlbumNum">
        update my_disk_album
        set update_time=now(),num=#{num}
        where album_id=#{albumId}
    </update>
    <update id="updateDiskAlbumCover">
        update my_disk_album
        set update_time=now(),cover_file_id=#{coverFileId}
        where album_id=#{albumId}
    </update>
    <update id="updateDiskAlbumFilePos">
        update my_disk_album_file
        set pos=#{pos}
        where id=#{id}
    </update>

    <delete id="deleteDiskAlbumFiles">
        delete from my_disk_album_file
        where album_id=#{albumId} and sha256sum in
        <foreach collection="sha256sumList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
    
    <select id="findDiskAlbum" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskAlbum">
        select *
        from my_disk_album
        where album_id=#{albumId}
    </select>
    <select id="findByCreateBy" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskAlbum">
        select *
        from my_disk_album
        where create_by=#{createBy}
    </select>
    <select id="findDiskAlbumFiles" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskAlbumFile">
        select *
        from my_disk_album_file
        where album_id=#{albumId}
        order by pos asc
    </select>
    <select id="findDiskAlbumFileList" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskAlbumFile">
        select *
        from my_disk_album_file
        where album_id=#{albumId} and sha256sum in
        <foreach collection="sha256sumList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="countDiskAlbumFile" resultType="java.lang.Integer">
        select count(*)
        from my_disk_album_file
        where album_id=#{albumId}
    </select>
    <select id="findDiskAlbumFileByPage" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskAlbumFile">
        select *
        from my_disk_album_file
        where album_id=#{albumId}
        order by pos asc
    </select>
</mapper>
