<?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.ai.mapper.TbKnowledgeFileMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.ai.dto.TbKnowledgeFile">
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="knowledge_id" property="knowledgeId" jdbcType="VARCHAR"/>
        <result column="file_root_id" property="fileRootId" jdbcType="VARCHAR"/>
        <result column="file_id" property="fileId" jdbcType="VARCHAR"/>
        <result column="dify_file_id" property="difyFileId" jdbcType="VARCHAR"/>
        <result column="version" property="version" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <resultMap id="KnowledgeFileVOResultMap" type="org.xyzh.api.ai.vo.KnowledgeFileVO">
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="knowledge_id" property="knowledgeId" jdbcType="VARCHAR"/>
        <result column="file_root_id" property="fileRootId" jdbcType="VARCHAR"/>
        <result column="file_id" property="fileId" jdbcType="VARCHAR"/>
        <result column="dify_file_id" property="difyFileId" jdbcType="VARCHAR"/>
        <result column="version" property="version" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
        <result column="file_name" property="fileName" jdbcType="VARCHAR"/>
        <result column="file_path" property="filePath" jdbcType="VARCHAR"/>
        <result column="file_size" property="fileSize" jdbcType="BIGINT"/>
        <result column="file_mime_type" property="fileMimeType" jdbcType="VARCHAR"/>
        <result column="file_url" property="fileUrl" jdbcType="VARCHAR"/>
        <result column="file_extension" property="fileExtension" jdbcType="VARCHAR"/>
        <result column="file_md5_hash" property="fileMd5Hash" jdbcType="VARCHAR"/>
        <result column="uploader_name" property="uploaderName" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        optsn, knowledge_id, file_root_id, file_id, dify_file_id, version,
        create_time, update_time, delete_time, deleted
    </sql>

    <insert id="insertKnowledgeFile" parameterType="org.xyzh.api.ai.dto.TbKnowledgeFile">
        INSERT INTO ai.tb_knowledge_file (
            optsn, knowledge_id, file_root_id, file_id, dify_file_id
            <if test="version != null">, version</if>
        ) VALUES (
            #{optsn}, #{knowledgeId}, #{fileRootId}, #{fileId}, #{difyFileId}
            <if test="version != null">, #{version}</if>
        )
    </insert>

    <update id="updateKnowledgeFile" parameterType="org.xyzh.api.ai.dto.TbKnowledgeFile">
        UPDATE ai.tb_knowledge_file
        <set>
            <if test="difyFileId != null">dify_file_id = #{difyFileId},</if>
            <if test="version != null">version = #{version},</if>
            update_time = now()
        </set>
        WHERE knowledge_id = #{knowledgeId} AND file_id = #{fileId} AND deleted = false
    </update>

    <update id="deleteKnowledgeFile" parameterType="org.xyzh.api.ai.dto.TbKnowledgeFile">
        UPDATE ai.tb_knowledge_file
        SET deleted = true,
            delete_time = now()
        WHERE knowledge_id = #{knowledgeId} AND file_id = #{fileId} AND deleted = false
    </update>

    <select id="selectKnowledgeFile" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_knowledge_file
        WHERE knowledge_id = #{knowledgeId} AND file_id = #{fileId} AND deleted = false
    </select>

    <select id="selectFilesByKnowledgeId" resultMap="KnowledgeFileVOResultMap">
        SELECT 
            kf.optsn, kf.knowledge_id, kf.file_root_id, kf.file_id, kf.dify_file_id, kf.version,
            kf.create_time, kf.update_time, kf.delete_time, kf.deleted,
            f.name as file_name,
            f.path as file_path,
            f.size as file_size,
            f.mime_type as file_mime_type,
            f.url as file_url,
            f.extension as file_extension,
            f.md5_hash as file_md5_hash,
            ui.username as uploader_name
        FROM ai.tb_knowledge_file kf
        INNER JOIN (
            SELECT file_root_id, MAX(version) as max_version
            FROM ai.tb_knowledge_file
            WHERE knowledge_id = #{knowledgeId} AND deleted = false
            GROUP BY file_root_id
        ) latest ON kf.file_root_id = latest.file_root_id AND kf.version = latest.max_version
        LEFT JOIN file.tb_sys_file f ON kf.file_id = f.file_id AND f.deleted = false
        LEFT JOIN sys.tb_sys_user_info ui ON f.uploader = ui.user_id AND ui.deleted = false
        WHERE kf.knowledge_id = #{knowledgeId} AND kf.deleted = false
        ORDER BY kf.create_time DESC
    </select>

    <select id="selectFileVersions" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_knowledge_file
        WHERE file_root_id = #{fileRootId} AND deleted = false
        ORDER BY version DESC
    </select>

    <select id="selectFilePage" resultMap="KnowledgeFileVOResultMap">
        SELECT 
            kf.optsn, kf.knowledge_id, kf.file_root_id, kf.file_id, kf.dify_file_id, kf.version,
            kf.create_time, kf.update_time, kf.delete_time, kf.deleted,
            f.name as file_name,
            f.path as file_path,
            f.size as file_size,
            f.mime_type as file_mime_type,
            f.url as file_url,
            f.extension as file_extension,
            f.md5_hash as file_md5_hash,
            ui.username as uploader_name
        FROM ai.tb_knowledge_file kf
        INNER JOIN (
            SELECT file_root_id, MAX(version) as max_version
            FROM ai.tb_knowledge_file
            WHERE knowledge_id = #{knowledgeId} AND deleted = false
            GROUP BY file_root_id
        ) latest ON kf.file_root_id = latest.file_root_id AND kf.version = latest.max_version
        LEFT JOIN file.tb_sys_file f ON kf.file_id = f.file_id AND f.deleted = false
        LEFT JOIN sys.tb_sys_user_info ui ON f.uploader = ui.user_id AND ui.deleted = false
        WHERE kf.knowledge_id = #{knowledgeId} AND kf.deleted = false
        ORDER BY kf.create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countFiles" resultType="long">
        SELECT COUNT(DISTINCT file_root_id)
        FROM ai.tb_knowledge_file
        WHERE knowledge_id = #{knowledgeId} AND deleted = false
    </select>

    <update id="deleteFilesByRootId">
        UPDATE ai.tb_knowledge_file
        SET deleted = true,
            delete_time = now()
        WHERE file_root_id = #{fileRootId} AND deleted = false
    </update>

    <select id="selectLatestVersion" resultType="java.lang.Integer">
        SELECT COALESCE(MAX(version), 0)
        FROM ai.tb_knowledge_file
        WHERE knowledge_id = #{knowledgeId} AND file_root_id = #{fileRootId}
    </select>

    <select id="selectLatestVersionFile" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_knowledge_file
        WHERE file_root_id = #{fileRootId} AND deleted = false
        ORDER BY version DESC
        LIMIT 1
    </select>

    <select id="selectFileVersionsWithDetail" resultMap="KnowledgeFileVOResultMap">
        SELECT 
            kf.optsn, kf.knowledge_id, kf.file_root_id, kf.file_id, kf.dify_file_id, kf.version,
            kf.create_time, kf.update_time, kf.delete_time, kf.deleted,
            f.name as file_name,
            f.path as file_path,
            f.size as file_size,
            f.mime_type as file_mime_type,
            f.url as file_url,
            f.extension as file_extension,
            f.md5_hash as file_md5_hash,
            ui.username as uploader_name
        FROM ai.tb_knowledge_file kf
        LEFT JOIN file.tb_sys_file f ON kf.file_id = f.file_id AND f.deleted = false
        LEFT JOIN sys.tb_sys_user_info ui ON f.uploader = ui.user_id AND ui.deleted = false
        WHERE kf.file_root_id = #{fileRootId} AND kf.deleted = false
        ORDER BY kf.version DESC
    </select>
</mapper>
