暂存一下,防止丢失
This commit is contained in:
@@ -263,7 +263,7 @@ func (s *AccountStore) GetPrimaryAccountsByShopIDs(ctx context.Context, shopIDs
|
||||
Where("shop_id IN ? AND is_primary = ?", shopIDs, true)
|
||||
// 注意:此处不再应用数据权限过滤
|
||||
// 因为调用方(Service 层)已经保证了 shopIDs 的合法性
|
||||
// 在 ListShopFundSummary 场景中,店铺列表已由 Service 层过滤
|
||||
// 在资金概况等批量投影场景中,调用方已经保证 shopIDs 位于当前数据范围内
|
||||
if err := query.Find(&accounts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -80,12 +80,12 @@ func (s *AgentWalletStore) CreateWithTx(ctx context.Context, tx *gorm.DB, wallet
|
||||
return tx.WithContext(ctx).Create(wallet).Error
|
||||
}
|
||||
|
||||
// DeductFrozenBalanceWithTx 从冻结余额扣款(带事务)
|
||||
// 用于提现完成后,从冻结余额中扣除金额
|
||||
// DeductFrozenBalanceWithTx 从分佣钱包冻结余额扣款(带事务)。
|
||||
// 代理主钱包必须使用 Wallet Application,禁止通过旧 Store 写接缝变更。
|
||||
func (s *AgentWalletStore) DeductFrozenBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
|
||||
// 扣除冻结余额和总余额
|
||||
result := tx.WithContext(ctx).Model(&model.AgentWallet{}).
|
||||
Where("id = ? AND frozen_balance >= ?", walletID, amount).
|
||||
Where("id = ? AND wallet_type = ? AND frozen_balance >= ?", walletID, constants.AgentWalletTypeCommission, amount).
|
||||
Updates(map[string]interface{}{
|
||||
"balance": gorm.Expr("balance - ?", amount),
|
||||
"frozen_balance": gorm.Expr("frozen_balance - ?", amount),
|
||||
@@ -106,12 +106,12 @@ func (s *AgentWalletStore) DeductFrozenBalanceWithTx(ctx context.Context, tx *go
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnfreezeBalanceWithTx 解冻余额到可用余额(带事务)
|
||||
// 用于提现取消,将冻结余额转回可用余额
|
||||
// UnfreezeBalanceWithTx 解冻分佣钱包余额(带事务)。
|
||||
// 代理主钱包必须使用 Wallet Application,禁止通过旧 Store 写接缝变更。
|
||||
func (s *AgentWalletStore) UnfreezeBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
|
||||
// 减少冻结余额(总余额不变)
|
||||
result := tx.WithContext(ctx).Model(&model.AgentWallet{}).
|
||||
Where("id = ? AND frozen_balance >= ?", walletID, amount).
|
||||
Where("id = ? AND wallet_type = ? AND frozen_balance >= ?", walletID, constants.AgentWalletTypeCommission, amount).
|
||||
Updates(map[string]interface{}{
|
||||
"frozen_balance": gorm.Expr("frozen_balance - ?", amount),
|
||||
"updated_at": time.Now(),
|
||||
@@ -131,37 +131,11 @@ func (s *AgentWalletStore) UnfreezeBalanceWithTx(ctx context.Context, tx *gorm.D
|
||||
return nil
|
||||
}
|
||||
|
||||
// FreezeBalanceWithTx 冻结余额(带事务,使用乐观锁)
|
||||
// 用于提现申请,将可用余额转为冻结状态
|
||||
func (s *AgentWalletStore) FreezeBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64, version int) error {
|
||||
// 增加冻结余额(总余额不变),使用乐观锁
|
||||
result := tx.WithContext(ctx).Model(&model.AgentWallet{}).
|
||||
Where("id = ? AND balance - frozen_balance >= ? AND version = ?", walletID, amount, version).
|
||||
Updates(map[string]interface{}{
|
||||
"frozen_balance": gorm.Expr("frozen_balance + ?", amount),
|
||||
"version": gorm.Expr("version + 1"),
|
||||
"updated_at": time.Now(),
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound // 可用余额不足或版本冲突
|
||||
}
|
||||
|
||||
// 删除缓存
|
||||
s.clearWalletCache(ctx, walletID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddBalanceWithTx 增加余额(带事务)
|
||||
// 用于充值、退款等增加余额的操作
|
||||
// AddBalanceWithTx 增加分佣钱包余额(带事务)。
|
||||
// 代理主钱包充值、退款和人工调整必须使用 Wallet Application。
|
||||
func (s *AgentWalletStore) AddBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
|
||||
result := tx.WithContext(ctx).Model(&model.AgentWallet{}).
|
||||
Where("id = ?", walletID).
|
||||
Where("id = ? AND wallet_type = ?", walletID, constants.AgentWalletTypeCommission).
|
||||
Updates(map[string]interface{}{
|
||||
"balance": gorm.Expr("balance + ?", amount),
|
||||
"version": gorm.Expr("version + 1"),
|
||||
@@ -182,32 +156,6 @@ func (s *AgentWalletStore) AddBalanceWithTx(ctx context.Context, tx *gorm.DB, wa
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeductBalanceWithTx 扣除余额(带事务,使用乐观锁)
|
||||
// 用于扣款操作,检查可用余额是否充足
|
||||
func (s *AgentWalletStore) DeductBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64, version int) error {
|
||||
// 使用乐观锁,检查可用余额是否充足
|
||||
result := tx.WithContext(ctx).Model(&model.AgentWallet{}).
|
||||
Where("id = ? AND balance - frozen_balance >= ? AND version = ?", walletID, amount, version).
|
||||
Updates(map[string]interface{}{
|
||||
"balance": gorm.Expr("balance - ?", amount),
|
||||
"version": gorm.Expr("version + 1"),
|
||||
"updated_at": time.Now(),
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound // 余额不足或版本冲突
|
||||
}
|
||||
|
||||
// 删除缓存
|
||||
s.clearWalletCache(ctx, walletID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetShopCommissionSummaryBatch 批量获取店铺佣金钱包汇总
|
||||
// 返回 map[shopID]*AgentWallet
|
||||
func (s *AgentWalletStore) GetShopCommissionSummaryBatch(ctx context.Context, shopIDs []uint) (map[uint]*model.AgentWallet, error) {
|
||||
|
||||
Reference in New Issue
Block a user