house_user_list.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house
  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-household/errors"
  13. dbmodel "property-household/model"
  14. pb_v1 "property-household/pb/v1"
  15. "property-household/utils"
  16. )
  17. func checkHouseholdUserListParam(req *pb_v1.HouseholdUserListRequest) error {
  18. switch {
  19. case req.GardenId < 1:
  20. return status.Error(10003, "id不能为空")
  21. }
  22. if req.Page == 0 {
  23. req.Page = 1
  24. }
  25. if req.PageSize == 0 {
  26. req.PageSize = 10
  27. }
  28. return nil
  29. }
  30. //
  31. func HouseholdUserList(ctx context.Context, req *pb_v1.HouseholdUserListRequest) (reply *pb_v1.HouseholdUserListReply, err error) {
  32. reply = &pb_v1.HouseholdUserListReply{}
  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. err = checkHouseholdUserListParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. p := dbmodel.THouseApproved{}
  50. where := map[string]interface{}{}
  51. if req.GardenId > 0 {
  52. where["garden_id"] = req.GardenId
  53. }
  54. if req.Name != "" {
  55. where["name"] = req.Name
  56. }
  57. reply.Page = req.Page
  58. reply.Total, err = p.CountGroup(database.DB(), where, nil, "uid")
  59. if err != nil {
  60. return nil, errors.DataBaseError
  61. }
  62. if reply.Total == 0 {
  63. return reply, nil
  64. }
  65. list, err := p.ListGroup(database.DB(), where, nil, "uid", int(req.Page), int(req.PageSize))
  66. if err != nil {
  67. return nil, errors.DataBaseError
  68. }
  69. gardenIdsM := map[int64]bool{}
  70. gardenIds := []int64{}
  71. for _, v := range list {
  72. if gardenIdsM[v.GardenId] {
  73. continue
  74. }
  75. gardenIdsM[v.GardenId] = true
  76. gardenIds = append(gardenIds, v.GardenId)
  77. }
  78. gardenInfosM, err := getGardenInfos(gardenIds)
  79. if err != nil {
  80. return nil, err
  81. }
  82. reply.List = make([]*pb_v1.HouseholdUserItem, len(list))
  83. for i, v := range list {
  84. reply.List[i] = &pb_v1.HouseholdUserItem{
  85. Id: v.ID,
  86. UserType: v.UserType,
  87. GardenName: gardenInfosM[v.GardenId].GardenName,
  88. IdType:v.IdType,
  89. IdNumber:v.IdNumber,
  90. Phone:v.Phone,
  91. Name:v.Name,
  92. }
  93. decrypt := utils.CertDecrypt(reply.List[i].IdNumber)
  94. if decrypt != "" {
  95. reply.List[i].IdNumber = decrypt
  96. }
  97. }
  98. return reply, nil
  99. }