<?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.file.app.db.mapper.LocalFileMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into file_local_file
        (`object_name`,`object_id`,`absolute_path`,`filename`,`file_type`,`content_type`,`sha256sum`,`size`,`owner`)
        values
        (#{objectName},#{objectId},#{absolutePath},#{filename},#{fileType},#{contentType},#{sha256sum},#{size},#{owner})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert into file_local_file
        (`object_name`,`object_id`,`absolute_path`,`filename`,`file_type`,`content_type`,`sha256sum`,`size`,`owner`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.objectName},#{item.objectId},#{item.absolutePath},#{item.filename},#{item.fileType},#{item.contentType},#{item.sha256sum},#{item.size},#{item.owner})
        </foreach>
    </insert>

    <select id="countByFileQuery" resultType="java.lang.Integer">
        select count(*)
        from file_local_file
        <where>
            deleted=0
            <if test="fileType != null">
                and file_type=#{fileType}
            </if>
            <if test="owner != null">
                and `owner`=#{owner}
            </if>
            <if test="objectId != null">
                and object_id=#{objectId}
            </if>
            <if test="objectName != null">
                and object_name=#{objectName}
            </if>
            <if test="sha256sum != null">
                and sha256sum=#{sha256sum}
            </if>
        </where>
    </select>
    <select id="findFileQueryByPage" resultType="cn.reghao.tnb.file.app.model.po.LocalFile">
        select *
        from file_local_file
        <where>
            deleted=0
            <if test="fileQuery.fileType != null">
                and file_type=#{fileQuery.fileType}
            </if>
            <if test="fileQuery.owner != null">
                and `owner`=#{fileQuery.owner}
            </if>
            <if test="fileQuery.objectId != null">
                and object_id=#{fileQuery.objectId}
            </if>
            <if test="fileQuery.objectName != null">
                and object_name=#{fileQuery.objectName}
            </if>
            <if test="fileQuery.sha256sum != null">
                and sha256sum=#{fileQuery.sha256sum}
            </if>
        </where>
        order by create_time desc
    </select>
</mapper>
