ai_config_repository.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package repository
  2. import (
  3. "github.com/2930134478/AI-CS/backend/models"
  4. "gorm.io/gorm"
  5. )
  6. // AIConfigRepository 封装与 AI 配置相关的数据库操作。
  7. type AIConfigRepository struct {
  8. db *gorm.DB
  9. }
  10. // NewAIConfigRepository 创建 AI 配置仓库实例。
  11. func NewAIConfigRepository(db *gorm.DB) *AIConfigRepository {
  12. return &AIConfigRepository{db: db}
  13. }
  14. // Create 创建新的 AI 配置记录。
  15. func (r *AIConfigRepository) Create(config *models.AIConfig) error {
  16. return r.db.Create(config).Error
  17. }
  18. // GetByID 根据主键查询 AI 配置。
  19. func (r *AIConfigRepository) GetByID(id uint) (*models.AIConfig, error) {
  20. var config models.AIConfig
  21. if err := r.db.First(&config, id).Error; err != nil {
  22. return nil, err
  23. }
  24. return &config, nil
  25. }
  26. // GetActiveByUserID 查询指定用户的活跃 AI 配置(按模型类型筛选)。
  27. func (r *AIConfigRepository) GetActiveByUserID(userID uint, modelType string) (*models.AIConfig, error) {
  28. var config models.AIConfig
  29. query := r.db.Where("user_id = ? AND is_active = ?", userID, true)
  30. if modelType != "" {
  31. query = query.Where("model_type = ?", modelType)
  32. }
  33. if err := query.Order("created_at desc").First(&config).Error; err != nil {
  34. return nil, err
  35. }
  36. return &config, nil
  37. }
  38. // ListByUserID 查询指定用户的所有 AI 配置。
  39. func (r *AIConfigRepository) ListByUserID(userID uint) ([]models.AIConfig, error) {
  40. var configs []models.AIConfig
  41. if err := r.db.Where("user_id = ?", userID).Order("created_at desc").Find(&configs).Error; err != nil {
  42. return nil, err
  43. }
  44. return configs, nil
  45. }
  46. // CountByUserID 统计指定用户拥有的 AI 配置数量。
  47. func (r *AIConfigRepository) CountByUserID(userID uint) (int64, error) {
  48. var count int64
  49. if err := r.db.Model(&models.AIConfig{}).Where("user_id = ?", userID).Count(&count).Error; err != nil {
  50. return 0, err
  51. }
  52. return count, nil
  53. }
  54. // ReassignUser 将某用户名下的 AI 配置归属转移到另一位用户。
  55. func (r *AIConfigRepository) ReassignUser(fromUserID, toUserID uint) (int64, error) {
  56. res := r.db.Model(&models.AIConfig{}).
  57. Where("user_id = ?", fromUserID).
  58. Update("user_id", toUserID)
  59. if res.Error != nil {
  60. return 0, res.Error
  61. }
  62. return res.RowsAffected, nil
  63. }
  64. // UpdateFields 更新 AI 配置的指定字段。
  65. func (r *AIConfigRepository) UpdateFields(id uint, values map[string]interface{}) error {
  66. if len(values) == 0 {
  67. return nil
  68. }
  69. return r.db.Model(&models.AIConfig{}).Where("id = ?", id).Updates(values).Error
  70. }
  71. // Delete 删除 AI 配置。
  72. func (r *AIConfigRepository) Delete(id uint) error {
  73. return r.db.Delete(&models.AIConfig{}, id).Error
  74. }
  75. // ListPublic 查询所有开放的模型配置(供访客选择)。
  76. func (r *AIConfigRepository) ListPublic(modelType string) ([]models.AIConfig, error) {
  77. var configs []models.AIConfig
  78. query := r.db.Where("is_active = ? AND is_public = ?", true, true)
  79. if modelType != "" {
  80. query = query.Where("model_type = ?", modelType)
  81. }
  82. if err := query.Order("provider, model").Find(&configs).Error; err != nil {
  83. return nil, err
  84. }
  85. return configs, nil
  86. }