trade_order.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type PaymentType string
  7. type PaymentStatus string
  8. const (
  9. PaymentTypePay PaymentType = "pay"
  10. PaymentTypeRefund PaymentType = "refund"
  11. PaymentStatusDone PaymentStatus = "done"
  12. PaymentStatusFailed PaymentStatus = "failed"
  13. PaymentStatusPending PaymentStatus = "pending"
  14. )
  15. type TradeOrderView struct {
  16. RealName string
  17. Mobile string
  18. ProductName string
  19. TransactionID string
  20. ProductOrderID string
  21. PaymentAccount string
  22. Amount string
  23. Currency string
  24. MerchantID string
  25. UserID int
  26. TemplateUserID int
  27. PaymentWay string
  28. PaymentType string
  29. PaymentStatus string
  30. DealTime string
  31. CreatedTime string
  32. }
  33. type TradeOrder struct {
  34. Id uint `gorm:"primaryKey;autoIncrement" json:"id"`
  35. TransactionId string `gorm:"column:transaction_id;size:255;default:null;comment:'第三方平台ID'" json:"transaction_id"`
  36. OrgTransactionId string `gorm:"column:org_transaction_id;size:255;default:null;comment:'原支付第三方平台ID'" json:"org_transaction_id"`
  37. ProductOrderId string `gorm:"column:product_order_id;size:255;default:null;comment:'商品订单号'" json:"product_order_id"`
  38. ProductName string `gorm:"column:product_name;size:255;default:null;comment:'商品名称'" json:"product_name"`
  39. RealName string `gorm:"column:real_name;default:null;comment:'姓名'" json:"real_name"`
  40. AreaCode string `gorm:"column:area_code;default:null;comment:'手机区号'" json:"area_code"`
  41. Mobile string `gorm:"column:mobile;default:null;comment:'手机号'" json:"mobile"`
  42. PaymentAccount string `gorm:"column:payment_account;size:255;default:null;comment:'支付账号'" json:"payment_account"`
  43. PaymentWay PaymentWay `gorm:"column:payment_way;type:enum('wechat');default:null;comment:'支付渠道'" json:"payment_way"`
  44. Amount string `gorm:"column:amount;size:20;default:null;comment:'支付金额'" json:"amount"`
  45. Currency string `gorm:"column:currency;size:255;default:null;comment:'货币'" json:"currency"`
  46. MerchantId string `gorm:"column:merchant_id;size:255;default:null;comment:'商户id'" json:"merchant_id"`
  47. UserId int `gorm:"column:user_id;default:null;comment:'用户id'" json:"user_id"`
  48. TemplateUserId int `gorm:"column:template_user_id;default:null;comment:'临时用户id'" json:"template_user_id"`
  49. PaymentType PaymentType `gorm:"column:payment_type;type:enum('pay','refund');default:null;comment:'订单类型'" json:"payment_type"`
  50. PaymentStatus PaymentStatus `gorm:"column:payment_status;type:enum('pending','done','failed');default:null;comment:'支付状态'" json:"payment_status"`
  51. DealTime time.Time `gorm:"column:deal_time;default:null;comment:'完成时间'" json:"deal_time"`
  52. CreatedTime time.Time `gorm:"column:created_time;default:null" json:"created_time"`
  53. UpdatedTime time.Time `gorm:"column:updated_time;default:null" json:"updated_time"`
  54. }
  55. func (TradeOrder) TableName() string {
  56. return "trade_orders"
  57. }
  58. func GetTradeOrderByCondition(condition string, sortCondition string, startSize int, pageSize int) (list []*TradeOrder, err error) {
  59. o := orm.NewOrm()
  60. sql := `select * from trade_orders where 1=1 `
  61. if condition != "" {
  62. sql += condition
  63. }
  64. if sortCondition != "" {
  65. sql += sortCondition
  66. }
  67. sql += ` LIMIT ?,?`
  68. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&list)
  69. return
  70. }
  71. func GetTradeOrderListByCondition(condition string, sortCondition string) (list []*TradeOrder, err error) {
  72. o := orm.NewOrm()
  73. sql := `select * from trade_orders where 1=1 `
  74. if condition != "" {
  75. sql += condition
  76. }
  77. if sortCondition != "" {
  78. sql += sortCondition
  79. }
  80. _, err = o.Raw(sql).QueryRows(&list)
  81. return
  82. }
  83. func GetTradeOrderCountByCondition(condition string) (total int, err error) {
  84. o := orm.NewOrm()
  85. err = o.Raw("select count(*) from trade_orders where 1=1 " + condition).QueryRow(&total)
  86. return
  87. }
  88. func GetTradeOrderByNo(tradeNo string) (tradeOrder TradeOrder, err error) {
  89. o := orm.NewOrm()
  90. err = o.Raw("select * from trade_orders where transaction_id=? ", tradeNo).QueryRow(&tradeOrder)
  91. return
  92. }
  93. func (to *TradeOrder) Refund(productOrder *ProductOrder) (err error) {
  94. o := orm.NewOrm()
  95. tx, _ := o.Begin()
  96. defer func() {
  97. if err != nil {
  98. tx.Rollback()
  99. } else {
  100. tx.Commit()
  101. }
  102. }()
  103. _, err = tx.Insert(to)
  104. _, err = tx.Update(productOrder, "status", "refund_status", "refund_trade_id", "remark", "refund_amount")
  105. return
  106. }