charge_vehicle_not_bind_list.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "gorm.io/gorm"
  13. "property-garden/impl/v1/charge_utils"
  14. "property-garden/errors"
  15. dbmodel "property-garden/model"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. )
  19. func checkChargeVehicleNotBindListParam(req *pb_v1.ChargeVehicleNotBindListRequest) error {
  20. switch {
  21. case req.GardenId < 1:
  22. return status.Error(10003, "小区不能为空")
  23. case req.ChargeId < 1:
  24. return status.Error(10003, "费用项目不能为空")
  25. }
  26. if req.Page == 0 {
  27. req.Page = 1
  28. }
  29. if req.PageSize == 0 {
  30. req.PageSize = 10
  31. }
  32. return nil
  33. }
  34. func ChargeVehicleNotBindList(ctx context.Context, req *pb_v1.ChargeVehicleNotBindListRequest) (reply *pb_v1.ChargeVehicleNotBindListReply, err error) {
  35. reply = &pb_v1.ChargeVehicleNotBindListReply{}
  36. // 捕获各个task中的异常并返回给调用者
  37. defer func() {
  38. if r := recover(); r != nil {
  39. err = fmt.Errorf("%+v", r)
  40. e := &status.Status{}
  41. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  42. logger.Error("err",
  43. zap.String("system_err", err.Error()),
  44. zap.Stack("stacktrace"))
  45. }
  46. }
  47. }()
  48. // 参数检查
  49. err = checkChargeVehicleNotBindListParam(req)
  50. if err != nil {
  51. return nil, err
  52. }
  53. reply.Page = req.Page
  54. dbname := utils.GetGardenDbName(req.GardenId)
  55. chargeInfo := dbmodel.NewChargeConf(dbname)
  56. filter := map[string]interface{}{
  57. "id":req.ChargeId,
  58. }
  59. err = chargeInfo.Find(database.DB(), filter)
  60. if err != nil && err != gorm.ErrRecordNotFound {
  61. return nil, errors.DataBaseError
  62. }
  63. if chargeInfo.ID == 0 {
  64. return nil, errors.ErrRecordNotFound
  65. }
  66. vehicle := dbmodel.NewVehicle(dbname)
  67. where := map[string]interface{}{}
  68. notExist := fmt.Sprintf("not exists(select obj_id from %s.t_charge_bind where t_charge_bind.obj_id=t_vehicle.id and t_charge_bind.charge_type=? && t_charge_bind.obj_type = %d)", dbname, charge_utils.ObjTypeVehicle)
  69. where[notExist] = chargeInfo.ChargeType
  70. where["bind_type !="] = 2
  71. reply.Total, err = vehicle.Count(database.DB(), where, nil)
  72. if err != nil {
  73. return nil, errors.DataBaseError
  74. }
  75. if reply.Total == 0 {
  76. return reply, nil
  77. }
  78. list, err := vehicle.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  79. if err != nil {
  80. return nil, errors.DataBaseError
  81. }
  82. reply.List = make([]*pb_v1.ChargeVehicleData, len(list))
  83. for i, v := range list {
  84. item := &pb_v1.ChargeVehicleData{
  85. VehicleType:v.VehicleType,
  86. PlateNo:v.PlateNo,
  87. VehicleId:v.ID,
  88. Brand:v.Brand,
  89. Displacement:fmt.Sprintf("%v%s", v.Displacement, v.DisplacementUnit),
  90. Seat:v.Seat,
  91. }
  92. reply.List[i] = item
  93. }
  94. return reply, nil
  95. }