2025-08-04 02:54:37 +08:00
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"one-api/common"
|
|
|
|
|
|
|
|
|
|
|
|
"gorm.io/datatypes"
|
2025-08-05 22:26:19 +08:00
|
|
|
|
"gorm.io/gorm"
|
2025-08-04 02:54:37 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// PrefillGroup 用于存储可复用的“组”信息,例如模型组、标签组、端点组等。
|
|
|
|
|
|
// Name 字段保持唯一,用于在前端下拉框中展示。
|
|
|
|
|
|
// Type 字段用于区分组的类别,可选值如:model、tag、endpoint。
|
|
|
|
|
|
// Items 字段使用 JSON 数组保存对应类型的字符串集合,示例:
|
|
|
|
|
|
// ["gpt-4o", "gpt-3.5-turbo"]
|
|
|
|
|
|
// 设计遵循 3NF,避免冗余,提供灵活扩展能力。
|
|
|
|
|
|
|
|
|
|
|
|
type PrefillGroup struct {
|
|
|
|
|
|
Id int `json:"id"`
|
2025-08-05 22:26:19 +08:00
|
|
|
|
Name string `json:"name" gorm:"size:64;not null;uniqueIndex:uk_prefill_name,where:deleted_at IS NULL"`
|
2025-08-04 02:54:37 +08:00
|
|
|
|
Type string `json:"type" gorm:"size:32;index;not null"`
|
|
|
|
|
|
Items datatypes.JSON `json:"items" gorm:"type:json"`
|
|
|
|
|
|
Description string `json:"description,omitempty" gorm:"type:varchar(255)"`
|
|
|
|
|
|
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
|
|
|
|
|
UpdatedTime int64 `json:"updated_time" gorm:"bigint"`
|
2025-08-05 22:26:19 +08:00
|
|
|
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
2025-08-04 02:54:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Insert 新建组
|
|
|
|
|
|
func (g *PrefillGroup) Insert() error {
|
|
|
|
|
|
now := common.GetTimestamp()
|
|
|
|
|
|
g.CreatedTime = now
|
|
|
|
|
|
g.UpdatedTime = now
|
|
|
|
|
|
return DB.Create(g).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
🚀 refactor: migrate vendor-count aggregation to model layer & align frontend logic
Summary
• Backend
– Moved duplicate-name validation and total vendor-count aggregation from controllers (`controller/model_meta.go`, `controller/vendor_meta.go`, `controller/prefill_group.go`) to model layer (`model/model_meta.go`, `model/vendor_meta.go`, `model/prefill_group.go`).
– Added `GetVendorModelCounts()` and `Is*NameDuplicated()` helpers; controllers now call these instead of duplicating queries.
– API response for `/api/models` now returns `vendor_counts` with per-vendor totals across all pages, plus `all` summary.
– Removed redundant checks and unused imports, eliminating `go vet` warnings.
• Frontend
– `useModelsData.js` updated to consume backend-supplied `vendor_counts`, calculate the `all` total once, and drop legacy client-side counting logic.
– Simplified initial data flow: first render now triggers only one models request.
– Deleted obsolete `updateVendorCounts` helper and related comments.
– Ensured search flow also sets `vendorCounts`, keeping tab badges accurate.
Why
This refactor enforces single-responsibility (aggregation in model layer), delivers consistent totals irrespective of pagination, and removes redundant client queries, leading to cleaner code and better performance.
2025-08-06 01:40:08 +08:00
|
|
|
|
// IsPrefillGroupNameDuplicated 检查组名称是否重复(排除自身 ID)
|
|
|
|
|
|
func IsPrefillGroupNameDuplicated(id int, name string) (bool, error) {
|
|
|
|
|
|
if name == "" {
|
|
|
|
|
|
return false, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
var cnt int64
|
|
|
|
|
|
err := DB.Model(&PrefillGroup{}).Where("name = ? AND id <> ?", name, id).Count(&cnt).Error
|
|
|
|
|
|
return cnt > 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-04 02:54:37 +08:00
|
|
|
|
// Update 更新组
|
|
|
|
|
|
func (g *PrefillGroup) Update() error {
|
|
|
|
|
|
g.UpdatedTime = common.GetTimestamp()
|
|
|
|
|
|
return DB.Save(g).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteByID 根据 ID 删除组
|
|
|
|
|
|
func DeletePrefillGroupByID(id int) error {
|
|
|
|
|
|
return DB.Delete(&PrefillGroup{}, id).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetAllPrefillGroups 获取全部组,可按类型过滤(为空则返回全部)
|
|
|
|
|
|
func GetAllPrefillGroups(groupType string) ([]*PrefillGroup, error) {
|
|
|
|
|
|
var groups []*PrefillGroup
|
|
|
|
|
|
query := DB.Model(&PrefillGroup{})
|
|
|
|
|
|
if groupType != "" {
|
|
|
|
|
|
query = query.Where("type = ?", groupType)
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := query.Order("updated_time DESC").Find(&groups).Error; err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return groups, nil
|
|
|
|
|
|
}
|