create_department_logic.go 10 KB

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