charge_order_info.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 checkChargeOrderInfoParam(req *pb_v1.ChargeOrderInfoRequest) 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 ChargeOrderInfo(ctx context.Context, req *pb_v1.ChargeOrderInfoRequest) (reply *pb_v1.ChargeOrderInfoReply, err error) {
  30. reply = &pb_v1.ChargeOrderInfoReply{}
  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 = checkChargeOrderInfoParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. dbname := utils.GetGardenDbName(req.GardenId)
  49. order := dbmodel.NewChargeOrder(dbname)
  50. where := map[string]interface{}{
  51. "order_id": req.OrderId,
  52. }
  53. err = order.Find(database.DB(), where)
  54. if err != nil && err != gorm.ErrRecordNotFound {
  55. return nil, errors.DataBaseError
  56. }
  57. if order.OrderId == "" {
  58. return nil, errors.ErrRecordNotFound
  59. }
  60. bill := dbmodel.NewChargeBillPayed(dbname)
  61. if order.Status == charge_utils.PayStatusUnPay {
  62. bill.SetTable(dbname)
  63. }
  64. where = map[string]interface{}{
  65. "order_id": req.OrderId,
  66. }
  67. // 账单列表
  68. var list []dbmodel.TChargeBill
  69. if order.OnlinePay == 1 && order.Status == charge_utils.PayStatusUnPay {
  70. onlineBill := dbmodel.TChargeBill{}
  71. json.Unmarshal([]byte(order.OnlinePayBill), &onlineBill)
  72. list = []dbmodel.TChargeBill{onlineBill}
  73. } else {
  74. list, err = bill.List(database.DB(), where, nil, -1, -1, "")
  75. if err != nil {
  76. return nil, errors.DataBaseError
  77. }
  78. }
  79. reply.List = make([]*pb_v1.ChargeOrderBillItem, len(list))
  80. for i, v := range list {
  81. chargeDesc := dbmodel.ChargeDesc{}
  82. json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc)
  83. item := &pb_v1.ChargeOrderBillItem{
  84. Start: time.Unix(v.ChargeStart, 0).Format("2006-01-02"),
  85. End: time.Unix(v.ChargeEnd, 0).Format("2006-01-02"),
  86. ChargeType: v.ChargeType,
  87. ChargeName: v.ChargeName,
  88. Amount: v.Amount,
  89. LastUsed: chargeDesc.PowerLastUsed,
  90. CurrentUsed: chargeDesc.PowerCurrentUsed,
  91. LateFee: v.LateFee,
  92. BillId: v.ID,
  93. ChargeDesc: charge_utils.GetChargeDesc(chargeDesc),
  94. ObjName: v.ObjName,
  95. }
  96. reply.List[i] = item
  97. }
  98. return reply, nil
  99. }