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

@@ -0,0 +1,125 @@
package postgres
import (
"context"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
// AgentRechargeStore 代理充值记录数据访问层
type AgentRechargeStore struct {
db *gorm.DB
redis *redis.Client
}
// NewAgentRechargeStore 创建代理充值记录 Store
func NewAgentRechargeStore(db *gorm.DB, redis *redis.Client) *AgentRechargeStore {
return &AgentRechargeStore{
db: db,
redis: redis,
}
}
// Create 创建充值记录
func (s *AgentRechargeStore) Create(ctx context.Context, record *model.AgentRechargeRecord) error {
return s.db.WithContext(ctx).Create(record).Error
}
// CreateWithTx 创建充值记录(带事务)
func (s *AgentRechargeStore) CreateWithTx(ctx context.Context, tx *gorm.DB, record *model.AgentRechargeRecord) error {
return tx.WithContext(ctx).Create(record).Error
}
// GetByRechargeNo 根据充值订单号查询
func (s *AgentRechargeStore) GetByRechargeNo(ctx context.Context, rechargeNo string) (*model.AgentRechargeRecord, error) {
var record model.AgentRechargeRecord
err := s.db.WithContext(ctx).
Where("recharge_no = ?", rechargeNo).
First(&record).Error
if err != nil {
return nil, err
}
return &record, nil
}
// GetByID 根据 ID 查询
func (s *AgentRechargeStore) GetByID(ctx context.Context, id uint) (*model.AgentRechargeRecord, error) {
var record model.AgentRechargeRecord
if err := s.db.WithContext(ctx).First(&record, id).Error; err != nil {
return nil, err
}
return &record, nil
}
// UpdateStatus 更新充值状态
func (s *AgentRechargeStore) UpdateStatus(ctx context.Context, id uint, status int) error {
return s.db.WithContext(ctx).
Model(&model.AgentRechargeRecord{}).
Where("id = ?", id).
Update("status", status).Error
}
// UpdateStatusWithTx 更新充值状态(带事务)
func (s *AgentRechargeStore) UpdateStatusWithTx(ctx context.Context, tx *gorm.DB, id uint, status int) error {
return tx.WithContext(ctx).
Model(&model.AgentRechargeRecord{}).
Where("id = ?", id).
Update("status", status).Error
}
// Update 更新充值记录
func (s *AgentRechargeStore) Update(ctx context.Context, record *model.AgentRechargeRecord) error {
return s.db.WithContext(ctx).Save(record).Error
}
// UpdateWithTx 更新充值记录(带事务)
func (s *AgentRechargeStore) UpdateWithTx(ctx context.Context, tx *gorm.DB, record *model.AgentRechargeRecord) error {
return tx.WithContext(ctx).Save(record).Error
}
// ListByShopID 按店铺查询充值记录(支持分页)
func (s *AgentRechargeStore) ListByShopID(ctx context.Context, shopID uint, offset, limit int) ([]*model.AgentRechargeRecord, error) {
var records []*model.AgentRechargeRecord
err := s.db.WithContext(ctx).
Where("shop_id = ?", shopID).
Order("created_at DESC").
Offset(offset).
Limit(limit).
Find(&records).Error
if err != nil {
return nil, err
}
return records, nil
}
// ListByUserID 按用户查询充值记录(支持分页)
func (s *AgentRechargeStore) ListByUserID(ctx context.Context, userID uint, offset, limit int) ([]*model.AgentRechargeRecord, error) {
var records []*model.AgentRechargeRecord
err := s.db.WithContext(ctx).
Where("user_id = ?", userID).
Order("created_at DESC").
Offset(offset).
Limit(limit).
Find(&records).Error
if err != nil {
return nil, err
}
return records, nil
}
// ListByStatus 按状态查询充值记录(支持分页)
func (s *AgentRechargeStore) ListByStatus(ctx context.Context, status int, offset, limit int) ([]*model.AgentRechargeRecord, error) {
var records []*model.AgentRechargeRecord
err := s.db.WithContext(ctx).
Where("status = ?", status).
Order("created_at DESC").
Offset(offset).
Limit(limit).
Find(&records).Error
if err != nil {
return nil, err
}
return records, nil
}

View File

@@ -0,0 +1,238 @@
package postgres
import (
"context"
"time"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
// AgentWalletStore 代理钱包数据访问层
type AgentWalletStore struct {
db *gorm.DB
redis *redis.Client
}
// NewAgentWalletStore 创建代理钱包 Store
func NewAgentWalletStore(db *gorm.DB, redis *redis.Client) *AgentWalletStore {
return &AgentWalletStore{
db: db,
redis: redis,
}
}
// GetCommissionWallet 获取店铺的分佣钱包
func (s *AgentWalletStore) GetCommissionWallet(ctx context.Context, shopID uint) (*model.AgentWallet, error) {
return s.GetByShopIDAndType(ctx, shopID, constants.AgentWalletTypeCommission)
}
// GetMainWallet 获取店铺的主钱包
func (s *AgentWalletStore) GetMainWallet(ctx context.Context, shopID uint) (*model.AgentWallet, error) {
return s.GetByShopIDAndType(ctx, shopID, constants.AgentWalletTypeMain)
}
// GetByShopIDAndType 根据店铺 ID 和钱包类型查询钱包
func (s *AgentWalletStore) GetByShopIDAndType(ctx context.Context, shopID uint, walletType string) (*model.AgentWallet, error) {
// 尝试从缓存获取
cacheKey := constants.RedisAgentWalletBalanceKey(shopID, walletType)
// 注意:这里简化处理,实际项目中可以缓存完整的钱包信息
var wallet model.AgentWallet
err := s.db.WithContext(ctx).
Where("shop_id = ? AND wallet_type = ?", shopID, walletType).
First(&wallet).Error
if err != nil {
return nil, err
}
// 更新缓存(可选)
// 这里简化处理,不缓存完整对象
_ = cacheKey
return &wallet, nil
}
// GetByID 根据钱包 ID 查询
func (s *AgentWalletStore) GetByID(ctx context.Context, id uint) (*model.AgentWallet, error) {
var wallet model.AgentWallet
if err := s.db.WithContext(ctx).First(&wallet, id).Error; err != nil {
return nil, err
}
return &wallet, nil
}
// Create 创建代理钱包
func (s *AgentWalletStore) Create(ctx context.Context, wallet *model.AgentWallet) error {
return s.db.WithContext(ctx).Create(wallet).Error
}
// CreateWithTx 创建代理钱包(带事务)
func (s *AgentWalletStore) CreateWithTx(ctx context.Context, tx *gorm.DB, wallet *model.AgentWallet) error {
return tx.WithContext(ctx).Create(wallet).Error
}
// DeductFrozenBalanceWithTx 从冻结余额扣款(带事务)
// 用于提现完成后,从冻结余额中扣除金额
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).
Updates(map[string]interface{}{
"balance": gorm.Expr("balance - ?", amount),
"frozen_balance": gorm.Expr("frozen_balance - ?", amount),
"updated_at": time.Now(),
})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound // 冻结余额不足
}
// 删除缓存
s.clearWalletCache(ctx, walletID)
return nil
}
// UnfreezeBalanceWithTx 解冻余额到可用余额(带事务)
// 用于提现取消,将冻结余额转回可用余额
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).
Updates(map[string]interface{}{
"frozen_balance": gorm.Expr("frozen_balance - ?", amount),
"updated_at": time.Now(),
})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound // 冻结余额不足
}
// 删除缓存
s.clearWalletCache(ctx, walletID)
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 增加余额(带事务)
// 用于充值、退款等增加余额的操作
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).
Updates(map[string]interface{}{
"balance": gorm.Expr("balance + ?", amount),
"updated_at": time.Now(),
})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
// 删除缓存
s.clearWalletCache(ctx, walletID)
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) {
if len(shopIDs) == 0 {
return make(map[uint]*model.AgentWallet), nil
}
var wallets []model.AgentWallet
err := s.db.WithContext(ctx).
Where("shop_id IN ? AND wallet_type = ?", shopIDs, constants.AgentWalletTypeCommission).
Find(&wallets).Error
if err != nil {
return nil, err
}
// 转换为 map
result := make(map[uint]*model.AgentWallet, len(wallets))
for i := range wallets {
result[wallets[i].ShopID] = &wallets[i]
}
return result, nil
}
// clearWalletCache 清除钱包缓存
func (s *AgentWalletStore) clearWalletCache(ctx context.Context, walletID uint) {
// 查询钱包信息以获取 shop_id 和 wallet_type
var wallet model.AgentWallet
if err := s.db.WithContext(ctx).Select("shop_id, wallet_type").First(&wallet, walletID).Error; err != nil {
return
}
cacheKey := constants.RedisAgentWalletBalanceKey(wallet.ShopID, wallet.WalletType)
_ = s.redis.Del(ctx, cacheKey).Err()
}

View File

@@ -0,0 +1,80 @@
package postgres
import (
"context"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
// AgentWalletTransactionStore 代理钱包交易记录数据访问层
type AgentWalletTransactionStore struct {
db *gorm.DB
redis *redis.Client
}
// NewAgentWalletTransactionStore 创建代理钱包交易记录 Store
func NewAgentWalletTransactionStore(db *gorm.DB, redis *redis.Client) *AgentWalletTransactionStore {
return &AgentWalletTransactionStore{
db: db,
redis: redis,
}
}
// CreateWithTx 创建代理钱包交易记录(带事务)
func (s *AgentWalletTransactionStore) CreateWithTx(ctx context.Context, tx *gorm.DB, transaction *model.AgentWalletTransaction) error {
return tx.WithContext(ctx).Create(transaction).Error
}
// ListByShopID 按店铺查询交易记录(支持分页)
func (s *AgentWalletTransactionStore) ListByShopID(ctx context.Context, shopID uint, offset, limit int) ([]*model.AgentWalletTransaction, error) {
var transactions []*model.AgentWalletTransaction
err := s.db.WithContext(ctx).
Where("shop_id = ?", shopID).
Order("created_at DESC").
Offset(offset).
Limit(limit).
Find(&transactions).Error
if err != nil {
return nil, err
}
return transactions, nil
}
// CountByShopID 统计店铺的交易记录数量
func (s *AgentWalletTransactionStore) CountByShopID(ctx context.Context, shopID uint) (int64, error) {
var count int64
err := s.db.WithContext(ctx).
Model(&model.AgentWalletTransaction{}).
Where("shop_id = ?", shopID).
Count(&count).Error
return count, err
}
// ListByWalletID 按钱包查询交易记录(支持分页)
func (s *AgentWalletTransactionStore) ListByWalletID(ctx context.Context, walletID uint, offset, limit int) ([]*model.AgentWalletTransaction, error) {
var transactions []*model.AgentWalletTransaction
err := s.db.WithContext(ctx).
Where("agent_wallet_id = ?", walletID).
Order("created_at DESC").
Offset(offset).
Limit(limit).
Find(&transactions).Error
if err != nil {
return nil, err
}
return transactions, nil
}
// GetByReference 根据关联业务查询交易记录
func (s *AgentWalletTransactionStore) GetByReference(ctx context.Context, referenceType string, referenceID uint) (*model.AgentWalletTransaction, error) {
var transaction model.AgentWalletTransaction
err := s.db.WithContext(ctx).
Where("reference_type = ? AND reference_id = ?", referenceType, referenceID).
First(&transaction).Error
if err != nil {
return nil, err
}
return &transaction, nil
}

View File

@@ -0,0 +1,240 @@
package postgres
import (
"context"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
// CardRechargeStore 卡充值记录数据访问层
type CardRechargeStore struct {
db *gorm.DB
redis *redis.Client
}
// NewCardRechargeStore 创建卡充值记录 Store
func NewCardRechargeStore(db *gorm.DB, redis *redis.Client) *CardRechargeStore {
return &CardRechargeStore{
db: db,
redis: redis,
}
}
// Create 创建充值记录
func (s *CardRechargeStore) Create(ctx context.Context, record *model.CardRechargeRecord) error {
return s.db.WithContext(ctx).Create(record).Error
}
// CreateWithTx 创建充值记录(带事务)
func (s *CardRechargeStore) CreateWithTx(ctx context.Context, tx *gorm.DB, record *model.CardRechargeRecord) 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
err := s.db.WithContext(ctx).
Where("recharge_no = ?", rechargeNo).
First(&record).Error
if err != nil {
return nil, err
}
return &record, nil
}
// GetByID 根据 ID 查询
func (s *CardRechargeStore) GetByID(ctx context.Context, id uint) (*model.CardRechargeRecord, error) {
var record model.CardRechargeRecord
if err := s.db.WithContext(ctx).First(&record, id).Error; err != nil {
return nil, err
}
return &record, nil
}
// UpdateStatus 更新充值状态
func (s *CardRechargeStore) UpdateStatus(ctx context.Context, id uint, status int) error {
return s.db.WithContext(ctx).
Model(&model.CardRechargeRecord{}).
Where("id = ?", id).
Update("status", status).Error
}
// UpdateStatusWithTx 更新充值状态(带事务)
func (s *CardRechargeStore) UpdateStatusWithTx(ctx context.Context, tx *gorm.DB, id uint, status int) error {
return tx.WithContext(ctx).
Model(&model.CardRechargeRecord{}).
Where("id = ?", id).
Update("status", status).Error
}
// Update 更新充值记录
func (s *CardRechargeStore) Update(ctx context.Context, record *model.CardRechargeRecord) error {
return s.db.WithContext(ctx).Save(record).Error
}
// UpdateWithTx 更新充值记录(带事务)
func (s *CardRechargeStore) UpdateWithTx(ctx context.Context, tx *gorm.DB, record *model.CardRechargeRecord) 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
err := s.db.WithContext(ctx).
Where("resource_type = ? AND resource_id = ?", resourceType, resourceID).
Order("created_at DESC").
Offset(offset).
Limit(limit).
Find(&records).Error
if err != nil {
return nil, err
}
return records, nil
}
// ListByUserID 按用户查询充值记录(支持分页)
func (s *CardRechargeStore) ListByUserID(ctx context.Context, userID uint, offset, limit int) ([]*model.CardRechargeRecord, error) {
var records []*model.CardRechargeRecord
err := s.db.WithContext(ctx).
Where("user_id = ?", userID).
Order("created_at DESC").
Offset(offset).
Limit(limit).
Find(&records).Error
if err != nil {
return nil, err
}
return records, nil
}
// ListByStatus 按状态查询充值记录(支持分页)
func (s *CardRechargeStore) ListByStatus(ctx context.Context, status int, offset, limit int) ([]*model.CardRechargeRecord, error) {
var records []*model.CardRechargeRecord
err := s.db.WithContext(ctx).
Where("status = ?", status).
Order("created_at DESC").
Offset(offset).
Limit(limit).
Find(&records).Error
if err != nil {
return nil, err
}
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{}
}
// List 查询充值记录列表(支持分页和筛选)
func (s *CardRechargeStore) List(ctx context.Context, params *ListCardRechargeParams) ([]*model.CardRechargeRecord, int64, error) {
var records []*model.CardRechargeRecord
var total int64
query := s.db.WithContext(ctx).Model(&model.CardRechargeRecord{})
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.ResourceType != nil {
query = query.Where("resource_type = ?", *params.ResourceType)
}
if params.ResourceID != nil {
query = query.Where("resource_id = ?", *params.ResourceID)
}
if params.Status != nil {
query = query.Where("status = ?", *params.Status)
}
if params.StartTime != nil {
query = query.Where("created_at >= ?", params.StartTime)
}
if params.EndTime != nil {
query = query.Where("created_at <= ?", params.EndTime)
}
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
page := params.Page
if page < 1 {
page = 1
}
pageSize := params.PageSize
if pageSize < 1 {
pageSize = 20
}
offset := (page - 1) * pageSize
if err := query.Order("id DESC").Offset(offset).Limit(pageSize).Find(&records).Error; err != nil {
return nil, 0, err
}
return records, total, nil
}
// UpdatePaymentInfo 更新支付信息
func (s *CardRechargeStore) UpdatePaymentInfo(ctx context.Context, id uint, paymentMethod *string, paymentTransactionID *string) error {
updates := map[string]interface{}{}
if paymentMethod != nil {
updates["payment_method"] = paymentMethod
}
if paymentTransactionID != nil {
updates["payment_transaction_id"] = paymentTransactionID
}
if len(updates) == 0 {
return nil
}
result := s.db.WithContext(ctx).Model(&model.CardRechargeRecord{}).Where("id = ?", id).Updates(updates)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}
// UpdateStatusWithOptimisticLock 更新充值状态(支持乐观锁)
func (s *CardRechargeStore) UpdateStatusWithOptimisticLock(ctx context.Context, id uint, oldStatus *int, newStatus int, paidAt interface{}, completedAt interface{}) error {
updates := map[string]interface{}{
"status": newStatus,
}
if paidAt != nil {
updates["paid_at"] = paidAt
}
if completedAt != nil {
updates["completed_at"] = completedAt
}
query := s.db.WithContext(ctx).Model(&model.CardRechargeRecord{}).Where("id = ?", id)
if oldStatus != nil {
query = query.Where("status = ?", *oldStatus)
}
result := query.Updates(updates)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}

View File

@@ -0,0 +1,116 @@
package postgres
import (
"context"
"time"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
// CardWalletStore 卡钱包数据访问层
type CardWalletStore struct {
db *gorm.DB
redis *redis.Client
}
// NewCardWalletStore 创建卡钱包 Store
func NewCardWalletStore(db *gorm.DB, redis *redis.Client) *CardWalletStore {
return &CardWalletStore{
db: db,
redis: redis,
}
}
// GetByResourceTypeAndID 根据资源类型和 ID 查询钱包
func (s *CardWalletStore) GetByResourceTypeAndID(ctx context.Context, resourceType string, resourceID uint) (*model.CardWallet, error) {
var wallet model.CardWallet
err := s.db.WithContext(ctx).
Where("resource_type = ? AND resource_id = ?", resourceType, resourceID).
First(&wallet).Error
if err != nil {
return nil, err
}
return &wallet, nil
}
// GetByID 根据钱包 ID 查询
func (s *CardWalletStore) GetByID(ctx context.Context, id uint) (*model.CardWallet, error) {
var wallet model.CardWallet
if err := s.db.WithContext(ctx).First(&wallet, id).Error; err != nil {
return nil, err
}
return &wallet, nil
}
// Create 创建卡钱包
func (s *CardWalletStore) Create(ctx context.Context, wallet *model.CardWallet) error {
return s.db.WithContext(ctx).Create(wallet).Error
}
// CreateWithTx 创建卡钱包(带事务)
func (s *CardWalletStore) CreateWithTx(ctx context.Context, tx *gorm.DB, wallet *model.CardWallet) 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 {
// 使用乐观锁,检查可用余额是否充足
result := tx.WithContext(ctx).Model(&model.CardWallet{}).
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
}
// AddBalanceWithTx 增加余额(带事务)
func (s *CardWalletStore) AddBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
result := tx.WithContext(ctx).Model(&model.CardWallet{}).
Where("id = ?", walletID).
Updates(map[string]interface{}{
"balance": gorm.Expr("balance + ?", amount),
"updated_at": time.Now(),
})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
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
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)
_ = s.redis.Del(ctx, cacheKey).Err()
}

View File

@@ -0,0 +1,80 @@
package postgres
import (
"context"
"github.com/break/junhong_cmp_fiber/internal/model"
"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
err := s.db.WithContext(ctx).
Where("resource_type = ? AND resource_id = ?", resourceType, resourceID).
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
err := s.db.WithContext(ctx).
Model(&model.CardWalletTransaction{}).
Where("resource_type = ? AND resource_id = ?", resourceType, resourceID).
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
err := s.db.WithContext(ctx).
Where("card_wallet_id = ?", walletID).
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
err := s.db.WithContext(ctx).
Where("reference_type = ? AND reference_id = ?", referenceType, referenceID).
First(&transaction).Error
if err != nil {
return nil, err
}
return &transaction, nil
}

View File

@@ -1,166 +0,0 @@
package postgres
import (
"context"
"time"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
type RechargeStore struct {
db *gorm.DB
redis *redis.Client
}
// NewRechargeStore 创建充值订单 Store 实例
func NewRechargeStore(db *gorm.DB, redis *redis.Client) *RechargeStore {
return &RechargeStore{
db: db,
redis: redis,
}
}
// Create 创建充值订单
func (s *RechargeStore) Create(ctx context.Context, recharge *model.RechargeRecord) error {
return s.db.WithContext(ctx).Create(recharge).Error
}
// GetByRechargeNo 根据充值订单号查询充值订单
// 不存在时返回 nil, nil
func (s *RechargeStore) GetByRechargeNo(ctx context.Context, rechargeNo string) (*model.RechargeRecord, error) {
var recharge model.RechargeRecord
err := s.db.WithContext(ctx).Where("recharge_no = ?", rechargeNo).First(&recharge).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return nil, err
}
return &recharge, nil
}
// GetByID 根据 ID 查询充值订单
func (s *RechargeStore) GetByID(ctx context.Context, id uint) (*model.RechargeRecord, error) {
var recharge model.RechargeRecord
if err := s.db.WithContext(ctx).First(&recharge, id).Error; err != nil {
return nil, err
}
return &recharge, nil
}
// ListRechargeParams 充值订单列表查询参数
type ListRechargeParams struct {
Page int // 页码(从 1 开始)
PageSize int // 每页数量
UserID *uint // 用户 ID 筛选
WalletID *uint // 钱包 ID 筛选
Status *int // 状态筛选
StartTime *time.Time // 开始时间
EndTime *time.Time // 结束时间
}
// List 查询充值订单列表(支持分页和筛选)
func (s *RechargeStore) List(ctx context.Context, params *ListRechargeParams) ([]*model.RechargeRecord, int64, error) {
var recharges []*model.RechargeRecord
var total int64
query := s.db.WithContext(ctx).Model(&model.RechargeRecord{})
// 应用筛选条件
if params.UserID != nil {
query = query.Where("user_id = ?", *params.UserID)
}
if params.WalletID != nil {
query = query.Where("wallet_id = ?", *params.WalletID)
}
if params.Status != nil {
query = query.Where("status = ?", *params.Status)
}
if params.StartTime != nil {
query = query.Where("created_at >= ?", *params.StartTime)
}
if params.EndTime != nil {
query = query.Where("created_at <= ?", *params.EndTime)
}
// 统计总数
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
// 分页查询
page := params.Page
if page < 1 {
page = 1
}
pageSize := params.PageSize
if pageSize < 1 {
pageSize = 20
}
offset := (page - 1) * pageSize
if err := query.Order("id DESC").Offset(offset).Limit(pageSize).Find(&recharges).Error; err != nil {
return nil, 0, err
}
return recharges, total, nil
}
// UpdateStatus 更新充值订单状态(支持乐观锁检查)
// oldStatus: 原状态(用于乐观锁检查,传 nil 则跳过检查)
// newStatus: 新状态
// paidAt: 支付时间(状态变为已支付时传入)
// completedAt: 完成时间(状态变为已完成时传入)
func (s *RechargeStore) UpdateStatus(ctx context.Context, id uint, oldStatus *int, newStatus int, paidAt *time.Time, completedAt *time.Time) error {
updates := map[string]interface{}{
"status": newStatus,
}
if paidAt != nil {
updates["paid_at"] = paidAt
}
if completedAt != nil {
updates["completed_at"] = completedAt
}
query := s.db.WithContext(ctx).Model(&model.RechargeRecord{}).Where("id = ?", id)
// 乐观锁检查
if oldStatus != nil {
query = query.Where("status = ?", *oldStatus)
}
result := query.Updates(updates)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}
// UpdatePaymentInfo 更新支付信息
func (s *RechargeStore) UpdatePaymentInfo(ctx context.Context, id uint, paymentChannel *string, paymentTransactionID *string) error {
updates := map[string]interface{}{}
if paymentChannel != nil {
updates["payment_channel"] = paymentChannel
}
if paymentTransactionID != nil {
updates["payment_transaction_id"] = paymentTransactionID
}
if len(updates) == 0 {
return nil
}
result := s.db.WithContext(ctx).Model(&model.RechargeRecord{}).Where("id = ?", id).Updates(updates)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}

View File

@@ -1,108 +0,0 @@
package postgres
import (
"context"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
type WalletStore struct {
db *gorm.DB
redis *redis.Client
}
func NewWalletStore(db *gorm.DB, redis *redis.Client) *WalletStore {
return &WalletStore{
db: db,
redis: redis,
}
}
func (s *WalletStore) GetByResourceTypeAndID(ctx context.Context, resourceType string, resourceID uint, walletType string) (*model.Wallet, error) {
var wallet model.Wallet
err := s.db.WithContext(ctx).
Where("resource_type = ? AND resource_id = ? AND wallet_type = ?", resourceType, resourceID, walletType).
First(&wallet).Error
if err != nil {
return nil, err
}
return &wallet, nil
}
func (s *WalletStore) GetShopCommissionWallet(ctx context.Context, shopID uint) (*model.Wallet, error) {
return s.GetByResourceTypeAndID(ctx, "shop", shopID, "commission")
}
type ShopCommissionSummary struct {
ShopID uint
Balance int64
FrozenBalance int64
}
func (s *WalletStore) GetShopCommissionSummaryBatch(ctx context.Context, shopIDs []uint) (map[uint]*ShopCommissionSummary, error) {
if len(shopIDs) == 0 {
return make(map[uint]*ShopCommissionSummary), nil
}
var wallets []model.Wallet
err := s.db.WithContext(ctx).
Where("resource_type = ? AND resource_id IN ? AND wallet_type = ?", "shop", shopIDs, "commission").
Find(&wallets).Error
if err != nil {
return nil, err
}
result := make(map[uint]*ShopCommissionSummary)
for _, w := range wallets {
result[w.ResourceID] = &ShopCommissionSummary{
ShopID: w.ResourceID,
Balance: w.Balance,
FrozenBalance: w.FrozenBalance,
}
}
return result, nil
}
func (s *WalletStore) GetByID(ctx context.Context, id uint) (*model.Wallet, error) {
var wallet model.Wallet
if err := s.db.WithContext(ctx).First(&wallet, id).Error; err != nil {
return nil, err
}
return &wallet, nil
}
func (s *WalletStore) DeductFrozenBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
result := tx.WithContext(ctx).
Model(&model.Wallet{}).
Where("id = ? AND frozen_balance >= ?", walletID, amount).
Updates(map[string]interface{}{
"frozen_balance": gorm.Expr("frozen_balance - ?", amount),
})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}
func (s *WalletStore) UnfreezeBalanceWithTx(ctx context.Context, tx *gorm.DB, walletID uint, amount int64) error {
result := tx.WithContext(ctx).
Model(&model.Wallet{}).
Where("id = ? AND frozen_balance >= ?", walletID, amount).
Updates(map[string]interface{}{
"balance": gorm.Expr("balance + ?", amount),
"frozen_balance": gorm.Expr("frozen_balance - ?", amount),
})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}

View File

@@ -1,37 +0,0 @@
package postgres
import (
"context"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
type WalletTransactionStore struct {
db *gorm.DB
redis *redis.Client
}
func NewWalletTransactionStore(db *gorm.DB, redis *redis.Client) *WalletTransactionStore {
return &WalletTransactionStore{
db: db,
redis: redis,
}
}
func (s *WalletTransactionStore) CreateWithTx(ctx context.Context, tx *gorm.DB, transaction *model.WalletTransaction) error {
return tx.WithContext(ctx).Create(transaction).Error
}
func (s *WalletTransactionStore) Create(ctx context.Context, transaction *model.WalletTransaction) error {
return s.db.WithContext(ctx).Create(transaction).Error
}
func (s *WalletTransactionStore) GetByID(ctx context.Context, id uint) (*model.WalletTransaction, error) {
var tx model.WalletTransaction
if err := s.db.WithContext(ctx).First(&tx, id).Error; err != nil {
return nil, err
}
return &tx, nil
}