create_department_logic.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package department
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  6. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  7. "github.com/zeromicro/go-zero/core/errorx"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "time"
  10. "wechat-api/internal/logic/fastgpt"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. team_members "wechat-api/mongo_model/team_members"
  14. teams "wechat-api/mongo_model/teams"
  15. users "wechat-api/mongo_model/users"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. )
  18. type CreateDepartmentLogic struct {
  19. logx.Logger
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. }
  23. func NewCreateDepartmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateDepartmentLogic {
  24. return &CreateDepartmentLogic{
  25. Logger: logx.WithContext(ctx),
  26. ctx: ctx,
  27. svcCtx: svcCtx}
  28. }
  29. func (l *CreateDepartmentLogic) CreateDepartment(req *types.DepartmentInfo) (resp *types.BaseMsgResp, err error) {
  30. department_info, err := l.svcCtx.CoreRpc.CreateDepartment(l.ctx, &core.DepartmentInfo{
  31. Id: req.Id,
  32. Sort: req.Sort,
  33. Name: req.Name,
  34. Ancestors: req.Ancestors,
  35. Leader: req.Leader,
  36. Phone: req.Phone,
  37. Email: req.Email,
  38. Remark: req.Remark,
  39. ParentId: req.ParentId,
  40. })
  41. if err != nil {
  42. return nil, err
  43. }
  44. //创建 fastgpt 用户
  45. user_info := &users.Users{
  46. Status: "active",
  47. Username: fmt.Sprintf("%d", department_info.Id),
  48. Password: "838b0ad79fa89e8b3a0cfdc6eab90ac24d20e30ca862855b93a4c736f8cdfedf",
  49. Avatar: "/icon/human.svg",
  50. Balance: int32(200000),
  51. PromotionRate: int32(15),
  52. Timezone: "Asia/Shanghai",
  53. }
  54. usersModel := users.NewUsersModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "users")
  55. err = usersModel.Insert(context.TODO(), user_info)
  56. if err != nil {
  57. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  58. }
  59. // 创建团队
  60. teams_info := &teams.Teams{
  61. Name: *req.Name,
  62. OwnerID: user_info.ID,
  63. DefaultPermission: int32(0),
  64. Avatar: "/icon/logo.svg",
  65. CreateTime: time.Date(2024, 7, 10, 12, 0, 18, 197000000, time.UTC),
  66. Balance: int32(999900000),
  67. }
  68. teamsModel := teams.NewTeamsModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "teams")
  69. err = teamsModel.Insert(context.TODO(), teams_info)
  70. if err != nil {
  71. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  72. }
  73. // 创建团队关系
  74. team_members_info := &team_members.TeamMembers{
  75. TeamID: teams_info.ID,
  76. UserID: user_info.ID,
  77. Name: "Owner",
  78. Role: "owner",
  79. Status: "active",
  80. CreateTime: time.Date(2024, 7, 10, 12, 0, 18, 197000000, time.UTC),
  81. DefaultTeam: true,
  82. Version: int32(0),
  83. }
  84. teamMembersModel := team_members.NewTeamMembersModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "team_members")
  85. err = teamMembersModel.Insert(context.TODO(), team_members_info)
  86. if err != nil {
  87. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  88. }
  89. // 创建默认智能体
  90. //apps_info := &apps.Apps{
  91. // ParentID: nil,
  92. // TeamID: teams_info.ID,
  93. // TmbID: team_members_info.ID,
  94. // Name: "默认智能体",
  95. // Type: "simple",
  96. // Version: "v2",
  97. // Avatar: "/imgs/app/avatar/simple.svg",
  98. // Intro: "",
  99. // TeamTags: []string{},
  100. // Modules: []apps.AppModule{
  101. // {
  102. // NodeID: "userGuide",
  103. // Name: "系统配置",
  104. // Intro: "",
  105. // FlowNodeType: "userGuide",
  106. // Position: apps.Position{
  107. // X: 531.242273606555,
  108. // Y: -486.761172954975,
  109. // },
  110. // Version: "481",
  111. // Inputs: []apps.AppInput{},
  112. // Outputs: []apps.AppOutput{},
  113. // },
  114. // {
  115. // NodeID: "workflowStartNodeId",
  116. // Name: "流程开始",
  117. // Intro: "",
  118. // Avatar: "core/workflow/template/workflowStart",
  119. // FlowNodeType: "workflowStart",
  120. // Position: apps.Position{
  121. // X: 558.40823764155,
  122. // Y: 123.723874291941,
  123. // },
  124. // Version: "481",
  125. // Inputs: []apps.AppInput{
  126. // {
  127. // Key: "userChatInput",
  128. // RenderTypeList: []string{"reference", "textarea"},
  129. // ValueType: "string",
  130. // Label: "workflow:user_question",
  131. // ToolDescription: "workflow:user_question",
  132. // Required: true,
  133. // },
  134. // },
  135. // Outputs: []apps.AppOutput{
  136. // {
  137. // ID: "userChatInput",
  138. // Key: "userChatInput",
  139. // Label: "common:core.module.input.label.user question",
  140. // Type: "static",
  141. // ValueType: "string",
  142. // },
  143. // {
  144. // ID: "userFiles",
  145. // Key: "userFiles",
  146. // Label: "app:workflow.user_file_input",
  147. // Description: "app:workflow.user_file_input_desc",
  148. // Type: "static",
  149. // ValueType: "arrayString",
  150. // },
  151. // },
  152. // },
  153. // {
  154. // NodeID: "7BdojPlukIQw",
  155. // Name: "AI 对话",
  156. // Intro: "AI 大模型对话",
  157. // Avatar: "core/workflow/template/aiChat",
  158. // FlowNodeType: "chatNode",
  159. // ShowStatus: true,
  160. // Position: apps.Position{
  161. // X: 1106.32383879608,
  162. // Y: -350.603067468347,
  163. // },
  164. // Version: "4813",
  165. // Inputs: []apps.AppInput{
  166. // {
  167. // Key: "model",
  168. // RenderTypeList: []string{"settingLLMModel", "reference"},
  169. // ValueType: "string",
  170. // Value: "DeepSeek-V3",
  171. // },
  172. // {
  173. // Key: "temperature",
  174. // RenderTypeList: []string{"hidden"},
  175. // ValueType: "number",
  176. // Value: int32(0),
  177. // Min: getInt32(0),
  178. // Max: getInt32(10),
  179. // Step: getInt32(1),
  180. // },
  181. // {
  182. // Key: "maxToken",
  183. // RenderTypeList: []string{"hidden"},
  184. // ValueType: "number",
  185. // Value: int32(2000),
  186. // Min: getInt32(100),
  187. // Max: getInt32(4000),
  188. // Step: getInt32(50),
  189. // },
  190. // {
  191. // Key: "isResponseAnswerText",
  192. // RenderTypeList: []string{"hidden"},
  193. // ValueType: "boolean",
  194. // Value: true,
  195. // },
  196. // {
  197. // Key: "aiChatQuoteRole",
  198. // RenderTypeList: []string{"hidden"},
  199. // ValueType: "string",
  200. // Value: "system",
  201. // },
  202. // {
  203. // Key: "quoteTemplate",
  204. // RenderTypeList: []string{"hidden"},
  205. // ValueType: "string",
  206. // },
  207. // {
  208. // Key: "quotePrompt",
  209. // RenderTypeList: []string{"hidden"},
  210. // ValueType: "string",
  211. // },
  212. // {
  213. // Key: "systemPrompt",
  214. // RenderTypeList: []string{"textarea", "reference"},
  215. // Max: getInt32(3000),
  216. // ValueType: "string",
  217. // Label: "core.ai.Prompt",
  218. // Description: "core.app.tip.systemPromptTip",
  219. // Placeholder: "core.app.tip.chatNodeSystemPromptTip",
  220. // Value: "",
  221. // },
  222. // {
  223. // Key: "history",
  224. // RenderTypeList: []string{"numberInput", "reference"},
  225. // ValueType: "chatHistory",
  226. // Label: "core.module.input.label.chat history",
  227. // Required: true,
  228. // Min: getInt32(0),
  229. // Max: getInt32(30),
  230. // Value: int32(6),
  231. // },
  232. // {
  233. // Key: "userChatInput",
  234. // RenderTypeList: []string{"reference", "textarea"},
  235. // ValueType: "string",
  236. // Label: "common:core.module.input.label.user question",
  237. // Required: true,
  238. // ToolDescription: "common:core.module.input.label.user question",
  239. // Value: []interface{}{"workflowStartNodeId", "userChatInput"},
  240. // },
  241. // {
  242. // Key: "quoteQA",
  243. // RenderTypeList: []string{"settingDatasetQuotePrompt"},
  244. // Label: "",
  245. // DebugLabel: "common:core.module.Dataset quote.label",
  246. // Description: "",
  247. // ValueType: "datasetQuote",
  248. // },
  249. // {
  250. // Key: "fileUrlList",
  251. // RenderTypeList: []string{"reference", "input"},
  252. // Label: "app:file_quote_link",
  253. // DebugLabel: "app:file_quote_link",
  254. // ValueType: "arrayString",
  255. // Value: [][]interface{}{{"workflowStartNodeId", "userFiles"}},
  256. // },
  257. // {
  258. // Key: "aiChatVision",
  259. // RenderTypeList: []string{"hidden"},
  260. // ValueType: "boolean",
  261. // Value: true,
  262. // },
  263. // },
  264. // Outputs: []apps.AppOutput{
  265. // {
  266. // ID: "history",
  267. // Key: "history",
  268. // Required: true,
  269. // Label: "common:core.module.output.label.New context",
  270. // Description: "common:core.module.output.description.New context",
  271. // ValueType: "chatHistory",
  272. // ValueDesc: "{\n obj: System | Human | AI;\n value: string;\n}[]",
  273. // Type: "static",
  274. // },
  275. // {
  276. // ID: "answerText",
  277. // Key: "answerText",
  278. // Required: true,
  279. // Label: "common:core.module.output.label.Ai response content",
  280. // Description: "common:core.module.output.description.Ai response content",
  281. // ValueType: "string",
  282. // Type: "static",
  283. // },
  284. // },
  285. // },
  286. // },
  287. // Edges: []apps.Edge{
  288. // {
  289. // Source: "workflowStartNodeId",
  290. // Target: "7BdojPlukIQw",
  291. // SourceHandle: "workflowStartNodeId-source-right",
  292. // TargetHandle: "7BdojPlukIQw-target-left",
  293. // },
  294. // },
  295. // PluginData: apps.PluginData{
  296. // ID: mustParseObjectID("67da46b29667c5bf21203554"),
  297. // NodeVersion: "67da46d29667c5bf2120361a",
  298. // },
  299. // InheritPermission: true,
  300. // VersionNumber: int32(0),
  301. // ChatConfig: apps.ChatConfig{
  302. // WelcomeText: "",
  303. // Variables: []interface{}{},
  304. // QuestionGuide: false,
  305. // TTSConfig: apps.TTSConfig{
  306. // Type: "web",
  307. // },
  308. // WhisperConfig: apps.WhisperConfig{
  309. // Open: false,
  310. // AutoSend: false,
  311. // AutoTTSResponse: false,
  312. // },
  313. // ScheduledTriggerConfig: nil,
  314. // ChatInputGuide: apps.ChatInputGuide{
  315. // Open: false,
  316. // TextList: []string{},
  317. // CustomUrl: "",
  318. // },
  319. // Instruction: "",
  320. // ID: mustParseObjectID("67da46d29667c5bf2120361d"),
  321. // },
  322. // UpdateTime: time.Date(2025, 3, 19, 4, 24, 4, 394000000, time.UTC),
  323. // ScheduledTriggerConfig: nil,
  324. // ScheduledTriggerNextTime: nil,
  325. //}
  326. //appsModel := apps.NewAppsModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "apps")
  327. //err = appsModel.Insert(context.TODO(), apps_info)
  328. //if err != nil {
  329. // return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  330. //}
  331. token, err := fastgpt.GetToken(fmt.Sprintf("%d", department_info.Id))
  332. if err != nil {
  333. return nil, fmt.Errorf("invalid token")
  334. }
  335. fastgpt.AppCreate("simple", "默认应用", token)
  336. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  337. }
  338. func mustParseObjectID(hex string) primitive.ObjectID {
  339. id, err := primitive.ObjectIDFromHex(hex)
  340. if err != nil {
  341. panic(err) // 或者根据需要处理错误
  342. }
  343. return id
  344. }
  345. func getInt32(n int32) *int32 {
  346. num := int32(n)
  347. return &num
  348. }