model_meta.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package model
  2. import (
  3. "strconv"
  4. "github.com/QuantumNous/new-api/common"
  5. "gorm.io/gorm"
  6. )
  7. const (
  8. NameRuleExact = iota
  9. NameRulePrefix
  10. NameRuleContains
  11. NameRuleSuffix
  12. )
  13. type BoundChannel struct {
  14. Name string `json:"name"`
  15. Type int `json:"type"`
  16. }
  17. type Model struct {
  18. Id int `json:"id"`
  19. ModelName string `json:"model_name" gorm:"size:128;not null;uniqueIndex:uk_model_name_delete_at,priority:1"`
  20. Description string `json:"description,omitempty" gorm:"type:text"`
  21. Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"`
  22. Tags string `json:"tags,omitempty" gorm:"type:varchar(255)"`
  23. VendorID int `json:"vendor_id,omitempty" gorm:"index"`
  24. Endpoints string `json:"endpoints,omitempty" gorm:"type:text"`
  25. Status int `json:"status" gorm:"default:1"`
  26. SyncOfficial int `json:"sync_official" gorm:"default:1"`
  27. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  28. UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
  29. DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_model_name_delete_at,priority:2"`
  30. BoundChannels []BoundChannel `json:"bound_channels,omitempty" gorm:"-"`
  31. EnableGroups []string `json:"enable_groups,omitempty" gorm:"-"`
  32. QuotaTypes []int `json:"quota_types,omitempty" gorm:"-"`
  33. NameRule int `json:"name_rule" gorm:"default:0"`
  34. MatchedModels []string `json:"matched_models,omitempty" gorm:"-"`
  35. MatchedCount int `json:"matched_count,omitempty" gorm:"-"`
  36. }
  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. func IsModelNameDuplicated(id int, name string) (bool, error) {
  44. if name == "" {
  45. return false, nil
  46. }
  47. var cnt int64
  48. err := DB.Model(&Model{}).Where("model_name = ? AND id <> ?", name, id).Count(&cnt).Error
  49. return cnt > 0, err
  50. }
  51. func (mi *Model) Update() error {
  52. mi.UpdatedTime = common.GetTimestamp()
  53. return DB.Model(&Model{}).Where("id = ?", mi.Id).Updates(map[string]interface{}{
  54. "model_name": mi.ModelName,
  55. "description": mi.Description,
  56. "icon": mi.Icon,
  57. "tags": mi.Tags,
  58. "vendor_id": mi.VendorID,
  59. "endpoints": mi.Endpoints,
  60. "status": mi.Status,
  61. "sync_official": mi.SyncOfficial,
  62. "name_rule": mi.NameRule,
  63. "updated_time": mi.UpdatedTime,
  64. }).Error
  65. }
  66. func (mi *Model) Delete() error {
  67. return DB.Delete(mi).Error
  68. }
  69. func GetVendorModelCounts() (map[int64]int64, error) {
  70. var stats []struct {
  71. VendorID int64
  72. Count int64
  73. }
  74. if err := DB.Model(&Model{}).
  75. Select("vendor_id as vendor_id, count(*) as count").
  76. Group("vendor_id").
  77. Scan(&stats).Error; err != nil {
  78. return nil, err
  79. }
  80. m := make(map[int64]int64, len(stats))
  81. for _, s := range stats {
  82. m[s.VendorID] = s.Count
  83. }
  84. return m, nil
  85. }
  86. func GetAllModels(offset int, limit int) ([]*Model, error) {
  87. var models []*Model
  88. err := DB.Order("id DESC").Offset(offset).Limit(limit).Find(&models).Error
  89. return models, err
  90. }
  91. func GetBoundChannelsByModelsMap(modelNames []string) (map[string][]BoundChannel, error) {
  92. result := make(map[string][]BoundChannel)
  93. if len(modelNames) == 0 {
  94. return result, nil
  95. }
  96. type row struct {
  97. Model string
  98. Name string
  99. Type int
  100. }
  101. var rows []row
  102. err := DB.Table("channels").
  103. Select("abilities.model as model, channels.name as name, channels.type as type").
  104. Joins("JOIN abilities ON abilities.channel_id = channels.id").
  105. Where("abilities.model IN ? AND abilities.enabled = ?", modelNames, true).
  106. Distinct().
  107. Scan(&rows).Error
  108. if err != nil {
  109. return nil, err
  110. }
  111. for _, r := range rows {
  112. result[r.Model] = append(result[r.Model], BoundChannel{Name: r.Name, Type: r.Type})
  113. }
  114. return result, nil
  115. }
  116. func SearchModels(keyword string, vendor string, offset int, limit int) ([]*Model, int64, error) {
  117. var models []*Model
  118. db := DB.Model(&Model{})
  119. if keyword != "" {
  120. like := "%" + keyword + "%"
  121. db = db.Where("model_name LIKE ? OR description LIKE ? OR tags LIKE ?", like, like, like)
  122. }
  123. if vendor != "" {
  124. if vid, err := strconv.Atoi(vendor); err == nil {
  125. db = db.Where("models.vendor_id = ?", vid)
  126. } else {
  127. db = db.Joins("JOIN vendors ON vendors.id = models.vendor_id").Where("vendors.name LIKE ?", "%"+vendor+"%")
  128. }
  129. }
  130. var total int64
  131. if err := db.Count(&total).Error; err != nil {
  132. return nil, 0, err
  133. }
  134. if err := db.Order("models.id DESC").Offset(offset).Limit(limit).Find(&models).Error; err != nil {
  135. return nil, 0, err
  136. }
  137. return models, total, nil
  138. }