del.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "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. )
  19. func checkChargeConfDelParam(req *pb_v1.ChargeConfDelRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case req.Id < 1:
  24. return status.Error(10003, "id 不能为空")
  25. }
  26. return nil
  27. }
  28. func ChargeConfDel(ctx context.Context, req *pb_v1.ChargeConfDelRequest) (reply *pb_v1.ChargeConfDelReply, err error) {
  29. reply = &pb_v1.ChargeConfDelReply{}
  30. // 捕获各个task中的异常并返回给调用者
  31. defer func() {
  32. if r := recover(); r != nil {
  33. err = fmt.Errorf("%+v", r)
  34. e := &status.Status{}
  35. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  36. logger.Error("err",
  37. zap.String("system_err", err.Error()),
  38. zap.Stack("stacktrace"))
  39. }
  40. }
  41. }()
  42. // 参数检查
  43. err = checkChargeConfDelParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. dbname := utils.GetGardenDbName(req.GardenId)
  48. // 获取旧数据
  49. old := dbmodel.NewChargeConf(dbname)
  50. where := map[string]interface{}{
  51. "id":req.Id,
  52. //"garden_id":req.GardenId,
  53. }
  54. err = old.Find(database.DB(), where)
  55. if err != nil && err != gorm.ErrRecordNotFound {
  56. return nil, errors.DataBaseError
  57. }
  58. if old.ID == 0 {
  59. return nil, errors.ErrRecordNotFound
  60. }
  61. reply.Origin = &pb_v1.ChargeConfUpdateRequest{
  62. GardenId:req.GardenId,
  63. ChargeType:old.ChargeType,
  64. ChargeName:old.ChargeName,
  65. ChargeTimeType:old.ChargeTimeType,
  66. ChargeBasis:old.ChargeBasis,
  67. FixAmount:old.FixAmount,
  68. FixAmountName:old.FixAmountName,
  69. UnitPrice:old.UnitPrice,
  70. ChargeEffectiveTime:old.ChargeEffectiveTime,
  71. BillPeriod:old.BillPeriod,
  72. BillPeriodType:old.BillPeriodType,
  73. LateFeeEnable:false,
  74. LateFeeMaxDays:old.LateFeeMaxDays,
  75. LateFeeDay:old.LateFeeDay,
  76. LateFeePercent:old.LateFeePercent,
  77. Id:req.Id,
  78. }
  79. if old.LateFeeEnable == 1 {
  80. reply.Origin.LateFeeEnable = true
  81. }
  82. // 删除数据
  83. chargeConf := dbmodel.NewChargeConf(dbname)
  84. db := database.DB().Begin()
  85. defer func() {
  86. if err != nil {
  87. db.Rollback()
  88. }
  89. }()
  90. err = chargeConf.Delete(db, where)
  91. if err != nil {
  92. return nil, errors.DataBaseError
  93. }
  94. isBind, err := charge_utils.IsChargeBindObj(req.Id, dbname)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if isBind {
  99. return nil, status.Error(10003, "操作失败,已绑定对象")
  100. }
  101. db.Commit()
  102. return reply, nil
  103. }