132 lines
4.5 KiB
Go
132 lines
4.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"StuAcaWorksAI/proto"
|
|
"StuAcaWorksAI/service"
|
|
"encoding/json"
|
|
"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)
|
|
funcGroup.POST("/find_models_by_function", FindFuncModelByFunction)
|
|
}
|
|
|
|
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"`
|
|
ModelIDS map[int]bool `json:"model_ids" form:"model_ids"`
|
|
}
|
|
|
|
func CreateFuncModel(c *gin.Context) {
|
|
id, _ := c.Get("id")
|
|
userID := int(id.(float64))
|
|
var req FuncModelReq
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
modelIDs, err3 := json.Marshal(req.ModelIDS)
|
|
if err3 != nil {
|
|
c.JSON(http.StatusOK, gin.H{"error": err3.Error(), "code": proto.FuncModelCreateFailed, "message": "failed"})
|
|
return
|
|
}
|
|
// 创建功能模型
|
|
err2, mid := service.CreateFuncModel(int(uint(userID)), req.ModelID, req.Name, req.Info, req.Function, string(modelIDs))
|
|
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 {
|
|
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": 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 {
|
|
modelIDs, err3 := json.Marshal(req.ModelIDS)
|
|
if err3 != nil {
|
|
c.JSON(http.StatusOK, gin.H{"error": err3.Error(), "code": proto.FuncModelCreateFailed, "message": "failed"})
|
|
return
|
|
}
|
|
// 修改功能模型
|
|
err2 := service.UpdateFuncModelByID(req.ID, uint(userID), uint(req.ModelID), req.Name, req.Info, req.Function, string(modelIDs))
|
|
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"})
|
|
}
|
|
}
|
|
|
|
func FindFuncModelByFunction(c *gin.Context) {
|
|
var req FuncModelReq
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
// 查找对应功能
|
|
models, err2 := service.FindFuncModelListByFunction(req.Function)
|
|
if err2 == nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": models})
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.ErrorCode, "message": "get models error:" + err.Error(), "data": models})
|
|
}
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
|
}
|
|
}
|