trade_order.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package order
  2. import (
  3. "eta/eta_mini_ht_api/models"
  4. "gorm.io/gorm"
  5. "time"
  6. )
  7. type PaymentStatus string
  8. type PaymentType string
  9. type PaymentWay string
  10. const (
  11. PaymentStatusPending PaymentStatus = "pending"
  12. PaymentStatusProcessing PaymentStatus = "processing"
  13. PaymentStatusDone PaymentStatus = "done"
  14. PaymentStatusFailed PaymentStatus = "failed"
  15. PaymentTypePay PaymentType = "pay"
  16. PaymentTypeRefund PaymentType = "refund"
  17. WechatPayWay PaymentWay = "wechat"
  18. AliPayWay PaymentWay = "alipay"
  19. )
  20. type TradeOrder struct {
  21. ID int `gorm:"column:id;primaryKey"`
  22. TransactionID string `gorm:"column:transaction_id;type:varchar(255);comment:第三方平台ID"`
  23. OrgTransactionID string `gorm:"column:org_transaction_id;type:varchar(255);comment:第三方平台ID"`
  24. ProductOrderID string `gorm:"column:product_order_id;type:varchar(255);comment:商品订单号"`
  25. ProductName string `gorm:"column:product_name;type:varchar(255);comment:商品名称"`
  26. RealName string `gorm:"column:real_name;default:null;comment:'姓名'" json:"real_name"`
  27. AreaCode string `gorm:"column:area_code;default:null;comment:'手机区号'" json:"area_code"`
  28. Mobile string `gorm:"column:mobile;default:null;comment:'手机号'" json:"mobile"`
  29. PaymentAccount string `gorm:"column:payment_account;type:varchar(255);comment:支付账号"`
  30. PaymentWay PaymentWay `gorm:"column:payment_way;type:enum('wechat');comment:支付渠道"`
  31. Amount string `gorm:"column:amount;type:varchar(20);comment:支付金额"`
  32. Currency string `gorm:"column:currency;type:varchar(255);comment:货币"`
  33. MerchantID string `gorm:"column:merchant_id;type:varchar(255);comment:商户id"`
  34. UserID int `gorm:"column:user_id;type:int(11);comment:用户id"`
  35. TemplateUserID int `gorm:"column:template_user_id;type:int(11);comment:临时用户id"`
  36. PaymentType PaymentType `gorm:"column:payment_type;type:enum('pay','refund');comment:订单类型"`
  37. PaymentStatus PaymentStatus `gorm:"column:payment_status;type:enum('pending','processing','done','failed');comment:支付状态"`
  38. DealTime time.Time `gorm:"column:deal_time;type:datetime;comment:完成时间"`
  39. CreatedTime time.Time `gorm:"column:created_time;type:datetime;comment:创建时间"`
  40. UpdatedTime time.Time `gorm:"column:updated_time;type:datetime;comment:更新时间"`
  41. }
  42. func (to *TradeOrder) TableName() string {
  43. return "trade_orders"
  44. }
  45. func (to *TradeOrder) BeforeCreate(_ *gorm.DB) (err error) {
  46. to.CreatedTime = time.Now()
  47. to.PaymentStatus = PaymentStatusPending
  48. return
  49. }
  50. func GetUnFailedTradeFlowByProductOrder(no string) (list []TradeOrder, err error) {
  51. db := models.Main()
  52. err = db.Where("product_order_id=? and payment_status!=? order by updated_time,created_time desc", no, PaymentStatusFailed).Find(&list).Error
  53. return
  54. }
  55. func GetTradeOrderByNo(tradeOrderNo string, paymentType PaymentType) (order TradeOrder, err error) {
  56. db := models.Main()
  57. err = db.Where("transaction_id=? and payment_type =?", tradeOrderNo, paymentType).First(&order).Error
  58. return
  59. }
  60. func DealRefundOrder(order TradeOrder, isSuccess bool) (err error) {
  61. var paymentStatus PaymentStatus
  62. var refundStatus RefundStatus
  63. if isSuccess {
  64. paymentStatus = PaymentStatusDone
  65. refundStatus = RefundStatusSuccess
  66. } else {
  67. paymentStatus = PaymentStatusFailed
  68. refundStatus = RefundStatusFailed
  69. }
  70. db := models.Main()
  71. tx := db.Begin()
  72. defer func() {
  73. if err != nil {
  74. tx.Rollback()
  75. return
  76. }
  77. tx.Commit()
  78. }()
  79. err = tx.Model(&TradeOrder{}).Where("id=?", order.ID).Updates(map[string]interface{}{
  80. "payment_status": paymentStatus,
  81. "deal_time": time.Now(),
  82. }).Error
  83. if err != nil {
  84. return
  85. }
  86. err = tx.Model(&ProductOrder{}).Where("order_id=?", order.ProductOrderID).Updates(map[string]interface{}{
  87. "refund_status": refundStatus,
  88. "refund_finish_time": time.Now(),
  89. }).Error
  90. return
  91. }
  92. func DealPaymentOrder(order TradeOrder, isSuccess bool, validDuration string) (err error) {
  93. var paymentStatus PaymentStatus
  94. if isSuccess {
  95. paymentStatus = PaymentStatusDone
  96. } else {
  97. paymentStatus = PaymentStatusFailed
  98. }
  99. db := models.Main()
  100. tx := db.Begin()
  101. defer func() {
  102. if err != nil {
  103. tx.Rollback()
  104. return
  105. }
  106. tx.Commit()
  107. }()
  108. err = tx.Model(&TradeOrder{}).Where("id=?", order.ID).Updates(map[string]interface{}{
  109. "payment_status": paymentStatus,
  110. "deal_time": time.Now(),
  111. }).Error
  112. if err != nil {
  113. return
  114. }
  115. if isSuccess {
  116. err = tx.Model(&ProductOrder{}).Where("order_id=?", order.ProductOrderID).Updates(map[string]interface{}{
  117. "status": OrderStatusPaid,
  118. "valid_duration": validDuration,
  119. }).Error
  120. }
  121. return
  122. }