53 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package entities
 | ||
| 
 | ||
| import (
 | ||
| 	"time"
 | ||
| 
 | ||
| 	"github.com/google/uuid"
 | ||
| 	"github.com/shopspring/decimal"
 | ||
| 	"gorm.io/gorm"
 | ||
| )
 | ||
| 
 | ||
| // WalletTransaction 钱包扣款记录
 | ||
| // 记录API调用产生的扣款操作
 | ||
| type WalletTransaction struct {
 | ||
| 	// 基础标识
 | ||
| 	ID            string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"交易记录唯一标识"`
 | ||
| 	UserID        string `gorm:"type:varchar(36);not null;index" json:"user_id" comment:"扣款用户ID"`
 | ||
| 	ApiCallID     string `gorm:"type:varchar(64);not null;uniqueIndex" json:"api_call_id" comment:"关联API调用ID"`
 | ||
| 	TransactionID string `gorm:"type:varchar(36);not null;uniqueIndex" json:"transaction_id" comment:"交易ID"`
 | ||
| 	ProductID     string `gorm:"type:varchar(64);not null;index" json:"product_id" comment:"产品ID"`
 | ||
| 
 | ||
| 	// 扣款信息
 | ||
| 	Amount decimal.Decimal `gorm:"type:decimal(20,8);not null" json:"amount" comment:"扣款金额"`
 | ||
| 
 | ||
| 	// 时间戳字段
 | ||
| 	CreatedAt time.Time      `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
 | ||
| 	UpdatedAt time.Time      `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"`
 | ||
| 	DeletedAt gorm.DeletedAt `gorm:"index" json:"-" comment:"软删除时间"`
 | ||
| }
 | ||
| 
 | ||
| // TableName 指定数据库表名
 | ||
| func (WalletTransaction) TableName() string {
 | ||
| 	return "wallet_transactions"
 | ||
| }
 | ||
| 
 | ||
| // BeforeCreate GORM钩子:创建前自动生成UUID
 | ||
| func (t *WalletTransaction) BeforeCreate(tx *gorm.DB) error {
 | ||
| 	if t.ID == "" {
 | ||
| 		t.ID = uuid.New().String()
 | ||
| 	}
 | ||
| 	return nil
 | ||
| }
 | ||
| 
 | ||
| // NewWalletTransaction 工厂方法 - 创建扣款记录
 | ||
| func NewWalletTransaction(userID, apiCallID, transactionID, productID string, amount decimal.Decimal) *WalletTransaction {
 | ||
| 	return &WalletTransaction{
 | ||
| 		UserID:        userID,
 | ||
| 		ApiCallID:     apiCallID,
 | ||
| 		TransactionID: transactionID,
 | ||
| 		ProductID:     productID,
 | ||
| 		Amount:        amount,
 | ||
| 	}
 | ||
| }
 |