charge_space_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 checkChargeSpaceNotBindListParam(req *pb_v1.ChargeSpaceNotBindListRequest) 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 ChargeSpaceNotBindList(ctx context.Context, req *pb_v1.ChargeSpaceNotBindListRequest) (reply *pb_v1.ChargeSpaceNotBindListReply, err error) {
  35. reply = &pb_v1.ChargeSpaceNotBindListReply{}
  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 = checkChargeSpaceNotBindListParam(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. space := dbmodel.NewSpacePark(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_space_park.id and t_charge_bind.charge_type=? && t_charge_bind.obj_type = %d)", dbname, charge_utils.ObjTypeSpace)
  69. where[notExist] = chargeInfo.ChargeType
  70. where["house_id >"] = 0
  71. reply.Total, err = space.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 := space.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.ChargeSpaceData, len(list))
  83. for i, v := range list {
  84. item := &pb_v1.ChargeSpaceData{
  85. SpaceNumber:v.SpaceNumber,
  86. ParkNumber:v.ParkNumber,
  87. ParkName:v.ParkName,
  88. SpaceId:v.ID,
  89. SpaceArea:v.SpaceArea,
  90. SpaceStatus:v.BindStatus,
  91. SpaceType:v.SpaceType,
  92. }
  93. reply.List[i] = item
  94. }
  95. return reply, nil
  96. }