charge_space_group.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 checkChargeSpaceGroupParam(req *pb_v1.ChargeSpaceGroupRequest) 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 ChargeSpaceGroup(ctx context.Context, req *pb_v1.ChargeSpaceGroupRequest) (reply *pb_v1.ChargeSpaceGroupReply, err error) {
  32. reply = &pb_v1.ChargeSpaceGroupReply{}
  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 = checkChargeSpaceGroupParam(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_space_park.id and t_charge_bind.obj_type = %d)", dbname, charge_utils.ObjTypeSpace)
  53. space := dbmodel.NewSpacePark(dbname)
  54. where := map[string]interface{}{
  55. exist: "",
  56. }
  57. if req.ObjId > 0 {
  58. where["id"] = req.ObjId
  59. }
  60. reply.Total, err = space.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 := space.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  68. if err != nil {
  69. return nil, errors.DataBaseError
  70. }
  71. spaceIds := make([]int64, len(list))
  72. for i, v := range list {
  73. spaceIds[i] = v.ID
  74. }
  75. bind := dbmodel.NewChargeBind(dbname)
  76. where = map[string]interface{}{
  77. "obj_id in": spaceIds,
  78. "obj_type": charge_utils.ObjTypeSpace,
  79. "charge_type": charge_utils.ChargeTypeSpace,
  80. }
  81. blist, err := bind.List(database.DB(), where, nil, -1, -1)
  82. if err != nil {
  83. return nil, errors.DataBaseError
  84. }
  85. m := map[int64]int64{}
  86. for _, v := range blist {
  87. m[v.ObjId] = v.ID
  88. }
  89. bill := dbmodel.NewChargeBill(dbname)
  90. where = map[string]interface{}{
  91. "obj_id in": spaceIds,
  92. "obj_type": charge_utils.ObjTypeSpace,
  93. }
  94. billList, err := bill.List(database.DB(), where, nil, -1, -1, "")
  95. if err != nil {
  96. return nil, errors.DataBaseError
  97. }
  98. bm := map[int64]bool{}
  99. for _, v := range billList {
  100. bm[v.ObjId] = true
  101. }
  102. reply.List = make([]*pb_v1.ChargeSpaceGroupItem, len(list))
  103. for i, v := range list {
  104. item := &pb_v1.ChargeSpaceGroupItem{
  105. SpaceNumber: v.SpaceNumber,
  106. ParkNumber: v.ParkNumber,
  107. ParkName: v.ParkName,
  108. SpaceId: v.ID,
  109. SpaceArea: v.SpaceArea,
  110. SpaceStatus: v.BindStatus,
  111. SpaceType: v.SpaceType,
  112. HasUnpayBill: bm[v.ID],
  113. SpaceBindId: m[v.ID],
  114. }
  115. reply.List[i] = item
  116. }
  117. return reply, nil
  118. }