trade.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package model
  2. import (
  3. "time"
  4. "go-common/library/log"
  5. )
  6. // LogOrder .
  7. type LogOrder struct {
  8. ID int64
  9. OrderID string
  10. FromState string
  11. ToState string
  12. Desc string
  13. CTime time.Time
  14. MTime time.Time
  15. }
  16. // Order .
  17. type Order struct {
  18. ID int64
  19. OrderID string
  20. MID int64
  21. Biz string
  22. Platform string
  23. OID int64
  24. OType string
  25. Fee int64
  26. RealFee int64
  27. Currency string
  28. PayID string
  29. PayReason string
  30. PayTime time.Time
  31. RefundTime time.Time
  32. Version int64
  33. State string // created 已创建, paying 支付中, paid 已支付, failed 支付失败, closed 已关闭, expired 已超时, finished 已完成(支付成功且对账成功)
  34. CTime time.Time
  35. MTime time.Time
  36. }
  37. // IsPay 是否已支付
  38. func (o Order) IsPay() bool {
  39. return o.State == OrderStatePaid || o.State == OrderStateSettled
  40. }
  41. // IsRefunding 是否为退款中
  42. func (o Order) IsRefunding() bool {
  43. return o.State == OrderStateRefunding
  44. }
  45. // ParsePaidTime 解析支付成功时间
  46. func (o *Order) ParsePaidTime(t string) {
  47. o.PayTime = o.parsePayTime(t)
  48. }
  49. // ParseRefundedTime 解析退款成功时间
  50. func (o *Order) ParseRefundedTime(t string) {
  51. o.RefundTime = o.parsePayTime(t)
  52. }
  53. func (o Order) parsePayTime(tstr string) (t time.Time) {
  54. var err error
  55. if t, err = time.ParseInLocation("2006-01-02 15:04:05", tstr, time.Local); err != nil {
  56. log.Error("Order parse pay time from timestr: %s, err: %+v", t, err)
  57. t = time.Now() // 兜底
  58. }
  59. return
  60. }
  61. // ReturnState 返回前端显示state,重复下单的退费状态,被认为是已付费
  62. func (o Order) ReturnState() string {
  63. if o.State == OrderStateDupRefunded {
  64. return OrderStatePaid
  65. }
  66. return o.State
  67. }
  68. // UpdateState 更新状态,return true 更新成功,false 更新失败(无需更新)
  69. func (o *Order) UpdateState(state string) (changed bool) {
  70. switch o.State { // 当前state
  71. case OrderStateCreated, OrderStatePaying:
  72. switch state {
  73. case OrderStatePaying, OrderStatePaid, OrderStateFailed, OrderStateClosed, OrderStateExpired, OrderStateDupRefunded:
  74. changed = true
  75. }
  76. case OrderStatePaid:
  77. if state == OrderStateSettled || state == OrderStateRefunding || state == OrderStateDupRefunded || state == OrderStateRefunded || state == OrderStateBadDebt {
  78. changed = true
  79. }
  80. case OrderStateFailed, OrderStateClosed, OrderStateExpired:
  81. if state == OrderStatePaid {
  82. changed = true
  83. }
  84. case OrderStateRefunding:
  85. if state == OrderStateRefunded {
  86. changed = true
  87. }
  88. case OrderStateSettled:
  89. if state == OrderStateSettledRefunding || state == OrderStateSettledRefunded {
  90. changed = true
  91. }
  92. case OrderStateSettledRefunding:
  93. if state == OrderStateSettledRefunded {
  94. changed = true
  95. }
  96. case OrderStateSettledRefunded:
  97. if state == OrderStateRefundFinished {
  98. changed = true
  99. }
  100. }
  101. if changed {
  102. o.State = state
  103. } else {
  104. log.Error("order update state from : %s , to : %s", o.State, state)
  105. }
  106. return
  107. }
  108. // OrderRefund .
  109. type OrderRefund struct {
  110. ID int64
  111. OrderID int64
  112. State string
  113. CTime time.Time
  114. MTime time.Time
  115. }
  116. // OrderBadDebt .
  117. type OrderBadDebt struct {
  118. ID int64
  119. OrderID int64
  120. Type string
  121. State string
  122. CTime time.Time
  123. MTime time.Time
  124. }