charge_pre_pay_info.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/errors"
  14. "property-garden/impl/v1/charge_utils"
  15. dbmodel "property-garden/model"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. "time"
  19. )
  20. func checkChargePrePayInfoParam(req *pb_v1.ChargePrePayInfoRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.BindId == 0:
  25. return status.Error(10003, "房屋/车位/车辆没有绑定费用")
  26. case req.Months == 0:
  27. return status.Error(10003, "月数不能为空")
  28. }
  29. return nil
  30. }
  31. func ChargePrePayInfo(ctx context.Context, req *pb_v1.ChargePrePayInfoRequest) (reply *pb_v1.ChargePrePayInfoReply, err error) {
  32. reply = &pb_v1.ChargePrePayInfoReply{}
  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 = checkChargePrePayInfoParam(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. dbname := utils.GetGardenDbName(req.GardenId)
  51. bind := dbmodel.NewChargeBind(dbname)
  52. where := map[string]interface{}{
  53. "id": req.BindId,
  54. }
  55. err = bind.Find(database.DB(), where)
  56. if err != nil && err != gorm.ErrRecordNotFound {
  57. return nil, errors.DataBaseError
  58. }
  59. if bind.ID == 0 {
  60. return nil, errors.ErrRecordNotFound
  61. }
  62. bill := dbmodel.NewChargeBill(dbname)
  63. where = map[string]interface{}{
  64. "charge_bind_id": req.BindId,
  65. }
  66. count, err := bill.Count(database.DB(), where, nil)
  67. if err != nil {
  68. return nil, errors.DataBaseError
  69. }
  70. if count > 0 {
  71. return nil, status.Error(10003, "当前费用有未缴账单,请先缴清欠费")
  72. }
  73. chargeInfo := dbmodel.NewChargeConf(dbname)
  74. where = map[string]interface{}{
  75. "id": bind.ChargeId,
  76. }
  77. err = chargeInfo.Find(database.DB(), where)
  78. if err != nil && err != gorm.ErrRecordNotFound {
  79. return nil, errors.DataBaseError
  80. }
  81. if chargeInfo.ID == 0 {
  82. return nil, errors.ErrRecordNotFound
  83. }
  84. start := int64(0)
  85. end := int64(0)
  86. now := time.Now()
  87. if bind.ChargeType == charge_utils.ChargeTypeVehicle {
  88. if bind.Start == 0 {
  89. start = getTimeDayTimestamp(now)
  90. end = time.Unix(start, 0).AddDate(0, int(req.Months), 0).Unix()
  91. } else {
  92. if bind.End < getTimeDayTimestamp(now) {
  93. return nil, status.Error(10003, "当前费用已断缴,请到物业缴费")
  94. }
  95. start = bind.End
  96. end = time.Unix(start, 0).AddDate(0, int(req.Months), 0).Unix()
  97. }
  98. } else {
  99. if bind.BillLastTime > 0 {
  100. start = bind.BillLastTime
  101. end = time.Unix(bind.BillLastTime, 0).AddDate(0, int(req.Months), 0).Unix()
  102. } else {
  103. if bind.Start == 0 {
  104. return nil, status.Error(10003, "当前费用未生效,请先变更开始时间")
  105. }
  106. start = bind.Start
  107. end = time.Unix(start, 0).AddDate(0, int(req.Months), 0).Unix()
  108. }
  109. }
  110. reply.ShouldPayAmount, reply.ChargeDesc, _ = charge_utils.PrePayInfo(chargeInfo, bind, req.Months)
  111. reply.ChargeType = bind.ChargeType
  112. reply.ChargeName = chargeInfo.ChargeName
  113. reply.Start = start
  114. reply.End = end
  115. return reply, nil
  116. }