50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package system
|
|
|
|
import (
|
|
"errors"
|
|
"go.uber.org/zap"
|
|
"sundynix-go/global"
|
|
"sundynix-go/model/system"
|
|
)
|
|
|
|
type SysAiConfigService struct{}
|
|
|
|
// Create 创建AI配置
|
|
func (s *SysAiConfigService) Create(cfg *system.SysAiConfig) error {
|
|
return global.DB.Create(cfg).Error
|
|
}
|
|
|
|
// Update 更新AI配置
|
|
func (s *SysAiConfigService) Update(cfg *system.SysAiConfig) error {
|
|
return global.DB.Updates(cfg).Error
|
|
}
|
|
|
|
// SetActive 设置激活配置
|
|
func (s *SysAiConfigService) SetActive(id string) error {
|
|
// 先将所有的设为 0
|
|
err := global.DB.Model(&system.SysAiConfig{}).Where("1 = 1").Update("is_active", 0).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// 再将指定的设为 1
|
|
return global.DB.Model(&system.SysAiConfig{}).Where("id = ?", id).Update("is_active", 1).Error
|
|
}
|
|
|
|
// GetActiveAiConfig 获取当前激活的AI配置
|
|
func (s *SysAiConfigService) GetActiveAiConfig() (*system.SysAiConfig, error) {
|
|
var cfg system.SysAiConfig
|
|
err := global.DB.Where("is_active = 1").First(&cfg).Error
|
|
if err != nil {
|
|
global.Logger.Error("No active AI Config found", zap.Error(err))
|
|
return nil, errors.New("无激活状态的AI配置")
|
|
}
|
|
return &cfg, nil
|
|
}
|
|
|
|
// GetList 获取所有配置
|
|
func (s *SysAiConfigService) GetList() ([]system.SysAiConfig, error) {
|
|
var list []system.SysAiConfig
|
|
err := global.DB.Order("created_at desc").Find(&list).Error
|
|
return list, err
|
|
}
|