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.
98 lines
2.6 KiB
98 lines
2.6 KiB
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// AIModelInsert 插入AI模型
|
|
func AIModelInsert(ctx context.Context, db *gorm.DB, aiModel *AIModel) (int64, error) {
|
|
result := db.WithContext(ctx).Create(aiModel)
|
|
if result.Error != nil {
|
|
return 0, result.Error
|
|
}
|
|
return aiModel.Id, nil
|
|
}
|
|
|
|
// AIModelFindOne 根据ID查询AI模型
|
|
func AIModelFindOne(ctx context.Context, db *gorm.DB, id int64) (*AIModel, error) {
|
|
var aiModel AIModel
|
|
result := db.WithContext(ctx).First(&aiModel, id)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &aiModel, nil
|
|
}
|
|
|
|
// AIModelFindList 查询AI模型列表(分页)
|
|
func AIModelFindList(ctx context.Context, db *gorm.DB, page, pageSize int64) ([]AIModel, int64, error) {
|
|
var models []AIModel
|
|
var total int64
|
|
|
|
query := db.WithContext(ctx).Model(&AIModel{})
|
|
|
|
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(&models).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return models, total, nil
|
|
}
|
|
|
|
// AIModelUpdate 更新AI模型
|
|
func AIModelUpdate(ctx context.Context, db *gorm.DB, aiModel *AIModel) error {
|
|
result := db.WithContext(ctx).Save(aiModel)
|
|
return result.Error
|
|
}
|
|
|
|
// AIModelDelete 删除AI模型
|
|
func AIModelDelete(ctx context.Context, db *gorm.DB, id int64) error {
|
|
result := db.WithContext(ctx).Delete(&AIModel{}, id)
|
|
return result.Error
|
|
}
|
|
|
|
// AIModelFindByModelId 根据模型标识查询AI模型
|
|
func AIModelFindByModelId(ctx context.Context, db *gorm.DB, modelId string) (*AIModel, error) {
|
|
var aiModel AIModel
|
|
result := db.WithContext(ctx).Where("model_id = ?", modelId).First(&aiModel)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &aiModel, nil
|
|
}
|
|
|
|
// AIModelFindByProvider 根据供应商ID查询AI模型列表
|
|
func AIModelFindByProvider(ctx context.Context, db *gorm.DB, providerId int64) ([]AIModel, error) {
|
|
var models []AIModel
|
|
err := db.WithContext(ctx).Where("provider_id = ?", providerId).Order("sort_order ASC, id ASC").Find(&models).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return models, nil
|
|
}
|
|
|
|
// AIModelFindAllActive 查询所有启用的AI模型
|
|
func AIModelFindAllActive(ctx context.Context, db *gorm.DB) ([]AIModel, error) {
|
|
var models []AIModel
|
|
err := db.WithContext(ctx).Where("is_active = ?", true).Order("sort_order ASC, id ASC").Find(&models).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return models, nil
|
|
}
|
|
|