info.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "gorm.io/gorm"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. "property-garden/pb"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkHouseInfoParam(req *pb_v1.HouseInfoRequest) error {
  20. if req.HouseId < 1 {
  21. return errors.ParamsError
  22. }
  23. return nil
  24. }
  25. func getGardenInfos(ids []int64) (map[int64]pb_v1.GardenItem, error) {
  26. ret := map[int64]pb_v1.GardenItem{}
  27. if len(ids) == 0 {
  28. return ret, nil
  29. }
  30. mreq := pb_v1.GardenInfosRequest{Ids:ids}
  31. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  32. if err != nil {
  33. return ret, err
  34. }
  35. for _, v := range mreply.List {
  36. ret[v.Id] = *v
  37. }
  38. return ret, nil
  39. }
  40. //
  41. func HouseInfo(ctx context.Context, req *pb_v1.HouseInfoRequest) (reply *pb_v1.HouseInfoReply, err error) {
  42. // 捕获各个task中的异常并返回给调用者
  43. defer func() {
  44. if r := recover(); r != nil {
  45. err = fmt.Errorf("%+v", r)
  46. e := &status.Status{}
  47. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  48. logger.Error("err",
  49. zap.String("system_err", err.Error()),
  50. zap.Stack("stacktrace"))
  51. }
  52. }
  53. }()
  54. err = checkHouseInfoParam(req)
  55. if err != nil {
  56. return nil, err
  57. }
  58. // 房屋
  59. dbname := utils.GetGardenDbName(req.GardenId)
  60. house := dbmodel.NewHouse(dbname)
  61. where := map[string]interface{}{
  62. "id": req.HouseId,
  63. }
  64. err = house.Find(database.DB(), where)
  65. if err != nil && err != gorm.ErrRecordNotFound {
  66. return nil, errors.DataBaseError
  67. }
  68. if house.ID == 0 {
  69. return nil, errors.ErrRecordNotFound
  70. }
  71. // 楼栋
  72. building := dbmodel.NewBuilding(dbname)
  73. where = map[string]interface{}{
  74. "id": house.BuildingId,
  75. }
  76. err = building.Find(database.DB(), where)
  77. if err != nil && err != gorm.ErrRecordNotFound {
  78. return nil, errors.DataBaseError
  79. }
  80. if building.ID == 0 {
  81. return nil, errors.ErrRecordNotFound
  82. }
  83. // 单元
  84. unit := dbmodel.NewUnit(dbname)
  85. where = map[string]interface{}{
  86. "id": house.UnitId,
  87. }
  88. err = unit.Find(database.DB(), where)
  89. if err != nil && err != gorm.ErrRecordNotFound {
  90. return nil, errors.DataBaseError
  91. }
  92. if unit.ID == 0 {
  93. return nil, errors.ErrRecordNotFound
  94. }
  95. // 小区
  96. gardenInfos, err := getGardenInfos([]int64{req.GardenId})
  97. if err != nil {
  98. return nil, err
  99. }
  100. garden := gardenInfos[req.GardenId]
  101. reply = &pb_v1.HouseInfoReply{
  102. GardenId: req.GardenId,
  103. BuildingNumber: building.BuildingNumber,
  104. UnitNumber: unit.UnitNumber,
  105. HouseNumber: house.HouseNumber,
  106. GardenName: garden.GardenName,
  107. Province: garden.Province,
  108. City: garden.City,
  109. Area: garden.Area,
  110. Street: garden.Street,
  111. Comittee: garden.Committee,
  112. ProvinceCode: garden.ProvinceCode,
  113. CityCode: garden.CityCode,
  114. AreaCode: garden.AreaCode,
  115. StreetCode: garden.StreetCode,
  116. ComitteeCode: garden.CommitteeCode,
  117. Layer: house.Layer,
  118. HouseArea: house.HouseArea,
  119. HouseUsedArea: house.HouseUsedArea,
  120. RoomCount: house.RoomCount,
  121. // 几厅
  122. HallCount: house.HallCount,
  123. // 房屋类型 1 住宅 2 商铺 3 办公
  124. HouseType: house.HouseType,
  125. GardenInUse: garden.InUse,
  126. HouseStatus:int32(house.Status),
  127. BuildingId:house.BuildingId,
  128. UnitId:house.UnitId,
  129. }
  130. if unit.HasLift == 1 {
  131. reply.HasLift = true
  132. }
  133. return reply, nil
  134. }