feat: 钱包系统分离 - 代理钱包与卡钱包完全隔离
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m17s

## 变更概述
将统一钱包系统拆分为代理钱包和卡钱包两个独立系统,实现数据表和代码层面的完全隔离。

## 数据库变更
- 新增 6 张表:tb_agent_wallet、tb_agent_wallet_transaction、tb_agent_recharge_record、tb_card_wallet、tb_card_wallet_transaction、tb_card_recharge_record
- 删除 3 张旧表:tb_wallet、tb_wallet_transaction、tb_recharge_record
- 代理钱包:按 (shop_id, wallet_type) 唯一标识,支持主钱包和分佣钱包
- 卡钱包:按 (resource_type, resource_id) 唯一标识,支持物联网卡和设备

## 代码变更
- Model 层:新增 AgentWallet、AgentWalletTransaction、AgentRechargeRecord、CardWallet、CardWalletTransaction、CardRechargeRecord 模型
- Store 层:新增 6 个独立 Store,支持事务、乐观锁、Redis 缓存
- Service 层:重构 commission_calculation、commission_withdrawal、order、recharge 等 8 个服务
- Bootstrap 层:更新 Store 和 Service 依赖注入
- 常量层:按钱包类型重新组织常量和 Redis Key 生成函数

## 技术特性
- 乐观锁:使用 version 字段防止并发冲突
- 多租户:支持 shop_id_tag 和 enterprise_id_tag 过滤
- 事务管理:所有余额变动使用事务保证 ACID
- 缓存策略:Cache-Aside 模式,余额变动后删除缓存

## 业务影响
- 代理钱包和卡钱包业务完全隔离,互不影响
- 为独立监控、优化、扩展打下基础
- 提升代理钱包的稳定性和独立性

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-25 09:51:00 +08:00
parent f32d32cd36
commit 18daeae65a
66 changed files with 4420 additions and 1090 deletions

View File

@@ -19,30 +19,30 @@ import (
type Service struct {
db *gorm.DB
shopStore *postgres.ShopStore
walletStore *postgres.WalletStore
agentWalletStore *postgres.AgentWalletStore
commissionWithdrawalRequestStore *postgres.CommissionWithdrawalRequestStore
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore
commissionRecordStore *postgres.CommissionRecordStore
walletTransactionStore *postgres.WalletTransactionStore
agentWalletTransactionStore *postgres.AgentWalletTransactionStore
}
func New(
db *gorm.DB,
shopStore *postgres.ShopStore,
walletStore *postgres.WalletStore,
agentWalletStore *postgres.AgentWalletStore,
commissionWithdrawalRequestStore *postgres.CommissionWithdrawalRequestStore,
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore,
commissionRecordStore *postgres.CommissionRecordStore,
walletTransactionStore *postgres.WalletTransactionStore,
agentWalletTransactionStore *postgres.AgentWalletTransactionStore,
) *Service {
return &Service{
db: db,
shopStore: shopStore,
walletStore: walletStore,
agentWalletStore: agentWalletStore,
commissionWithdrawalRequestStore: commissionWithdrawalRequestStore,
commissionWithdrawalSettingStore: commissionWithdrawalSettingStore,
commissionRecordStore: commissionRecordStore,
walletTransactionStore: walletTransactionStore,
agentWalletTransactionStore: agentWalletTransactionStore,
}
}
@@ -63,8 +63,8 @@ func (s *Service) GetCommissionSummary(ctx context.Context) (*dto.MyCommissionSu
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
}
// 使用 GetShopCommissionWallet 获取店铺佣金钱包
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, shopID)
// 使用 GetCommissionWallet 获取店铺佣金钱包
wallet, err := s.agentWalletStore.GetCommissionWallet(ctx, shopID)
if err != nil {
// 钱包不存在时返回空数据
return &dto.MyCommissionSummaryResp{
@@ -120,7 +120,7 @@ func (s *Service) CreateWithdrawalRequest(ctx context.Context, req *dto.CreateMy
}
// 获取钱包
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, shopID)
wallet, err := s.agentWalletStore.GetCommissionWallet(ctx, shopID)
if err != nil {
return nil, errors.New(errors.CodeInsufficientBalance, "钱包不存在")
}
@@ -160,7 +160,7 @@ func (s *Service) CreateWithdrawalRequest(ctx context.Context, req *dto.CreateMy
err = s.db.Transaction(func(tx *gorm.DB) error {
// 冻结余额
if err := tx.WithContext(ctx).Model(&model.Wallet{}).
if err := tx.WithContext(ctx).Model(&model.AgentWallet{}).
Where("id = ? AND balance >= ?", wallet.ID, req.Amount).
Updates(map[string]interface{}{
"balance": gorm.Expr("balance - ?", req.Amount),
@@ -192,8 +192,9 @@ func (s *Service) CreateWithdrawalRequest(ctx context.Context, req *dto.CreateMy
// 创建钱包流水记录
remark := fmt.Sprintf("提现冻结,单号:%s", withdrawalNo)
refType := constants.ReferenceTypeWithdrawal
transaction := &model.WalletTransaction{
WalletID: wallet.ID,
transaction := &model.AgentWalletTransaction{
AgentWalletID: wallet.ID,
ShopID: shopID,
UserID: currentUserID,
TransactionType: constants.TransactionTypeWithdrawal,
Amount: -req.Amount, // 冻结为负数
@@ -204,6 +205,7 @@ func (s *Service) CreateWithdrawalRequest(ctx context.Context, req *dto.CreateMy
ReferenceID: &withdrawalRequest.ID,
Remark: &remark,
Creator: currentUserID,
ShopIDTag: shopID,
}
if err := tx.WithContext(ctx).Create(transaction).Error; err != nil {