list.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "property-system/errors"
  13. dbmodel "property-system/model"
  14. "property-system/pb"
  15. pb_v1 "property-system/pb/v1"
  16. )
  17. func openimRegister(userId int64, realName string, gender int32) string {
  18. openimId := fmt.Sprintf("property%d", userId)
  19. mreq := pb_v1.OpenImRegisterRequest{UserId: openimId, Platform: 5, NickName: realName, Gender: gender}
  20. _, err := pb.Thirdparty.OpenImRegister(context.Background(), &mreq)
  21. if err != nil {
  22. return ""
  23. }
  24. user := dbmodel.TUser{}
  25. where := map[string]interface{}{
  26. "id": userId,
  27. }
  28. values := map[string]interface{}{
  29. "openim_id": openimId,
  30. }
  31. err = user.Update(database.DB(), where, values)
  32. if err != nil {
  33. return ""
  34. }
  35. return openimId
  36. }
  37. //
  38. func UserList(ctx context.Context, req *pb_v1.UserListRequest) (reply *pb_v1.UserListReply, err error) {
  39. reply = &pb_v1.UserListReply{}
  40. // 捕获各个task中的异常并返回给调用者
  41. defer func() {
  42. if r := recover(); r != nil {
  43. err = fmt.Errorf("%+v", r)
  44. e := &status.Status{}
  45. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  46. logger.Error("err",
  47. zap.String("system_err", err.Error()),
  48. zap.Stack("stacktrace"))
  49. }
  50. }
  51. }()
  52. if req.Cid == 0 && req.GardenId == 0 {
  53. return nil, errors.ParamsError
  54. }
  55. if req.PageSize == 0 {
  56. req.PageSize = 10
  57. }
  58. if req.Page == 0 {
  59. req.Page = 1
  60. }
  61. reply.Page = req.Page
  62. p := &dbmodel.TUserGroup{}
  63. where := map[string]interface{}{}
  64. if req.Cid > 0 {
  65. where["cid"] = req.Cid
  66. }
  67. if req.GardenId > 0 {
  68. where["garden_id"] = req.GardenId
  69. }
  70. if len(req.Ids) > 0 {
  71. where["id in"] = req.Ids
  72. }
  73. if req.UserName != "" {
  74. where["user like"] = "%" + req.UserName + "%"
  75. }
  76. if req.GardenName != "" {
  77. where["garden_name like"] = "%" + req.GardenName + "%"
  78. }
  79. if req.ByCompany {
  80. where["super"] = 1
  81. }
  82. if req.Phone != "" {
  83. where["phone"] = req.Phone
  84. }
  85. if req.GroupId > 0 {
  86. where["group_id"] = req.GroupId
  87. }
  88. if len(req.Exclude) > 0 {
  89. where["id notin"] = req.Exclude
  90. }
  91. reply.Total, err = p.Count(database.DB(), where, nil)
  92. if err != nil {
  93. return nil, errors.DataBaseError
  94. }
  95. if reply.Total == 0 {
  96. return reply, nil
  97. }
  98. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  99. if err != nil {
  100. return nil, errors.DataBaseError
  101. }
  102. reply.List = make([]*pb_v1.UserItem, len(list))
  103. for i, v := range list {
  104. item := &pb_v1.UserItem{
  105. Cid: v.Cid,
  106. GardenId: v.GardenId,
  107. Phone: v.Phone,
  108. UserName: v.User,
  109. Email: v.Email,
  110. GroupId: v.GroupId,
  111. GroupName: v.GroupName,
  112. RealName: v.RealName,
  113. DepartmentName: v.DepartmentName,
  114. CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
  115. Id: v.ID,
  116. OpenimId: v.OpenimId,
  117. BasePermission: v.BasePermission,
  118. Gender: v.Gender,
  119. }
  120. if item.DepartmentName == "" {
  121. item.DepartmentName = "暂未指定部门"
  122. }
  123. if item.OpenimId == "" {
  124. item.OpenimId = openimRegister(v.ID, v.RealName, v.Gender)
  125. }
  126. reply.List[i] = item
  127. }
  128. return reply, nil
  129. }