super_user_list.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package user
  4. import (
  5. "context"
  6. "cp-organization-management/errors"
  7. "cp-organization-management/model"
  8. pb_v1 "cp-organization-management/pb/v1"
  9. "cp-organization-management/utils"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/jaryhe/gopkgs/database"
  13. "github.com/jaryhe/gopkgs/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. )
  17. func ManagementSuperUserList(ctx context.Context, req *pb_v1.ManagementSuperUserListRequest) (reply *pb_v1.ManagementSuperUserListReply, err error) {
  18. reply = &pb_v1.ManagementSuperUserListReply{}
  19. // 捕获各个task中的异常并返回给调用者
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err = fmt.Errorf("%+v", r)
  23. e := &status.Status{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. logger.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. if req.OrganizationCode == "" {
  32. return reply, status.Error(10003, "参数错误")
  33. }
  34. if req.Page == 0 {
  35. req.Page = 1
  36. }
  37. if req.PageSize == 0 {
  38. req.PageSize = 10
  39. }
  40. dbname := utils.GetDbName(req.OrganizationCode)
  41. p := model.NewRbacUser(dbname)
  42. where := map[string]interface{}{
  43. "is_super_user":true,
  44. }
  45. offset := (req.Page - 1)*req.PageSize
  46. count, err := p.Count(database.DB(), where)
  47. if err != nil {
  48. return nil, errors.DataBaseError
  49. }
  50. reply.Page = req.Page
  51. reply.Total = count
  52. if count == 0 {
  53. return reply, nil
  54. }
  55. list, err := p.ListNew(database.DB(), where, nil, req.PageSize, offset)
  56. if err != nil {
  57. return nil, errors.DataBaseError
  58. }
  59. reply.List = make([]*pb_v1.ManagementSuperUserItem, len(list))
  60. for i, v := range list {
  61. item := &pb_v1.ManagementSuperUserItem{
  62. OrganizationCode:req.OrganizationCode,
  63. Username:v.Username,
  64. Phone:v.Phone,
  65. Email:v.Email,
  66. Id:v.Id,
  67. CreatedAt:v.CreatedAt.Format("2006-01-02 15:04:05"),
  68. }
  69. reply.List[i] = item
  70. }
  71. return reply, err
  72. }