From 9c768e07192a314f543925111bd46d32e0008c06 Mon Sep 17 00:00:00 2001 From: huang Date: Mon, 16 Mar 2026 15:43:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E5=91=BD=E5=90=8D=20card?= =?UTF-8?q?=5Fwallet=20store=20=E4=B8=BA=20asset=5Fwallet=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20transaction=20store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus --- ...harge_store.go => asset_recharge_store.go} | 86 ++++++------ ..._wallet_store.go => asset_wallet_store.go} | 45 +++---- .../asset_wallet_transaction_store.go | 123 ++++++++++++++++++ .../postgres/card_wallet_transaction_store.go | 89 ------------- 4 files changed, 187 insertions(+), 156 deletions(-) rename internal/store/postgres/{card_recharge_store.go => asset_recharge_store.go} (57%) rename internal/store/postgres/{card_wallet_store.go => asset_wallet_store.go} (59%) create mode 100644 internal/store/postgres/asset_wallet_transaction_store.go delete mode 100644 internal/store/postgres/card_wallet_transaction_store.go diff --git a/internal/store/postgres/card_recharge_store.go b/internal/store/postgres/asset_recharge_store.go similarity index 57% rename from internal/store/postgres/card_recharge_store.go rename to internal/store/postgres/asset_recharge_store.go index 6eb685c..7da920f 100644 --- a/internal/store/postgres/card_recharge_store.go +++ b/internal/store/postgres/asset_recharge_store.go @@ -8,33 +8,33 @@ import ( "gorm.io/gorm" ) -// CardRechargeStore 卡充值记录数据访问层 -type CardRechargeStore struct { +// AssetRechargeStore 资产充值记录数据访问层 +type AssetRechargeStore struct { db *gorm.DB redis *redis.Client } -// NewCardRechargeStore 创建卡充值记录 Store -func NewCardRechargeStore(db *gorm.DB, redis *redis.Client) *CardRechargeStore { - return &CardRechargeStore{ +// NewAssetRechargeStore 创建资产充值记录 Store +func NewAssetRechargeStore(db *gorm.DB, redis *redis.Client) *AssetRechargeStore { + return &AssetRechargeStore{ db: db, redis: redis, } } // Create 创建充值记录 -func (s *CardRechargeStore) Create(ctx context.Context, record *model.CardRechargeRecord) error { +func (s *AssetRechargeStore) Create(ctx context.Context, record *model.AssetRechargeRecord) error { return s.db.WithContext(ctx).Create(record).Error } // CreateWithTx 创建充值记录(带事务) -func (s *CardRechargeStore) CreateWithTx(ctx context.Context, tx *gorm.DB, record *model.CardRechargeRecord) error { +func (s *AssetRechargeStore) CreateWithTx(ctx context.Context, tx *gorm.DB, record *model.AssetRechargeRecord) error { return tx.WithContext(ctx).Create(record).Error } // GetByRechargeNo 根据充值订单号查询 -func (s *CardRechargeStore) GetByRechargeNo(ctx context.Context, rechargeNo string) (*model.CardRechargeRecord, error) { - var record model.CardRechargeRecord +func (s *AssetRechargeStore) GetByRechargeNo(ctx context.Context, rechargeNo string) (*model.AssetRechargeRecord, error) { + var record model.AssetRechargeRecord err := s.db.WithContext(ctx). Where("recharge_no = ?", rechargeNo). First(&record).Error @@ -45,8 +45,8 @@ func (s *CardRechargeStore) GetByRechargeNo(ctx context.Context, rechargeNo stri } // GetByID 根据 ID 查询 -func (s *CardRechargeStore) GetByID(ctx context.Context, id uint) (*model.CardRechargeRecord, error) { - var record model.CardRechargeRecord +func (s *AssetRechargeStore) GetByID(ctx context.Context, id uint) (*model.AssetRechargeRecord, error) { + var record model.AssetRechargeRecord if err := s.db.WithContext(ctx).First(&record, id).Error; err != nil { return nil, err } @@ -54,34 +54,34 @@ func (s *CardRechargeStore) GetByID(ctx context.Context, id uint) (*model.CardRe } // UpdateStatus 更新充值状态 -func (s *CardRechargeStore) UpdateStatus(ctx context.Context, id uint, status int) error { +func (s *AssetRechargeStore) UpdateStatus(ctx context.Context, id uint, status int) error { return s.db.WithContext(ctx). - Model(&model.CardRechargeRecord{}). + Model(&model.AssetRechargeRecord{}). Where("id = ?", id). Update("status", status).Error } // UpdateStatusWithTx 更新充值状态(带事务) -func (s *CardRechargeStore) UpdateStatusWithTx(ctx context.Context, tx *gorm.DB, id uint, status int) error { +func (s *AssetRechargeStore) UpdateStatusWithTx(ctx context.Context, tx *gorm.DB, id uint, status int) error { return tx.WithContext(ctx). - Model(&model.CardRechargeRecord{}). + Model(&model.AssetRechargeRecord{}). Where("id = ?", id). Update("status", status).Error } // Update 更新充值记录 -func (s *CardRechargeStore) Update(ctx context.Context, record *model.CardRechargeRecord) error { +func (s *AssetRechargeStore) Update(ctx context.Context, record *model.AssetRechargeRecord) error { return s.db.WithContext(ctx).Save(record).Error } // UpdateWithTx 更新充值记录(带事务) -func (s *CardRechargeStore) UpdateWithTx(ctx context.Context, tx *gorm.DB, record *model.CardRechargeRecord) error { +func (s *AssetRechargeStore) UpdateWithTx(ctx context.Context, tx *gorm.DB, record *model.AssetRechargeRecord) error { return tx.WithContext(ctx).Save(record).Error } // ListByResourceID 按资源查询充值记录(支持分页) -func (s *CardRechargeStore) ListByResourceID(ctx context.Context, resourceType string, resourceID uint, offset, limit int) ([]*model.CardRechargeRecord, error) { - var records []*model.CardRechargeRecord +func (s *AssetRechargeStore) ListByResourceID(ctx context.Context, resourceType string, resourceID uint, offset, limit int) ([]*model.AssetRechargeRecord, error) { + var records []*model.AssetRechargeRecord err := s.db.WithContext(ctx). Where("resource_type = ? AND resource_id = ?", resourceType, resourceID). Order("created_at DESC"). @@ -95,8 +95,8 @@ func (s *CardRechargeStore) ListByResourceID(ctx context.Context, resourceType s } // ListByUserID 按用户查询充值记录(支持分页) -func (s *CardRechargeStore) ListByUserID(ctx context.Context, userID uint, offset, limit int) ([]*model.CardRechargeRecord, error) { - var records []*model.CardRechargeRecord +func (s *AssetRechargeStore) ListByUserID(ctx context.Context, userID uint, offset, limit int) ([]*model.AssetRechargeRecord, error) { + var records []*model.AssetRechargeRecord err := s.db.WithContext(ctx). Where("user_id = ?", userID). Order("created_at DESC"). @@ -110,8 +110,8 @@ func (s *CardRechargeStore) ListByUserID(ctx context.Context, userID uint, offse } // ListByStatus 按状态查询充值记录(支持分页) -func (s *CardRechargeStore) ListByStatus(ctx context.Context, status int, offset, limit int) ([]*model.CardRechargeRecord, error) { - var records []*model.CardRechargeRecord +func (s *AssetRechargeStore) ListByStatus(ctx context.Context, status int, offset, limit int) ([]*model.AssetRechargeRecord, error) { + var records []*model.AssetRechargeRecord err := s.db.WithContext(ctx). Where("status = ?", status). Order("created_at DESC"). @@ -124,31 +124,31 @@ func (s *CardRechargeStore) ListByStatus(ctx context.Context, status int, offset return records, nil } -// ListRechargeParams 充值记录列表查询参数 -type ListCardRechargeParams struct { - Page int - PageSize int - UserID *uint - CardWalletID *uint - ResourceType *string - ResourceID *uint - Status *int - StartTime interface{} - EndTime interface{} +// ListAssetRechargeParams 充值记录列表查询参数 +type ListAssetRechargeParams struct { + Page int + PageSize int + UserID *uint + AssetWalletID *uint + ResourceType *string + ResourceID *uint + Status *int + StartTime interface{} + EndTime interface{} } // List 查询充值记录列表(支持分页和筛选) -func (s *CardRechargeStore) List(ctx context.Context, params *ListCardRechargeParams) ([]*model.CardRechargeRecord, int64, error) { - var records []*model.CardRechargeRecord +func (s *AssetRechargeStore) List(ctx context.Context, params *ListAssetRechargeParams) ([]*model.AssetRechargeRecord, int64, error) { + var records []*model.AssetRechargeRecord var total int64 - query := s.db.WithContext(ctx).Model(&model.CardRechargeRecord{}) + query := s.db.WithContext(ctx).Model(&model.AssetRechargeRecord{}) if params.UserID != nil { query = query.Where("user_id = ?", *params.UserID) } - if params.CardWalletID != nil { - query = query.Where("card_wallet_id = ?", *params.CardWalletID) + if params.AssetWalletID != nil { + query = query.Where("asset_wallet_id = ?", *params.AssetWalletID) } if params.ResourceType != nil { query = query.Where("resource_type = ?", *params.ResourceType) @@ -188,7 +188,7 @@ func (s *CardRechargeStore) List(ctx context.Context, params *ListCardRechargePa } // UpdatePaymentInfo 更新支付信息 -func (s *CardRechargeStore) UpdatePaymentInfo(ctx context.Context, id uint, paymentMethod *string, paymentTransactionID *string) error { +func (s *AssetRechargeStore) UpdatePaymentInfo(ctx context.Context, id uint, paymentMethod *string, paymentTransactionID *string) error { updates := map[string]interface{}{} if paymentMethod != nil { updates["payment_method"] = paymentMethod @@ -201,7 +201,7 @@ func (s *CardRechargeStore) UpdatePaymentInfo(ctx context.Context, id uint, paym return nil } - result := s.db.WithContext(ctx).Model(&model.CardRechargeRecord{}).Where("id = ?", id).Updates(updates) + result := s.db.WithContext(ctx).Model(&model.AssetRechargeRecord{}).Where("id = ?", id).Updates(updates) if result.Error != nil { return result.Error } @@ -212,7 +212,7 @@ func (s *CardRechargeStore) UpdatePaymentInfo(ctx context.Context, id uint, paym } // UpdateStatusWithOptimisticLock 更新充值状态(支持乐观锁) -func (s *CardRechargeStore) UpdateStatusWithOptimisticLock(ctx context.Context, id uint, oldStatus *int, newStatus int, paidAt interface{}, completedAt interface{}) error { +func (s *AssetRechargeStore) UpdateStatusWithOptimisticLock(ctx context.Context, id uint, oldStatus *int, newStatus int, paidAt interface{}, completedAt interface{}) error { updates := map[string]interface{}{ "status": newStatus, } @@ -223,7 +223,7 @@ func (s *CardRechargeStore) UpdateStatusWithOptimisticLock(ctx context.Context, updates["completed_at"] = completedAt } - query := s.db.WithContext(ctx).Model(&model.CardRechargeRecord{}).Where("id = ?", id) + query := s.db.WithContext(ctx).Model(&model.AssetRechargeRecord{}).Where("id = ?", id) if oldStatus != nil { query = query.Where("status = ?", *oldStatus) diff --git a/internal/store/postgres/card_wallet_store.go b/internal/store/postgres/asset_wallet_store.go similarity index 59% rename from internal/store/postgres/card_wallet_store.go rename to internal/store/postgres/asset_wallet_store.go index 7ad0e89..6d36bf5 100644 --- a/internal/store/postgres/card_wallet_store.go +++ b/internal/store/postgres/asset_wallet_store.go @@ -11,23 +11,23 @@ import ( "gorm.io/gorm" ) -// CardWalletStore 卡钱包数据访问层 -type CardWalletStore struct { +// AssetWalletStore 资产钱包数据访问层 +type AssetWalletStore struct { db *gorm.DB redis *redis.Client } -// NewCardWalletStore 创建卡钱包 Store -func NewCardWalletStore(db *gorm.DB, redis *redis.Client) *CardWalletStore { - return &CardWalletStore{ +// NewAssetWalletStore 创建资产钱包 Store +func NewAssetWalletStore(db *gorm.DB, redis *redis.Client) *AssetWalletStore { + return &AssetWalletStore{ db: db, redis: redis, } } // GetByResourceTypeAndID 根据资源类型和 ID 查询钱包 -func (s *CardWalletStore) GetByResourceTypeAndID(ctx context.Context, resourceType string, resourceID uint) (*model.CardWallet, error) { - var wallet model.CardWallet +func (s *AssetWalletStore) GetByResourceTypeAndID(ctx context.Context, resourceType string, resourceID uint) (*model.AssetWallet, error) { + var wallet model.AssetWallet query := s.db.WithContext(ctx). Where("resource_type = ? AND resource_id = ?", resourceType, resourceID) // 应用数据权限过滤(使用 shop_id_tag 字段) @@ -40,8 +40,8 @@ func (s *CardWalletStore) GetByResourceTypeAndID(ctx context.Context, resourceTy } // GetByID 根据钱包 ID 查询 -func (s *CardWalletStore) GetByID(ctx context.Context, id uint) (*model.CardWallet, error) { - var wallet model.CardWallet +func (s *AssetWalletStore) GetByID(ctx context.Context, id uint) (*model.AssetWallet, error) { + var wallet model.AssetWallet query := s.db.WithContext(ctx).Where("id = ?", id) // 应用数据权限过滤(使用 shop_id_tag 字段) query = middleware.ApplyShopTagFilter(ctx, query) @@ -51,20 +51,20 @@ func (s *CardWalletStore) GetByID(ctx context.Context, id uint) (*model.CardWall return &wallet, nil } -// Create 创建卡钱包 -func (s *CardWalletStore) Create(ctx context.Context, wallet *model.CardWallet) error { +// Create 创建资产钱包 +func (s *AssetWalletStore) Create(ctx context.Context, wallet *model.AssetWallet) error { return s.db.WithContext(ctx).Create(wallet).Error } -// CreateWithTx 创建卡钱包(带事务) -func (s *CardWalletStore) CreateWithTx(ctx context.Context, tx *gorm.DB, wallet *model.CardWallet) error { +// CreateWithTx 创建资产钱包(带事务) +func (s *AssetWalletStore) CreateWithTx(ctx context.Context, tx *gorm.DB, wallet *model.AssetWallet) error { return tx.WithContext(ctx).Create(wallet).Error } // DeductBalanceWithTx 扣款(带事务,使用乐观锁) -func (s *CardWalletStore) DeductBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64, version int) error { +func (s *AssetWalletStore) DeductBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64, version int) error { // 使用乐观锁,检查可用余额是否充足 - result := tx.WithContext(ctx).Model(&model.CardWallet{}). + result := tx.WithContext(ctx).Model(&model.AssetWallet{}). Where("id = ? AND balance - frozen_balance >= ? AND version = ?", walletID, amount, version). Updates(map[string]interface{}{ "balance": gorm.Expr("balance - ?", amount), @@ -77,18 +77,17 @@ func (s *CardWalletStore) DeductBalanceWithTx(ctx context.Context, tx *gorm.DB, } if result.RowsAffected == 0 { - return gorm.ErrRecordNotFound // 余额不足或版本冲突 + return gorm.ErrRecordNotFound } - // 删除缓存 s.clearWalletCache(ctx, walletID) return nil } // AddBalanceWithTx 增加余额(带事务) -func (s *CardWalletStore) AddBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error { - result := tx.WithContext(ctx).Model(&model.CardWallet{}). +func (s *AssetWalletStore) AddBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error { + result := tx.WithContext(ctx).Model(&model.AssetWallet{}). Where("id = ?", walletID). Updates(map[string]interface{}{ "balance": gorm.Expr("balance + ?", amount), @@ -103,20 +102,18 @@ func (s *CardWalletStore) AddBalanceWithTx(ctx context.Context, tx *gorm.DB, wal return gorm.ErrRecordNotFound } - // 删除缓存 s.clearWalletCache(ctx, walletID) return nil } // clearWalletCache 清除钱包缓存 -func (s *CardWalletStore) clearWalletCache(ctx context.Context, walletID uint) { - // 查询钱包信息以获取 resource_type 和 resource_id - var wallet model.CardWallet +func (s *AssetWalletStore) clearWalletCache(ctx context.Context, walletID uint) { + var wallet model.AssetWallet if err := s.db.WithContext(ctx).Select("resource_type, resource_id").First(&wallet, walletID).Error; err != nil { return } - cacheKey := constants.RedisCardWalletBalanceKey(wallet.ResourceType, wallet.ResourceID) + cacheKey := constants.RedisAssetWalletBalanceKey(wallet.ResourceType, wallet.ResourceID) _ = s.redis.Del(ctx, cacheKey).Err() } diff --git a/internal/store/postgres/asset_wallet_transaction_store.go b/internal/store/postgres/asset_wallet_transaction_store.go new file mode 100644 index 0000000..ac0a70b --- /dev/null +++ b/internal/store/postgres/asset_wallet_transaction_store.go @@ -0,0 +1,123 @@ +package postgres + +import ( + "context" + "time" + + "github.com/break/junhong_cmp_fiber/internal/model" + "github.com/break/junhong_cmp_fiber/pkg/middleware" + "github.com/redis/go-redis/v9" + "gorm.io/gorm" +) + +// AssetWalletTransactionStore 资产钱包交易记录数据访问层 +type AssetWalletTransactionStore struct { + db *gorm.DB + redis *redis.Client +} + +// NewAssetWalletTransactionStore 创建资产钱包交易记录 Store +func NewAssetWalletTransactionStore(db *gorm.DB, redis *redis.Client) *AssetWalletTransactionStore { + return &AssetWalletTransactionStore{ + db: db, + redis: redis, + } +} + +// CreateWithTx 创建资产钱包交易记录(带事务) +func (s *AssetWalletTransactionStore) CreateWithTx(ctx context.Context, tx *gorm.DB, transaction *model.AssetWalletTransaction) error { + return tx.WithContext(ctx).Create(transaction).Error +} + +// ListByResourceID 按资源查询交易记录(支持分页) +func (s *AssetWalletTransactionStore) ListByResourceID(ctx context.Context, resourceType string, resourceID uint, offset, limit int) ([]*model.AssetWalletTransaction, error) { + var transactions []*model.AssetWalletTransaction + query := s.db.WithContext(ctx). + Where("resource_type = ? AND resource_id = ?", resourceType, resourceID) + // 应用数据权限过滤(使用 shop_id_tag 字段) + query = middleware.ApplyShopTagFilter(ctx, query) + err := query.Order("created_at DESC"). + Offset(offset). + Limit(limit). + Find(&transactions).Error + if err != nil { + return nil, err + } + return transactions, nil +} + +// CountByResourceID 统计资源的交易记录数量 +func (s *AssetWalletTransactionStore) CountByResourceID(ctx context.Context, resourceType string, resourceID uint) (int64, error) { + var count int64 + query := s.db.WithContext(ctx). + Model(&model.AssetWalletTransaction{}). + Where("resource_type = ? AND resource_id = ?", resourceType, resourceID) + // 应用数据权限过滤(使用 shop_id_tag 字段) + query = middleware.ApplyShopTagFilter(ctx, query) + err := query.Count(&count).Error + return count, err +} + +// ListByWalletID 按钱包查询交易记录(支持分页) +func (s *AssetWalletTransactionStore) ListByWalletID(ctx context.Context, walletID uint, offset, limit int) ([]*model.AssetWalletTransaction, error) { + var transactions []*model.AssetWalletTransaction + query := s.db.WithContext(ctx). + Where("asset_wallet_id = ?", walletID) + // 应用数据权限过滤(使用 shop_id_tag 字段) + query = middleware.ApplyShopTagFilter(ctx, query) + err := query.Order("created_at DESC"). + Offset(offset). + Limit(limit). + Find(&transactions).Error + if err != nil { + return nil, err + } + return transactions, nil +} + +// ListByResourceIDWithFilter 按资源查询交易记录(支持 transaction_type 和时间范围过滤,分页) +func (s *AssetWalletTransactionStore) ListByResourceIDWithFilter(ctx context.Context, resourceType string, resourceID uint, transactionType *string, startTime, endTime *time.Time, offset, limit int) ([]*model.AssetWalletTransaction, error) { + var transactions []*model.AssetWalletTransaction + query := s.db.WithContext(ctx). + Where("resource_type = ? AND resource_id = ?", resourceType, resourceID) + if transactionType != nil { + query = query.Where("transaction_type = ?", *transactionType) + } + if startTime != nil { + query = query.Where("created_at >= ?", *startTime) + } + if endTime != nil { + query = query.Where("created_at <= ?", *endTime) + } + // 应用数据权限过滤(使用 shop_id_tag 字段) + query = middleware.ApplyShopTagFilter(ctx, query) + err := query.Order("created_at DESC"). + Offset(offset). + Limit(limit). + Find(&transactions).Error + if err != nil { + return nil, err + } + return transactions, nil +} + +// CountByResourceIDWithFilter 统计资源的交易记录数量(支持 transaction_type 和时间范围过滤) +func (s *AssetWalletTransactionStore) CountByResourceIDWithFilter(ctx context.Context, resourceType string, resourceID uint, transactionType *string, startTime, endTime *time.Time) (int64, error) { + var count int64 + query := s.db.WithContext(ctx). + Model(&model.AssetWalletTransaction{}). + Where("resource_type = ? AND resource_id = ?", resourceType, resourceID) + if transactionType != nil { + query = query.Where("transaction_type = ?", *transactionType) + } + if startTime != nil { + query = query.Where("created_at >= ?", *startTime) + } + if endTime != nil { + query = query.Where("created_at <= ?", *endTime) + } + // 应用数据权限过滤(使用 shop_id_tag 字段) + query = middleware.ApplyShopTagFilter(ctx, query) + err := query.Count(&count).Error + return count, err +} diff --git a/internal/store/postgres/card_wallet_transaction_store.go b/internal/store/postgres/card_wallet_transaction_store.go deleted file mode 100644 index 439dfdc..0000000 --- a/internal/store/postgres/card_wallet_transaction_store.go +++ /dev/null @@ -1,89 +0,0 @@ -package postgres - -import ( - "context" - - "github.com/break/junhong_cmp_fiber/internal/model" - "github.com/break/junhong_cmp_fiber/pkg/middleware" - "github.com/redis/go-redis/v9" - "gorm.io/gorm" -) - -// CardWalletTransactionStore 卡钱包交易记录数据访问层 -type CardWalletTransactionStore struct { - db *gorm.DB - redis *redis.Client -} - -// NewCardWalletTransactionStore 创建卡钱包交易记录 Store -func NewCardWalletTransactionStore(db *gorm.DB, redis *redis.Client) *CardWalletTransactionStore { - return &CardWalletTransactionStore{ - db: db, - redis: redis, - } -} - -// CreateWithTx 创建卡钱包交易记录(带事务) -func (s *CardWalletTransactionStore) CreateWithTx(ctx context.Context, tx *gorm.DB, transaction *model.CardWalletTransaction) error { - return tx.WithContext(ctx).Create(transaction).Error -} - -// ListByResourceID 按资源查询交易记录(支持分页) -func (s *CardWalletTransactionStore) ListByResourceID(ctx context.Context, resourceType string, resourceID uint, offset, limit int) ([]*model.CardWalletTransaction, error) { - var transactions []*model.CardWalletTransaction - query := s.db.WithContext(ctx). - Where("resource_type = ? AND resource_id = ?", resourceType, resourceID) - // 应用数据权限过滤(使用 shop_id_tag 字段) - query = middleware.ApplyShopTagFilter(ctx, query) - err := query.Order("created_at DESC"). - Offset(offset). - Limit(limit). - Find(&transactions).Error - if err != nil { - return nil, err - } - return transactions, nil -} - -// CountByResourceID 统计资源的交易记录数量 -func (s *CardWalletTransactionStore) CountByResourceID(ctx context.Context, resourceType string, resourceID uint) (int64, error) { - var count int64 - query := s.db.WithContext(ctx). - Model(&model.CardWalletTransaction{}). - Where("resource_type = ? AND resource_id = ?", resourceType, resourceID) - // 应用数据权限过滤(使用 shop_id_tag 字段) - query = middleware.ApplyShopTagFilter(ctx, query) - err := query.Count(&count).Error - return count, err -} - -// ListByWalletID 按钱包查询交易记录(支持分页) -func (s *CardWalletTransactionStore) ListByWalletID(ctx context.Context, walletID uint, offset, limit int) ([]*model.CardWalletTransaction, error) { - var transactions []*model.CardWalletTransaction - query := s.db.WithContext(ctx). - Where("card_wallet_id = ?", walletID) - // 应用数据权限过滤(使用 shop_id_tag 字段) - query = middleware.ApplyShopTagFilter(ctx, query) - err := query.Order("created_at DESC"). - Offset(offset). - Limit(limit). - Find(&transactions).Error - if err != nil { - return nil, err - } - return transactions, nil -} - -// GetByReference 根据关联业务查询交易记录 -func (s *CardWalletTransactionStore) GetByReference(ctx context.Context, referenceType string, referenceID uint) (*model.CardWalletTransaction, error) { - var transaction model.CardWalletTransaction - query := s.db.WithContext(ctx). - Where("reference_type = ? AND reference_id = ?", referenceType, referenceID) - // 应用数据权限过滤(使用 shop_id_tag 字段) - query = middleware.ApplyShopTagFilter(ctx, query) - err := query.First(&transaction).Error - if err != nil { - return nil, err - } - return &transaction, nil -}