list.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package vehicle
  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. "property-garden/parser"
  17. )
  18. func checkVehicleListParam(req *pb_v1.VehicleListRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. }
  23. if req.PageSize == 0 {
  24. req.PageSize = 10
  25. }
  26. if req.Page == 0 {
  27. req.Page = 1
  28. }
  29. return nil
  30. }
  31. func getHouseholdInfos(uids []int64, dbname string) (map[int64]dbmodel.THouseApprovedGarden, error) {
  32. p := dbmodel.NewHouseApprovedGarden(dbname)
  33. ret := map[int64]dbmodel.THouseApprovedGarden{}
  34. if len(uids) == 0 {
  35. return ret, nil
  36. }
  37. where := map[string]interface{}{
  38. "uid in": uids,
  39. }
  40. list, err := p.List(database.DB(), where, nil, -1, -1)
  41. if err != nil {
  42. return nil, errors.DataBaseError
  43. }
  44. for _, v := range list {
  45. ret[v.Uid] = v
  46. }
  47. return ret, nil
  48. }
  49. func getSpaceInfos(spaceIds []int64, dbname string) (map[int64]dbmodel.TSpacePark, error) {
  50. p := dbmodel.NewSpacePark(dbname)
  51. ret := map[int64]dbmodel.TSpacePark{}
  52. if len(spaceIds) == 0 {
  53. return ret, nil
  54. }
  55. where := map[string]interface{}{
  56. "id in": spaceIds,
  57. }
  58. list, err := p.List(database.DB(), where, nil, -1, -1)
  59. if err != nil {
  60. return nil, errors.DataBaseError
  61. }
  62. for _, v := range list {
  63. ret[v.ID] = v
  64. }
  65. return ret, nil
  66. }
  67. func VehicleList(ctx context.Context, req *pb_v1.VehicleListRequest) (reply *pb_v1.VehicleListReply, err error) {
  68. reply = &pb_v1.VehicleListReply{}
  69. // 捕获各个task中的异常并返回给调用者
  70. defer func() {
  71. if r := recover(); r != nil {
  72. err = fmt.Errorf("%+v", r)
  73. e := &status.Status{}
  74. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  75. logger.Error("err",
  76. zap.String("system_err", err.Error()),
  77. zap.Stack("stacktrace"))
  78. }
  79. }
  80. }()
  81. // 参数检查
  82. err = checkVehicleListParam(req)
  83. if err != nil {
  84. return nil, err
  85. }
  86. dbname := utils.GetGardenDbName(req.GardenId)
  87. p := dbmodel.NewVehicle(dbname)
  88. where := map[string]interface{}{
  89. //"garden_id":req.GardenId,
  90. }
  91. if req.VehicleType > 0 {
  92. where["vehicle_type"] = req.VehicleType
  93. }
  94. if req.PlateNo != "" {
  95. where["plate_no"] = req.PlateNo
  96. }
  97. if req.BindType > 0 {
  98. where["bind_type"] = req.BindType
  99. }
  100. if req.HouseholdUid > 0 {
  101. where["household_uid"] = req.HouseholdUid
  102. }
  103. reply.Page = req.Page
  104. reply.Total, err = p.Count(database.DB(), where, nil)
  105. if err != nil {
  106. return nil, errors.DataBaseError
  107. }
  108. if reply.Total == 0 {
  109. return reply, nil
  110. }
  111. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  112. if err != nil {
  113. return nil, errors.DataBaseError
  114. }
  115. householdUids := []int64{}
  116. spaceIds := []int64{}
  117. for _, v := range list {
  118. if v.HouseholdUid > 0 {
  119. householdUids = append(householdUids, v.HouseholdUid)
  120. }
  121. if v.SpaceId > 0 {
  122. spaceIds = append(spaceIds, v.SpaceId)
  123. }
  124. }
  125. householdInfoM, err := getHouseholdInfos(householdUids, dbname)
  126. if err != nil {
  127. return nil, err
  128. }
  129. spackeInfoM, err := getSpaceInfos(spaceIds, dbname)
  130. if err != nil {
  131. return nil, err
  132. }
  133. reply.List = make([]*pb_v1.VehicleItem, len(list))
  134. for i, v := range list {
  135. item := &pb_v1.VehicleItem{
  136. PlateNo: v.PlateNo,
  137. VehicleType: v.VehicleType,
  138. Vin: v.Vin,
  139. EngineNo: v.EngineNo,
  140. Brand: v.Brand,
  141. Color: v.Color,
  142. SpaceId: v.SpaceId,
  143. HouseholdUid: v.HouseholdUid,
  144. BindType: v.BindType,
  145. Displacement: v.Displacement,
  146. DisplacementUnit: v.DisplacementUnit,
  147. Seat: v.Seat,
  148. Id: v.ID,
  149. HouseholdPhone: householdInfoM[v.HouseholdUid].Phone,
  150. HouseholdName: householdInfoM[v.HouseholdUid].Name,
  151. ParkId: v.ParkId,
  152. ParkNumber: spackeInfoM[v.SpaceId].ParkNumber,
  153. ParkName: spackeInfoM[v.SpaceId].ParkName,
  154. SpaceNumber: spackeInfoM[v.SpaceId].SpaceNumber,
  155. }
  156. if utils.BrandMap[v.Brand] {
  157. if v.Brand != "其他" {
  158. item.BrandPic = parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.BrandBucket + "/" + v.Brand + ".png"
  159. }
  160. }
  161. reply.List[i] = item
  162. }
  163. return reply, nil
  164. }