<?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="com.yushu.system.mapper.SysFileMapper">
    
    <resultMap type="SysFileInfo" id="SysFileResult">
        <result property="fileId"    column="file_id"    />
        <result property="folderId"    column="folder_id"    />
        <result property="fileName"    column="file_name"    />
        <result property="originalName"    column="original_name"    />
        <result property="filePath"    column="file_path"    />
        <result property="fileSize"    column="file_size"    />
        <result property="fileType"    column="file_type"    />
        <result property="mimeType"    column="mime_type"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="downloadCount"    column="download_count"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectSysFileVo">
        select file_id, folder_id, file_name, original_name, file_path, file_size, file_type, mime_type, user_id, user_name, download_count, create_by, create_time, update_by, update_time, remark from sys_file
    </sql>

    <select id="selectSysFileList" parameterType="SysFileInfo" resultMap="SysFileResult">
        <include refid="selectSysFileVo"/>
        <where>  
            <if test="folderId != null "> and folder_id = #{folderId}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
            <if test="fileType != null  and fileType != ''"> and file_type = #{fileType}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectSysFileByFileId" parameterType="Long" resultMap="SysFileResult">
        <include refid="selectSysFileVo"/>
        where file_id = #{fileId}
    </select>
        
    <insert id="insertSysFile" parameterType="SysFileInfo" useGeneratedKeys="true" keyProperty="fileId">
        insert into sys_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="folderId != null">folder_id,</if>
            <if test="fileName != null and fileName != ''">file_name,</if>
            <if test="originalName != null">original_name,</if>
            <if test="filePath != null and filePath != ''">file_path,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="fileType != null">file_type,</if>
            <if test="mimeType != null">mime_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="downloadCount != null">download_count,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="folderId != null">#{folderId},</if>
            <if test="fileName != null and fileName != ''">#{fileName},</if>
            <if test="originalName != null">#{originalName},</if>
            <if test="filePath != null and filePath != ''">#{filePath},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="fileType != null">#{fileType},</if>
            <if test="mimeType != null">#{mimeType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="downloadCount != null">#{downloadCount},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateSysFile" parameterType="SysFileInfo">
        update sys_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="folderId != null">folder_id = #{folderId},</if>
            <if test="fileName != null and fileName != ''">file_name = #{fileName},</if>
            <if test="originalName != null">original_name = #{originalName},</if>
            <if test="filePath != null and filePath != ''">file_path = #{filePath},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="fileType != null">file_type = #{fileType},</if>
            <if test="mimeType != null">mime_type = #{mimeType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="downloadCount != null">download_count = #{downloadCount},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where file_id = #{fileId}
    </update>

    <delete id="deleteSysFileByFileId" parameterType="Long">
        delete from sys_file where file_id = #{fileId}
    </delete>

    <delete id="deleteSysFileByFileIds" parameterType="String">
        delete from sys_file where file_id in 
        <foreach item="fileId" collection="array" open="(" separator="," close=")">
            #{fileId}
        </foreach>
    </delete>
    
    <select id="selectFileCountByFolderId" parameterType="Long" resultType="int">
        select count(*) from sys_file where folder_id = #{folderId}
    </select>
    
    <update id="increaseDownloadCount" parameterType="Long">
        update sys_file set download_count = download_count + 1 where file_id = #{fileId}
    </update>
    
    <delete id="deleteFilesByFolderId" parameterType="Long">
        delete from sys_file where folder_id = #{folderId}
    </delete>

</mapper>
