添加功能模型增删改查

This commit is contained in:
junleea 2025-03-24 12:42:47 +08:00
parent 08d134ac41
commit c8c55b4cf4
4 changed files with 234 additions and 0 deletions

View File

@ -13,6 +13,14 @@ type Model struct {
Parameter string `gorm:"column:parameter"` //模型参数,存储json
Description string `gorm:"column:description"` //模型描述
}
type FunctionModel struct {
gorm.Model
Name string `gorm:"column:name"` //功能名称
Function string `gorm:"column:function"` //功能函数,唯一标识
UserID uint `gorm:"column:user_id"` //用户id
ModelID uint `gorm:"column:model_id"` //模型id
Info string `gorm:"column:info"` //功能信息
}
// 创建模型
func CreateModel(userID uint, modelType, url, parameter, description string) (error, uint) {
@ -70,3 +78,60 @@ func UpdateModelByID(id int, userID uint, modelType, url, parameter, description
}
return res.Error
}
// 创建功能模型
func CreateFunctionModel(userID uint, modelID uint, name, info, function string) (error, uint) {
functionModel := FunctionModel{UserID: userID, ModelID: modelID, Name: name, Info: info, Function: function}
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Create(&functionModel)
} else {
res = DB.Create(&functionModel)
}
return res.Error, functionModel.ID
}
// 根据id查找功能模型
func FindFunctionModelByID(id, userID int) []FunctionModel {
var functionModel []FunctionModel
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("id = ? and user_id = ?", id, userID).Find(&functionModel)
} else {
DB.Where("id = ? and user_id = ?", id, userID).Find(&functionModel)
}
return functionModel
}
// 根据用户id查找功能模型
func FindFunctionModelByUserID(userID int) []FunctionModel {
var functionModels []FunctionModel
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("user_id = ?", userID).Find(&functionModels)
} else {
DB.Where("user_id = ?", userID).Find(&functionModels)
}
return functionModels
}
// 修改功能模型
func UpdateFunctionModelByID(id int, userID uint, modelID uint, name, info, function string) error {
functionModel := FunctionModel{UserID: userID, ModelID: modelID, Name: name, Info: info, Function: function}
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Model(&FunctionModel{}).Where("id = ?", id).Updates(&functionModel)
} else {
res = DB.Model(&FunctionModel{}).Where("id = ?", id).Updates(&functionModel)
}
return res.Error
}
// 根据id删除功能模型
func DeleteFunctionModelByID(id int) error {
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Delete(&FunctionModel{}, id)
} else {
res = DB.Delete(&FunctionModel{}, id)
}
return res.Error
}

105
handler/func_model.go Normal file
View File

@ -0,0 +1,105 @@
package handler
import (
"StuAcaWorksAI/proto"
"StuAcaWorksAI/service"
"github.com/gin-gonic/gin"
"net/http"
)
func SetUpFuncModelGroup(router *gin.Engine) {
funcGroup := router.Group("/func")
funcGroup.POST("/create", CreateFuncModel)
funcGroup.POST("/find", FindFuncModel)
funcGroup.POST("/delete", DeleteFuncModel)
funcGroup.POST("/update", UpdateFuncModel)
}
type FuncModelReq struct {
ID int `json:"id" form:"id"`
ModelID int `json:"model_id" form:"model_id"`
Function string `json:"function" form:"function"`
Name string `json:"name" form:"name"`
Info string `json:"info" form:"info"`
}
func CreateFuncModel(c *gin.Context) {
id, _ := c.Get("id")
userID := int(id.(float64))
var req FuncModelReq
if err := c.ShouldBind(&req); err == nil {
// 创建功能模型
err2, mid := service.CreateFuncModel(int(uint(userID)), req.ModelID, req.Name, req.Info, req.Function)
if err2 == nil {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": mid})
} else {
c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.FuncModelCreateFailed, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func FindFuncModel(c *gin.Context) {
id, _ := c.Get("id")
userID := int(id.(float64))
var req FuncModelReq
if err := c.ShouldBind(&req); err == nil {
// 查找功能模型
if req.ID != 0 {
funcModels, err2 := service.FindFuncModelByID(req.ID, userID)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.FuncModelSearchFailed, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": funcModels})
} else if req.ModelID != 0 {
funcModels, err3 := service.FindFuncModelByUserID(userID)
if err3 != nil {
c.JSON(http.StatusOK, gin.H{"error": err3.Error(), "code": proto.FuncModelSearchFailed, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": funcModels})
} else {
c.JSON(http.StatusOK, gin.H{"error": "type error", "code": proto.FuncModelSearchFailed, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
// 修改
func UpdateFuncModel(c *gin.Context) {
id, _ := c.Get("id")
userID := int(id.(float64))
var req FuncModelReq
if err := c.ShouldBind(&req); err == nil {
// 修改功能模型
err2 := service.UpdateFuncModelByID(req.ID, uint(userID), uint(req.ModelID), req.Name, req.Info, req.Function)
if err2 == nil {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
} else {
c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.FuncModelUpdateFailed, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
// 删除
func DeleteFuncModel(c *gin.Context) {
id, _ := c.Get("id")
userID := int(id.(float64))
var req FuncModelReq
if err := c.ShouldBind(&req); err == nil {
// 删除功能模型
err2 := service.DeleteFuncModelByID(req.ID, userID)
if err2 == nil {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
} else {
c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.FuncModelDeleteFailed, "message": "failed"})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}

View File

@ -83,4 +83,9 @@ const (
SessionCreateFailed = 121 // 创建会话失败
SessionDeleteFailed = 122 // 删除会话失败
SessionUpdateFailed = 123 // 更新会话失败
FuncModelCreateFailed = 130 // 创建功能模型失败
FuncModelUpdateFailed = 131 // 更新功能模型失败
FuncModelDeleteFailed = 132 // 删除功能模型失败
FuncModelSearchFailed = 133 // 获取功能模型失败
)

View File

@ -47,3 +47,62 @@ func UpdateModelByID(id int, userID uint, modelType, url, parameter, description
return dao.UpdateModelByID(id, userID, modelType, url, parameter, description)
}
func CreateFuncModel(userID, modelID int, name, info, function string) (error, uint) {
//查看用户是否有权限创建模型
user := GetUserByIDWithCache(userID)
if user.ID == 0 {
return errors.New("user not exist"), 0
}
if user.Role != "admin" {
return errors.New("user not admin,no permission"), 0
}
return dao.CreateFunctionModel(uint(userID), uint(modelID), name, info, function)
}
func FindFuncModelByID(id, userID int) ([]dao.FunctionModel, error) {
user := GetUserByIDWithCache(userID)
if user.ID == 0 {
return nil, errors.New("user not exist")
}
if user.Role != "admin" {
return nil, errors.New("user not admin,no permission")
}
return dao.FindFunctionModelByID(id, userID), nil
}
// 根据userID查找功能模型
func FindFuncModelByUserID(userID int) ([]dao.FunctionModel, error) {
user := GetUserByIDWithCache(userID)
if user.ID == 0 {
return nil, errors.New("user not exist")
}
if user.Role != "admin" {
return nil, errors.New("user not admin,no permission")
}
return dao.FindFunctionModelByUserID(userID), nil
}
func DeleteFuncModelByID(id, userID int) error {
//查看用户是否有权限删除模型
user := GetUserByIDWithCache(userID)
model := dao.FindFunctionModelByID(id, userID)
if user.ID == 0 {
return errors.New("user not exist")
}
if len(model) == 0 && user.Role != "admin" {
return errors.New("model not exist or no permission")
}
return dao.DeleteFunctionModelByID(id)
}
func UpdateFuncModelByID(id int, userID, modelID uint, name, info, function string) error {
user := GetUserByIDWithCache(int(userID))
if user.ID == 0 {
return errors.New("user not exist")
}
if user.Role != "admin" {
return errors.New("user not admin,no permission")
}
return dao.UpdateFunctionModelByID(id, userID, modelID, name, info, function)
}