house_list.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "property-garden/errors"
  9. dbmodel "property-garden/model"
  10. pb_v1 "property-garden/pb/v1"
  11. "property-garden/utils"
  12. "git.getensh.com/common/gopkgs/database"
  13. "git.getensh.com/common/gopkgs/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. )
  17. func checkHouseListParam(req *pb_v1.HouseListRequest) error {
  18. if req.Page == 0 {
  19. req.Page = 1
  20. }
  21. if req.PageSize == 0 {
  22. req.PageSize = 10
  23. }
  24. if req.GardenId < 1 && req.UnitId == 0 && req.BuildingId == 0 {
  25. return errors.ParamsError
  26. }
  27. return nil
  28. }
  29. //
  30. func HouseList(ctx context.Context, req *pb_v1.HouseListRequest) (reply *pb_v1.HouseListReply, err error) {
  31. reply = &pb_v1.HouseListReply{}
  32. // 捕获各个task中的异常并返回给调用者
  33. defer func() {
  34. if r := recover(); r != nil {
  35. err = fmt.Errorf("%+v", r)
  36. e := &status.Status{}
  37. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  38. logger.Error("err",
  39. zap.String("system_err", err.Error()),
  40. zap.Stack("stacktrace"))
  41. }
  42. }
  43. }()
  44. err = checkHouseListParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. dbname := utils.GetGardenDbName(req.GardenId)
  49. p := dbmodel.NewHouseUnitBuilding(dbname)
  50. where := map[string]interface{}{}
  51. if req.GardenId > 0 {
  52. //where["garden_id"] = req.GardenId
  53. }
  54. if req.BuildingId > 0 {
  55. where["building_id"] = req.BuildingId
  56. }
  57. if req.UnitId > 0 {
  58. where["unit_id"] = req.UnitId
  59. }
  60. if req.HouseType > 0 {
  61. where["house_type"] = req.HouseType
  62. }
  63. if req.HouseNumber != "" {
  64. where["house_number"] = req.HouseNumber
  65. }
  66. if req.HouseStatus > 0 {
  67. where["status"] = req.HouseStatus
  68. }
  69. if req.Layer > 0 {
  70. where["layer"] = req.Layer
  71. }
  72. if req.HouseId > 0 {
  73. where["id"] = req.HouseId
  74. }
  75. if req.HouseRent {
  76. where["id not in(select house_id from "+dbname+".t_house_rent where approve_status < 4)"] = ""
  77. }
  78. if req.Uid > 0 {
  79. p := dbmodel.NewHouseApprovedGarden(dbname)
  80. whereTmp := map[string]interface{}{
  81. "uid": req.Uid,
  82. }
  83. list, err := p.List(database.DB(), whereTmp, nil, -1, -1)
  84. if err != nil {
  85. return nil, errors.DataBaseError
  86. }
  87. houseIds := []int64{}
  88. for _, v := range list {
  89. houseIds = append(houseIds, v.HouseId)
  90. }
  91. if len(houseIds) == 0 {
  92. houseIds = []int64{0}
  93. }
  94. where["id in"] = houseIds
  95. }
  96. reply.Page = req.Page
  97. reply.Total, err = p.Count(database.DB(), where, nil)
  98. if err != nil {
  99. return nil, errors.DataBaseError
  100. }
  101. if reply.Total == 0 {
  102. return reply, nil
  103. }
  104. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  105. if err != nil {
  106. return nil, errors.DataBaseError
  107. }
  108. reply.List = make([]*pb_v1.HouseItem, len(list))
  109. for i, v := range list {
  110. reply.List[i] = &pb_v1.HouseItem{
  111. Id: v.ID,
  112. HallCount: v.HallCount,
  113. RoomCount: v.RoomCount,
  114. HouseArea: v.HouseArea,
  115. HouseUsedArea: v.HouseUsedArea,
  116. HouseNumber: v.HouseNumber,
  117. Layer: v.Layer,
  118. HouseType: v.HouseType,
  119. Status: int32(v.Status),
  120. HouseName: fmt.Sprintf("%s-%d-%s", v.BuildingNumber, v.UnitNumber, v.HouseNumber),
  121. BuildingId: v.BuildingId,
  122. UnitId: v.UnitId,
  123. UnitNumber: v.UnitNumber,
  124. BuildingNumber: v.BuildingNumber,
  125. }
  126. if v.HasLift == 1 {
  127. reply.List[i].HasLift = true
  128. }
  129. /*
  130. if !strings.Contains(v.HouseNumber, "-") {
  131. if v.UnitNumber > 0 {
  132. reply.List[i].HouseName = fmt.Sprintf("%s-%d-%s", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
  133. } else {
  134. reply.List[i].HouseName = fmt.Sprintf("%s-%s", v.BuildingNumber, v.HouseNumber)
  135. }
  136. }*/
  137. }
  138. return reply, nil
  139. }