add.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package charge_conf
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "property-garden/errors"
  9. "property-garden/impl/v1/charge_utils"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkVehicleChargeConfAddParam(req *pb_v1.ChargeConfAddRequest) error {
  20. switch {
  21. case req.GardenId < 1:
  22. return status.Error(10003, "小区不能为空")
  23. case charge_utils.ChargeTypeM[req.ChargeType] == "":
  24. return status.Error(10003, "费用类型不能为空")
  25. case req.ChargeBasis == charge_utils.ChargeBasisFix && req.FixAmount < 1:
  26. return status.Error(10003, "按固定费用计价,固定费用不能为空")
  27. case req.ChargeName == "":
  28. return status.Error(10003, "费用名称不能为空")
  29. case req.ChargeBasis != charge_utils.ChargeBasisFix && req.ChargeBasis != charge_utils.ChargeBasisSelf:
  30. return status.Error(10003, "月租车费用只能为固定/自定义")
  31. }
  32. return nil
  33. }
  34. func checkChargeConfAddParam(req *pb_v1.ChargeConfAddRequest) error {
  35. if charge_utils.ChargeTypeM[req.ChargeType] == "" {
  36. return status.Error(10003, "费用类型不能为空")
  37. }
  38. if !req.LateFeeEnable {
  39. req.LateFeePercent = 0
  40. req.LateFeeMaxDays = 0
  41. req.LateFeeDay = 0
  42. }
  43. req.BillPeriodType = 1
  44. switch {
  45. case req.ChargeType == charge_utils.ChargeTypeProperty:
  46. req.ChargeTimeType = charge_utils.ChargeTimeTypePeriod
  47. if req.ChargeBasis != charge_utils.ChargeBasisUsedArea &&
  48. req.ChargeBasis != charge_utils.ChargeBasisArea &&
  49. req.ChargeBasis != charge_utils.ChargeBasisFix {
  50. return status.Error(10003, "物业费只能按房屋面积/使用面积/固定费用")
  51. }
  52. case charge_utils.IsPowerChargeType[req.ChargeType]:
  53. req.ChargeTimeType = 0
  54. if req.ChargeBasis != charge_utils.ChargeBasisUsed {
  55. return status.Error(10003, "水电气费只能按使用量")
  56. }
  57. case req.ChargeType == charge_utils.ChargeTypeSpace:
  58. req.ChargeTimeType = charge_utils.ChargeTimeTypePeriod
  59. if req.ChargeBasis != charge_utils.ChargeBasisSpaceArea &&
  60. req.ChargeBasis != charge_utils.ChargeBasisFix {
  61. return status.Error(10003, "车位管理费只能按房屋面积/使用面积/固定费用")
  62. }
  63. case req.ChargeType == charge_utils.ChargeTypeVehicle:
  64. req.ChargeTimeType = 0
  65. return checkVehicleChargeConfAddParam(req)
  66. }
  67. switch {
  68. case req.GardenId < 1:
  69. return status.Error(10003, "小区不能为空")
  70. case charge_utils.ChargeBasisM[req.ChargeBasis] == "" && !charge_utils.IsPowerChargeType[req.ChargeType]:
  71. return status.Error(10003, "计费规则不能为空")
  72. case charge_utils.ChargeTypeM[req.ChargeType] == "":
  73. return status.Error(10003, "费用类型不能为空")
  74. case charge_utils.ChargeTimeTypeM[req.ChargeTimeType] == "" && !charge_utils.IsPowerChargeType[req.ChargeType]:
  75. return status.Error(10003, "缴费时间类型不能为空")
  76. case req.ChargeBasis == charge_utils.ChargeBasisFix && req.FixAmount < 1:
  77. return status.Error(10003, "按固定费用计价,固定费用不能为空")
  78. case req.ChargeName == "":
  79. return status.Error(10003, "费用名称不能为空")
  80. case req.ChargeBasis != charge_utils.ChargeBasisFix && req.UnitPrice < 1:
  81. return status.Error(10003, "单价不能为空")
  82. case req.ChargeTimeType == charge_utils.ChargeTimeTypePeriod && charge_utils.BillPeriodM[req.BillPeriod] == "":
  83. return status.Error(10003, "账单生成周期错误")
  84. case req.ChargeEffectiveTime < 1:
  85. return status.Error(10003, "费用生效时间错误")
  86. case req.LateFeeEnable:
  87. if req.LateFeeDay == 0 || req.LateFeePercent <= 0 {
  88. return status.Error(10003, "滞纳金参数不能为空")
  89. }
  90. }
  91. return nil
  92. }
  93. func ChargeConfAdd(ctx context.Context, req *pb_v1.ChargeConfAddRequest) (reply *pb_v1.ChargeConfAddReply, err error) {
  94. reply = &pb_v1.ChargeConfAddReply{}
  95. // 捕获各个task中的异常并返回给调用者
  96. defer func() {
  97. if r := recover(); r != nil {
  98. err = fmt.Errorf("%+v", r)
  99. e := &status.Status{}
  100. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  101. logger.Error("err",
  102. zap.String("system_err", err.Error()),
  103. zap.Stack("stacktrace"))
  104. }
  105. }
  106. }()
  107. // 参数检查
  108. err = checkChargeConfAddParam(req)
  109. if err != nil {
  110. return nil, err
  111. }
  112. now := time.Now()
  113. dbname := utils.GetGardenDbName(req.GardenId)
  114. lateFeeEnable := int32(1)
  115. if !req.LateFeeEnable {
  116. lateFeeEnable = 2
  117. }
  118. if req.ChargeBasis == charge_utils.ChargeBasisSelf {
  119. req.FixAmount = 0
  120. req.UnitPrice = 0
  121. }
  122. chargeConf := &dbmodel.TChargeConf{
  123. ChargeEffectiveTime: req.ChargeEffectiveTime,
  124. BillPeriodType: req.BillPeriodType,
  125. BillPeriod: req.BillPeriod,
  126. LateFeePercent: req.LateFeePercent,
  127. LateFeeDay: req.LateFeeDay,
  128. LateFeeEnable: lateFeeEnable,
  129. LateFeeMaxDays: req.LateFeeMaxDays,
  130. ChargeType: req.ChargeType,
  131. ChargeName: req.ChargeName,
  132. ChargeTimeType: req.ChargeTimeType,
  133. ChargeBasis: req.ChargeBasis,
  134. FixAmount: req.FixAmount,
  135. FixAmountName: req.FixAmountName,
  136. UnitPrice: req.UnitPrice,
  137. UpdatedAt: now,
  138. CreatedAt: now,
  139. }
  140. chargeConf.SetTable(dbname)
  141. err = chargeConf.Insert(database.DB())
  142. if err != nil {
  143. return nil, errors.DataBaseError
  144. }
  145. reply.Id = chargeConf.ID
  146. return reply, nil
  147. }