charge_order_ticket.go 3.2 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 checkChargeOrderTicketParam(req *pb_v1.ChargeOrderTicketRequest) 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 ChargeOrderTicket(ctx context.Context, req *pb_v1.ChargeOrderTicketRequest) (reply *pb_v1.ChargeOrderTicketReply, err error) {
  30. reply = &pb_v1.ChargeOrderTicketReply{}
  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 = checkChargeOrderTicketParam(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. if order.Status != charge_utils.PayStatusPayed {
  61. return nil, status.Error(10003, "当前订单未支付")
  62. }
  63. reply.ShouldPayAmount = order.Amount
  64. reply.Comment = order.Comment
  65. bill := dbmodel.NewChargeBillPayed(dbname)
  66. where = map[string]interface{}{
  67. "order_id": req.OrderId,
  68. }
  69. // 账单列表
  70. list, err := bill.List(database.DB(), where, nil, -1, -1, "")
  71. if err != nil {
  72. return nil, errors.DataBaseError
  73. }
  74. reply.List = make([]*pb_v1.ChargeOrderTicketItem, len(list))
  75. for i, v := range list {
  76. chargeDesc := dbmodel.ChargeDesc{}
  77. json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc)
  78. item := &pb_v1.ChargeOrderTicketItem{
  79. Start: time.Unix(v.ChargeStart, 0).Format("2006-01-02"),
  80. End: time.Unix(v.ChargeEnd, 0).Format("2006-01-02"),
  81. ChargeType: v.ChargeType,
  82. ChargeName: v.ChargeName,
  83. Amount: v.Amount,
  84. ObjName: v.ObjName,
  85. ObjType: v.ObjType,
  86. LateFee: v.LateFee,
  87. UnitPrice: chargeDesc.UnitPrice,
  88. PayTime: time.Unix(v.PayTime, 0).Format("2006-01-02 15:04:05"),
  89. }
  90. if v.ObjType == charge_utils.ObjTypeHouse {
  91. if charge_utils.IsPowerChargeType[v.ChargeType] {
  92. item.AreaUsed = chargeDesc.PowerCurrentUsed - chargeDesc.PowerLastUsed
  93. } else {
  94. item.AreaUsed = chargeDesc.ObjArea
  95. }
  96. }
  97. if v.ObjType == charge_utils.ObjTypeSpace {
  98. item.AreaUsed = chargeDesc.ObjArea
  99. }
  100. if v.ObjType == charge_utils.ObjTypeVehicle {
  101. if charge_utils.ChargeBasisSelf == chargeDesc.ChargeBasis {
  102. item.CustomFee = chargeDesc.CustomFee
  103. }
  104. }
  105. reply.List[i] = item
  106. }
  107. return reply, nil
  108. }