charge_recover_bill.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 checkChargeRecoverBillParam(req *pb_v1.ChargeRecoverBillRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. case req.BillId == 0:
  23. return status.Error(10003, "账单id不能为空")
  24. }
  25. return nil
  26. }
  27. func ChargeRecoverBill(ctx context.Context, req *pb_v1.ChargeRecoverBillRequest) (reply *pb_v1.ChargeRecoverBillReply, err error) {
  28. reply = &pb_v1.ChargeRecoverBillReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. // 参数检查
  42. err = checkChargeRecoverBillParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. dbname := utils.GetGardenDbName(req.GardenId)
  47. bill := dbmodel.NewChargeBillDel(dbname)
  48. where := map[string]interface{}{
  49. "id": req.BillId,
  50. }
  51. err = bill.Find(database.DB(), where)
  52. if err != nil && err != gorm.ErrRecordNotFound {
  53. return nil, errors.DataBaseError
  54. }
  55. if bill.ID == 0 {
  56. return nil, errors.ErrRecordNotFound
  57. }
  58. recover := dbmodel.TChargeBill{
  59. ID: 0,
  60. ObjType: bill.ObjType,
  61. ObjId: bill.ObjId,
  62. ChargeId: bill.ChargeId,
  63. ChargeBindId: bill.ChargeBindId,
  64. ChargeTimeType: bill.ChargeTimeType,
  65. ChargeDesc: bill.ChargeDesc,
  66. Comment: bill.Comment,
  67. Amount: bill.Amount,
  68. ChargePowerId: bill.ChargePowerId,
  69. ChargeType: bill.ChargeType,
  70. ChargeName: bill.ChargeName,
  71. Uid: bill.Uid,
  72. PayMode: bill.PayMode,
  73. PackageId: bill.PackageId,
  74. ChargeEnd: bill.ChargeEnd,
  75. ChargeStart: bill.ChargeStart,
  76. OrderId: bill.OrderId,
  77. PayTime: bill.PayTime,
  78. HouseId: bill.HouseId,
  79. ObjName: bill.ObjName,
  80. LateFee: bill.LateFee,
  81. CreatedAt: bill.CreatedAt,
  82. UpdatedAt: bill.UpdatedAt,
  83. PayType: bill.PayType,
  84. Status: bill.Status,
  85. }
  86. db := database.DB().Begin()
  87. defer func() {
  88. if err != nil {
  89. db.Rollback()
  90. }
  91. }()
  92. recover.SetTable(dbname)
  93. oldBill := dbmodel.NewChargeBill(dbname)
  94. filter := map[string]interface{}{
  95. "charge_bind_id": bill.ChargeBindId,
  96. "charge_id": bill.ChargeId,
  97. }
  98. or := map[string]interface{}{
  99. fmt.Sprintf("charge_end <= %d and charge_end >= %d", bill.ChargeEnd, bill.ChargeStart): "",
  100. fmt.Sprintf("charge_start <= %d and charge_start >= %d", bill.ChargeEnd, bill.ChargeStart): "",
  101. }
  102. count := int64(0)
  103. count, err = oldBill.Count(db, filter, or)
  104. if err != nil {
  105. return nil, errors.DataBaseError
  106. }
  107. if count > 0 {
  108. return nil, status.Error(10003, "无法恢复,当前账单和已有账单有冲突")
  109. }
  110. err = recover.Insert(db)
  111. if err != nil {
  112. return nil, errors.DataBaseError
  113. }
  114. if recover.ChargePowerId > 0 {
  115. power := dbmodel.NewChargePower(dbname)
  116. filter := map[string]interface{}{
  117. "bill_id": 0,
  118. "id": recover.ChargePowerId,
  119. }
  120. values := map[string]interface{}{
  121. "bill_id": recover.ID,
  122. }
  123. err = power.Update(db, filter, values)
  124. if err != nil {
  125. return nil, errors.DataBaseError
  126. }
  127. }
  128. err = bill.Delete(db, where)
  129. if err != nil {
  130. return nil, errors.DataBaseError
  131. }
  132. db.Commit()
  133. return reply, nil
  134. }