model_meta.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package model
  2. import (
  3. "one-api/common"
  4. "strconv"
  5. "gorm.io/gorm"
  6. )
  7. // Model 用于存储模型的元数据,例如描述、标签等
  8. // ModelName 字段具有唯一性约束,确保每个模型只会出现一次
  9. // Tags 字段使用逗号分隔的字符串保存标签集合,后期可根据需要扩展为 JSON 类型
  10. // Status: 1 表示启用,0 表示禁用,保留以便后续功能扩展
  11. // CreatedTime 和 UpdatedTime 使用 Unix 时间戳(秒)保存方便跨数据库移植
  12. // DeletedAt 采用 GORM 的软删除特性,便于后续数据恢复
  13. //
  14. // 该表设计遵循第三范式(3NF):
  15. // 1. 每一列都与主键(Id 或 ModelName)直接相关
  16. // 2. 不存在部分依赖(ModelName 是唯一键)
  17. // 3. 不存在传递依赖(描述、标签等都依赖于 ModelName,而非依赖于其他非主键列)
  18. // 这样既保证了数据一致性,也方便后期扩展
  19. type BoundChannel struct {
  20. Name string `json:"name"`
  21. Type int `json:"type"`
  22. }
  23. type Model struct {
  24. Id int `json:"id"`
  25. ModelName string `json:"model_name" gorm:"uniqueIndex;size:128;not null"`
  26. Description string `json:"description,omitempty" gorm:"type:text"`
  27. Tags string `json:"tags,omitempty" gorm:"type:varchar(255)"`
  28. VendorID int `json:"vendor_id,omitempty" gorm:"index"`
  29. Endpoints string `json:"endpoints,omitempty" gorm:"type:text"`
  30. Status int `json:"status" gorm:"default:1"`
  31. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  32. UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
  33. DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
  34. BoundChannels []BoundChannel `json:"bound_channels,omitempty" gorm:"-"`
  35. EnableGroups []string `json:"enable_groups,omitempty" gorm:"-"`
  36. }
  37. // Insert 创建新的模型元数据记录
  38. func (mi *Model) Insert() error {
  39. now := common.GetTimestamp()
  40. mi.CreatedTime = now
  41. mi.UpdatedTime = now
  42. return DB.Create(mi).Error
  43. }
  44. // Update 更新现有模型记录
  45. func (mi *Model) Update() error {
  46. // 仅更新需要变更的字段,避免覆盖 CreatedTime
  47. mi.UpdatedTime = common.GetTimestamp()
  48. // 排除 created_time,其余字段自动更新,避免新增字段时需要维护列表
  49. return DB.Model(&Model{}).Where("id = ?", mi.Id).Omit("created_time").Updates(mi).Error
  50. }
  51. // Delete 软删除模型记录
  52. func (mi *Model) Delete() error {
  53. return DB.Delete(mi).Error
  54. }
  55. // GetModelByName 根据模型名称查询元数据
  56. func GetModelByName(name string) (*Model, error) {
  57. var mi Model
  58. err := DB.Where("model_name = ?", name).First(&mi).Error
  59. if err != nil {
  60. return nil, err
  61. }
  62. return &mi, nil
  63. }
  64. // GetAllModels 分页获取所有模型元数据
  65. func GetAllModels(offset int, limit int) ([]*Model, error) {
  66. var models []*Model
  67. err := DB.Offset(offset).Limit(limit).Find(&models).Error
  68. return models, err
  69. }
  70. // GetBoundChannels 查询支持该模型的渠道(名称+类型)
  71. func GetBoundChannels(modelName string) ([]BoundChannel, error) {
  72. var channels []BoundChannel
  73. err := DB.Table("channels").
  74. Select("channels.name, channels.type").
  75. Joins("join abilities on abilities.channel_id = channels.id").
  76. Where("abilities.model = ? AND abilities.enabled = ?", modelName, true).
  77. Group("channels.id").
  78. Scan(&channels).Error
  79. return channels, err
  80. }
  81. // SearchModels 根据关键词和供应商搜索模型,支持分页
  82. func SearchModels(keyword string, vendor string, offset int, limit int) ([]*Model, int64, error) {
  83. var models []*Model
  84. db := DB.Model(&Model{})
  85. if keyword != "" {
  86. like := "%" + keyword + "%"
  87. db = db.Where("model_name LIKE ? OR description LIKE ? OR tags LIKE ?", like, like, like)
  88. }
  89. if vendor != "" {
  90. // 如果是数字,按供应商 ID 精确匹配;否则按名称模糊匹配
  91. if vid, err := strconv.Atoi(vendor); err == nil {
  92. db = db.Where("models.vendor_id = ?", vid)
  93. } else {
  94. db = db.Joins("JOIN vendors ON vendors.id = models.vendor_id").Where("vendors.name LIKE ?", "%"+vendor+"%")
  95. }
  96. }
  97. var total int64
  98. err := db.Count(&total).Error
  99. if err != nil {
  100. return nil, 0, err
  101. }
  102. err = db.Offset(offset).Limit(limit).Order("models.id DESC").Find(&models).Error
  103. return models, total, err
  104. }