package models import ( "github.com/beego/beego/v2/client/orm" "time" ) type PaymentType string type PaymentStatus string const ( PaymentTypePay PaymentType = "pay" PaymentTypeRefund PaymentType = "refund" PaymentStatusDone PaymentStatus = "done" PaymentStatusFailed PaymentStatus = "failed" PaymentStatusPending PaymentStatus = "pending" ) type TradeOrderView struct { RealName string Mobile string ProductName string TransactionID string ProductOrderID string PaymentAccount string Amount string Currency string MerchantID string UserID int TemplateUserID int PaymentWay string PaymentType string PaymentStatus string DealTime string CreatedTime string } type TradeOrder struct { Id uint `gorm:"primaryKey;autoIncrement" json:"id"` TransactionId string `gorm:"column:transaction_id;size:255;default:null;comment:'第三方平台ID'" json:"transaction_id"` OrgTransactionId string `gorm:"column:org_transaction_id;size:255;default:null;comment:'原支付第三方平台ID'" json:"org_transaction_id"` ProductOrderId string `gorm:"column:product_order_id;size:255;default:null;comment:'商品订单号'" json:"product_order_id"` ProductName string `gorm:"column:product_name;size:255;default:null;comment:'商品名称'" json:"product_name"` RealName string `gorm:"column:real_name;default:null;comment:'姓名'" json:"real_name"` AreaCode string `gorm:"column:area_code;default:null;comment:'手机区号'" json:"area_code"` Mobile string `gorm:"column:mobile;default:null;comment:'手机号'" json:"mobile"` PaymentAccount string `gorm:"column:payment_account;size:255;default:null;comment:'支付账号'" json:"payment_account"` PaymentWay PaymentWay `gorm:"column:payment_way;type:enum('wechat');default:null;comment:'支付渠道'" json:"payment_way"` Amount string `gorm:"column:amount;size:20;default:null;comment:'支付金额'" json:"amount"` Currency string `gorm:"column:currency;size:255;default:null;comment:'货币'" json:"currency"` MerchantId string `gorm:"column:merchant_id;size:255;default:null;comment:'商户id'" json:"merchant_id"` UserId int `gorm:"column:user_id;default:null;comment:'用户id'" json:"user_id"` TemplateUserId int `gorm:"column:template_user_id;default:null;comment:'临时用户id'" json:"template_user_id"` PaymentType PaymentType `gorm:"column:payment_type;type:enum('pay','refund');default:null;comment:'订单类型'" json:"payment_type"` PaymentStatus PaymentStatus `gorm:"column:payment_status;type:enum('pending','done','failed');default:null;comment:'支付状态'" json:"payment_status"` DealTime time.Time `gorm:"column:deal_time;default:null;comment:'完成时间'" json:"deal_time"` CreatedTime time.Time `gorm:"column:created_time;default:null" json:"created_time"` UpdatedTime time.Time `gorm:"column:updated_time;default:null" json:"updated_time"` } func (TradeOrder) TableName() string { return "trade_orders" } func GetTradeOrderByCondition(condition string, sortCondition string, startSize int, pageSize int) (list []*TradeOrder, err error) { o := orm.NewOrm() sql := `select * from trade_orders where 1=1 ` if condition != "" { sql += condition } if sortCondition != "" { sql += sortCondition } sql += ` LIMIT ?,?` _, err = o.Raw(sql, startSize, pageSize).QueryRows(&list) return } func GetTradeOrderListByCondition(condition string, sortCondition string) (list []*TradeOrder, err error) { o := orm.NewOrm() sql := `select * from trade_orders where 1=1 ` if condition != "" { sql += condition } if sortCondition != "" { sql += sortCondition } _, err = o.Raw(sql).QueryRows(&list) return } func GetTradeOrderCountByCondition(condition string) (total int, err error) { o := orm.NewOrm() err = o.Raw("select count(*) from trade_orders where 1=1 " + condition).QueryRow(&total) return } func GetTradeOrderByNo(tradeNo string) (tradeOrder TradeOrder, err error) { o := orm.NewOrm() err = o.Raw("select * from trade_orders where transaction_id=? ", tradeNo).QueryRow(&tradeOrder) return } func (to *TradeOrder) Refund(productOrder *ProductOrder) (err error) { o := orm.NewOrm() tx, _ := o.Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() _, err = tx.Insert(to) _, err = tx.Update(productOrder, "status", "refund_status", "refund_trade_id", "remark", "refund_amount") return }