You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.4 KiB
88 lines
2.4 KiB
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// AIProviderInsert 插入AI供应商
|
|
func AIProviderInsert(ctx context.Context, db *gorm.DB, provider *AIProvider) (int64, error) {
|
|
result := db.WithContext(ctx).Create(provider)
|
|
if result.Error != nil {
|
|
return 0, result.Error
|
|
}
|
|
return provider.Id, nil
|
|
}
|
|
|
|
// AIProviderFindOne 根据ID查询AI供应商
|
|
func AIProviderFindOne(ctx context.Context, db *gorm.DB, id int64) (*AIProvider, error) {
|
|
var provider AIProvider
|
|
result := db.WithContext(ctx).First(&provider, id)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &provider, nil
|
|
}
|
|
|
|
// AIProviderFindList 查询AI供应商列表(分页)
|
|
func AIProviderFindList(ctx context.Context, db *gorm.DB, page, pageSize int64) ([]AIProvider, int64, error) {
|
|
var providers []AIProvider
|
|
var total int64
|
|
|
|
query := db.WithContext(ctx).Model(&AIProvider{})
|
|
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (page - 1) * pageSize
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
err := query.Order("sort_order ASC, id ASC").Offset(int(offset)).Limit(int(pageSize)).Find(&providers).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return providers, total, nil
|
|
}
|
|
|
|
// AIProviderUpdate 更新AI供应商
|
|
func AIProviderUpdate(ctx context.Context, db *gorm.DB, provider *AIProvider) error {
|
|
result := db.WithContext(ctx).Save(provider)
|
|
return result.Error
|
|
}
|
|
|
|
// AIProviderDelete 删除AI供应商
|
|
func AIProviderDelete(ctx context.Context, db *gorm.DB, id int64) error {
|
|
result := db.WithContext(ctx).Delete(&AIProvider{}, id)
|
|
return result.Error
|
|
}
|
|
|
|
// AIProviderFindByName 根据名称查询AI供应商
|
|
func AIProviderFindByName(ctx context.Context, db *gorm.DB, name string) (*AIProvider, error) {
|
|
var provider AIProvider
|
|
result := db.WithContext(ctx).Where("name = ?", name).First(&provider)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &provider, nil
|
|
}
|
|
|
|
// AIProviderFindAllActive 查询所有启用的AI供应商
|
|
func AIProviderFindAllActive(ctx context.Context, db *gorm.DB) ([]AIProvider, error) {
|
|
var providers []AIProvider
|
|
err := db.WithContext(ctx).Where("is_active = ?", true).Order("sort_order ASC, id ASC").Find(&providers).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return providers, nil
|
|
}
|
|
|