app-cloudep-wallet-service/pkg/repository/wallet.go

97 lines
2.4 KiB
Go

package repository
import (
"code.30cm.net/digimon/app-cloudep-wallet-service/pkg/domain/entity"
"code.30cm.net/digimon/app-cloudep-wallet-service/pkg/domain/repository"
"code.30cm.net/digimon/app-cloudep-wallet-service/pkg/domain/wallet"
"context"
"database/sql"
"github.com/shopspring/decimal"
"gorm.io/gorm"
)
type WalletRepositoryParam struct {
DB *gorm.DB `name:"dbM"`
}
type WalletRepository struct {
WalletRepositoryParam
}
func MustCategoryRepository(param WalletRepositoryParam) repository.WalletRepository {
return &WalletRepository{
param,
}
}
func (repo *WalletRepository) NewDB() *gorm.DB {
return repo.DB
}
func (repo *WalletRepository) Transaction(fn func(db *gorm.DB) error) error {
db := repo.DB.Begin(&sql.TxOptions{
Isolation: sql.LevelReadCommitted,
ReadOnly: false,
})
defer db.Rollback()
if err := fn(db); err != nil {
return err
}
if err := db.Commit().Error; err != nil {
return err
}
return nil
}
func (repo *WalletRepository) Session(uid, asset string) repository.UserWalletService {
return NewUserWallet(repo.DB, uid, asset)
}
func (repo *WalletRepository) SessionWithTx(db *gorm.DB, uid, asset string) repository.UserWalletService {
return NewUserWallet(db, uid, asset)
}
func (repo *WalletRepository) InitWallets(ctx context.Context, param repository.Wallet) ([]entity.Wallet, error) {
w := make([]entity.Wallet, 0, len(wallet.AllTypes))
for _, t := range wallet.AllTypes {
var balance decimal.Decimal
// 合約模擬初始資金
if t == wallet.TypeSimulationAvailable {
balance = wallet.InitContractSimulationAvailable
}
w = append(w, entity.Wallet{
Brand: param.Brand,
UID: param.UID,
Asset: param.Asset,
Balance: balance,
Type: t,
})
}
if err := repo.DB.WithContext(ctx).Create(&w).Error; err != nil {
return nil, err
}
return w, nil
}
func (repo *WalletRepository) QueryBalances(ctx context.Context, req repository.BalanceQuery) ([]entity.Wallet, error) {
//TODO implement me
panic("implement me")
}
func (repo *WalletRepository) QueryBalancesByUIDs(ctx context.Context, uids []string, req repository.BalanceQuery) ([]entity.Wallet, error) {
//TODO implement me
panic("implement me")
}
func (repo *WalletRepository) GetDailyTxAmount(ctx context.Context, uid string, txTypes []wallet.TransactionType, business wallet.BusinessName) ([]entity.Wallet, error) {
//TODO implement me
panic("implement me")
}