charge_vehicle_group.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 checkChargeVehicleGroupParam(req *pb_v1.ChargeVehicleGroupRequest) error {
  19. switch {
  20. case req.GardenId < 1:
  21. return status.Error(10003, "小区不能为空")
  22. }
  23. if req.Page == 0 {
  24. req.Page = 1
  25. }
  26. if req.PageSize == 0 {
  27. req.PageSize = 10
  28. }
  29. return nil
  30. }
  31. func ChargeVehicleGroup(ctx context.Context, req *pb_v1.ChargeVehicleGroupRequest) (reply *pb_v1.ChargeVehicleGroupReply, err error) {
  32. reply = &pb_v1.ChargeVehicleGroupReply{}
  33. // 捕获各个task中的异常并返回给调用者
  34. defer func() {
  35. if r := recover(); r != nil {
  36. err = fmt.Errorf("%+v", r)
  37. e := &status.Status{}
  38. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  39. logger.Error("err",
  40. zap.String("system_err", err.Error()),
  41. zap.Stack("stacktrace"))
  42. }
  43. }
  44. }()
  45. // 参数检查
  46. err = checkChargeVehicleGroupParam(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. reply.Page = req.Page
  51. dbname := utils.GetGardenDbName(req.GardenId)
  52. 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.obj_type = %d)", dbname, charge_utils.ObjTypeVehicle)
  53. vehicle := dbmodel.NewVehicle(dbname)
  54. where := map[string]interface{}{
  55. exist:"",
  56. }
  57. if req.ObjId > 0 {
  58. where["id"] = req.ObjId
  59. }
  60. reply.Total, err = vehicle.Count(database.DB(), where, nil)
  61. if err != nil {
  62. return nil, errors.DataBaseError
  63. }
  64. if reply.Total == 0 {
  65. return reply, nil
  66. }
  67. list, err := vehicle.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  68. if err != nil {
  69. return nil, errors.DataBaseError
  70. }
  71. reply.List = make([]*pb_v1.ChargeVehicleGroupItem, len(list))
  72. for i, v := range list {
  73. item := &pb_v1.ChargeVehicleGroupItem{
  74. VehicleType:v.VehicleType,
  75. PlateNo:v.PlateNo,
  76. VehicleId:v.ID,
  77. Brand:v.Brand,
  78. Displacement:fmt.Sprintf("%v%s", v.Displacement, v.DisplacementUnit),
  79. Seat:v.Seat,
  80. }
  81. reply.List[i] = item
  82. }
  83. return reply, nil
  84. }