charge_order_cancel.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "property-garden/impl/v1/charge_utils"
  15. dbmodel "property-garden/model"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. "time"
  19. )
  20. func checkChargeOrderCancelParam(req *pb_v1.ChargeOrderCancelRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.OrderId == "":
  25. return status.Error(10003, "订单号不能为空")
  26. }
  27. return nil
  28. }
  29. func ChargeOrderCancel(ctx context.Context, req *pb_v1.ChargeOrderCancelRequest) (reply *pb_v1.ChargeOrderCancelReply, err error) {
  30. reply = &pb_v1.ChargeOrderCancelReply{}
  31. // 捕获各个task中的异常并返回给调用者
  32. defer func() {
  33. if r := recover(); r != nil {
  34. err = fmt.Errorf("%+v", r)
  35. e := &status.Status{}
  36. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  37. logger.Error("err",
  38. zap.String("system_err", err.Error()),
  39. zap.Stack("stacktrace"))
  40. }
  41. }
  42. }()
  43. // 参数检查
  44. err = checkChargeOrderCancelParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. dbname := utils.GetGardenDbName(req.GardenId)
  49. // 查看订单
  50. order := dbmodel.NewChargeOrder(dbname)
  51. where := map[string]interface{}{
  52. "order_id":req.OrderId,
  53. }
  54. err = order.Find(database.DB(), where)
  55. if err != nil && err != gorm.ErrRecordNotFound{
  56. return nil, errors.DataBaseError
  57. }
  58. if order.OrderId == "" {
  59. return nil, errors.ErrRecordNotFound
  60. }
  61. if order.Status != charge_utils.PayStatusUnPay {
  62. return nil, status.Error(10003, "当前状态不能取消")
  63. }
  64. if time.Now().Unix() - order.CreatedAt.Unix() < 10 *60 {
  65. //return nil, status.Error(10003, "订单10钟内不能取消")
  66. }
  67. if req.ByUser {
  68. values := map[string]interface{}{
  69. "deleted":1,
  70. }
  71. err = order.Update(database.DB(), where, values)
  72. if err != nil {
  73. return nil, errors.DataBaseError
  74. }
  75. return reply, nil
  76. }
  77. // 删除订单
  78. db := database.DB().Begin()
  79. defer func () {
  80. if err != nil {
  81. db.Rollback()
  82. }
  83. }()
  84. err = order.Delete(db, where)
  85. if err != nil {
  86. return nil, errors.DataBaseError
  87. }
  88. // 修改账单状态为待缴
  89. bill := dbmodel.NewChargeBill(dbname)
  90. where = map[string]interface{}{
  91. "order_id":req.OrderId,
  92. }
  93. values := map[string]interface{}{
  94. "status":charge_utils.PayStatusUnPay,
  95. "order_id":"",
  96. }
  97. err = bill.Update(db, where, values)
  98. if err != nil {
  99. return nil, errors.DataBaseError
  100. }
  101. db.Commit()
  102. return reply, nil
  103. }