list.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "time"
  13. "xingjia-management-gateway/apis"
  14. "xingjia-management-gateway/consts"
  15. "xingjia-management-gateway/errors"
  16. dbmodel "xingjia-management-gateway/model"
  17. )
  18. func checkUserListParam(req *apis.UserListRequest) error {
  19. if req.SelfId == 0 {
  20. return status.Error(10003, "未知操作者")
  21. }
  22. if req.PageSize == 0 {
  23. req.PageSize = 10
  24. }
  25. if req.Page == 0 {
  26. req.Page = 1
  27. }
  28. return nil
  29. }
  30. //
  31. func UserList(ctx context.Context, req *apis.UserListRequest) (reply *apis.UserListReply, err error) {
  32. reply = &apis.UserListReply{}
  33. // 捕获各个task中的异常并返回给调用者
  34. defer func() {
  35. if r := recover(); r != nil {
  36. err = fmt.Errorf("%+v", r)
  37. e := &status.Status{}
  38. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  39. logger.Error("err",
  40. zap.String("system_err", err.Error()),
  41. zap.Stack("stacktrace"))
  42. }
  43. }
  44. }()
  45. if err = checkUserListParam(req); err != nil {
  46. return nil, err
  47. }
  48. if err = checkAdmin(req.SelfId); err != nil {
  49. return nil, err
  50. }
  51. where := map[string]interface{}{}
  52. if req.User != "" {
  53. where["user like"] = "%" + req.User + "%"
  54. }
  55. if req.RealName != "" {
  56. where["real_name"] = "%" + req.RealName + "%"
  57. }
  58. p := &dbmodel.TUser{}
  59. count, err := p.Count(database.DB(), where, nil)
  60. if err != nil {
  61. return nil, errors.DataBaseError
  62. }
  63. reply.Page = req.Page
  64. reply.Total = count
  65. if count == 0 {
  66. return reply, nil
  67. }
  68. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  69. if err != nil {
  70. return nil, errors.DataBaseError
  71. }
  72. reply.List = make([]*apis.UserItem, len(list))
  73. for i, v := range list {
  74. item := &apis.UserItem{
  75. Uid: v.ID,
  76. User: v.User,
  77. UserType: v.UserType,
  78. EffectiveStart: v.EffectiveStart,
  79. EffectiveEnd: v.EffectiveEnd,
  80. Status: consts.UserStatusEffective,
  81. RealName: v.RealName,
  82. }
  83. if v.UserType == consts.UserTypeTemp && item.EffectiveStart > time.Now().Unix() {
  84. item.Status = consts.UserStatusWait
  85. }
  86. if v.UserType == consts.UserTypeTemp && item.EffectiveEnd < time.Now().Unix() {
  87. item.Status = consts.UserStatusExpired
  88. }
  89. reply.List[i] = item
  90. }
  91. return reply, nil
  92. }