package repository import ( "code.30cm.net/digimon/app-cloudep-wallet-service/pkg/domain/entity" "code.30cm.net/digimon/app-cloudep-wallet-service/pkg/domain/wallet" "context" "github.com/shopspring/decimal" ) // UserWalletManager 負責單一使用者對單一資產的錢包操作: // · 建立初始錢包 // · 查詢/鎖定餘額 // · 本地暫存變動(錢包層面與訂單層面) // · 最後一次性寫回資料庫 type UserWalletManager interface { // InitWallets 為新使用者建立所有類型錢包,並寫入資料庫與本地緩存 InitWallets(ctx context.Context, brand string) ([]entity.Wallet, error) // FetchAllWallets 查詢資料庫所有錢包類型餘額,不鎖表,並更新本地緩存 FetchAllWallets(ctx context.Context) ([]entity.Wallet, error) // FetchWalletsByTypes 查詢指定類型錢包餘額,不鎖表 FetchWalletsByTypes(ctx context.Context, kinds []wallet.Types) ([]entity.Wallet, error) // LockWalletsByTypes 查詢並鎖定指定類型錢包 (FOR UPDATE) LockWalletsByTypes(ctx context.Context, kinds []wallet.Types) ([]entity.Wallet, error) // GetCachedBalance 從本地緩存讀取指定類型錢包餘額 GetCachedBalance(kind wallet.Types) decimal.Decimal // CreditWallet 增加本地緩存錢包餘額,並記錄一筆 WalletTransaction CreditWallet(kind wallet.Types, orderID string, amount decimal.Decimal) error // DebitWallet 扣減本地緩存錢包餘額,並記錄一筆 WalletTransaction DebitWallet(kind wallet.Types, orderID string, amount decimal.Decimal) error // BuildWalletTransactions 填充所有暫存 WalletTransaction 的共用欄位,回傳可落庫之切片 BuildWalletTransactions(txID int64, brand string, biz wallet.BusinessName) []entity.WalletTransaction // CommitWalletBalances 將本地緩存的最終錢包餘額一次性寫回 wallet 表 CommitWalletBalances(ctx context.Context) error // FetchOrderTx 從 transaction 表讀取一筆訂單交易,並緩存其後餘額 FetchOrderTx(ctx context.Context, orderID string) (entity.Transaction, error) // LockOrderTx 同 FetchOrderTx 但加上 FOR UPDATE LockOrderTx(ctx context.Context, orderID string) (entity.Transaction, error) // CreditOrderBalance 增加本地緩存的訂單後餘額 CreditOrderBalance(txID int64, amount decimal.Decimal) error // DebitOrderBalance 扣減本地緩存的訂單後餘額 DebitOrderBalance(txID int64, amount decimal.Decimal) error // CommitOrderBalances 將本地暫存的訂單後餘額寫回 transaction.post_transfer_balance CommitOrderBalances(ctx context.Context) error // HasAvailableWallet 檢查是否已有「可用餘額」類型的錢包 HasAvailableWallet(ctx context.Context) (bool, error) // Reset 清空本地所有緩存 Reset() }