// Copyright © 2023 OpenIM. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package database import ( "context" "git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model" "github.com/openimsdk/tools/db/pagination" ) // WalletUpdateParams 钱包更新参数 type WalletUpdateParams struct { UserID string // 用户ID Operation string // 操作类型:set(设置为指定金额)、add(增加金额)、subtract(减少金额) Amount int64 // 金额(分) OldBalance int64 // 旧余额(用于乐观锁检查) OldVersion int64 // 旧版本号(用于乐观锁检查) } // WalletUpdateResult 钱包更新结果 type WalletUpdateResult struct { NewBalance int64 // 新余额 NewVersion int64 // 新版本号 Success bool // 是否成功 } // Wallet defines the operations for managing user wallets in MongoDB. type Wallet interface { // Create creates a new wallet record. Create(ctx context.Context, wallet *model.Wallet) error // Take retrieves a wallet by user ID. Returns an error if not found. Take(ctx context.Context, userID string) (*model.Wallet, error) // UpdateBalance updates the balance of a wallet. UpdateBalance(ctx context.Context, userID string, balance int64) error // UpdateBalanceByAmount updates the balance by adding/subtracting an amount. UpdateBalanceByAmount(ctx context.Context, userID string, amount int64) error // UpdateBalanceWithVersion 使用版本号更新余额(防止并发覆盖) // 如果 oldVersion 与当前版本号不匹配,返回错误 UpdateBalanceWithVersion(ctx context.Context, params *WalletUpdateParams) (*WalletUpdateResult, error) // FindAllWallets finds all wallets with pagination. FindAllWallets(ctx context.Context, pagination pagination.Pagination) (total int64, wallets []*model.Wallet, err error) // FindWalletsByUserIDs finds wallets by user IDs. FindWalletsByUserIDs(ctx context.Context, userIDs []string) ([]*model.Wallet, error) } // WalletBalanceRecord defines the operations for managing wallet balance records in MongoDB. type WalletBalanceRecord interface { // Create creates a new wallet balance record. Create(ctx context.Context, record *model.WalletBalanceRecord) error // FindByUserID finds all balance records for a user with pagination. FindByUserID(ctx context.Context, userID string, pagination pagination.Pagination) (total int64, records []*model.WalletBalanceRecord, err error) }