update.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/charge_utils"
  11. dbmodel "property-garden/model"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "time"
  15. "git.getensh.com/common/gopkgs/database"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. func checkVehicleChargeConfUpdateParam(req *pb_v1.ChargeConfUpdateRequest) error {
  21. switch {
  22. case req.Id < 1:
  23. return status.Error(10003, "id不能为空")
  24. case req.GardenId < 1:
  25. return status.Error(10003, "小区不能为空")
  26. case charge_utils.ChargeTypeM[req.ChargeType] == "":
  27. return status.Error(10003, "费用类型不能为空")
  28. case req.ChargeBasis == charge_utils.ChargeBasisFix && req.FixAmount < 1:
  29. return status.Error(10003, "按固定费用计价,固定费用不能为空")
  30. case req.ChargeName == "":
  31. return status.Error(10003, "费用名称不能为空")
  32. case req.ChargeBasis != charge_utils.ChargeBasisFix && req.ChargeBasis != charge_utils.ChargeBasisSelf:
  33. return status.Error(10003, "月租车费用只能为固定/自定义")
  34. }
  35. return nil
  36. }
  37. func checkChargeConfUpdateParam(req *pb_v1.ChargeConfUpdateRequest) error {
  38. if charge_utils.ChargeTypeM[req.ChargeType] == "" {
  39. return status.Error(10003, "费用类型不能为空")
  40. }
  41. req.BillPeriodType = 1
  42. if !req.LateFeeEnable {
  43. req.LateFeePercent = 0
  44. req.LateFeeMaxDays = 0
  45. req.LateFeeDay = 0
  46. }
  47. switch {
  48. case req.ChargeType == charge_utils.ChargeTypeProperty:
  49. req.ChargeTimeType = charge_utils.ChargeTimeTypePeriod
  50. if req.ChargeBasis != charge_utils.ChargeBasisUsedArea &&
  51. req.ChargeBasis != charge_utils.ChargeBasisArea &&
  52. req.ChargeBasis != charge_utils.ChargeBasisFix {
  53. return status.Error(10003, "物业费只能按房屋面积/使用面积/固定费用")
  54. }
  55. case charge_utils.IsPowerChargeType[req.ChargeType]:
  56. req.ChargeTimeType = 0
  57. if req.ChargeBasis != charge_utils.ChargeBasisUsed {
  58. return status.Error(10003, "水电气费只能按使用量")
  59. }
  60. case req.ChargeType == charge_utils.ChargeTypeSpace:
  61. req.ChargeTimeType = charge_utils.ChargeTimeTypePeriod
  62. if req.ChargeBasis != charge_utils.ChargeBasisSpaceArea &&
  63. req.ChargeBasis != charge_utils.ChargeBasisFix {
  64. return status.Error(10003, "车位管理费只能按房屋面积/使用面积/固定费用")
  65. }
  66. case req.ChargeType == charge_utils.ChargeTypeVehicle:
  67. req.ChargeTimeType = 0
  68. return checkVehicleChargeConfUpdateParam(req)
  69. }
  70. switch {
  71. case req.Id < 1:
  72. return status.Error(10003, "id不能为空")
  73. case req.GardenId < 1:
  74. return status.Error(10003, "小区不能为空")
  75. case charge_utils.ChargeBasisM[req.ChargeBasis] == "" && !charge_utils.IsPowerChargeType[req.ChargeType]:
  76. return status.Error(10003, "计费规则不能为空")
  77. case charge_utils.ChargeTypeM[req.ChargeType] == "":
  78. return status.Error(10003, "费用类型不能为空")
  79. case charge_utils.ChargeTimeTypeM[req.ChargeTimeType] == "" && !charge_utils.IsPowerChargeType[req.ChargeType]:
  80. return status.Error(10003, "缴费时间类型不能为空")
  81. case req.ChargeBasis == charge_utils.ChargeBasisFix && req.FixAmount < 1:
  82. return status.Error(10003, "按固定费用计价,固定费用不能为空")
  83. case req.ChargeName == "":
  84. return status.Error(10003, "费用名称不能为空")
  85. case req.ChargeBasis != charge_utils.ChargeBasisFix && req.UnitPrice < 1:
  86. return status.Error(10003, "单价不能为空")
  87. case req.ChargeTimeType == charge_utils.ChargeTimeTypePeriod && charge_utils.BillPeriodM[req.BillPeriod] == "":
  88. return status.Error(10003, "账单生成周期错误")
  89. case req.ChargeEffectiveTime < 1:
  90. return status.Error(10003, "费用生效时间错误")
  91. case req.LateFeeEnable:
  92. if req.LateFeeDay == 0 || req.LateFeePercent <= 0 {
  93. return status.Error(10003, "滞纳金参数不能为空")
  94. }
  95. }
  96. return nil
  97. }
  98. func ChargeConfUpdate(ctx context.Context, req *pb_v1.ChargeConfUpdateRequest) (reply *pb_v1.ChargeConfUpdateReply, err error) {
  99. reply = &pb_v1.ChargeConfUpdateReply{}
  100. // 捕获各个task中的异常并返回给调用者
  101. defer func() {
  102. if r := recover(); r != nil {
  103. err = fmt.Errorf("%+v", r)
  104. e := &status.Status{}
  105. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  106. logger.Error("err",
  107. zap.String("system_err", err.Error()),
  108. zap.Stack("stacktrace"))
  109. }
  110. }
  111. }()
  112. // 参数检查
  113. err = checkChargeConfUpdateParam(req)
  114. if err != nil {
  115. return nil, err
  116. }
  117. dbname := utils.GetGardenDbName(req.GardenId)
  118. // 获取旧数据
  119. old := dbmodel.NewChargeConf(dbname)
  120. where := map[string]interface{}{
  121. "id": req.Id,
  122. //"garden_id":req.GardenId,
  123. }
  124. err = old.Find(database.DB(), where)
  125. if err != nil && err != gorm.ErrRecordNotFound {
  126. return nil, errors.DataBaseError
  127. }
  128. if old.ID == 0 {
  129. return nil, errors.ErrRecordNotFound
  130. }
  131. if old.ChargeTimeType != req.ChargeTimeType ||
  132. old.ChargeType != req.ChargeType ||
  133. old.ChargeBasis != req.ChargeBasis {
  134. isBind, err := charge_utils.IsChargeBindObj(req.Id, dbname)
  135. if err != nil {
  136. return nil, err
  137. }
  138. if isBind {
  139. return nil, status.Error(10003, "已绑定对象,无法修改")
  140. }
  141. }
  142. reply.Origin = &pb_v1.ChargeConfUpdateRequest{
  143. GardenId: req.GardenId,
  144. ChargeType: old.ChargeType,
  145. ChargeName: old.ChargeName,
  146. ChargeTimeType: old.ChargeTimeType,
  147. ChargeBasis: old.ChargeBasis,
  148. FixAmount: old.FixAmount,
  149. FixAmountName: old.FixAmountName,
  150. UnitPrice: old.UnitPrice,
  151. ChargeEffectiveTime: old.ChargeEffectiveTime,
  152. BillPeriod: old.BillPeriod,
  153. BillPeriodType: old.BillPeriodType,
  154. LateFeeEnable: false,
  155. LateFeeMaxDays: old.LateFeeMaxDays,
  156. LateFeeDay: old.LateFeeDay,
  157. LateFeePercent: old.LateFeePercent,
  158. Id: req.Id,
  159. }
  160. if old.LateFeeEnable == 1 {
  161. reply.Origin.LateFeeEnable = true
  162. }
  163. // 更新新数据
  164. now := time.Now()
  165. chargeConf := dbmodel.NewChargeConf(dbname)
  166. lateFeeEnable := int32(1)
  167. if !req.LateFeeEnable {
  168. lateFeeEnable = 2
  169. }
  170. if req.ChargeBasis == charge_utils.ChargeBasisSelf {
  171. req.FixAmount = 0
  172. req.UnitPrice = 0
  173. }
  174. values := map[string]interface{}{
  175. "charge_name": req.ChargeName,
  176. "charge_basis": req.ChargeBasis,
  177. "fix_amount": req.FixAmount,
  178. "fix_amount_name": req.FixAmountName,
  179. "unit_price": req.UnitPrice,
  180. "updated_at": now,
  181. "charge_type": req.ChargeType,
  182. "charge_time_type": req.ChargeTimeType,
  183. "bill_period": req.BillPeriod,
  184. "charge_effective_time": req.ChargeEffectiveTime,
  185. "late_fee_enable": lateFeeEnable,
  186. "late_fee_day": req.LateFeeDay,
  187. "late_fee_max_days": req.LateFeeMaxDays,
  188. "late_fee_percent": req.LateFeePercent,
  189. }
  190. where = map[string]interface{}{
  191. "id": req.Id,
  192. }
  193. db := database.DB().Begin()
  194. err = chargeConf.Update(db, where, values)
  195. if err != nil {
  196. db.Rollback()
  197. return nil, errors.DataBaseError
  198. }
  199. // 同步更新账单中的滞纳金比例
  200. if lateFeeEnable != old.LateFeeEnable && !req.LateFeeEnable {
  201. bill := dbmodel.NewChargeBill(dbname)
  202. where := map[string]interface{}{
  203. "charge_id": req.Id,
  204. }
  205. values := map[string]interface{}{
  206. "late_fee": 0,
  207. }
  208. err = bill.Update(db, where, values)
  209. if err != nil {
  210. db.Rollback()
  211. return nil, errors.DataBaseError
  212. }
  213. }
  214. db.Commit()
  215. return reply, nil
  216. }