2025-03-21 14:44:40 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"StuAcaWorksAI/dao"
|
2025-03-31 13:09:25 +08:00
|
|
|
|
"StuAcaWorksAI/proto"
|
2025-03-30 18:37:18 +08:00
|
|
|
|
"encoding/json"
|
2025-03-21 14:44:40 +08:00
|
|
|
|
"errors"
|
2025-03-30 18:37:18 +08:00
|
|
|
|
"log"
|
2025-03-21 14:44:40 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 创建模型信息
|
2025-03-21 16:20:24 +08:00
|
|
|
|
func CreateModel(userID uint, modelType, url, parameter, description string) (error, uint) {
|
2025-03-21 14:44:40 +08:00
|
|
|
|
//查看用户是否有权限创建模型
|
|
|
|
|
|
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)
|
2025-03-21 14:44:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据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
|
2025-03-21 14:44:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据用户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
|
2025-03-21 14:44:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据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 14:44:40 +08:00
|
|
|
|
|
2025-03-21 16:20:24 +08:00
|
|
|
|
return dao.UpdateModelByID(id, userID, modelType, url, parameter, description)
|
2025-03-21 14:44:40 +08:00
|
|
|
|
}
|
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" {
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2025-03-24 14:47:53 +08:00
|
|
|
|
|
|
|
|
|
|
// 根据功能查找对应功能
|
2025-03-27 14:36:21 +08:00
|
|
|
|
func FindFuncModelByFunction(function string, userID int) ([]dao.Model, []dao.FunctionModel) {
|
2025-03-24 14:47:53 +08:00
|
|
|
|
//先查找对应功能
|
|
|
|
|
|
funcModels := dao.FindFunctionModelByFunction(function)
|
|
|
|
|
|
if len(funcModels) == 0 {
|
2025-03-28 15:21:57 +08:00
|
|
|
|
return []dao.Model{}, funcModels
|
2025-03-24 14:47:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
modelID := funcModels[0].ModelID
|
|
|
|
|
|
//再查找对应模型
|
2025-03-28 15:28:21 +08:00
|
|
|
|
return dao.FindModelByModelID(modelID), funcModels
|
2025-03-24 14:47:53 +08:00
|
|
|
|
}
|
2025-03-30 18:37:18 +08:00
|
|
|
|
|
|
|
|
|
|
// 根据功能查找对应的功能模型列表(不查询密钥等信息,只差id,类型及名称)
|
|
|
|
|
|
func FindFuncModelListByFunction(function string) ([]dao.Model, error) {
|
|
|
|
|
|
funcModels := dao.FindFunctionModelByFunction(function)
|
2025-03-31 13:09:25 +08:00
|
|
|
|
|
|
|
|
|
|
var functionModelIDs []proto.FunctionModelIDs
|
|
|
|
|
|
err := json.Unmarshal([]byte(funcModels[0].ModelIDS), &functionModelIDs)
|
2025-03-30 18:37:18 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Println("FindFuncModelListByFunction json unmarshal error:", err)
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2025-03-31 13:09:25 +08:00
|
|
|
|
modelIDs := map[int]bool{}
|
|
|
|
|
|
for _, v := range functionModelIDs {
|
|
|
|
|
|
modelIDs[v.ID] = true
|
|
|
|
|
|
}
|
2025-03-30 18:37:18 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|