saw-go/dao/model.go

73 lines
2.0 KiB
Go
Raw Normal View History

package dao
import (
"StuAcaWorksAI/proto"
"gorm.io/gorm"
)
type Model struct {
gorm.Model
2025-03-21 16:20:24 +08:00
Type string `gorm:"column:type"` //模型类型
UserID uint `gorm:"column:user_id"` //用户id
Url string `gorm:"column:url"` //模型地址
Parameter string `gorm:"column:parameter"` //模型参数,存储json
Description string `gorm:"column:description"` //模型描述
}
// 创建模型
2025-03-21 16:20:24 +08:00
func CreateModel(userID uint, modelType, url, parameter, description string) (error, uint) {
model := Model{UserID: userID, Type: modelType, Url: url, Parameter: parameter, Description: description}
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Create(&model)
} else {
res = DB.Create(&model)
}
return res.Error, model.ID
}
// 根据id查找模型
func FindModelByID(id, userID int) []Model {
var model []Model
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("id = ? and user_id = ?", id, userID).Find(&model)
} else {
DB.Where("id = ? and user_id = ?", id, userID).Find(&model)
}
return model
}
// 根据用户id查找模型
func FindModelByUserID(userID int) []Model {
var models []Model
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("user_id = ?", userID).Find(&models)
} else {
DB.Where("user_id = ?", userID).Find(&models)
}
return models
}
// 根据id删除模型
func DeleteModelByID(id int) error {
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Delete(&Model{}, id)
} else {
res = DB.Delete(&Model{}, id)
}
return res.Error
}
// 根据id更新模型
2025-03-21 16:20:24 +08:00
func UpdateModelByID(id int, userID uint, modelType, url, parameter, description string) error {
2025-03-21 19:20:20 +08:00
model := Model{UserID: userID, Type: modelType, Url: url, Parameter: parameter, Description: description}
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Model(&Model{}).Where("id = ?", id).Updates(&model)
} else {
res = DB.Model(&Model{}).Where("id = ?", id).Updates(&model)
}
return res.Error
}