From 04270914b897994d2e636bfe89c988de7b8d414c Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 30 Mar 2025 18:37:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=99=AE=E9=80=9A=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E5=8A=9F=E8=83=BD=E8=8E=B7=E5=8F=96=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/model.go | 11 +++++++++++ handler/func_model.go | 16 ++++++++++++++++ service/modelService.go | 24 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/dao/model.go b/dao/model.go index 4d4140c..9a766fb 100644 --- a/dao/model.go +++ b/dao/model.go @@ -60,6 +60,17 @@ func FindModelByID(id, userID int) []Model { return model } +// 根据id查找模型 +func FindModelByIDV2(id int) []Model { + var model []Model + if proto.Config.SERVER_SQL_LOG { + DB.Debug().Where("id = ?", id).Find(&model) + } else { + DB.Where("id = ?", id).Find(&model) + } + return model +} + // 根据modelID查找模型 func FindModelByModelID(modelID uint) []Model { var model []Model diff --git a/handler/func_model.go b/handler/func_model.go index 3b06f9a..3dbd72e 100644 --- a/handler/func_model.go +++ b/handler/func_model.go @@ -14,6 +14,7 @@ func SetUpFuncModelGroup(router *gin.Engine) { funcGroup.POST("/find", FindFuncModel) funcGroup.POST("/delete", DeleteFuncModel) funcGroup.POST("/update", UpdateFuncModel) + funcGroup.POST("/find_models_by_function", FindFuncModelByFunction) } type FuncModelReq struct { @@ -113,3 +114,18 @@ func DeleteFuncModel(c *gin.Context) { 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"}) + } +} diff --git a/service/modelService.go b/service/modelService.go index e53fe23..cf6cd1b 100644 --- a/service/modelService.go +++ b/service/modelService.go @@ -2,7 +2,9 @@ package service import ( "StuAcaWorksAI/dao" + "encoding/json" "errors" + "log" ) // 创建模型信息 @@ -144,3 +146,25 @@ func FindFuncModelByFunction(function string, userID int) ([]dao.Model, []dao.Fu //再查找对应模型 return dao.FindModelByModelID(modelID), funcModels } + +// 根据功能查找对应的功能模型列表(不查询密钥等信息,只差id,类型及名称) +func FindFuncModelListByFunction(function string) ([]dao.Model, error) { + funcModels := dao.FindFunctionModelByFunction(function) + modelIDs := map[int]bool{} + err := json.Unmarshal([]byte(funcModels[0].ModelIDS), &modelIDs) + if err != nil { + log.Println("FindFuncModelListByFunction json unmarshal error:", err) + return nil, err + } + var models []dao.Model + for k := range modelIDs { + models_ := dao.FindModelByIDV2(k) + for _, v := range models_ { + v.Url = "" + v.Parameter = "" + v.Description = "" + models = append(models, v) + } + } + return models, nil +}