<?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="org.xyzh.file.mapper.FileMapper">

    <!-- 文件信息结果映射 -->
    <resultMap id="fileResultMap" type="org.xyzh.api.file.dto.TbSysFileDTO">
        <!-- 主键字段必须在最前面 -->
        <id column="file_id" property="fileId"/>
        
        <!-- BaseDTO 字段 -->
        <result column="optsn" property="optsn"/>
        <result column="creator" property="creator"/>
        <result column="updater" property="updater"/>
        <result column="dept_path" property="deptPath"/>
        <result column="remark" property="remark"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="delete_time" property="deleteTime"/>
        <result column="deleted" property="deleted"/>
        
        <!-- TbSysFileDTO 特有字段 -->
        <result column="name" property="name"/>
        <result column="path" property="path"/>
        <result column="size" property="size"/>
        <result column="type" property="type"/>
        <result column="storage_type" property="storageType"/>
        <result column="mime_type" property="mimeType"/>
        <result column="url" property="url"/>
        <result column="status" property="status"/>
        <result column="module" property="module"/>
        <result column="business_id" property="businessId"/>
        <result column="uploader" property="uploader"/>
        <result column="object_name" property="objectName"/>
        <result column="bucket_name" property="bucketName"/>
        <result column="md5_hash" property="md5Hash"/>
        <result column="extension" property="extension"/>
    </resultMap>

    <!-- 插入文件记录 -->
    <insert id="insertFile" parameterType="org.xyzh.api.file.dto.TbSysFileDTO">
        INSERT INTO file.tb_sys_file (
            <!-- 必填字段（无默认值的NOT NULL字段） -->
            optsn, file_id, name, path, size
            <!-- 可选字段（包括有默认值的字段） -->
            <if test="creator != null">, creator</if>
            <if test="updater != null">, updater</if>
            <if test="deptPath != null">, dept_path</if>
            <if test="remark != null">, remark</if>
            <if test="createTime != null">, create_time</if>
            <if test="updateTime != null">, update_time</if>
            <if test="deleteTime != null">, delete_time</if>
            <if test="deleted != null">, deleted</if>
            <if test="type != null">, type</if>
            <if test="storageType != null">, storage_type</if>
            <if test="mimeType != null">, mime_type</if>
            <if test="url != null">, url</if>
            <if test="status != null">, status</if>
            <if test="module != null">, module</if>
            <if test="businessId != null">, business_id</if>
            <if test="uploader != null">, uploader</if>
            <if test="objectName != null">, object_name</if>
            <if test="bucketName != null">, bucket_name</if>
            <if test="md5Hash != null">, md5_hash</if>
            <if test="extension != null">, extension</if>
            <if test="fileRootId != null">, file_root_id</if>
            <if test="version != null">, version</if>
        ) VALUES (
            <!-- 必填字段值 -->
            #{optsn}, #{fileId}, #{name}, #{path}, #{size}
            <!-- 可选字段值 -->
            <if test="creator != null">, #{creator}</if>
            <if test="updater != null">, #{updater}</if>
            <if test="deptPath != null">, #{deptPath}</if>
            <if test="remark != null">, #{remark}</if>
            <if test="createTime != null">, #{createTime}</if>
            <if test="updateTime != null">, #{updateTime}</if>
            <if test="deleteTime != null">, #{deleteTime}</if>
            <if test="deleted != null">, #{deleted}</if>
            <if test="type != null">, #{type}</if>
            <if test="storageType != null">, #{storageType}</if>
            <if test="mimeType != null">, #{mimeType}</if>
            <if test="url != null">, #{url}</if>
            <if test="status != null">, #{status}</if>
            <if test="module != null">, #{module}</if>
            <if test="businessId != null">, #{businessId}</if>
            <if test="uploader != null">, #{uploader}</if>
            <if test="objectName != null">, #{objectName}</if>
            <if test="bucketName != null">, #{bucketName}</if>
            <if test="md5Hash != null">, #{md5Hash}</if>
            <if test="extension != null">, #{extension}</if>
            <if test="fileRootId != null">, #{fileRootId}</if>
            <if test="version != null">, #{version}</if>
        )
    </insert>

    <!-- 根据文件ID更新文件信息 -->
    <update id="updateByFileId" parameterType="org.xyzh.api.file.dto.TbSysFileDTO">
        UPDATE file.tb_sys_file
        <set>
            <if test="updater != null">updater = #{updater},</if>
            <if test="deptPath != null">dept_path = #{deptPath},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = CURRENT_TIMESTAMP,
            <if test="name != null">name = #{name},</if>
            <if test="path != null">path = #{path},</if>
            <if test="size != null">size = #{size},</if>
            <if test="type != null">type = #{type},</if>
            <if test="storageType != null">storage_type = #{storageType},</if>
            <if test="mimeType != null">mime_type = #{mimeType},</if>
            <if test="url != null">url = #{url},</if>
            <if test="status != null">status = #{status},</if>
            <if test="module != null">module = #{module},</if>
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="uploader != null">uploader = #{uploader},</if>
            <if test="objectName != null">object_name = #{objectName},</if>
            <if test="bucketName != null">bucket_name = #{bucketName},</if>
            <if test="md5Hash != null">md5_hash = #{md5Hash},</if>
            <if test="extension != null">extension = #{extension}</if>
        </set>
        WHERE file_id = #{fileId} AND ( deleted IS NULL OR deleted = false)
    </update>

</mapper>
