<?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.aegis.file.mapper.FileMapper">

    <resultMap type="FileInfo" id="FileInfoResult">
        <id     property="id"                column="id"                 />
        <result property="filePath"          column="file_path"          />
        <result property="originalName"      column="original_name"      />
        <result property="fileSize"          column="file_size"          />
        <result property="fileType"           column="file_type"          />
        <result property="mimeType"           column="mime_type"          />
        <result property="uploadUserId"       column="upload_user_id"      />
        <result property="uploadUserName"    column="upload_user_name"   />
        <result property="fileCategory"      column="file_category"      />
        <result property="status"            column="status"             />
        <result property="downloadCount"     column="download_count"     />
        <result property="delFlag"           column="del_flag"           />
        <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="selectFileInfoVo">
        select id, file_path, original_name, file_size, file_type, mime_type,
               upload_user_id, upload_user_name, file_category, status, download_count,
               del_flag, create_by, create_time, update_by, update_time, remark
        from sys_file_info
    </sql>

    <select id="selectFileInfoById" parameterType="Long" resultMap="FileInfoResult">
        <include refid="selectFileInfoVo"/>
        where id = #{fileId} and del_flag = '0'
    </select>

    <select id="selectFileInfosByIds" parameterType="Long" resultMap="FileInfoResult">
        <include refid="selectFileInfoVo"/>
        where del_flag = '0'
        <if test="array != null and array.length > 0">
            and id in
            <foreach collection="array" item="fileId" open="(" separator="," close=")">
                #{fileId}
            </foreach>
        </if>
    </select>

    <select id="selectFileList" parameterType="FileInfo" resultMap="FileInfoResult">
        <include refid="selectFileInfoVo"/>
        <where>
            del_flag = '0'
            <if test="uploadUserId != null">
                AND upload_user_id = #{uploadUserId}
            </if>
            <if test="fileCategory != null and fileCategory != ''">
                AND file_category = #{fileCategory}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="fileType != null and fileType != ''">
                AND file_type = #{fileType}
            </if>
        </where>
        order by create_time desc
    </select>

    <insert id="insertFileInfo" parameterType="FileInfo" useGeneratedKeys="true" keyProperty="id">
        insert into sys_file_info(
            <if test="filePath != null and filePath != ''">file_path,</if>
            <if test="originalName != null and originalName != ''">original_name,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="fileType != null and fileType != ''">file_type,</if>
            <if test="mimeType != null and mimeType != ''">mime_type,</if>
            <if test="uploadUserId != null">upload_user_id,</if>
            <if test="uploadUserName != null and uploadUserName != ''">upload_user_name,</if>
            <if test="fileCategory != null and fileCategory != ''">file_category,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="downloadCount != null">download_count,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="remark != null and remark != ''">remark,</if>
            create_time,
            del_flag
        )values(
            <if test="filePath != null and filePath != ''">#{filePath},</if>
            <if test="originalName != null and originalName != ''">#{originalName},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="fileType != null and fileType != ''">#{fileType},</if>
            <if test="mimeType != null and mimeType != ''">#{mimeType},</if>
            <if test="uploadUserId != null">#{uploadUserId},</if>
            <if test="uploadUserName != null and uploadUserName != ''">#{uploadUserName},</if>
            <if test="fileCategory != null and fileCategory != ''">#{fileCategory},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="downloadCount != null">#{downloadCount},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            sysdate(),
            '0'
        )
    </insert>

    <update id="updateFileInfo" parameterType="FileInfo">
        update sys_file_info
        <set>
            <if test="filePath != null and filePath != ''">file_path = #{filePath},</if>
            <if test="originalName != null and originalName != ''">original_name = #{originalName},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="fileType != null and fileType != ''">file_type = #{fileType},</if>
            <if test="mimeType != null and mimeType != ''">mime_type = #{mimeType},</if>
            <if test="uploadUserId != null">upload_user_id = #{uploadUserId},</if>
            <if test="uploadUserName != null and uploadUserName != ''">upload_user_name = #{uploadUserName},</if>
            <if test="fileCategory != null and fileCategory != ''">file_category = #{fileCategory},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="downloadCount != null">download_count = #{downloadCount},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

</mapper>