charge_space_bind_list.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package charge
  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 checkChargeSpaceBindedListParam(req *pb_v1.ChargeSpaceBindedListRequest) error {
  19. switch {
  20. case req.GardenId < 1:
  21. return status.Error(10003, "小区不能为空")
  22. case req.ChargeId < 1:
  23. return status.Error(10003, "费用项目不能为空")
  24. }
  25. if req.Page == 0 {
  26. req.Page = 1
  27. }
  28. if req.PageSize == 0 {
  29. req.PageSize = 10
  30. }
  31. return nil
  32. }
  33. func ChargeSpaceBindedList(ctx context.Context, req *pb_v1.ChargeSpaceBindedListRequest) (reply *pb_v1.ChargeSpaceBindedListReply, err error) {
  34. reply = &pb_v1.ChargeSpaceBindedListReply{}
  35. // 捕获各个task中的异常并返回给调用者
  36. defer func() {
  37. if r := recover(); r != nil {
  38. err = fmt.Errorf("%+v", r)
  39. e := &status.Status{}
  40. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  41. logger.Error("err",
  42. zap.String("system_err", err.Error()),
  43. zap.Stack("stacktrace"))
  44. }
  45. }
  46. }()
  47. // 参数检查
  48. err = checkChargeSpaceBindedListParam(req)
  49. if err != nil {
  50. return nil, err
  51. }
  52. reply.Page = req.Page
  53. dbname := utils.GetGardenDbName(req.GardenId)
  54. exist := fmt.Sprintf("exists(select obj_id from %s.t_charge_bind where t_charge_bind.obj_id=t_space_park.id and t_charge_bind.charge_id=? and t_charge_bind.obj_type = %d)", dbname, charge_utils.ObjTypeSpace)
  55. space := dbmodel.NewSpacePark(dbname)
  56. where := map[string]interface{}{
  57. exist:req.ChargeId,
  58. }
  59. reply.Total, err = space.Count(database.DB(), where, nil)
  60. if err != nil {
  61. return nil, errors.DataBaseError
  62. }
  63. if reply.Total == 0 {
  64. return reply, nil
  65. }
  66. list, err := space.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  67. if err != nil {
  68. return nil, errors.DataBaseError
  69. }
  70. reply.List = make([]*pb_v1.ChargeSpaceData, len(list))
  71. ids := make([]int64, len(list))
  72. for i, v := range list {
  73. ids[i] = v.ID
  74. }
  75. bind := dbmodel.NewChargeBind(dbname)
  76. where = map[string]interface{}{
  77. "obj_id in":ids,
  78. "obj_type":charge_utils.ObjTypeSpace,
  79. "charge_id":req.ChargeId,
  80. }
  81. blist, _ := bind.List(database.DB(), where, nil, -1, -1)
  82. m := map[int64]dbmodel.TChargeBind{}
  83. for _, v := range blist {
  84. m[v.ObjId] = v
  85. }
  86. reply.List = make([]*pb_v1.ChargeSpaceData, len(list))
  87. for i, v := range list {
  88. item := &pb_v1.ChargeSpaceData{
  89. SpaceNumber:v.SpaceNumber,
  90. ParkNumber:v.ParkNumber,
  91. ParkName:v.ParkName,
  92. SpaceId:v.ID,
  93. SpaceArea:v.SpaceArea,
  94. SpaceStatus:v.BindStatus,
  95. SpaceType:v.SpaceType,
  96. Start:m[v.ID].Start,
  97. }
  98. reply.List[i] = item
  99. }
  100. return reply, nil
  101. }