household_user_list.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package household
  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-garden/errors"
  13. dbmodel "property-garden/model"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. )
  17. func checkGardenHouseholdUserListParam(req *pb_v1.GardenHouseholdUserListRequest) 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 GardenHouseholdUserList(ctx context.Context, req *pb_v1.GardenHouseholdUserListRequest) (reply *pb_v1.GardenHouseholdUserListReply, err error) {
  32. reply = &pb_v1.GardenHouseholdUserListReply{}
  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 = checkGardenHouseholdUserListParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. dbname := utils.GetGardenDbName(req.GardenId)
  50. p := dbmodel.NewHouseApprovedGarden(dbname)
  51. where := map[string]interface{}{}
  52. if req.Name != "" {
  53. where["name like"] = "%" + req.Name + "%"
  54. }
  55. if req.IdNumber != "" {
  56. where["id_number"] = req.IdNumber
  57. }
  58. if req.Phone != "" {
  59. where["phone like"] = "%" + req.Phone + "%"
  60. }
  61. if req.UserType > 0 {
  62. where["user_type"] = req.UserType
  63. }
  64. if req.HouseId > 0 {
  65. where["house_id"] = req.HouseId
  66. }
  67. if len(req.Uids) > 0 {
  68. where["uid in"] = req.Uids
  69. }
  70. if req.BuildingId > 0 {
  71. where["building_id"] = req.BuildingId
  72. }
  73. if req.UnitId > 0 {
  74. where["unit_id"] = req.UnitId
  75. }
  76. if len(req.ExcludeUids) > 0 {
  77. where["uid not in ?"] = req.ExcludeUids
  78. }
  79. reply.Page = req.Page
  80. reply.Total, err = p.CountGroup(database.DB(), where, nil, "uid")
  81. if err != nil {
  82. return nil, errors.DataBaseError
  83. }
  84. if reply.Total == 0 {
  85. return reply, nil
  86. }
  87. list, err := p.ListGroup(database.DB(), where, nil, "uid", int(req.Page), int(req.PageSize))
  88. if err != nil {
  89. return nil, errors.DataBaseError
  90. }
  91. reply.List = make([]*pb_v1.GardenHouseholdUserItem, len(list))
  92. for i, v := range list {
  93. reply.List[i] = &pb_v1.GardenHouseholdUserItem{
  94. Id: v.Uid,
  95. UserType: v.UserType,
  96. IdType: v.IdType,
  97. IdNumber: v.IdNumber,
  98. Phone: v.Phone,
  99. Name: v.Name,
  100. HouseName: fmt.Sprintf("%s-%d-%s", v.BuildingNumber, v.UnitNumber, v.HouseNumber),
  101. }
  102. decrypt := utils.CertDecrypt(reply.List[i].IdNumber)
  103. if decrypt != "" {
  104. reply.List[i].IdNumber = decrypt
  105. }
  106. }
  107. return reply, nil
  108. }