document_chunk_repository.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package repository
  2. import (
  3. "github.com/2930134478/AI-CS/backend/models"
  4. "gorm.io/gorm"
  5. )
  6. // DocumentChunkRepository 封装与文档分段相关的数据库操作
  7. type DocumentChunkRepository struct {
  8. db *gorm.DB
  9. }
  10. // NewDocumentChunkRepository 创建文档分段仓库实例
  11. func NewDocumentChunkRepository(db *gorm.DB) *DocumentChunkRepository {
  12. return &DocumentChunkRepository{db: db}
  13. }
  14. // Create 创建分段
  15. func (r *DocumentChunkRepository) Create(chunk *models.DocumentChunk) error {
  16. return r.db.Create(chunk).Error
  17. }
  18. // BatchCreate 批量创建分段
  19. func (r *DocumentChunkRepository) BatchCreate(chunks []*models.DocumentChunk) error {
  20. return r.db.Create(&chunks).Error
  21. }
  22. // GetByDocumentID 获取文档的所有分段(按 chunk_index 排序)
  23. func (r *DocumentChunkRepository) GetByDocumentID(documentID uint) ([]models.DocumentChunk, error) {
  24. var chunks []models.DocumentChunk
  25. if err := r.db.Where("document_id = ?", documentID).Order("chunk_index ASC").Find(&chunks).Error; err != nil {
  26. return nil, err
  27. }
  28. return chunks, nil
  29. }
  30. // GetByDocumentIDPaginated 分页获取文档的分段
  31. func (r *DocumentChunkRepository) GetByDocumentIDPaginated(documentID uint, offset, limit int) ([]models.DocumentChunk, int64, error) {
  32. var total int64
  33. if err := r.db.Model(&models.DocumentChunk{}).Where("document_id = ?", documentID).Count(&total).Error; err != nil {
  34. return nil, 0, err
  35. }
  36. var chunks []models.DocumentChunk
  37. if err := r.db.Where("document_id = ?", documentID).
  38. Order("chunk_index ASC").
  39. Offset(offset).
  40. Limit(limit).
  41. Find(&chunks).Error; err != nil {
  42. return nil, 0, err
  43. }
  44. return chunks, total, nil
  45. }
  46. // GetByID 根据 ID 获取单个分段
  47. func (r *DocumentChunkRepository) GetByID(id uint) (*models.DocumentChunk, error) {
  48. var chunk models.DocumentChunk
  49. if err := r.db.Where("id = ?", id).First(&chunk).Error; err != nil {
  50. return nil, err
  51. }
  52. return &chunk, nil
  53. }
  54. // Update 更新分段
  55. func (r *DocumentChunkRepository) Update(chunk *models.DocumentChunk) error {
  56. return r.db.Save(chunk).Error
  57. }
  58. // UpdateContent 更新分段内容
  59. func (r *DocumentChunkRepository) UpdateContent(id uint, content string) error {
  60. return r.db.Model(&models.DocumentChunk{}).Where("id = ?", id).Updates(map[string]interface{}{
  61. "content": content,
  62. "embedding_status": "pending",
  63. }).Error
  64. }
  65. // UpdateEmbeddingStatus 更新分段的向量化状态
  66. func (r *DocumentChunkRepository) UpdateEmbeddingStatus(id uint, status string) error {
  67. return r.db.Model(&models.DocumentChunk{}).Where("id = ?", id).Update("embedding_status", status).Error
  68. }
  69. // DeleteByDocumentID 删除文档的所有分段
  70. func (r *DocumentChunkRepository) DeleteByDocumentID(documentID uint) error {
  71. return r.db.Where("document_id = ?", documentID).Delete(&models.DocumentChunk{}).Error
  72. }
  73. // DeleteByID 按 ID 删除单个分段
  74. func (r *DocumentChunkRepository) DeleteByID(id uint) error {
  75. return r.db.Delete(&models.DocumentChunk{}, id).Error
  76. }
  77. // CountByDocumentID 统计文档的分段数
  78. func (r *DocumentChunkRepository) CountByDocumentID(documentID uint) (int64, error) {
  79. var count int64
  80. if err := r.db.Model(&models.DocumentChunk{}).Where("document_id = ?", documentID).Count(&count).Error; err != nil {
  81. return 0, err
  82. }
  83. return count, nil
  84. }
  85. // GetByIDs 根据 ID 列表查询分段
  86. func (r *DocumentChunkRepository) GetByIDs(ids []uint) ([]models.DocumentChunk, error) {
  87. var chunks []models.DocumentChunk
  88. if len(ids) == 0 {
  89. return chunks, nil
  90. }
  91. if err := r.db.Where("id IN ?", ids).Find(&chunks).Error; err != nil {
  92. return nil, err
  93. }
  94. return chunks, nil
  95. }