unbind.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. )
  18. func checkChargeUnbindParam(req *pb_v1.ChargeUnbindRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. case req.ObjId == 0:
  23. return status.Error(10003, "id不能为空")
  24. case req.ChargeId == 0:
  25. return status.Error(10003, "项目不能为空")
  26. }
  27. return nil
  28. }
  29. func setPowerBillIdZero(db *gorm.DB, billId int64, dbname string) error {
  30. p := dbmodel.NewChargePower(dbname)
  31. where := map[string]interface{}{
  32. "bill_id":billId,
  33. }
  34. values := map[string]interface{}{
  35. "bill_id":0,
  36. }
  37. err := p.Update(db, where, values)
  38. if err != nil {
  39. return errors.DataBaseError
  40. }
  41. return nil
  42. }
  43. func ChargeUnbind(ctx context.Context, req *pb_v1.ChargeUnbindRequest) (reply *pb_v1.ChargeUnbindReply, err error) {
  44. reply = &pb_v1.ChargeUnbindReply{}
  45. // 捕获各个task中的异常并返回给调用者
  46. defer func() {
  47. if r := recover(); r != nil {
  48. err = fmt.Errorf("%+v", r)
  49. e := &status.Status{}
  50. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  51. logger.Error("err",
  52. zap.String("system_err", err.Error()),
  53. zap.Stack("stacktrace"))
  54. }
  55. }
  56. }()
  57. // 参数检查
  58. err = checkChargeUnbindParam(req)
  59. if err != nil {
  60. return nil, err
  61. }
  62. dbname := utils.GetGardenDbName(req.GardenId)
  63. old := dbmodel.NewChargeBind(dbname)
  64. where := map[string]interface{}{
  65. "obj_id": req.ObjId,
  66. "charge_id": req.ChargeId,
  67. }
  68. err = old.Find(database.DB(), where)
  69. if err != nil && err != gorm.ErrRecordNotFound {
  70. return nil, errors.DataBaseError
  71. }
  72. if old.ID == 0 {
  73. return nil, errors.ErrRecordNotFound
  74. }
  75. reply.Origin = &pb_v1.ChargeUnbindData{
  76. Id: old.ID,
  77. GargenId: req.GardenId,
  78. ObjId: old.ObjId,
  79. ChargeId: old.ChargeId,
  80. }
  81. //chargeHouseId := old.ID
  82. //chargeType := old.ChargeType
  83. //objType := old.ObjType
  84. db := database.DB().Begin()
  85. defer func() {
  86. if err != nil {
  87. db.Rollback()
  88. }
  89. }()
  90. err = old.Delete(db, where)
  91. if err != nil {
  92. return nil, errors.DataBaseError
  93. }
  94. /* 先不删账单
  95. // 更新抄表记录 billid; 删除未缴账单,不删除支付中账
  96. bill := dbmodel.NewChargeBill(dbname)
  97. where = map[string]interface{}{
  98. "charge_bind_id":old.ID,
  99. }
  100. billList, err := bill.List(database.DB(), where, nil, -1, -1)
  101. if err != nil {
  102. return nil, errors.DataBaseError
  103. }
  104. for _, v := range billList {
  105. if v.ChargePowerId > 0 {
  106. err = setPowerBillIdZero(db, v.ID, dbname)
  107. if err != nil {
  108. return nil, err
  109. }
  110. }
  111. }
  112. err = bill.Delete(db, where)
  113. if err != nil {
  114. return nil, errors.DataBaseError
  115. }
  116. */
  117. db.Commit()
  118. return reply, nil
  119. }