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

161 lines
3.9 KiB
Go
Raw Normal View History

2025-04-21 07:46:43 +00:00
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"
"errors"
"gorm.io/gorm"
"time"
)
type TransactionRepositoryParam struct {
DB *gorm.DB `name:"dbM"`
}
type TransactionRepository struct {
TransactionRepositoryParam
}
func MustTransactionRepository(param TransactionRepositoryParam) repository.TransactionRepository {
return &TransactionRepository{
param,
}
}
func (repo *TransactionRepository) FindByOrderID(ctx context.Context, orderID string) (entity.Transaction, error) {
var result entity.Transaction
err := repo.DB.WithContext(ctx).Where("order_id = ?", orderID).Take(&result).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return entity.Transaction{}, repository.ErrRecordNotFound
}
}
return result, nil
}
func (repo *TransactionRepository) Insert(ctx context.Context, tx *entity.Transaction) error {
return repo.DB.Create(tx).Error
}
func (repo *TransactionRepository) BatchInsert(ctx context.Context, txs []*entity.Transaction) error {
return repo.DB.Create(txs).Error
}
func (repo *TransactionRepository) List(ctx context.Context, query repository.TransactionQuery) ([]entity.Transaction, int64, error) {
sql := repo.DB.WithContext(ctx)
if len(query.BusinessType) != 0 {
sql = sql.Where("business_type IN ?", query.BusinessType)
}
if query.UID != nil {
sql = sql.Where("uid = ?", query.UID)
}
if query.OrderID != nil {
sql = sql.Where("order_id = ?", query.OrderID)
}
if query.Assets != nil {
sql = sql.Where("asset = ?", query.Assets)
}
if len(query.TxTypes) > 0 {
sql = sql.Where("type IN ?", query.TxTypes)
}
//sql = sql.Where("status = ?", 0)
if query.StartTime != nil {
if query.EndTime != nil {
sql = sql.Where("create_at BETWEEN ? AND ?", query.StartTime, query.EndTime)
}
}
var transactions []entity.Transaction
var count int64
if err := sql.Model(&entity.Transaction{}).Count(&count).Error; err != nil {
return []entity.Transaction{}, 0, err
}
if count == 0 {
return []entity.Transaction{}, 0, repository.ErrRecordNotFound
}
if query.PageIndex == 0 {
query.PageIndex = 1
}
if query.PageSize == 0 {
query.PageSize = 20
}
err := sql.Offset(int((query.PageIndex - 1) * query.PageSize)).
Limit(int(query.PageSize)).
Order("create_at desc").
Find(&transactions).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return []entity.Transaction{}, 0, repository.ErrRecordNotFound
}
return []entity.Transaction{}, 0, err
}
return transactions, count, nil
}
func (repo *TransactionRepository) FindByDueTimeRange(ctx context.Context, start time.Time, txType []wallet.TxType) ([]entity.Transaction, error) {
var data []entity.Transaction
err := repo.DB.WithContext(ctx).
Where("type IN ?", txType).
Where("status = ?", wallet.WalletNonStatus).
Where("due_time <= ?", start.UTC().Unix()).
Where("due_time != ?", 0).
Find(&data).Error
if err != nil {
return nil, err
}
return data, nil
}
func (repo *TransactionRepository) UpdateStatusByID(ctx context.Context, id int64, status int) error {
err := repo.DB.WithContext(ctx).
Model(&entity.Transaction{}).
Where("id = ?", id).
UpdateColumn("status", status).Error
if err != nil {
return err
}
return nil
}
func (repo *TransactionRepository) ListWalletTransactions(ctx context.Context, uid string, orderIDs []string, walletType wallet.Types) ([]entity.WalletTransaction, error) {
sql := repo.DB.WithContext(ctx)
if uid != "" {
sql = sql.Where("uid = ?", uid)
}
sql = sql.Where("order_id IN ?", orderIDs)
if walletType > 0 {
sql = sql.Where("wallet_type", walletType)
}
result := make([]entity.WalletTransaction, len(orderIDs))
if err := sql.Order("create_at desc").Find(&result).Error; err != nil {
return []entity.WalletTransaction{}, err
}
return result, nil
}