house_infos.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/cache"
  9. "git.getensh.com/common/gopkgs/database"
  10. "git.getensh.com/common/gopkgs/logger"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "property-household/errors"
  14. dbmodel "property-household/model"
  15. pb_v1 "property-household/pb/v1"
  16. )
  17. const HouseholdHousesPrefix = "household_gardens"
  18. func checkHouseholdHousesParam(req *pb_v1.HouseholdHousesRequest) error {
  19. switch {
  20. case req.Uid < 1:
  21. return status.Error(10003, "id不能为空")
  22. }
  23. return nil
  24. }
  25. func getHouseholdHousesKey(uid int64) string {
  26. return fmt.Sprintf("%s_%d", HouseholdHousesPrefix, uid)
  27. }
  28. func householdGardensFromRedis(uid int64) []*pb_v1.HouseholdHouseInfo {
  29. key := getHouseholdHousesKey(uid)
  30. str, _ := cache.Redis().Get(key)
  31. if str == "" {
  32. return nil
  33. }
  34. ret := []*pb_v1.HouseholdHouseInfo{}
  35. json.Unmarshal([]byte(str), &ret)
  36. return ret
  37. }
  38. func householdGardensToRedis(uid int64, data []*pb_v1.HouseholdHouseInfo) {
  39. key := getHouseholdHousesKey(uid)
  40. bytes, _ := json.Marshal(data)
  41. cache.Redis().SetEx(key, 30, string(bytes))
  42. }
  43. func householdGardensDelFromRedis(uid int64) {
  44. key := getHouseholdHousesKey(uid)
  45. cache.Redis().Del(key)
  46. }
  47. //
  48. func HouseholdHouses(ctx context.Context, req *pb_v1.HouseholdHousesRequest) (reply *pb_v1.HouseholdHousesReply, err error) {
  49. reply = &pb_v1.HouseholdHousesReply{}
  50. // 捕获各个task中的异常并返回给调用者
  51. defer func() {
  52. if r := recover(); r != nil {
  53. err = fmt.Errorf("%+v", r)
  54. e := &status.Status{}
  55. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  56. logger.Error("err",
  57. zap.String("system_err", err.Error()),
  58. zap.Stack("stacktrace"))
  59. }
  60. }
  61. }()
  62. err = checkHouseholdHousesParam(req)
  63. if err != nil {
  64. return nil, err
  65. }
  66. reply.List = householdGardensFromRedis(req.Uid)
  67. if reply.List != nil {
  68. return reply, nil
  69. }
  70. p := dbmodel.THouseApproved{}
  71. where := map[string]interface{}{}
  72. where["uid"] = req.Uid
  73. if req.ForRent {
  74. where[fmt.Sprintf("id not in(select t1.id from (select * from t_house_approved where uid=%d) t1 inner join t_house_rent as t2 on t1.garden_id = t2.garden_id and t1.house_id=t2.house_id and t2.approve_status < 4)", req.Uid)] = ""
  75. }
  76. list, err := p.List(database.DB(), where, nil, -1, -1)
  77. if err != nil {
  78. return nil, errors.DataBaseError
  79. }
  80. gardenIdsM := map[int64]bool{}
  81. gardenIds := []int64{}
  82. ghIds := [][2]int64{}
  83. for _, v := range list {
  84. ghIds = append(ghIds, [2]int64{v.GardenId, v.HouseId})
  85. if gardenIdsM[v.GardenId] {
  86. continue
  87. }
  88. gardenIdsM[v.GardenId] = true
  89. gardenIds = append(gardenIds, v.GardenId)
  90. }
  91. gardenInfosM, err := getGardenInfos(gardenIds)
  92. if err != nil {
  93. return nil, err
  94. }
  95. reply.List = make([]*pb_v1.HouseholdHouseInfo, len(list))
  96. for i, v := range list {
  97. ginfo := gardenInfosM[v.GardenId]
  98. reply.List[i] = &pb_v1.HouseholdHouseInfo{
  99. Id: v.ID,
  100. HouseName: fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber),
  101. GardenName: ginfo.GardenName,
  102. HouseId: v.HouseId,
  103. GardenId: v.GardenId,
  104. UserType: v.UserType,
  105. CanPay: false,
  106. }
  107. if ginfo.PayMode == 3 {
  108. reply.List[i].CanPay = true
  109. }
  110. if ginfo.PayMode == 2 && ginfo.MchId != "" {
  111. reply.List[i].CanPay = true
  112. }
  113. }
  114. householdGardensToRedis(req.Uid, reply.List)
  115. return reply, nil
  116. }