saw-go/service/modelService.go

202 lines
5.6 KiB
Go
Raw Normal View History

package service
import (
"StuAcaWorksAI/dao"
"StuAcaWorksAI/proto"
"encoding/json"
"errors"
"log"
)
// 创建模型信息
2025-03-21 16:20:24 +08:00
func CreateModel(userID uint, modelType, url, parameter, description string) (error, uint) {
//查看用户是否有权限创建模型
user := GetUserByIDWithCache(int(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
}
2025-03-21 16:20:24 +08:00
return dao.CreateModel(userID, modelType, url, parameter, description)
}
// 根据id查找模型
2025-03-30 14:38:49 +08:00
func FindModelByID(id, userID int) []dao.ModelResponse {
models := dao.FindModelByID(id, userID)
modelTokens := dao.FindModelTotalTokensByModelID(id)
var res []dao.ModelResponse
mt := make(map[uint]uint)
for _, v := range modelTokens {
mt[v.ModelID] = v.Token
}
for _, v := range models {
var model dao.ModelResponse
model.Model = v
model.Token = mt[v.ID]
res = append(res, model)
}
return res
}
// 根据用户id查找模型
2025-03-30 14:38:49 +08:00
func FindModelByUserID(userID int) []dao.ModelResponse {
models := dao.FindModelByUserID(userID)
modelTokens := dao.FindModelTotalTokensByModelID(userID)
var res []dao.ModelResponse
mt := make(map[uint]uint)
for _, v := range modelTokens {
mt[v.ModelID] = v.Token
}
for _, v := range models {
var model dao.ModelResponse
model.Model = v
model.Token = mt[v.ID]
res = append(res, model)
}
return res
}
// 根据id删除模型
func DeleteModelByID(id, userID int) error {
//查看用户是否有权限删除模型
user := GetUserByIDWithCache(userID)
model := dao.FindModelByID(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.DeleteModelByID(id)
}
// 更新模型信息
2025-03-21 16:20:24 +08:00
func UpdateModelByID(id int, userID uint, modelType, url, parameter, description string) error {
2025-03-21 16:20:24 +08:00
return dao.UpdateModelByID(id, userID, modelType, url, parameter, description)
}
2025-03-24 12:42:47 +08:00
2025-03-30 18:25:55 +08:00
func CreateFuncModel(userID, modelID int, name, info, function, modelIDs string) (error, uint) {
2025-03-24 12:42:47 +08:00
//查看用户是否有权限创建模型
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
}
2025-03-30 18:25:55 +08:00
return dao.CreateFunctionModel(uint(userID), uint(modelID), name, info, function, modelIDs)
2025-03-24 12:42:47 +08:00
}
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" {
res := dao.FindFunctionModelByUserID(1)
for i := 0; i < len(res); i++ {
res[i].Info = "" //隐藏信息
}
return res, nil
2025-03-24 12:42:47 +08:00
}
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)
}
2025-03-30 18:25:55 +08:00
func UpdateFuncModelByID(id int, userID, modelID uint, name, info, function, modelIDs string) error {
2025-03-24 12:42:47 +08:00
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")
}
2025-03-30 18:25:55 +08:00
return dao.UpdateFunctionModelByID(id, userID, modelID, name, info, function, modelIDs)
2025-03-24 12:42:47 +08:00
}
// 根据功能查找对应功能
func FindFuncModelByFunction(function string, userID int) ([]dao.Model, []dao.FunctionModel) {
//先查找对应功能
funcModels := dao.FindFunctionModelByFunction(function)
if len(funcModels) == 0 {
return []dao.Model{}, funcModels
}
modelID := funcModels[0].ModelID
//再查找对应模型
return dao.FindModelByModelID(modelID), funcModels
}
// 根据功能查找对应的功能模型列表不查询密钥等信息只差id,类型及名称)
func FindFuncModelListByFunction(function string) ([]dao.Model, error) {
funcModels := dao.FindFunctionModelByFunction(function)
var functionModelIDs []proto.FunctionModelIDs
err := json.Unmarshal([]byte(funcModels[0].ModelIDS), &functionModelIDs)
if err != nil {
log.Println("FindFuncModelListByFunction json unmarshal error:", err)
return nil, err
}
modelIDs := map[int]bool{}
for _, v := range functionModelIDs {
modelIDs[v.ID] = true
}
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
}
func FindFuncModelListByFunctionV2(function string) ([]dao.Model, []dao.FunctionModel, error) {
funcModels := dao.FindFunctionModelByFunction(function)
var functionModelIDs []proto.FunctionModelIDs
err := json.Unmarshal([]byte(funcModels[0].ModelIDS), &functionModelIDs)
if err != nil {
log.Println("FindFuncModelListByFunction json unmarshal error:", err)
return nil, funcModels, err
}
modelIDs := map[int]bool{}
for _, v := range functionModelIDs {
modelIDs[v.ID] = true
}
var models []dao.Model
for k := range modelIDs {
models_ := dao.FindModelByIDV2(k)
models = append(models, models_...)
}
return models, funcModels, nil
}