<?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">
<!--
PreviewKnowledge Mapper
-->
<mapper namespace="com.iflytek.astron.console.toolkit.mapper.knowledge.PreviewKnowledgeMapper">
    <resultMap id="resultMap" type="com.iflytek.astron.console.toolkit.entity.table.knowledge.MysqlPreviewKnowledge">
        <result column="content" property="content"
                typeHandler="com.iflytek.astron.console.toolkit.handler.MySqlJsonHandler"
        />
    </resultMap>
    <!--
        Query preview knowledge list by fileId
        Ordered by seq_id to preserve insertion order
        @param fileId the file identifier
        @return List<MysqlPreviewKnowledge>
    -->
    <select id="findByFileId" resultType="com.iflytek.astron.console.toolkit.entity.table.knowledge.MysqlPreviewKnowledge">
        SELECT * FROM preview_knowledge
        WHERE file_id = #{fileId}
        ORDER BY seq_id ASC
    </select>

    <!--
        Delete preview knowledge by fileId
        @param fileId the file identifier
        @return int number of rows deleted
    -->
    <delete id="deleteByFileId">
        DELETE FROM preview_knowledge WHERE file_id = #{fileId}
    </delete>

    <!--
        Count preview knowledge entries by fileId
        @param fileId the file identifier
        @return Long total count
    -->
    <select id="countByFileId" resultType="java.lang.Long">
        SELECT COUNT(*) FROM preview_knowledge WHERE file_id = #{fileId}
    </select>

    <!--
        Query preview knowledge list by multiple fileIds
        Ordered by file_id and seq_id to preserve insertion order
        @param fileIds list of file identifiers
        @return List<MysqlPreviewKnowledge>
    -->
    <select id="findByFileIdIn" resultType="com.iflytek.astron.console.toolkit.entity.table.knowledge.MysqlPreviewKnowledge">
        SELECT * FROM preview_knowledge WHERE file_id IN
        <foreach collection="fileIds" item="fileId" open="(" separator="," close=")">
            #{fileId}
        </foreach>
        ORDER BY file_id ASC, seq_id ASC
    </select>

    <!--
        Count preview knowledge entries by multiple fileIds
        @param fileIds list of file identifiers
        @return Long total count
    -->
    <select id="countByFileIdIn" resultType="java.lang.Long">
        SELECT COUNT(*) FROM preview_knowledge WHERE file_id IN
        <foreach collection="fileIds" item="fileId" open="(" separator="," close=")">
            #{fileId}
        </foreach>
    </select>

    <!--
        Query preview knowledge list by fileIds and audit type
        Only entries whose auditSuggest field is 'block' or 'review' will be returned
        Ordered by file_id and seq_id to preserve insertion order
        @param fileIds list of file identifiers
        @return List<MysqlPreviewKnowledge>
    -->
    <select id="findByFileIdInAndAuditType" resultType="com.iflytek.astron.console.toolkit.entity.table.knowledge.MysqlPreviewKnowledge">
        SELECT * FROM preview_knowledge WHERE file_id IN
        <foreach collection="fileIds" item="fileId" open="(" separator="," close=")">
            #{fileId}
        </foreach>
        AND JSON_EXTRACT(content, '$.auditSuggest') IN ('block', 'review')
        ORDER BY file_id ASC, seq_id ASC
    </select>

    <!--
        Batch insert preview knowledge entries
        Note: seq_id is auto-generated by database AUTO_INCREMENT
        @param list collection of MysqlPreviewKnowledge entities
        @return int number of rows inserted
    -->
    <insert id="insertBatch" useGeneratedKeys="true" keyProperty="seqId" keyColumn="seq_id">
        INSERT INTO preview_knowledge (id, file_id, content, char_count, created_at, updated_at)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.id}, #{item.fileId}, #{item.content}, #{item.charCount}, #{item.createdAt}, #{item.updatedAt})
        </foreach>
    </insert>

</mapper>