create_department_logic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "time"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. team_members "wechat-api/mongo_model/team_members"
  12. teams "wechat-api/mongo_model/teams"
  13. users "wechat-api/mongo_model/users"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type CreateDepartmentLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewCreateDepartmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateDepartmentLogic {
  22. return &CreateDepartmentLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. func (l *CreateDepartmentLogic) CreateDepartment(req *types.DepartmentInfo) (resp *types.BaseMsgResp, err error) {
  28. department_info, err := l.svcCtx.CoreRpc.CreateDepartment(l.ctx, &core.DepartmentInfo{
  29. Id: req.Id,
  30. Sort: req.Sort,
  31. Name: req.Name,
  32. Ancestors: req.Ancestors,
  33. Leader: req.Leader,
  34. Phone: req.Phone,
  35. Email: req.Email,
  36. Remark: req.Remark,
  37. ParentId: req.ParentId,
  38. })
  39. if err != nil {
  40. return nil, err
  41. }
  42. //创建 fastgpt 用户
  43. user_info := &users.Users{
  44. Status: "active",
  45. Username: fmt.Sprintf("%d", department_info.Id),
  46. Password: "838b0ad79fa89e8b3a0cfdc6eab90ac24d20e30ca862855b93a4c736f8cdfedf",
  47. Avatar: "/icon/human.svg",
  48. Balance: int32(200000),
  49. PromotionRate: int32(15),
  50. Timezone: "Asia/Shanghai",
  51. }
  52. err = l.svcCtx.MongoModel.UsersModel.Insert(context.TODO(), user_info)
  53. if err != nil {
  54. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  55. }
  56. // 创建团队
  57. teams_info := &teams.Teams{
  58. Name: *req.Name,
  59. OwnerID: user_info.ID,
  60. DefaultPermission: int32(0),
  61. Avatar: "/icon/logo.svg",
  62. CreateTime: time.Date(2024, 7, 10, 12, 0, 18, 197000000, time.UTC),
  63. Balance: int32(999900000),
  64. }
  65. err = l.svcCtx.MongoModel.TeamsModel.Insert(context.TODO(), teams_info)
  66. if err != nil {
  67. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  68. }
  69. // 创建团队关系
  70. team_members_info := &team_members.TeamMembers{
  71. TeamID: teams_info.ID,
  72. UserID: user_info.ID,
  73. Name: "Owner",
  74. Role: "owner",
  75. Status: "active",
  76. CreateTime: time.Date(2024, 7, 10, 12, 0, 18, 197000000, time.UTC),
  77. DefaultTeam: true,
  78. Version: int32(0),
  79. }
  80. err = l.svcCtx.MongoModel.TeamMembersModel.Insert(context.TODO(), team_members_info)
  81. if err != nil {
  82. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  83. }
  84. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  85. }