list.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package park_space
  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. "property-garden/impl/v1/charge_utils"
  14. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. )
  18. func getHouseholdInfos(uids []int64, dbname string) (map[int64]dbmodel.THouseApprovedGarden, error) {
  19. p := dbmodel.NewHouseApprovedGarden(dbname)
  20. ret := map[int64]dbmodel.THouseApprovedGarden{}
  21. if len(uids) == 0 {
  22. return ret, nil
  23. }
  24. where := map[string]interface{}{
  25. "uid in": uids,
  26. }
  27. list, err := p.List(database.DB(), where, nil, -1, -1)
  28. if err != nil {
  29. return nil, errors.DataBaseError
  30. }
  31. for _, v := range list {
  32. ret[v.Uid] = v
  33. }
  34. return ret, nil
  35. }
  36. func getHouseholdHouseIds(uid int64, dbname string) ([]int64, error) {
  37. where := map[string]interface{}{
  38. "uid": uid,
  39. }
  40. h := dbmodel.NewHouseApprovedGarden(dbname)
  41. hlist, err := h.List(database.DB(), where, nil, -1, -1)
  42. if err != nil {
  43. return nil, errors.DataBaseError
  44. }
  45. hids := make([]int64, len(hlist))
  46. for i, v := range hlist {
  47. hids[i] = v.HouseId
  48. }
  49. return hids, nil
  50. }
  51. func GetVehicleInfos(ids []int64, dbname string) (map[int64]dbmodel.TVehicle, error) {
  52. ret := map[int64]dbmodel.TVehicle{}
  53. if len(ids) == 0 {
  54. return ret, nil
  55. }
  56. p := dbmodel.NewVehicle(dbname)
  57. where := map[string]interface{}{
  58. "id in": ids,
  59. }
  60. list, err := p.List(database.DB(), where, nil, -1, -1)
  61. if err != nil {
  62. return nil, errors.DataBaseError
  63. }
  64. for _, v := range list {
  65. ret[v.ID] = v
  66. }
  67. return ret, nil
  68. }
  69. //
  70. func ParkSpaceList(ctx context.Context, req *pb_v1.ParkSpaceListRequest) (reply *pb_v1.ParkSpaceListReply, err error) {
  71. reply = &pb_v1.ParkSpaceListReply{}
  72. // 捕获各个task中的异常并返回给调用者
  73. defer func() {
  74. if r := recover(); r != nil {
  75. err = fmt.Errorf("%+v", r)
  76. e := &status.Status{}
  77. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  78. logger.Error("err",
  79. zap.String("system_err", err.Error()),
  80. zap.Stack("stacktrace"))
  81. }
  82. }
  83. }()
  84. if req.GardenId < 1 {
  85. return nil, errors.ParamsError
  86. }
  87. if req.Page == 0 {
  88. req.Page = 1
  89. }
  90. if req.PageSize == 0 {
  91. req.PageSize = 10
  92. }
  93. dbname := utils.GetGardenDbName(req.GardenId)
  94. p := dbmodel.NewSpacePark(dbname)
  95. where := map[string]interface{}{
  96. //"garden_id":req.GardenId,
  97. }
  98. if req.SpaceType > 0 {
  99. where["space_type"] = req.SpaceType
  100. }
  101. if req.ParkId > 0 {
  102. where["park_id"] = req.ParkId
  103. }
  104. if req.SpaceNumber != "" {
  105. where["space_number"] = req.SpaceNumber
  106. }
  107. if req.HouseId > 0 {
  108. where["house_id"] = req.HouseId
  109. }
  110. or := map[string]interface{}{}
  111. if req.BindType == BindTypeRent {
  112. or["bind_status"] = SpaceBindStatusEmpty
  113. if req.BindSpaceId > 0 {
  114. or["id"] = req.BindSpaceId
  115. }
  116. }
  117. // 查找业主购买的车位
  118. if req.BindType == BindTypeSell {
  119. if req.HouseholdUid < 0 {
  120. return nil, status.Error(10003, "搜索已售车位,业主uid不能为空")
  121. }
  122. hids, err := getHouseholdHouseIds(req.HouseholdUid, dbname)
  123. if err != nil {
  124. return nil, err
  125. }
  126. if len(hids) == 0 {
  127. return nil, status.Error(10003, "未找到该业主房屋")
  128. }
  129. where["house_id in"] = hids
  130. }
  131. reply.Page = req.Page
  132. reply.Total, err = p.Count(database.DB(), where, or)
  133. if err != nil {
  134. return nil, errors.DataBaseError
  135. }
  136. if reply.Total == 0 {
  137. return reply, nil
  138. }
  139. list, err := p.List(database.DB(), where, or, int(req.Page), int(req.PageSize))
  140. if err != nil {
  141. return nil, errors.DataBaseError
  142. }
  143. reply.List = make([]*pb_v1.ParkSpaceItem, len(list))
  144. houseIds := []int64{}
  145. vehicleIds := []int64{}
  146. for _, v := range list {
  147. if v.HouseId > 0 {
  148. houseIds = append(houseIds, v.HouseId)
  149. }
  150. if v.VehicleId > 0 {
  151. vehicleIds = append(vehicleIds, v.VehicleId)
  152. }
  153. }
  154. houseInfoM, err := charge_utils.GetHouseInfos(houseIds, dbname)
  155. if err != nil {
  156. return nil, err
  157. }
  158. vehicleInfoM, err := GetVehicleInfos(vehicleIds, dbname)
  159. if err != nil {
  160. return nil, err
  161. }
  162. for i, v := range list {
  163. reply.List[i] = &pb_v1.ParkSpaceItem{
  164. Id: v.ID,
  165. SpaceNumber: v.SpaceNumber,
  166. SpaceType: v.SpaceType,
  167. SpaceArea: v.SpaceArea,
  168. ParkId: v.ParkId,
  169. ParkType: v.ParkType,
  170. ParkNumber: v.ParkNumber,
  171. ParkName: v.ParkName,
  172. Comment: v.Comment,
  173. HouseId: v.HouseId,
  174. Status: v.Status,
  175. HouseName: fmt.Sprintf("%v-%v-%v", houseInfoM[v.HouseId].BuildingNumber, houseInfoM[v.HouseId].UnitNumber, houseInfoM[v.HouseId].HouseNumber),
  176. CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
  177. }
  178. if v.Status == SpaceStatusRent && v.VehicleId > 0 {
  179. reply.List[i].PlateNo = vehicleInfoM[v.VehicleId].PlateNo
  180. }
  181. }
  182. return reply, nil
  183. }