package com.lingyue.graph.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lingyue.graph.entity.TextChunk;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 文本分块数据访问层
 *
 * @author lingyue
 * @since 2026-01-15
 */
@Mapper
public interface TextChunkRepository extends BaseMapper<TextChunk> {

    /**
     * 根据文档ID查询所有分块
     */
    @Select("SELECT * FROM text_chunks WHERE document_id = #{documentId} ORDER BY chunk_index")
    List<TextChunk> findByDocumentId(@Param("documentId") String documentId);

    /**
     * 根据文本存储ID查询所有分块
     */
    @Select("SELECT * FROM text_chunks WHERE text_storage_id = #{textStorageId} ORDER BY chunk_index")
    List<TextChunk> findByTextStorageId(@Param("textStorageId") String textStorageId);

    /**
     * 根据文档ID删除所有分块
     */
    @Delete("DELETE FROM text_chunks WHERE document_id = #{documentId}")
    int deleteByDocumentId(@Param("documentId") String documentId);

    /**
     * 根据文本存储ID删除所有分块
     */
    @Delete("DELETE FROM text_chunks WHERE text_storage_id = #{textStorageId}")
    int deleteByTextStorageId(@Param("textStorageId") String textStorageId);

    /**
     * 统计文档的分块数量
     */
    @Select("SELECT COUNT(*) FROM text_chunks WHERE document_id = #{documentId}")
    int countByDocumentId(@Param("documentId") String documentId);
}
