super_group.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package permission
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-system/errors"
  10. dbmodel "property-system/model"
  11. pb_v1 "property-system/pb/v1"
  12. "git.getensh.com/common/gopkgs/database"
  13. "git.getensh.com/common/gopkgs/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. )
  17. //
  18. func SuperGroup(ctx context.Context, req *pb_v1.SuperGroupRequest) (reply *pb_v1.SuperGroupReply, err error) {
  19. reply = &pb_v1.SuperGroupReply{}
  20. // 捕获各个task中的异常并返回给调用者
  21. defer func() {
  22. if r := recover(); r != nil {
  23. err = fmt.Errorf("%+v", r)
  24. e := &status.Status{}
  25. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  26. logger.Error("err",
  27. zap.String("system_err", err.Error()),
  28. zap.Stack("stacktrace"))
  29. }
  30. }
  31. }()
  32. if req.GardenId == 0 || req.Cid == 0 {
  33. return nil, errors.ParamsError
  34. }
  35. p := dbmodel.TGroup{}
  36. where := map[string]interface{}{
  37. "cid":req.Cid,
  38. "garden_id":req.GardenId,
  39. "is_super":1,
  40. }
  41. err = p.Find(database.DB(), where)
  42. if err != nil && err != gorm.ErrRecordNotFound {
  43. return nil, errors.DataBaseError
  44. }
  45. if p.ID > 0 {
  46. reply.Id = p.ID
  47. return reply, nil
  48. }
  49. mreq := &pb_v1.GroupAddRequest{IsSuper:true, Cid:req.Cid, GardenId:req.GardenId, GroupName:"超级管理员", GroupDesc:"超级管理员组"}
  50. mreply, err := GroupAdd(context.Background(), mreq)
  51. if err != nil {
  52. return nil, err
  53. }
  54. reply.Id = mreply.Id
  55. return reply, nil
  56. }