prefill_group.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package model
  2. import (
  3. "one-api/common"
  4. "gorm.io/datatypes"
  5. )
  6. // PrefillGroup 用于存储可复用的“组”信息,例如模型组、标签组、端点组等。
  7. // Name 字段保持唯一,用于在前端下拉框中展示。
  8. // Type 字段用于区分组的类别,可选值如:model、tag、endpoint。
  9. // Items 字段使用 JSON 数组保存对应类型的字符串集合,示例:
  10. // ["gpt-4o", "gpt-3.5-turbo"]
  11. // 设计遵循 3NF,避免冗余,提供灵活扩展能力。
  12. type PrefillGroup struct {
  13. Id int `json:"id"`
  14. Name string `json:"name" gorm:"uniqueIndex;size:64;not null"`
  15. Type string `json:"type" gorm:"size:32;index;not null"`
  16. Items datatypes.JSON `json:"items" gorm:"type:json"`
  17. Description string `json:"description,omitempty" gorm:"type:varchar(255)"`
  18. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  19. UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
  20. }
  21. // Insert 新建组
  22. func (g *PrefillGroup) Insert() error {
  23. now := common.GetTimestamp()
  24. g.CreatedTime = now
  25. g.UpdatedTime = now
  26. return DB.Create(g).Error
  27. }
  28. // Update 更新组
  29. func (g *PrefillGroup) Update() error {
  30. g.UpdatedTime = common.GetTimestamp()
  31. return DB.Save(g).Error
  32. }
  33. // DeleteByID 根据 ID 删除组
  34. func DeletePrefillGroupByID(id int) error {
  35. return DB.Delete(&PrefillGroup{}, id).Error
  36. }
  37. // GetAllPrefillGroups 获取全部组,可按类型过滤(为空则返回全部)
  38. func GetAllPrefillGroups(groupType string) ([]*PrefillGroup, error) {
  39. var groups []*PrefillGroup
  40. query := DB.Model(&PrefillGroup{})
  41. if groupType != "" {
  42. query = query.Where("type = ?", groupType)
  43. }
  44. if err := query.Order("updated_time DESC").Find(&groups).Error; err != nil {
  45. return nil, err
  46. }
  47. return groups, nil
  48. }