package department import ( "context" "fmt" "github.com/suyuan32/simple-admin-common/msg/errormsg" "github.com/suyuan32/simple-admin-core/rpc/types/core" "github.com/zeromicro/go-zero/core/errorx" "go.mongodb.org/mongo-driver/bson/primitive" "time" "wechat-api/internal/logic/fastgpt" "wechat-api/internal/svc" "wechat-api/internal/types" team_members "wechat-api/mongo_model/team_members" teams "wechat-api/mongo_model/teams" users "wechat-api/mongo_model/users" "github.com/zeromicro/go-zero/core/logx" ) type CreateDepartmentLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewCreateDepartmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateDepartmentLogic { return &CreateDepartmentLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx} } func (l *CreateDepartmentLogic) CreateDepartment(req *types.DepartmentInfo) (resp *types.BaseMsgResp, err error) { department_info, err := l.svcCtx.CoreRpc.CreateDepartment(l.ctx, &core.DepartmentInfo{ Id: req.Id, Sort: req.Sort, Name: req.Name, Ancestors: req.Ancestors, Leader: req.Leader, Phone: req.Phone, Email: req.Email, Remark: req.Remark, ParentId: req.ParentId, }) if err != nil { return nil, err } //创建 fastgpt 用户 user_info := &users.Users{ Status: "active", Username: fmt.Sprintf("%d", department_info.Id), Password: "838b0ad79fa89e8b3a0cfdc6eab90ac24d20e30ca862855b93a4c736f8cdfedf", Avatar: "/icon/human.svg", Balance: int32(200000), PromotionRate: int32(15), Timezone: "Asia/Shanghai", } usersModel := users.NewUsersModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "users") err = usersModel.Insert(context.TODO(), user_info) if err != nil { return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error()) } // 创建团队 teams_info := &teams.Teams{ Name: *req.Name, OwnerID: user_info.ID, DefaultPermission: int32(0), Avatar: "/icon/logo.svg", CreateTime: time.Date(2024, 7, 10, 12, 0, 18, 197000000, time.UTC), Balance: int32(999900000), } teamsModel := teams.NewTeamsModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "teams") err = teamsModel.Insert(context.TODO(), teams_info) if err != nil { return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error()) } // 创建团队关系 team_members_info := &team_members.TeamMembers{ TeamID: teams_info.ID, UserID: user_info.ID, Name: "Owner", Role: "owner", Status: "active", CreateTime: time.Date(2024, 7, 10, 12, 0, 18, 197000000, time.UTC), DefaultTeam: true, Version: int32(0), } teamMembersModel := team_members.NewTeamMembersModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "team_members") err = teamMembersModel.Insert(context.TODO(), team_members_info) if err != nil { return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error()) } // 创建默认智能体 //apps_info := &apps.Apps{ // ParentID: nil, // TeamID: teams_info.ID, // TmbID: team_members_info.ID, // Name: "默认智能体", // Type: "simple", // Version: "v2", // Avatar: "/imgs/app/avatar/simple.svg", // Intro: "", // TeamTags: []string{}, // Modules: []apps.AppModule{ // { // NodeID: "userGuide", // Name: "系统配置", // Intro: "", // FlowNodeType: "userGuide", // Position: apps.Position{ // X: 531.242273606555, // Y: -486.761172954975, // }, // Version: "481", // Inputs: []apps.AppInput{}, // Outputs: []apps.AppOutput{}, // }, // { // NodeID: "workflowStartNodeId", // Name: "流程开始", // Intro: "", // Avatar: "core/workflow/template/workflowStart", // FlowNodeType: "workflowStart", // Position: apps.Position{ // X: 558.40823764155, // Y: 123.723874291941, // }, // Version: "481", // Inputs: []apps.AppInput{ // { // Key: "userChatInput", // RenderTypeList: []string{"reference", "textarea"}, // ValueType: "string", // Label: "workflow:user_question", // ToolDescription: "workflow:user_question", // Required: true, // }, // }, // Outputs: []apps.AppOutput{ // { // ID: "userChatInput", // Key: "userChatInput", // Label: "common:core.module.input.label.user question", // Type: "static", // ValueType: "string", // }, // { // ID: "userFiles", // Key: "userFiles", // Label: "app:workflow.user_file_input", // Description: "app:workflow.user_file_input_desc", // Type: "static", // ValueType: "arrayString", // }, // }, // }, // { // NodeID: "7BdojPlukIQw", // Name: "AI 对话", // Intro: "AI 大模型对话", // Avatar: "core/workflow/template/aiChat", // FlowNodeType: "chatNode", // ShowStatus: true, // Position: apps.Position{ // X: 1106.32383879608, // Y: -350.603067468347, // }, // Version: "4813", // Inputs: []apps.AppInput{ // { // Key: "model", // RenderTypeList: []string{"settingLLMModel", "reference"}, // ValueType: "string", // Value: "DeepSeek-V3", // }, // { // Key: "temperature", // RenderTypeList: []string{"hidden"}, // ValueType: "number", // Value: int32(0), // Min: getInt32(0), // Max: getInt32(10), // Step: getInt32(1), // }, // { // Key: "maxToken", // RenderTypeList: []string{"hidden"}, // ValueType: "number", // Value: int32(2000), // Min: getInt32(100), // Max: getInt32(4000), // Step: getInt32(50), // }, // { // Key: "isResponseAnswerText", // RenderTypeList: []string{"hidden"}, // ValueType: "boolean", // Value: true, // }, // { // Key: "aiChatQuoteRole", // RenderTypeList: []string{"hidden"}, // ValueType: "string", // Value: "system", // }, // { // Key: "quoteTemplate", // RenderTypeList: []string{"hidden"}, // ValueType: "string", // }, // { // Key: "quotePrompt", // RenderTypeList: []string{"hidden"}, // ValueType: "string", // }, // { // Key: "systemPrompt", // RenderTypeList: []string{"textarea", "reference"}, // Max: getInt32(3000), // ValueType: "string", // Label: "core.ai.Prompt", // Description: "core.app.tip.systemPromptTip", // Placeholder: "core.app.tip.chatNodeSystemPromptTip", // Value: "", // }, // { // Key: "history", // RenderTypeList: []string{"numberInput", "reference"}, // ValueType: "chatHistory", // Label: "core.module.input.label.chat history", // Required: true, // Min: getInt32(0), // Max: getInt32(30), // Value: int32(6), // }, // { // Key: "userChatInput", // RenderTypeList: []string{"reference", "textarea"}, // ValueType: "string", // Label: "common:core.module.input.label.user question", // Required: true, // ToolDescription: "common:core.module.input.label.user question", // Value: []interface{}{"workflowStartNodeId", "userChatInput"}, // }, // { // Key: "quoteQA", // RenderTypeList: []string{"settingDatasetQuotePrompt"}, // Label: "", // DebugLabel: "common:core.module.Dataset quote.label", // Description: "", // ValueType: "datasetQuote", // }, // { // Key: "fileUrlList", // RenderTypeList: []string{"reference", "input"}, // Label: "app:file_quote_link", // DebugLabel: "app:file_quote_link", // ValueType: "arrayString", // Value: [][]interface{}{{"workflowStartNodeId", "userFiles"}}, // }, // { // Key: "aiChatVision", // RenderTypeList: []string{"hidden"}, // ValueType: "boolean", // Value: true, // }, // }, // Outputs: []apps.AppOutput{ // { // ID: "history", // Key: "history", // Required: true, // Label: "common:core.module.output.label.New context", // Description: "common:core.module.output.description.New context", // ValueType: "chatHistory", // ValueDesc: "{\n obj: System | Human | AI;\n value: string;\n}[]", // Type: "static", // }, // { // ID: "answerText", // Key: "answerText", // Required: true, // Label: "common:core.module.output.label.Ai response content", // Description: "common:core.module.output.description.Ai response content", // ValueType: "string", // Type: "static", // }, // }, // }, // }, // Edges: []apps.Edge{ // { // Source: "workflowStartNodeId", // Target: "7BdojPlukIQw", // SourceHandle: "workflowStartNodeId-source-right", // TargetHandle: "7BdojPlukIQw-target-left", // }, // }, // PluginData: apps.PluginData{ // ID: mustParseObjectID("67da46b29667c5bf21203554"), // NodeVersion: "67da46d29667c5bf2120361a", // }, // InheritPermission: true, // VersionNumber: int32(0), // ChatConfig: apps.ChatConfig{ // WelcomeText: "", // Variables: []interface{}{}, // QuestionGuide: false, // TTSConfig: apps.TTSConfig{ // Type: "web", // }, // WhisperConfig: apps.WhisperConfig{ // Open: false, // AutoSend: false, // AutoTTSResponse: false, // }, // ScheduledTriggerConfig: nil, // ChatInputGuide: apps.ChatInputGuide{ // Open: false, // TextList: []string{}, // CustomUrl: "", // }, // Instruction: "", // ID: mustParseObjectID("67da46d29667c5bf2120361d"), // }, // UpdateTime: time.Date(2025, 3, 19, 4, 24, 4, 394000000, time.UTC), // ScheduledTriggerConfig: nil, // ScheduledTriggerNextTime: nil, //} //appsModel := apps.NewAppsModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "apps") //err = appsModel.Insert(context.TODO(), apps_info) //if err != nil { // return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error()) //} token, err := fastgpt.GetToken(fmt.Sprintf("%d", department_info.Id)) if err != nil { return nil, fmt.Errorf("invalid token") } fastgpt.AppCreate("simple", "默认应用", token) return &types.BaseMsgResp{Msg: errormsg.Success}, nil } func mustParseObjectID(hex string) primitive.ObjectID { id, err := primitive.ObjectIDFromHex(hex) if err != nil { panic(err) // 或者根据需要处理错误 } return id } func getInt32(n int32) *int32 { num := int32(n) return &num }