feat: 实现账号与佣金管理模块
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m35s
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 依赖问题
This commit is contained in:
381
tests/unit/my_commission_service_test.go
Normal file
381
tests/unit/my_commission_service_test.go
Normal file
@@ -0,0 +1,381 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/my_commission"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createMyCommissionTestContext(userID uint, shopID uint, userType int) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, userType)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyShopID, shopID)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestMyCommissionService_GetCommissionSummary(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("佣金概览-代理商用户成功", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "概览测试店铺",
|
||||
ShopCode: "MY_SHOP_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
result, err := service.GetCommissionSummary(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, shop.ID, result.ShopID)
|
||||
assert.Equal(t, "概览测试店铺", result.ShopName)
|
||||
})
|
||||
|
||||
t.Run("佣金概览-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
_, err := service.GetCommissionSummary(ctx)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("佣金概览-店铺不存在应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 99999, constants.UserTypeAgent)
|
||||
|
||||
_, err := service.GetCommissionSummary(ctx)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("发起提现-无提现配置应失败", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "提现测试店铺",
|
||||
ShopCode: "MY_SHOP_002",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 10000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err = service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("发起提现-金额低于最低限制应失败", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "限额测试店铺",
|
||||
ShopCode: "MY_SHOP_003",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000003",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
setting := &model.CommissionWithdrawalSetting{
|
||||
DailyWithdrawalLimit: 5,
|
||||
MinWithdrawalAmount: 10000,
|
||||
FeeRate: 100,
|
||||
IsActive: true,
|
||||
}
|
||||
setting.Creator = 1
|
||||
setting.Updater = 1
|
||||
err = db.Create(setting).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 5000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err = service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("发起提现-余额不足应失败", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "余额测试店铺",
|
||||
ShopCode: "MY_SHOP_004",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000004",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
wallet := &model.Wallet{
|
||||
ResourceType: constants.WalletResourceTypeShop,
|
||||
ResourceID: shop.ID,
|
||||
WalletType: constants.WalletTypeCommission,
|
||||
Balance: 5000,
|
||||
}
|
||||
err = db.Create(wallet).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 50000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err = service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("发起提现-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 10000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err := service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("查询提现记录-空结果", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "提现记录测试店铺",
|
||||
ShopCode: "MY_SHOP_005",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000005",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.MyWithdrawalListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListMyWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询提现记录-按状态筛选", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "状态筛选测试店铺",
|
||||
ShopCode: "MY_SHOP_006",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000006",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
status := 1
|
||||
req := &model.MyWithdrawalListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Status: &status,
|
||||
}
|
||||
|
||||
result, err := service.ListMyWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
|
||||
t.Run("查询提现记录-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
req := &model.MyWithdrawalListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListMyWithdrawalRequests(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyCommissionRecords(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("查询佣金明细-空结果", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "佣金明细测试店铺",
|
||||
ShopCode: "MY_SHOP_007",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000007",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.MyCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListMyCommissionRecords(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询佣金明细-按类型筛选", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "类型筛选测试店铺",
|
||||
ShopCode: "MY_SHOP_008",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000008",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
commissionType := "one_time"
|
||||
req := &model.MyCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
CommissionType: &commissionType,
|
||||
}
|
||||
|
||||
result, err := service.ListMyCommissionRecords(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
|
||||
t.Run("查询佣金明细-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
req := &model.MyCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListMyCommissionRecords(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user