<?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.microservices.file.mapper.FileUploadSessionMapper">

    <resultMap type="com.microservices.file.domain.FileUploadSession" id="FileUploadSessionResult">
        <result property="sessionId" column="session_id"/>
        <result property="uploadId" column="upload_id"/>
        <result property="fileId" column="file_id"/>
        <result property="fileName" column="file_name"/>
        <result property="fileSize" column="file_size"/>
        <result property="fileSignature" column="file_signature"/>
        <result property="chunkSize" column="chunk_size"/>
        <result property="totalChunks" column="total_chunks"/>
        <result property="uploadedChunks" column="uploaded_chunks"/>
        <result property="uploadType" column="upload_type"/>
        <result property="hierarchy" column="hierarchy"/>
        <result property="status" column="status"/>
        <result property="tempFilePath" column="temp_file_path"/>
        <result property="errorMessage" column="error_message"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="expireTime" column="expire_time"/>
        <result property="hashType" column="hash_type"/>
        <result property="sampleChunks" column="sample_chunks"/>
    </resultMap>

    <sql id="selectFileUploadSessionVo">
        select session_id,
               upload_id,
               file_id,
               file_name,
               file_size,
               file_signature,
               chunk_size,
               total_chunks,
               uploaded_chunks,
               upload_type,
               hierarchy,
               status,
               temp_file_path,
               error_message,
               create_by,
               create_time,
               update_time,
               expire_time,
               hash_type,
               sample_chunks
        from sys_file_upload_session
    </sql>

    <select id="selectByUploadId" parameterType="String" resultMap="FileUploadSessionResult">
        <include refid="selectFileUploadSessionVo"/>
        where upload_id = #{uploadId}
    </select>

    <select id="selectSessionList" parameterType="com.microservices.file.domain.FileUploadSession"
            resultMap="FileUploadSessionResult">
        <include refid="selectFileUploadSessionVo"/>
        <where>
            <if test="fileName != null and fileName != ''">and file_name like concat('%', #{fileName}, '%')</if>
            <if test="fileSignature != null and fileSignature != ''">and file_signature = #{fileSignature}</if>
            <if test="uploadType != null and uploadType != ''">and upload_type = #{uploadType}</if>
            <if test="status != null and status != ''">and status = #{status}</if>
            <if test="hierarchy != null and hierarchy != ''">and hierarchy = #{hierarchy}</if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectExpiredSessions" resultMap="FileUploadSessionResult">
        <include refid="selectFileUploadSessionVo"/>
        where status IN ('INIT', 'UPLOADING', 'FAILED')
          and expire_time &lt; NOW()
    </select>

    <select id="selectCompletedByFileSignature" parameterType="String" resultMap="FileUploadSessionResult">
        <include refid="selectFileUploadSessionVo"/>
        where file_signature = #{fileSignature}
          and status = 'COMPLETED'
          and file_id IS NOT NULL
        limit 1
    </select>

    <select id="selectCompletedByFileSignatureAndType" resultMap="FileUploadSessionResult">
        <include refid="selectFileUploadSessionVo"/>
        where file_signature = #{fileSignature}
          and hash_type = #{hashType}
          and status = 'COMPLETED'
          and file_id IS NOT NULL
        limit 1
    </select>
    <select id="selectUploadingOrMergingByFileSignatureAndType" resultMap="FileUploadSessionResult">
        <include refid="selectFileUploadSessionVo"/>
        where file_signature = #{fileSignature}
        and hash_type = #{hashType}
        and (status = 'UPLOADING' or status = 'MERGING')
        limit 1
    </select>

    <insert id="insertSession" parameterType="com.microservices.file.domain.FileUploadSession"
            useGeneratedKeys="true" keyProperty="sessionId">
        insert into sys_file_upload_session
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="uploadId != null and uploadId != ''">upload_id,</if>
            <if test="fileId != null">file_id,</if>
            <if test="fileName != null and fileName != ''">file_name,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="fileSignature != null and fileSignature != ''">file_signature,</if>
            <if test="chunkSize != null">chunk_size,</if>
            <if test="totalChunks != null">total_chunks,</if>
            <if test="uploadedChunks != null">uploaded_chunks,</if>
            <if test="uploadType != null and uploadType != ''">upload_type,</if>
            <if test="hierarchy != null">hierarchy,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="tempFilePath != null">temp_file_path,</if>
            <if test="errorMessage != null">error_message,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="expireTime != null">expire_time,</if>
            <if test="hashType != null and hashType != ''">hash_type,</if>
            <if test="sampleChunks != null">sample_chunks,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="uploadId != null and uploadId != ''">#{uploadId},</if>
            <if test="fileId != null">#{fileId},</if>
            <if test="fileName != null and fileName != ''">#{fileName},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="fileSignature != null and fileSignature != ''">#{fileSignature},</if>
            <if test="chunkSize != null">#{chunkSize},</if>
            <if test="totalChunks != null">#{totalChunks},</if>
            <if test="uploadedChunks != null">#{uploadedChunks},</if>
            <if test="uploadType != null and uploadType != ''">#{uploadType},</if>
            <if test="hierarchy != null">#{hierarchy},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="tempFilePath != null">#{tempFilePath},</if>
            <if test="errorMessage != null">#{errorMessage},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="expireTime != null">#{expireTime},</if>
            <if test="hashType != null and hashType != ''">#{hashType},</if>
            <if test="sampleChunks != null">#{sampleChunks},</if>
        </trim>
    </insert>

    <update id="updateSession" parameterType="com.microservices.file.domain.FileUploadSession">
        update sys_file_upload_session
        <trim prefix="SET" suffixOverrides=",">
            <if test="fileId != null">file_id = #{fileId},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="uploadedChunks != null">uploaded_chunks = #{uploadedChunks},</if>
            <if test="tempFilePath != null">temp_file_path = #{tempFilePath},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="fileSignature != null and fileSignature != ''">file_signature = #{fileSignature},</if>
            <if test="hashType != null and hashType != ''">hash_type = #{hashType},</if>
            <if test="sampleChunks != null">sample_chunks = #{sampleChunks},</if>
        </trim>
        where upload_id = #{uploadId}
    </update>

    <update id="updateStatus">
        update sys_file_upload_session
        set status = #{status},
            update_time = NOW()
        where upload_id = #{uploadId}
    </update>

    <update id="incrementUploadedChunks">
        update sys_file_upload_session
        set uploaded_chunks = uploaded_chunks + 1,
            update_time = NOW()
        where upload_id = #{uploadId}
    </update>

    <delete id="deleteByUploadId" parameterType="String">
        delete
        from sys_file_upload_session
        where upload_id = #{uploadId}
    </delete>

    <delete id="deleteByUploadIds" parameterType="String">
        delete from sys_file_upload_session where upload_id in
        <foreach item="uploadId" collection="array" open="(" separator="," close=")">
            #{uploadId}
        </foreach>
    </delete>
</mapper>
