document_repository.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package repository
  2. import (
  3. "github.com/2930134478/AI-CS/backend/models"
  4. "gorm.io/gorm"
  5. )
  6. // DocumentRepository 封装与文档相关的数据库操作
  7. type DocumentRepository struct {
  8. db *gorm.DB
  9. }
  10. // NewDocumentRepository 创建文档仓库实例
  11. func NewDocumentRepository(db *gorm.DB) *DocumentRepository {
  12. return &DocumentRepository{db: db}
  13. }
  14. // Create 创建新的文档
  15. func (r *DocumentRepository) Create(doc *models.Document) error {
  16. return r.db.Create(doc).Error
  17. }
  18. // GetByID 根据ID查询文档
  19. func (r *DocumentRepository) GetByID(id uint) (*models.Document, error) {
  20. var doc models.Document
  21. if err := r.db.Where("id = ?", id).First(&doc).Error; err != nil {
  22. return nil, err
  23. }
  24. return &doc, nil
  25. }
  26. // GetByKnowledgeBaseID 根据知识库ID查询文档列表
  27. func (r *DocumentRepository) GetByKnowledgeBaseID(knowledgeBaseID uint, page, pageSize int, keyword string, status string) ([]models.Document, int64, error) {
  28. var docs []models.Document
  29. var total int64
  30. query := r.db.Model(&models.Document{}).Where("knowledge_base_id = ?", knowledgeBaseID)
  31. // 关键词搜索
  32. if keyword != "" {
  33. query = query.Where("title LIKE ? OR content LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
  34. }
  35. // 状态过滤
  36. if status != "" {
  37. query = query.Where("status = ?", status)
  38. }
  39. // 统计总数
  40. if err := query.Count(&total).Error; err != nil {
  41. return nil, 0, err
  42. }
  43. // 分页查询
  44. offset := (page - 1) * pageSize
  45. if err := query.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&docs).Error; err != nil {
  46. return nil, 0, err
  47. }
  48. return docs, total, nil
  49. }
  50. // Update 更新文档
  51. func (r *DocumentRepository) Update(doc *models.Document) error {
  52. return r.db.Save(doc).Error
  53. }
  54. // Delete 删除文档
  55. func (r *DocumentRepository) Delete(id uint) error {
  56. return r.db.Delete(&models.Document{}, id).Error
  57. }
  58. // DeleteByKnowledgeBaseID 根据知识库ID删除所有文档
  59. func (r *DocumentRepository) DeleteByKnowledgeBaseID(knowledgeBaseID uint) error {
  60. return r.db.Where("knowledge_base_id = ?", knowledgeBaseID).Delete(&models.Document{}).Error
  61. }
  62. // CountByKnowledgeBaseID 统计知识库的文档数量
  63. func (r *DocumentRepository) CountByKnowledgeBaseID(knowledgeBaseID uint) (int64, error) {
  64. var count int64
  65. if err := r.db.Model(&models.Document{}).Where("knowledge_base_id = ?", knowledgeBaseID).Count(&count).Error; err != nil {
  66. return 0, err
  67. }
  68. return count, nil
  69. }
  70. // GetByIDs 根据ID列表查询文档
  71. func (r *DocumentRepository) GetByIDs(ids []uint) ([]models.Document, error) {
  72. var docs []models.Document
  73. if err := r.db.Where("id IN ?", ids).Find(&docs).Error; err != nil {
  74. return nil, err
  75. }
  76. return docs, nil
  77. }
  78. // UpdateEmbeddingStatus 更新文档的向量化状态
  79. func (r *DocumentRepository) UpdateEmbeddingStatus(id uint, status string) error {
  80. return r.db.Model(&models.Document{}).Where("id = ?", id).Update("embedding_status", status).Error
  81. }
  82. // UpdateStatus 更新文档的状态
  83. func (r *DocumentRepository) UpdateStatus(id uint, status string) error {
  84. return r.db.Model(&models.Document{}).Where("id = ?", id).Update("status", status).Error
  85. }