charge_vehicle_bind_list.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 checkChargeVehicleBindedListParam(req *pb_v1.ChargeVehicleBindedListRequest) 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 ChargeVehicleBindedList(ctx context.Context, req *pb_v1.ChargeVehicleBindedListRequest) (reply *pb_v1.ChargeVehicleBindedListReply, err error) {
  34. reply = &pb_v1.ChargeVehicleBindedListReply{}
  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 = checkChargeVehicleBindedListParam(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_vehicle.id and t_charge_bind.charge_id=? and t_charge_bind.obj_type = %d)", dbname, charge_utils.ObjTypeVehicle)
  55. vehicle := dbmodel.NewVehicle(dbname)
  56. where := map[string]interface{}{
  57. exist: req.ChargeId,
  58. }
  59. reply.Total, err = vehicle.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 := vehicle.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.ChargeVehicleData, 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.ObjTypeVehicle,
  79. "charge_id": req.ChargeId,
  80. }
  81. blist, _ := bind.List(database.DB(), where, nil, -1, -1)
  82. confIds := []int64{}
  83. m := map[int64]dbmodel.TChargeBind{}
  84. for _, v := range blist {
  85. m[v.ObjId] = v
  86. confIds = append(confIds, v.ChargeId)
  87. }
  88. cconf := dbmodel.NewChargeConf(dbname)
  89. where = map[string]interface{}{
  90. "id in": confIds,
  91. }
  92. cconfM := map[int64]dbmodel.TChargeConf{}
  93. if len(confIds) > 0 {
  94. li, err := cconf.List(database.DB(), where, nil, -1, -1)
  95. if err != nil {
  96. return nil, errors.DataBaseError
  97. }
  98. for _, v := range li {
  99. cconfM[v.ID] = v
  100. }
  101. }
  102. for i, v := range list {
  103. item := &pb_v1.ChargeVehicleData{
  104. VehicleType: v.VehicleType,
  105. PlateNo: v.PlateNo,
  106. VehicleId: v.ID,
  107. Brand: v.Brand,
  108. Displacement: fmt.Sprintf("%v%s", v.Displacement, v.DisplacementUnit),
  109. Seat: v.Seat,
  110. Start: m[v.ID].Start,
  111. End: m[v.ID].End,
  112. }
  113. confId := m[v.ID].ChargeId
  114. if cconfM[confId].ChargeBasis == charge_utils.ChargeBasisSelf {
  115. item.IsCustom = true
  116. item.CustomFee = m[v.ID].CustomFee
  117. }
  118. reply.List[i] = item
  119. }
  120. return reply, nil
  121. }