model_meta.go 3.8 KB

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