feat(models): 对齐 vivid 新改动——模型删除改软删,重导不丢计数/定价

- model_configs 加 deleted_at(gorm 软删),List/Get 自动过滤,
  GetAny/Unscoped 用于复活与孤儿清理路径
- 渠道同步/删渠道/管理端删除模型的孤儿清理全部走软删
- 导入时若命中软删行:UpdateAny + deleted_at=NULL 复活,
  generation_count 与原定价保留(vivid a42f32c 同语义)
- provider 时代残留仍物理删除(DeleteHard),不留软删垃圾
- OrphanModelIDs 跳过已软删行,避免重复清扫
- btn-primary 禁用态改浅灰填充(vivid d636105)

核对过已等价的:vivid 僵尸任务兜底(我们的 ResumeStaleJob+
PurgeStale+MarkRefunded 同语义)、产物 MIME 魔数嗅探、
retention 按创建时间、渠道同步弹窗(开关+别名)均已覆盖。

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
2026-10-27 21:48:45 +08:00
co-authored by Devin
parent 76510d34c9
commit f3b9e570c2
5 changed files with 54 additions and 9 deletions
+5
View File
@@ -5,6 +5,7 @@ import (
"time" "time"
"gorm.io/datatypes" "gorm.io/datatypes"
"gorm.io/gorm"
) )
type User struct { type User struct {
@@ -160,6 +161,10 @@ type ModelConfig struct {
// successful generation. Independent of the event_log (which is subject to // successful generation. Independent of the event_log (which is subject to
// retention / manual clearing), so the admin "次数" is a true running total. // retention / manual clearing), so the admin "次数" is a true running total.
GenerationCount int64 `gorm:"not null;default:0"` GenerationCount int64 `gorm:"not null;default:0"`
// DeletedAt soft-deletes the row (vivid parity): hidden from every normal
// path, but the row + its counters survive so a re-import revives it
// without losing generation_count or pricing.
DeletedAt gorm.DeletedAt `gorm:"index"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }
+2 -1
View File
@@ -212,13 +212,14 @@ func (r *ChannelRepository) ChannelIDsByModel(ctx context.Context, modelID strin
} }
// OrphanModelIDs returns local model ids that no channel binds anymore. // OrphanModelIDs returns local model ids that no channel binds anymore.
// Soft-deleted rows are skipped — they're already gone from every live path.
func (r *ChannelRepository) OrphanModelIDs(ctx context.Context) ([]string, error) { func (r *ChannelRepository) OrphanModelIDs(ctx context.Context) ([]string, error) {
var ids []string var ids []string
err := r.db.WithContext(ctx). err := r.db.WithContext(ctx).
Table("model_configs"). Table("model_configs").
Select("model_configs.id"). Select("model_configs.id").
Joins("LEFT JOIN channel_models ON channel_models.model_id = model_configs.id"). Joins("LEFT JOIN channel_models ON channel_models.model_id = model_configs.id").
Where("channel_models.model_id IS NULL"). Where("channel_models.model_id IS NULL AND model_configs.deleted_at IS NULL").
Scan(&ids).Error Scan(&ids).Error
return ids, err return ids, err
} }
+34 -2
View File
@@ -44,6 +44,16 @@ func (r *ModelRepository) Get(ctx context.Context, modelID string) (*model.Model
return &item, nil return &item, nil
} }
// GetAny includes soft-deleted rows — the import/revive path and orphan
// cleanup need to see rows normal queries hide.
func (r *ModelRepository) GetAny(ctx context.Context, modelID string) (*model.ModelConfig, error) {
var item model.ModelConfig
if err := r.db.WithContext(ctx).Unscoped().First(&item, "(alias <> '' AND alias = ?) OR (alias = '' AND id = ?)", modelID, modelID).Error; err != nil {
return nil, err
}
return &item, nil
}
func (r *ModelRepository) NameMap(ctx context.Context) (map[string]string, error) { func (r *ModelRepository) NameMap(ctx context.Context) (map[string]string, error) {
items, err := r.List(ctx) items, err := r.List(ctx)
if err != nil { if err != nil {
@@ -72,18 +82,40 @@ func (r *ModelRepository) Create(ctx context.Context, item *model.ModelConfig) e
} }
func (r *ModelRepository) Update(ctx context.Context, modelID string, patch map[string]any) (*model.ModelConfig, error) { func (r *ModelRepository) Update(ctx context.Context, modelID string, patch map[string]any) (*model.ModelConfig, error) {
return r.update(ctx, modelID, patch, false)
}
// UpdateAny can reach soft-deleted rows (revive path: patch deleted_at=NULL).
func (r *ModelRepository) UpdateAny(ctx context.Context, modelID string, patch map[string]any) (*model.ModelConfig, error) {
return r.update(ctx, modelID, patch, true)
}
func (r *ModelRepository) update(ctx context.Context, modelID string, patch map[string]any, anyState bool) (*model.ModelConfig, error) {
patch["updated_at"] = time.Now() patch["updated_at"] = time.Now()
if err := r.db.WithContext(ctx).Model(&model.ModelConfig{}).Where("id = ?", modelID).Updates(patch).Error; err != nil { q := r.db.WithContext(ctx)
if anyState {
q = q.Unscoped()
}
if err := q.Model(&model.ModelConfig{}).Where("id = ?", modelID).Updates(patch).Error; err != nil {
return nil, err return nil, err
} }
var item model.ModelConfig var item model.ModelConfig
if err := r.db.WithContext(ctx).First(&item, "id = ?", modelID).Error; err != nil { if err := q.First(&item, "id = ?", modelID).Error; err != nil {
return nil, err return nil, err
} }
return &item, nil return &item, nil
} }
// Delete soft-deletes the model (gorm.DeletedAt): the row keeps its counters
// and pricing so re-importing it later revives the same record.
func (r *ModelRepository) Delete(ctx context.Context, modelID string) (int64, error) { func (r *ModelRepository) Delete(ctx context.Context, modelID string) (int64, error) {
res := r.db.WithContext(ctx).Delete(&model.ModelConfig{}, "id = ?", modelID) res := r.db.WithContext(ctx).Delete(&model.ModelConfig{}, "id = ?", modelID)
return res.RowsAffected, res.Error return res.RowsAffected, res.Error
} }
// DeleteHard physically removes the row — only for legacy/provider-era rows
// that must not linger as soft-deleted junk.
func (r *ModelRepository) DeleteHard(ctx context.Context, modelID string) (int64, error) {
res := r.db.WithContext(ctx).Unscoped().Delete(&model.ModelConfig{}, "id = ?", modelID)
return res.RowsAffected, res.Error
}
+9 -4
View File
@@ -381,11 +381,12 @@ func (s *ChannelService) PurgeOrphanModels(ctx context.Context) (int, error) {
} }
n := 0 n := 0
for _, id := range ids { for _, id := range ids {
m, gerr := s.models.Get(ctx, id) m, gerr := s.models.GetAny(ctx, id)
if gerr != nil || m == nil || m.Provider == "channel" { if gerr != nil || m == nil || m.Provider == "channel" {
continue continue
} }
if _, derr := s.models.Delete(ctx, id); derr == nil { // provider 时代残留行没有复活路径,直接物理删除,不留软删垃圾。
if _, derr := s.models.DeleteHard(ctx, id); derr == nil {
n++ n++
} }
} }
@@ -454,7 +455,8 @@ func (s *ChannelService) ImportModels(ctx context.Context, channelID string, spe
return fmt.Errorf("别名 %s 被多个上游模型复用", localID) return fmt.Errorf("别名 %s 被多个上游模型复用", localID)
} }
seenAliases[localID] = upID seenAliases[localID] = upID
existing, gerr := s.models.Get(ctx, localID) // GetAny: 软删行也纳入——重导同一模型时复活原行,计数/定价不丢。
existing, gerr := s.models.GetAny(ctx, localID)
if gerr != nil && !errors.Is(gerr, gorm.ErrRecordNotFound) { if gerr != nil && !errors.Is(gerr, gorm.ErrRecordNotFound) {
existing = nil existing = nil
} }
@@ -481,7 +483,10 @@ func (s *ChannelService) ImportModels(ctx context.Context, channelID string, spe
return fmt.Errorf("model %s: %w", localID, err) return fmt.Errorf("model %s: %w", localID, err)
} }
} else { } else {
if _, err := s.models.Update(ctx, localID, patch); err != nil { if local.DeletedAt.Valid {
patch["deleted_at"] = nil // revive
}
if _, err := s.models.UpdateAny(ctx, localID, patch); err != nil {
return fmt.Errorf("model %s: %w", localID, err) return fmt.Errorf("model %s: %w", localID, err)
} }
} }
+2
View File
@@ -478,6 +478,8 @@ button:not(:disabled), [role="button"]:not([aria-disabled="true"]) { cursor: poi
} }
.btn-primary:hover:not(:disabled) { background: #27272a; box-shadow: 0 10px 22px -8px rgb(9 9 11 / 0.5); } .btn-primary:hover:not(:disabled) { background: #27272a; box-shadow: 0 10px 22px -8px rgb(9 9 11 / 0.5); }
.btn-primary:active:not(:disabled) { transform: scale(0.97); box-shadow: none; } .btn-primary:active:not(:disabled) { transform: scale(0.97); box-shadow: none; }
/* 禁用态:浅灰填充(vivid 同款),不再是变淡的黑块 */
.btn-primary:disabled { background: var(--surface-2); color: var(--fg-3); border-color: transparent; box-shadow: none; opacity: 1; }
.btn-soft { .btn-soft {
@apply inline-flex items-center justify-center gap-1.5 rounded-lg text-xs font-semibold @apply inline-flex items-center justify-center gap-1.5 rounded-lg text-xs font-semibold
disabled:opacity-50 disabled:cursor-not-allowed px-3 py-1.5; disabled:opacity-50 disabled:cursor-not-allowed px-3 py-1.5;