<?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.FileUploadChunkMapper">

    <resultMap type="com.microservices.file.domain.FileUploadChunk" id="FileUploadChunkResult">
        <result property="chunkId" column="chunk_id"/>
        <result property="uploadId" column="upload_id"/>
        <result property="chunkNumber" column="chunk_number"/>
        <result property="chunkSize" column="chunk_size"/>
        <result property="chunkPath" column="chunk_path"/>
        <result property="status" column="status"/>
        <result property="uploadTime" column="upload_time"/>
    </resultMap>

    <sql id="selectFileUploadChunkVo">
        select chunk_id,
               upload_id,
               chunk_number,
               chunk_size,
               chunk_path,
               status,
               upload_time
        from sys_file_upload_chunk
    </sql>

    <select id="selectByUploadIdAndChunkNumber" resultMap="FileUploadChunkResult">
        <include refid="selectFileUploadChunkVo"/>
        where upload_id = #{uploadId}
          and chunk_number = #{chunkNumber}
    </select>

    <select id="selectByUploadId" parameterType="String" resultMap="FileUploadChunkResult">
        <include refid="selectFileUploadChunkVo"/>
        where upload_id = #{uploadId}
        order by chunk_number
    </select>

    <select id="selectChunkList" parameterType="com.microservices.file.domain.FileUploadChunk"
            resultMap="FileUploadChunkResult">
        <include refid="selectFileUploadChunkVo"/>
        <where>
            <if test="uploadId != null and uploadId != ''">and upload_id = #{uploadId}</if>
            <if test="chunkNumber != null">and chunk_number = #{chunkNumber}</if>
            <if test="status != null and status != ''">and status = #{status}</if>
        </where>
        ORDER BY chunk_number
    </select>

    <select id="countUploadedChunks" parameterType="String" resultType="int">
        select count(*)
        from sys_file_upload_chunk
        where upload_id = #{uploadId}
          and status = 'UPLOADED'
    </select>

    <select id="selectByUploadIdAndStatus" resultMap="FileUploadChunkResult">
        <include refid="selectFileUploadChunkVo"/>
        where upload_id = #{uploadId}
          and status = #{status}
        order by chunk_number
    </select>

    <insert id="insertChunk" parameterType="com.microservices.file.domain.FileUploadChunk"
            useGeneratedKeys="true" keyProperty="chunkId">
        insert into sys_file_upload_chunk
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="uploadId != null and uploadId != ''">upload_id,</if>
            <if test="chunkNumber != null">chunk_number,</if>
            <if test="chunkSize != null">chunk_size,</if>
            <if test="chunkPath != null and chunkPath != ''">chunk_path,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="uploadTime != null">upload_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="uploadId != null and uploadId != ''">#{uploadId},</if>
            <if test="chunkNumber != null">#{chunkNumber},</if>
            <if test="chunkSize != null">#{chunkSize},</if>
            <if test="chunkPath != null and chunkPath != ''">#{chunkPath},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="uploadTime != null">#{uploadTime},</if>
        </trim>
    </insert>

    <insert id="batchInsertChunks">
        insert into sys_file_upload_chunk (upload_id, chunk_number, chunk_size, status)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.uploadId}, #{item.chunkNumber}, #{item.chunkSize}, #{item.status})
        </foreach>
    </insert>

    <update id="updateChunk" parameterType="com.microservices.file.domain.FileUploadChunk">
        update sys_file_upload_chunk
        <trim prefix="SET" suffixOverrides=",">
            <if test="chunkSize != null">chunk_size = #{chunkSize},</if>
            <if test="chunkPath != null and chunkPath != ''">chunk_path = #{chunkPath},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="uploadTime != null">upload_time = #{uploadTime},</if>
        </trim>
        where chunk_id = #{chunkId}
    </update>

    <update id="updateStatusByUploadId">
        update sys_file_upload_chunk
        set status = #{status}
        where upload_id = #{uploadId}
    </update>

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

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