<?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.backendsys.modules.upload.dao.SysFileDao">

    <sql id="includeFileSimple">
        f.id,
        COALESCE(f.category_id, '') category_id,
        COALESCE(fc.category_name, '') category_name,
        f.upload_id,
        f.name,
        f.object_key,
        f.md5,
        f.url,
        f.upload_time,
        f.create_time,
        f.update_time
    </sql>
    <sql id="includeFile">
        f.id,
        COALESCE(f.category_id, '') category_id,
        COALESCE(fc.category_name, '') category_name,
        COALESCE(f.request_id, '') request_id,
        COALESCE(f.upload_id, '') upload_id,
        COALESCE(f.upload_chunk_count, '') upload_chunk_count,
        COALESCE(f.upload_chunk_index, '') upload_chunk_index,
        f.user_id,
        fu.username,
        f.name,
        f.content_type,
        f.url,
        COALESCE(f.url_thumb, '') url_thumb,
        COALESCE(f.object_key, '') object_key,
        f.size,
        f.md5,
        f.target,
        f.upload_time,
        f.create_time,
        f.update_time
    </sql>

    <resultMap id="resultMapFileSimple" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="category_id" column="category_id" javaType="java.lang.Long" />
        <result property="category_name" column="category_name" />
        <result property="upload_id" column="upload_id" />
        <result property="name" column="name" />
        <result property="object_key" column="object_key" />
        <result property="url" column="url" />
        <result property="md5" column="md5" />
        <result property="upload_time" column="upload_time" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>
    <resultMap id="resultMapFileList" type="com.backendsys.modules.upload.entity.SysFile">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="category_id" column="category_id" javaType="java.lang.Long" />
        <result property="category_name" column="category_name" />
        <result property="request_id" column="request_id" />
        <result property="upload_id" column="upload_id" />
        <result property="upload_chunk_count" column="upload_chunk_count" javaType="java.lang.Integer" />
        <result property="upload_chunk_index" column="upload_chunk_index" javaType="java.lang.Integer" />
        <result property="user_id" column="user_id" javaType="java.lang.Long" />
        <result property="username" column="username" />
        <result property="name" column="name" />
        <result property="content_type" column="content_type" />
        <result property="url" column="url" />
        <result property="url_thumb" column="url_thumb" />
        <result property="object_key" column="object_key" />
        <result property="size" column="size" javaType="java.lang.Long" />
        <result property="md5" column="md5" />
        <result property="target" column="target" javaType="java.lang.Integer" />
        <result property="upload_time" column="upload_time" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <select id="selectUploadFileSimple" resultMap="resultMapFileSimple">
        SELECT <include refid="includeFileSimple" />
        FROM sys_file f
        LEFT JOIN sys_file_category fc ON f.category_id = fc.id
        <where>
            <if test="user_id != null and user_id != ''">
                AND f.user_id = #{user_id}
            </if>
            <if test="upload_id != ''">
                AND f.upload_id = #{upload_id}
            </if>
            <if test="object_key != null and object_key != ''">
                AND f.object_key = #{object_key}
            </if>
            <if test="md5 != null and md5 != ''">
                AND f.md5 = #{md5}
            </if>
        </where>
        ORDER BY f.upload_time DESC
    </select>

    <select id="selectUploadFileList" resultMap="resultMapFileList">
        SELECT <include refid="includeFile" />
        FROM sys_file f
        LEFT JOIN sys_file_category fc ON f.category_id = fc.id
        LEFT JOIN sys_user fu ON f.user_id = fu.id
        <where>
            <if test="name != null and name != ''">
                AND f.name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="category_id != null and category_id != ''">
                AND f.category_id = #{category_id}
            </if>
            <if test="user_id != null and user_id != ''">
                AND f.user_id = #{user_id}
            </if>
            <if test="upload_id != null and upload_id != ''">
                AND f.upload_id = #{upload_id}
            </if>
            <if test="object_key != null and object_key != ''">
                AND f.object_key = #{object_key}
            </if>
            <if test="target != null and target != ''">
                AND f.target = #{target}
            </if>
        </where>
        ORDER BY f.upload_time DESC
    </select>

    <update id="updateFile" parameterType="com.backendsys.modules.upload.entity.SysFile"
        useGeneratedKeys="true" keyProperty="id">
        UPDATE sys_file
        <set>
            <trim suffixOverrides="," suffix=" ">
                name = #{name},
                category_id = #{category_id},
            </trim>
        </set>
        WHERE id = #{id}
    </update>

    <update id="updateCompleteFile" parameterType="com.backendsys.modules.upload.entity.SysFile"
        useGeneratedKeys="true" keyProperty="id">
        UPDATE sys_file
        SET upload_id = null, upload_chunk_count = null, upload_chunk_index = null
        <if test="request_id != null and request_id != ''">, request_id = #{request_id}</if>
        <if test="url != null and url != ''">, url = #{url}</if>
        <if test="url_thumb != null and url_thumb != ''">, url_thumb = #{url_thumb}</if>
        WHERE id = #{id}
    </update>

</mapper>