saw-go/service/modelService.go

202 lines
5.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"StuAcaWorksAI/dao"
"StuAcaWorksAI/proto"
"encoding/json"
"errors"
"log"
)
// 创建模型信息
func CreateModel(userID uint, modelType, url, parameter, description string) (error, uint) {
//查看用户是否有权限创建模型
user := GetUserByIDFromUserCenter(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
}
return dao.CreateModel(userID, modelType, url, parameter, description)
}
// 根据id查找模型
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查找模型
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 := GetUserByIDFromUserCenter(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)
}
// 更新模型信息
func UpdateModelByID(id int, userID uint, modelType, url, parameter, description string) error {
return dao.UpdateModelByID(id, userID, modelType, url, parameter, description)
}
func CreateFuncModel(userID, modelID int, name, info, function, modelIDs string) (error, uint) {
//查看用户是否有权限创建模型
user := GetUserByIDFromUserCenter(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, modelIDs)
}
func FindFuncModelByID(id, userID int) ([]dao.FunctionModel, error) {
user := GetUserByIDFromUserCenter(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 := GetUserByIDFromUserCenter(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
}
return dao.FindFunctionModelByUserID(userID), nil
}
func DeleteFuncModelByID(id, userID int) error {
//查看用户是否有权限删除模型
user := GetUserByIDFromUserCenter(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, modelIDs string) error {
user := GetUserByIDFromUserCenter(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, modelIDs)
}
// 根据功能查找对应功能
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
}