model_meta.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }
  36. // Insert 创建新的模型元数据记录
  37. func (mi *Model) Insert() error {
  38. now := common.GetTimestamp()
  39. mi.CreatedTime = now
  40. mi.UpdatedTime = now
  41. return DB.Create(mi).Error
  42. }
  43. // Update 更新现有模型记录
  44. func (mi *Model) Update() error {
  45. // 仅更新需要变更的字段,避免覆盖 CreatedTime
  46. mi.UpdatedTime = common.GetTimestamp()
  47. // 排除 created_time,其余字段自动更新,避免新增字段时需要维护列表
  48. return DB.Model(&Model{}).Where("id = ?", mi.Id).Omit("created_time").Updates(mi).Error
  49. }
  50. // Delete 软删除模型记录
  51. func (mi *Model) Delete() error {
  52. return DB.Delete(mi).Error
  53. }
  54. // GetModelByName 根据模型名称查询元数据
  55. func GetModelByName(name string) (*Model, error) {
  56. var mi Model
  57. err := DB.Where("model_name = ?", name).First(&mi).Error
  58. if err != nil {
  59. return nil, err
  60. }
  61. return &mi, nil
  62. }
  63. // GetAllModels 分页获取所有模型元数据
  64. func GetAllModels(offset int, limit int) ([]*Model, error) {
  65. var models []*Model
  66. err := DB.Offset(offset).Limit(limit).Find(&models).Error
  67. return models, err
  68. }
  69. // GetBoundChannels 查询支持该模型的渠道(名称+类型)
  70. func GetBoundChannels(modelName string) ([]BoundChannel, error) {
  71. var channels []BoundChannel
  72. err := DB.Table("channels").
  73. Select("channels.name, channels.type").
  74. Joins("join abilities on abilities.channel_id = channels.id").
  75. Where("abilities.model = ? AND abilities.enabled = ?", modelName, true).
  76. Group("channels.id").
  77. Scan(&channels).Error
  78. return channels, err
  79. }
  80. // SearchModels 根据关键词和供应商搜索模型,支持分页
  81. func SearchModels(keyword string, vendor string, offset int, limit int) ([]*Model, int64, error) {
  82. var models []*Model
  83. db := DB.Model(&Model{})
  84. if keyword != "" {
  85. like := "%" + keyword + "%"
  86. db = db.Where("model_name LIKE ? OR description LIKE ? OR tags LIKE ?", like, like, like)
  87. }
  88. if vendor != "" {
  89. // 如果是数字,按供应商 ID 精确匹配;否则按名称模糊匹配
  90. if vid, err := strconv.Atoi(vendor); err == nil {
  91. db = db.Where("models.vendor_id = ?", vid)
  92. } else {
  93. db = db.Joins("JOIN vendors ON vendors.id = models.vendor_id").Where("vendors.name LIKE ?", "%"+vendor+"%")
  94. }
  95. }
  96. var total int64
  97. err := db.Count(&total).Error
  98. if err != nil {
  99. return nil, 0, err
  100. }
  101. err = db.Offset(offset).Limit(limit).Order("models.id DESC").Find(&models).Error
  102. return models, total, err
  103. }