create_department_logic.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. TeamID: teams_info.ID,
  89. TmbID: team_members_info.ID,
  90. Name: "默认智能体",
  91. Type: "simple",
  92. Version: "v2",
  93. Avatar: "/imgs/app/avatar/simple.svg",
  94. Intro: "",
  95. TeamTags: []string{},
  96. Modules: []apps.AppModule{},
  97. Edges: []apps.Edge{
  98. {
  99. Source: "workflowStartNodeId",
  100. Target: "7BdojPlukIQw",
  101. SourceHandle: "workflowStartNodeId-source-right",
  102. TargetHandle: "7BdojPlukIQw-target-left",
  103. },
  104. },
  105. PluginData: apps.PluginData{
  106. ID: primitive.NewObjectID(),
  107. NodeVersion: "67da46d29667c5bf2120361a",
  108. },
  109. InheritPermission: true,
  110. VersionNumber: int32(0),
  111. ChatConfig: apps.ChatConfig{
  112. WelcomeText: "",
  113. Variables: []interface{}{},
  114. QuestionGuide: false,
  115. TTSConfig: apps.TTSConfig{
  116. Type: "web",
  117. },
  118. WhisperConfig: apps.WhisperConfig{
  119. Open: false,
  120. AutoSend: false,
  121. AutoTTSResponse: false,
  122. },
  123. ScheduledTriggerConfig: nil,
  124. ChatInputGuide: apps.ChatInputGuide{
  125. Open: false,
  126. TextList: []string{},
  127. CustomUrl: "",
  128. },
  129. Instruction: "",
  130. ID: primitive.NewObjectID(),
  131. },
  132. UpdateTime: time.Date(2024, 7, 10, 12, 0, 18, 197000000, time.UTC),
  133. }
  134. err = l.svcCtx.MongoModel.TeamMembersModel.Insert(context.TODO(), team_members_info)
  135. if err != nil {
  136. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  137. }
  138. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  139. }