All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m35s
新增功能: - 店铺佣金查询:店铺佣金统计、店铺佣金记录列表、店铺提现记录 - 佣金提现审批:提现申请列表、审批通过、审批拒绝 - 提现配置管理:配置列表、新增配置、获取当前生效配置 - 企业管理:企业列表、创建、更新、删除、获取详情 - 企业卡授权:授权列表、批量授权、批量取消授权、统计 - 客户账号管理:账号列表、创建、更新状态、重置密码 - 我的佣金:佣金统计、佣金记录、提现申请、提现记录 数据库变更: - 扩展 tb_commission_withdrawal_request 新增提现单号等字段 - 扩展 tb_account 新增 is_primary 字段 - 扩展 tb_commission_record 新增 shop_id、balance_after - 扩展 tb_commission_withdrawal_setting 新增每日提现次数限制 - 扩展 tb_iot_card、tb_device 新增 shop_id 冗余字段 - 新建 tb_enterprise_card_authorization 企业卡授权表 - 新建 tb_asset_allocation_record 资产分配记录表 - 数据迁移:owner_type 枚举值 agent 统一为 shop 测试: - 新增 7 个单元测试文件覆盖各服务 - 修复集成测试 Redis 依赖问题
109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
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
|
|
}
|