refactor: 重命名 card_wallet store 为 asset_wallet,新增 transaction store
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -8,33 +8,33 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CardRechargeStore 卡充值记录数据访问层
|
// AssetRechargeStore 资产充值记录数据访问层
|
||||||
type CardRechargeStore struct {
|
type AssetRechargeStore struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
redis *redis.Client
|
redis *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCardRechargeStore 创建卡充值记录 Store
|
// NewAssetRechargeStore 创建资产充值记录 Store
|
||||||
func NewCardRechargeStore(db *gorm.DB, redis *redis.Client) *CardRechargeStore {
|
func NewAssetRechargeStore(db *gorm.DB, redis *redis.Client) *AssetRechargeStore {
|
||||||
return &CardRechargeStore{
|
return &AssetRechargeStore{
|
||||||
db: db,
|
db: db,
|
||||||
redis: redis,
|
redis: redis,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create 创建充值记录
|
// 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
|
return s.db.WithContext(ctx).Create(record).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateWithTx 创建充值记录(带事务)
|
// 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
|
return tx.WithContext(ctx).Create(record).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByRechargeNo 根据充值订单号查询
|
// GetByRechargeNo 根据充值订单号查询
|
||||||
func (s *CardRechargeStore) GetByRechargeNo(ctx context.Context, rechargeNo string) (*model.CardRechargeRecord, error) {
|
func (s *AssetRechargeStore) GetByRechargeNo(ctx context.Context, rechargeNo string) (*model.AssetRechargeRecord, error) {
|
||||||
var record model.CardRechargeRecord
|
var record model.AssetRechargeRecord
|
||||||
err := s.db.WithContext(ctx).
|
err := s.db.WithContext(ctx).
|
||||||
Where("recharge_no = ?", rechargeNo).
|
Where("recharge_no = ?", rechargeNo).
|
||||||
First(&record).Error
|
First(&record).Error
|
||||||
@@ -45,8 +45,8 @@ func (s *CardRechargeStore) GetByRechargeNo(ctx context.Context, rechargeNo stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetByID 根据 ID 查询
|
// GetByID 根据 ID 查询
|
||||||
func (s *CardRechargeStore) GetByID(ctx context.Context, id uint) (*model.CardRechargeRecord, error) {
|
func (s *AssetRechargeStore) GetByID(ctx context.Context, id uint) (*model.AssetRechargeRecord, error) {
|
||||||
var record model.CardRechargeRecord
|
var record model.AssetRechargeRecord
|
||||||
if err := s.db.WithContext(ctx).First(&record, id).Error; err != nil {
|
if err := s.db.WithContext(ctx).First(&record, id).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -54,34 +54,34 @@ func (s *CardRechargeStore) GetByID(ctx context.Context, id uint) (*model.CardRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateStatus 更新充值状态
|
// 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).
|
return s.db.WithContext(ctx).
|
||||||
Model(&model.CardRechargeRecord{}).
|
Model(&model.AssetRechargeRecord{}).
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
Update("status", status).Error
|
Update("status", status).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateStatusWithTx 更新充值状态(带事务)
|
// 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).
|
return tx.WithContext(ctx).
|
||||||
Model(&model.CardRechargeRecord{}).
|
Model(&model.AssetRechargeRecord{}).
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
Update("status", status).Error
|
Update("status", status).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update 更新充值记录
|
// 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
|
return s.db.WithContext(ctx).Save(record).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateWithTx 更新充值记录(带事务)
|
// 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
|
return tx.WithContext(ctx).Save(record).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListByResourceID 按资源查询充值记录(支持分页)
|
// ListByResourceID 按资源查询充值记录(支持分页)
|
||||||
func (s *CardRechargeStore) ListByResourceID(ctx context.Context, resourceType string, resourceID uint, offset, limit int) ([]*model.CardRechargeRecord, error) {
|
func (s *AssetRechargeStore) ListByResourceID(ctx context.Context, resourceType string, resourceID uint, offset, limit int) ([]*model.AssetRechargeRecord, error) {
|
||||||
var records []*model.CardRechargeRecord
|
var records []*model.AssetRechargeRecord
|
||||||
err := s.db.WithContext(ctx).
|
err := s.db.WithContext(ctx).
|
||||||
Where("resource_type = ? AND resource_id = ?", resourceType, resourceID).
|
Where("resource_type = ? AND resource_id = ?", resourceType, resourceID).
|
||||||
Order("created_at DESC").
|
Order("created_at DESC").
|
||||||
@@ -95,8 +95,8 @@ func (s *CardRechargeStore) ListByResourceID(ctx context.Context, resourceType s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListByUserID 按用户查询充值记录(支持分页)
|
// ListByUserID 按用户查询充值记录(支持分页)
|
||||||
func (s *CardRechargeStore) ListByUserID(ctx context.Context, userID uint, offset, limit int) ([]*model.CardRechargeRecord, error) {
|
func (s *AssetRechargeStore) ListByUserID(ctx context.Context, userID uint, offset, limit int) ([]*model.AssetRechargeRecord, error) {
|
||||||
var records []*model.CardRechargeRecord
|
var records []*model.AssetRechargeRecord
|
||||||
err := s.db.WithContext(ctx).
|
err := s.db.WithContext(ctx).
|
||||||
Where("user_id = ?", userID).
|
Where("user_id = ?", userID).
|
||||||
Order("created_at DESC").
|
Order("created_at DESC").
|
||||||
@@ -110,8 +110,8 @@ func (s *CardRechargeStore) ListByUserID(ctx context.Context, userID uint, offse
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListByStatus 按状态查询充值记录(支持分页)
|
// ListByStatus 按状态查询充值记录(支持分页)
|
||||||
func (s *CardRechargeStore) ListByStatus(ctx context.Context, status int, offset, limit int) ([]*model.CardRechargeRecord, error) {
|
func (s *AssetRechargeStore) ListByStatus(ctx context.Context, status int, offset, limit int) ([]*model.AssetRechargeRecord, error) {
|
||||||
var records []*model.CardRechargeRecord
|
var records []*model.AssetRechargeRecord
|
||||||
err := s.db.WithContext(ctx).
|
err := s.db.WithContext(ctx).
|
||||||
Where("status = ?", status).
|
Where("status = ?", status).
|
||||||
Order("created_at DESC").
|
Order("created_at DESC").
|
||||||
@@ -124,31 +124,31 @@ func (s *CardRechargeStore) ListByStatus(ctx context.Context, status int, offset
|
|||||||
return records, nil
|
return records, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListRechargeParams 充值记录列表查询参数
|
// ListAssetRechargeParams 充值记录列表查询参数
|
||||||
type ListCardRechargeParams struct {
|
type ListAssetRechargeParams struct {
|
||||||
Page int
|
Page int
|
||||||
PageSize int
|
PageSize int
|
||||||
UserID *uint
|
UserID *uint
|
||||||
CardWalletID *uint
|
AssetWalletID *uint
|
||||||
ResourceType *string
|
ResourceType *string
|
||||||
ResourceID *uint
|
ResourceID *uint
|
||||||
Status *int
|
Status *int
|
||||||
StartTime interface{}
|
StartTime interface{}
|
||||||
EndTime interface{}
|
EndTime interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// List 查询充值记录列表(支持分页和筛选)
|
// List 查询充值记录列表(支持分页和筛选)
|
||||||
func (s *CardRechargeStore) List(ctx context.Context, params *ListCardRechargeParams) ([]*model.CardRechargeRecord, int64, error) {
|
func (s *AssetRechargeStore) List(ctx context.Context, params *ListAssetRechargeParams) ([]*model.AssetRechargeRecord, int64, error) {
|
||||||
var records []*model.CardRechargeRecord
|
var records []*model.AssetRechargeRecord
|
||||||
var total int64
|
var total int64
|
||||||
|
|
||||||
query := s.db.WithContext(ctx).Model(&model.CardRechargeRecord{})
|
query := s.db.WithContext(ctx).Model(&model.AssetRechargeRecord{})
|
||||||
|
|
||||||
if params.UserID != nil {
|
if params.UserID != nil {
|
||||||
query = query.Where("user_id = ?", *params.UserID)
|
query = query.Where("user_id = ?", *params.UserID)
|
||||||
}
|
}
|
||||||
if params.CardWalletID != nil {
|
if params.AssetWalletID != nil {
|
||||||
query = query.Where("card_wallet_id = ?", *params.CardWalletID)
|
query = query.Where("asset_wallet_id = ?", *params.AssetWalletID)
|
||||||
}
|
}
|
||||||
if params.ResourceType != nil {
|
if params.ResourceType != nil {
|
||||||
query = query.Where("resource_type = ?", *params.ResourceType)
|
query = query.Where("resource_type = ?", *params.ResourceType)
|
||||||
@@ -188,7 +188,7 @@ func (s *CardRechargeStore) List(ctx context.Context, params *ListCardRechargePa
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdatePaymentInfo 更新支付信息
|
// 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{}{}
|
updates := map[string]interface{}{}
|
||||||
if paymentMethod != nil {
|
if paymentMethod != nil {
|
||||||
updates["payment_method"] = paymentMethod
|
updates["payment_method"] = paymentMethod
|
||||||
@@ -201,7 +201,7 @@ func (s *CardRechargeStore) UpdatePaymentInfo(ctx context.Context, id uint, paym
|
|||||||
return nil
|
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 {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
@@ -212,7 +212,7 @@ func (s *CardRechargeStore) UpdatePaymentInfo(ctx context.Context, id uint, paym
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateStatusWithOptimisticLock 更新充值状态(支持乐观锁)
|
// 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{}{
|
updates := map[string]interface{}{
|
||||||
"status": newStatus,
|
"status": newStatus,
|
||||||
}
|
}
|
||||||
@@ -223,7 +223,7 @@ func (s *CardRechargeStore) UpdateStatusWithOptimisticLock(ctx context.Context,
|
|||||||
updates["completed_at"] = completedAt
|
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 {
|
if oldStatus != nil {
|
||||||
query = query.Where("status = ?", *oldStatus)
|
query = query.Where("status = ?", *oldStatus)
|
||||||
@@ -11,23 +11,23 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CardWalletStore 卡钱包数据访问层
|
// AssetWalletStore 资产钱包数据访问层
|
||||||
type CardWalletStore struct {
|
type AssetWalletStore struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
redis *redis.Client
|
redis *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCardWalletStore 创建卡钱包 Store
|
// NewAssetWalletStore 创建资产钱包 Store
|
||||||
func NewCardWalletStore(db *gorm.DB, redis *redis.Client) *CardWalletStore {
|
func NewAssetWalletStore(db *gorm.DB, redis *redis.Client) *AssetWalletStore {
|
||||||
return &CardWalletStore{
|
return &AssetWalletStore{
|
||||||
db: db,
|
db: db,
|
||||||
redis: redis,
|
redis: redis,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByResourceTypeAndID 根据资源类型和 ID 查询钱包
|
// GetByResourceTypeAndID 根据资源类型和 ID 查询钱包
|
||||||
func (s *CardWalletStore) GetByResourceTypeAndID(ctx context.Context, resourceType string, resourceID uint) (*model.CardWallet, error) {
|
func (s *AssetWalletStore) GetByResourceTypeAndID(ctx context.Context, resourceType string, resourceID uint) (*model.AssetWallet, error) {
|
||||||
var wallet model.CardWallet
|
var wallet model.AssetWallet
|
||||||
query := s.db.WithContext(ctx).
|
query := s.db.WithContext(ctx).
|
||||||
Where("resource_type = ? AND resource_id = ?", resourceType, resourceID)
|
Where("resource_type = ? AND resource_id = ?", resourceType, resourceID)
|
||||||
// 应用数据权限过滤(使用 shop_id_tag 字段)
|
// 应用数据权限过滤(使用 shop_id_tag 字段)
|
||||||
@@ -40,8 +40,8 @@ func (s *CardWalletStore) GetByResourceTypeAndID(ctx context.Context, resourceTy
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetByID 根据钱包 ID 查询
|
// GetByID 根据钱包 ID 查询
|
||||||
func (s *CardWalletStore) GetByID(ctx context.Context, id uint) (*model.CardWallet, error) {
|
func (s *AssetWalletStore) GetByID(ctx context.Context, id uint) (*model.AssetWallet, error) {
|
||||||
var wallet model.CardWallet
|
var wallet model.AssetWallet
|
||||||
query := s.db.WithContext(ctx).Where("id = ?", id)
|
query := s.db.WithContext(ctx).Where("id = ?", id)
|
||||||
// 应用数据权限过滤(使用 shop_id_tag 字段)
|
// 应用数据权限过滤(使用 shop_id_tag 字段)
|
||||||
query = middleware.ApplyShopTagFilter(ctx, query)
|
query = middleware.ApplyShopTagFilter(ctx, query)
|
||||||
@@ -51,20 +51,20 @@ func (s *CardWalletStore) GetByID(ctx context.Context, id uint) (*model.CardWall
|
|||||||
return &wallet, nil
|
return &wallet, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create 创建卡钱包
|
// Create 创建资产钱包
|
||||||
func (s *CardWalletStore) Create(ctx context.Context, wallet *model.CardWallet) error {
|
func (s *AssetWalletStore) Create(ctx context.Context, wallet *model.AssetWallet) error {
|
||||||
return s.db.WithContext(ctx).Create(wallet).Error
|
return s.db.WithContext(ctx).Create(wallet).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateWithTx 创建卡钱包(带事务)
|
// CreateWithTx 创建资产钱包(带事务)
|
||||||
func (s *CardWalletStore) CreateWithTx(ctx context.Context, tx *gorm.DB, wallet *model.CardWallet) error {
|
func (s *AssetWalletStore) CreateWithTx(ctx context.Context, tx *gorm.DB, wallet *model.AssetWallet) error {
|
||||||
return tx.WithContext(ctx).Create(wallet).Error
|
return tx.WithContext(ctx).Create(wallet).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeductBalanceWithTx 扣款(带事务,使用乐观锁)
|
// 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).
|
Where("id = ? AND balance - frozen_balance >= ? AND version = ?", walletID, amount, version).
|
||||||
Updates(map[string]interface{}{
|
Updates(map[string]interface{}{
|
||||||
"balance": gorm.Expr("balance - ?", amount),
|
"balance": gorm.Expr("balance - ?", amount),
|
||||||
@@ -77,18 +77,17 @@ func (s *CardWalletStore) DeductBalanceWithTx(ctx context.Context, tx *gorm.DB,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if result.RowsAffected == 0 {
|
if result.RowsAffected == 0 {
|
||||||
return gorm.ErrRecordNotFound // 余额不足或版本冲突
|
return gorm.ErrRecordNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除缓存
|
|
||||||
s.clearWalletCache(ctx, walletID)
|
s.clearWalletCache(ctx, walletID)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddBalanceWithTx 增加余额(带事务)
|
// AddBalanceWithTx 增加余额(带事务)
|
||||||
func (s *CardWalletStore) AddBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
|
func (s *AssetWalletStore) AddBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
|
||||||
result := tx.WithContext(ctx).Model(&model.CardWallet{}).
|
result := tx.WithContext(ctx).Model(&model.AssetWallet{}).
|
||||||
Where("id = ?", walletID).
|
Where("id = ?", walletID).
|
||||||
Updates(map[string]interface{}{
|
Updates(map[string]interface{}{
|
||||||
"balance": gorm.Expr("balance + ?", amount),
|
"balance": gorm.Expr("balance + ?", amount),
|
||||||
@@ -103,20 +102,18 @@ func (s *CardWalletStore) AddBalanceWithTx(ctx context.Context, tx *gorm.DB, wal
|
|||||||
return gorm.ErrRecordNotFound
|
return gorm.ErrRecordNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除缓存
|
|
||||||
s.clearWalletCache(ctx, walletID)
|
s.clearWalletCache(ctx, walletID)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// clearWalletCache 清除钱包缓存
|
// clearWalletCache 清除钱包缓存
|
||||||
func (s *CardWalletStore) clearWalletCache(ctx context.Context, walletID uint) {
|
func (s *AssetWalletStore) clearWalletCache(ctx context.Context, walletID uint) {
|
||||||
// 查询钱包信息以获取 resource_type 和 resource_id
|
var wallet model.AssetWallet
|
||||||
var wallet model.CardWallet
|
|
||||||
if err := s.db.WithContext(ctx).Select("resource_type, resource_id").First(&wallet, walletID).Error; err != nil {
|
if err := s.db.WithContext(ctx).Select("resource_type, resource_id").First(&wallet, walletID).Error; err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheKey := constants.RedisCardWalletBalanceKey(wallet.ResourceType, wallet.ResourceID)
|
cacheKey := constants.RedisAssetWalletBalanceKey(wallet.ResourceType, wallet.ResourceID)
|
||||||
_ = s.redis.Del(ctx, cacheKey).Err()
|
_ = s.redis.Del(ctx, cacheKey).Err()
|
||||||
}
|
}
|
||||||
123
internal/store/postgres/asset_wallet_transaction_store.go
Normal file
123
internal/store/postgres/asset_wallet_transaction_store.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user