prefill_group.go 2.0 KB

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