prefill_group.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // IsPrefillGroupNameDuplicated 检查组名称是否重复(排除自身 ID)
  31. func IsPrefillGroupNameDuplicated(id int, name string) (bool, error) {
  32. if name == "" {
  33. return false, nil
  34. }
  35. var cnt int64
  36. err := DB.Model(&PrefillGroup{}).Where("name = ? AND id <> ?", name, id).Count(&cnt).Error
  37. return cnt > 0, err
  38. }
  39. // Update 更新组
  40. func (g *PrefillGroup) Update() error {
  41. g.UpdatedTime = common.GetTimestamp()
  42. return DB.Save(g).Error
  43. }
  44. // DeleteByID 根据 ID 删除组
  45. func DeletePrefillGroupByID(id int) error {
  46. return DB.Delete(&PrefillGroup{}, id).Error
  47. }
  48. // GetAllPrefillGroups 获取全部组,可按类型过滤(为空则返回全部)
  49. func GetAllPrefillGroups(groupType string) ([]*PrefillGroup, error) {
  50. var groups []*PrefillGroup
  51. query := DB.Model(&PrefillGroup{})
  52. if groupType != "" {
  53. query = query.Where("type = ?", groupType)
  54. }
  55. if err := query.Order("updated_time DESC").Find(&groups).Error; err != nil {
  56. return nil, err
  57. }
  58. return groups, nil
  59. }