feat(channels): 渠道管理 - 可视化排序与别名并修复注册及外观清理 (#444)

This commit is contained in:
Ray authored and GitHub committed 2026-09-08 20:56:42 +08:00
1 parent 381edf37e3
commit 8360b2c933
41 files changed
+1485 -148

No files matched your search

@@ -0,0 +1,62 @@
package repository
import (
"errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"infinite-canvas/backend/internal/model"
"slices"
"time"
)
var ErrChannelOrderChanged = errors.New("列表已发生变化,请重新打开排序后再保存")
// 全量顺序在事务中保存;拒绝过期快照,避免覆盖另一管理员的调整。
func (r *Repository) SaveChannelOrder(channelID string, ids, expected []string) error {
return r.db.Transaction(func(tx *gorm.DB) error {
var current []string
var target any = &model.ModelChannel{}
query := tx.Model(target).Where("scope = ?", model.ChannelScopeSystem)
if channelID != "" {
if err := r.lockSystemChannelForModelMutation(tx, channelID); err != nil {
return err
}
target = &model.ChannelModel{}
query = tx.Model(target).Where("channel_id = ?", channelID)
}
if tx.Dialector.Name() == "postgres" {
// Lock in a stable order, then read a fresh ordered snapshot after waiting.
// PostgreSQL can otherwise return pre-wait ORDER BY results after another sort commits.
var locked []string
if err := query.Session(&gorm.Session{}).Clauses(clause.Locking{Strength: "UPDATE"}).Order("id asc").Pluck("id", &locked).Error; err != nil {
return err
}
}
if err := query.Order("sort_order asc, created_at asc, id asc").Pluck("id", &current).Error; err != nil {
return err
}
if !slices.Equal(current, expected) || len(ids) != len(current) {
return ErrChannelOrderChanged
}
allowed := make(map[string]bool, len(current))
for _, id := range current {
allowed[id] = true
}
for _, id := range ids {
if !allowed[id] {
return ErrChannelOrderChanged
}
delete(allowed, id)
}
now := time.Now()
for index, id := range ids {
if err := tx.Model(target).Where("id = ?", id).Updates(map[string]any{"sort_order": index, "updated_at": now}).Error; err != nil {
return err
}
}
if channelID != "" {
return refreshChannelModelNames(tx, channelID, now)
}
return nil
})
}
@@ -0,0 +1,45 @@
package repository
import (
"time"
"infinite-canvas/backend/internal/model"
"gorm.io/gorm"
)
func (r *Repository) UpdateSystemChannelPresentation(channelID string, alias *string, sortOrder *int, now time.Time) error {
updates := map[string]any{"updated_at": now}
if alias != nil {
updates["public_alias"] = *alias
}
if sortOrder != nil {
updates["sort_order"] = *sortOrder
}
result := r.db.Model(&model.ModelChannel{}).Where("id = ? AND scope = ?", channelID, model.ChannelScopeSystem).Updates(updates)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}
// 排序是展示配置,不重写价格、能力版本或模型标识。
func (r *Repository) UpdateChannelModelSort(channelID, modelID string, sortOrder int, now time.Time) error {
return r.db.Transaction(func(tx *gorm.DB) error {
if err := r.lockSystemChannelForModelMutation(tx, channelID); err != nil {
return err
}
result := tx.Model(&model.ChannelModel{}).Where("channel_id = ? AND id = ?", channelID, modelID).
Updates(map[string]any{"sort_order": sortOrder, "updated_at": now})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return refreshChannelModelNames(tx, channelID, now)
})
}
+2 -2
View File
@@ -70,7 +70,7 @@ type AdminRedeemCodeRow struct {
func (r *Repository) ChannelModels(channelID string, includeDisabled bool) ([]model.ChannelModel, error) {
var items []model.ChannelModel
query := r.db.Where("channel_id = ?", channelID).Order("created_at asc")
query := r.db.Where("channel_id = ?", channelID).Order("sort_order asc, created_at asc, id asc")
if !includeDisabled {
query = query.Where("enabled = ?", true)
}
@@ -329,7 +329,7 @@ func refreshChannelModelNames(tx *gorm.DB, channelID string, now time.Time) erro
var names []string
if err := tx.Model(&model.ChannelModel{}).
Where("channel_id = ? AND enabled = ?", channelID, true).
Order("created_at asc").
Order("sort_order asc, created_at asc, id asc").
Pluck("model_key", &names).Error; err != nil {
return err
}
+3 -3
View File
@@ -755,7 +755,7 @@ func (r *Repository) TaskLogs(userID string, taskID string) ([]model.TaskLog, er
func (r *Repository) SystemChannels(includeDisabled bool) ([]model.ModelChannel, error) {
var channels []model.ModelChannel
query := r.db.Order("created_at asc").Where("scope = ?", model.ChannelScopeSystem)
query := r.db.Order("sort_order asc, created_at asc, id asc").Where("scope = ?", model.ChannelScopeSystem)
if !includeDisabled {
query = query.Where("enabled = ?", true)
}
@@ -775,7 +775,7 @@ func (r *Repository) AdminSystemChannels(keyword string, status string, limit in
query := r.db.Model(&model.ModelChannel{}).Where("scope = ?", model.ChannelScopeSystem)
if value := strings.TrimSpace(keyword); value != "" {
pattern := "%" + strings.ToLower(value) + "%"
query = query.Where("lower(name) LIKE ? OR lower(base_url) LIKE ?", pattern, pattern)
query = query.Where("lower(name) LIKE ? OR lower(public_alias) LIKE ? OR lower(base_url) LIKE ?", pattern, pattern, pattern)
}
if status == "enabled" {
query = query.Where("enabled = ?", true)
@@ -785,7 +785,7 @@ func (r *Repository) AdminSystemChannels(keyword string, status string, limit in
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
if err := query.Order("created_at desc").Limit(limit).Offset(offset).Find(&channels).Error; err != nil {
if err := query.Order("sort_order asc, created_at asc, id asc").Limit(limit).Offset(offset).Find(&channels).Error; err != nil {
return nil, 0, err
}
return channels, total, nil