69 lines
1.6 KiB
Go
69 lines
1.6 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"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type WalletTransactionRepositoryParam struct {
|
||
|
DB *gorm.DB `name:"dbM"`
|
||
|
}
|
||
|
|
||
|
type WalletTransactionRepository struct {
|
||
|
WalletTransactionRepositoryParam
|
||
|
}
|
||
|
|
||
|
func MustWalletTransactionRepository(param WalletTransactionRepositoryParam) repository.WalletTransactionRepo {
|
||
|
return &WalletTransactionRepository{
|
||
|
param,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (repo *WalletTransactionRepository) Create(ctx context.Context, db *gorm.DB, tx []entity.WalletTransaction) error {
|
||
|
err := db.WithContext(ctx).Create(tx).Error
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (repo *WalletTransactionRepository) HistoryBalance(ctx context.Context, req repository.HistoryReq) ([]entity.WalletTransaction, error) {
|
||
|
var data []entity.WalletTransaction
|
||
|
|
||
|
err := repo.DB.WithContext(ctx).Raw(
|
||
|
`SELECT
|
||
|
MAX(t.id) as id
|
||
|
FROM (
|
||
|
SELECT * FROM wallet_transaction
|
||
|
WHERE uid = ? AND wallet_type IN ? AND create_at <= ?
|
||
|
) As t
|
||
|
GROUP BY t.crypto, t.wallet_type`,
|
||
|
req.UID, wallet.AllTypes, req.StartTime,
|
||
|
).Find(&data).Error
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if len(data) == 0 {
|
||
|
return nil, repository.ErrRecordNotFound
|
||
|
}
|
||
|
|
||
|
ids := make([]int64, 0, len(wallet.AllTypes))
|
||
|
for _, v := range data {
|
||
|
ids = append(ids, v.ID)
|
||
|
}
|
||
|
|
||
|
err = repo.DB.WithContext(ctx).Where(ids).Find(&data).Error
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return data, nil
|
||
|
}
|