list.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "strings"
  17. )
  18. func checkGardenHouseholdListParam(req *pb_v1.GardenHouseholdListRequest) error {
  19. switch {
  20. case req.GardenId < 1:
  21. return status.Error(10003, "id不能为空")
  22. }
  23. if req.Page == 0 {
  24. req.Page = 1
  25. }
  26. if req.PageSize == 0 {
  27. req.PageSize = 10
  28. }
  29. return nil
  30. }
  31. //
  32. func GardenHouseholdList(ctx context.Context, req *pb_v1.GardenHouseholdListRequest) (reply *pb_v1.GardenHouseholdListReply, err error) {
  33. reply = &pb_v1.GardenHouseholdListReply{}
  34. // 捕获各个task中的异常并返回给调用者
  35. defer func() {
  36. if r := recover(); r != nil {
  37. err = fmt.Errorf("%+v", r)
  38. e := &status.Status{}
  39. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  40. logger.Error("err",
  41. zap.String("system_err", err.Error()),
  42. zap.Stack("stacktrace"))
  43. }
  44. }
  45. }()
  46. err = checkGardenHouseholdListParam(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. dbname := utils.GetGardenDbName(req.GardenId)
  51. p := dbmodel.NewHouseApprovedGarden(dbname)
  52. where := map[string]interface{}{}
  53. if req.Uid > 0 {
  54. where["uid"] = req.Uid
  55. }
  56. if req.Name != "" {
  57. where["name"] = req.Name
  58. }
  59. if req.UserType > 0 {
  60. where["user_type"] = req.UserType
  61. }
  62. reply.Page = req.Page
  63. reply.Total, err = p.Count(database.DB(), where, nil)
  64. if err != nil {
  65. return nil, errors.DataBaseError
  66. }
  67. if reply.Total == 0 {
  68. return reply, nil
  69. }
  70. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  71. if err != nil {
  72. return nil, errors.DataBaseError
  73. }
  74. reply.List = make([]*pb_v1.GardenHouseholdItem, len(list))
  75. for i, v := range list {
  76. reply.List[i] = &pb_v1.GardenHouseholdItem{
  77. Id: v.ID,
  78. HouseName: fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber),
  79. UserType: v.UserType,
  80. ApproveStatus: 2,
  81. IdType:v.IdType,
  82. IdNumber:v.IdNumber,
  83. Phone:v.Phone,
  84. Name:v.Name,
  85. Uid:v.Uid,
  86. HouseId:v.HouseId,
  87. }
  88. decrypt := utils.CertDecrypt(reply.List[i].IdNumber)
  89. if decrypt != "" {
  90. reply.List[i].IdNumber = decrypt
  91. }
  92. if v.Appendix != "" {
  93. reply.List[i].Appendix = strings.Split(v.Appendix, ";")
  94. }
  95. if !strings.Contains(v.HouseNumber, "-") {
  96. if v.UnitNumber > 0 {
  97. reply.List[i].HouseName = fmt.Sprintf("%s-%d-%s", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
  98. } else {
  99. reply.List[i].HouseName = fmt.Sprintf("%s-%s", v.BuildingNumber, v.HouseNumber)
  100. }
  101. }
  102. }
  103. return reply, nil
  104. }