Quellcode durchsuchen

feat: auto-sync channel models to models table on creation/update

When a channel is created or updated, automatically sync its models to the
models table if they don't exist. Models created this way are marked with
SyncOfficial=0 to prevent official sync from overwriting them.
feat/alipay-payment
fengsilin vor 1 Monat
Ursprung
Commit
2a2647c062
1 geänderte Dateien mit 33 neuen und 0 gelöschten Zeilen
  1. +33
    -0
      model/ability.go

+ 33
- 0
model/ability.go Datei anzeigen

@@ -146,6 +146,39 @@ func GetChannel(group string, model string, retry int) (*Channel, error) {

func (channel *Channel) AddAbilities(tx *gorm.DB) error {
models_ := strings.Split(channel.Models, ",")

// === 自动同步模型到 models 表 ===
// 选择数据库连接(优先使用事务)
syncDB := DB
if tx != nil {
syncDB = tx
}
for _, modelName := range models_ {
modelName = strings.TrimSpace(modelName)
if modelName == "" {
continue
}
// 检查 models 表是否存在该模型
var existingModel Model
err := syncDB.Where("model_name = ?", modelName).First(&existingModel).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
// 自动创建模型记录
newModel := &Model{
ModelName: modelName,
Status: 1, // 启用状态
SyncOfficial: 0, // 标记为用户模型,不被官方同步覆盖
}
if err := newModel.Insert(); err != nil {
common.SysLog(fmt.Sprintf("failed to auto-create model %s: %v", modelName, err))
}
} else {
common.SysLog(fmt.Sprintf("failed to check model %s existence: %v", modelName, err))
}
}
}
// === 自动同步结束 ===

groups_ := strings.Split(channel.Group, ",")
abilitySet := make(map[string]struct{})
abilities := make([]Ability, 0, len(models_))


Laden…
Abbrechen
Speichern