feat: 实现门店套餐分配功能并统一测试基础设施
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m30s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m30s
新增功能: - 门店套餐分配管理(shop_package_allocation):支持门店套餐库存管理 - 门店套餐系列分配管理(shop_series_allocation):支持套餐系列分配和佣金层级设置 - 我的套餐查询(my_package):支持门店查询自己的套餐分配情况 测试改进: - 统一集成测试基础设施,新增 testutils.NewIntegrationTestEnv - 重构所有集成测试使用新的测试环境设置 - 移除旧的测试辅助函数和冗余测试文件 - 新增 test_helpers_test.go 统一任务测试辅助 技术细节: - 新增数据库迁移 000025_create_shop_allocation_tables - 新增 3 个 Handler、Service、Store 和对应的单元测试 - 更新 OpenAPI 文档和文档生成器 - 测试覆盖率:Service 层 > 90% Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
306
internal/service/my_package/service.go
Normal file
306
internal/service/my_package/service.go
Normal file
@@ -0,0 +1,306 @@
|
||||
package my_package
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
seriesAllocationStore *postgres.ShopSeriesAllocationStore
|
||||
packageAllocationStore *postgres.ShopPackageAllocationStore
|
||||
packageSeriesStore *postgres.PackageSeriesStore
|
||||
packageStore *postgres.PackageStore
|
||||
shopStore *postgres.ShopStore
|
||||
}
|
||||
|
||||
func New(
|
||||
seriesAllocationStore *postgres.ShopSeriesAllocationStore,
|
||||
packageAllocationStore *postgres.ShopPackageAllocationStore,
|
||||
packageSeriesStore *postgres.PackageSeriesStore,
|
||||
packageStore *postgres.PackageStore,
|
||||
shopStore *postgres.ShopStore,
|
||||
) *Service {
|
||||
return &Service{
|
||||
seriesAllocationStore: seriesAllocationStore,
|
||||
packageAllocationStore: packageAllocationStore,
|
||||
packageSeriesStore: packageSeriesStore,
|
||||
packageStore: packageStore,
|
||||
shopStore: shopStore,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) ListMyPackages(ctx context.Context, req *dto.MyPackageListRequest) ([]*dto.MyPackageResponse, int64, error) {
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
if shopID == 0 {
|
||||
return nil, 0, errors.New(errors.CodeUnauthorized, "当前用户不属于任何店铺")
|
||||
}
|
||||
|
||||
seriesAllocations, err := s.seriesAllocationStore.GetByShopID(ctx, shopID)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("获取系列分配失败: %w", err)
|
||||
}
|
||||
|
||||
if len(seriesAllocations) == 0 {
|
||||
return []*dto.MyPackageResponse{}, 0, nil
|
||||
}
|
||||
|
||||
seriesIDs := make([]uint, 0, len(seriesAllocations))
|
||||
for _, sa := range seriesAllocations {
|
||||
seriesIDs = append(seriesIDs, sa.SeriesID)
|
||||
}
|
||||
|
||||
opts := &store.QueryOptions{
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
OrderBy: "id DESC",
|
||||
}
|
||||
if opts.Page == 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
opts.PageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
filters := make(map[string]interface{})
|
||||
filters["series_ids"] = seriesIDs
|
||||
filters["status"] = constants.StatusEnabled
|
||||
filters["shelf_status"] = 1
|
||||
|
||||
if req.SeriesID != nil {
|
||||
found := false
|
||||
for _, sid := range seriesIDs {
|
||||
if sid == *req.SeriesID {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return []*dto.MyPackageResponse{}, 0, nil
|
||||
}
|
||||
filters["series_id"] = *req.SeriesID
|
||||
}
|
||||
if req.PackageType != nil {
|
||||
filters["package_type"] = *req.PackageType
|
||||
}
|
||||
|
||||
packages, total, err := s.packageStore.List(ctx, opts, filters)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("查询套餐列表失败: %w", err)
|
||||
}
|
||||
|
||||
packageOverrides, _ := s.packageAllocationStore.GetByShopID(ctx, shopID)
|
||||
overrideMap := make(map[uint]*model.ShopPackageAllocation)
|
||||
for _, po := range packageOverrides {
|
||||
overrideMap[po.PackageID] = po
|
||||
}
|
||||
|
||||
allocationMap := make(map[uint]*model.ShopSeriesAllocation)
|
||||
for _, sa := range seriesAllocations {
|
||||
allocationMap[sa.SeriesID] = sa
|
||||
}
|
||||
|
||||
responses := make([]*dto.MyPackageResponse, len(packages))
|
||||
for i, pkg := range packages {
|
||||
series, _ := s.packageSeriesStore.GetByID(ctx, pkg.SeriesID)
|
||||
seriesName := ""
|
||||
if series != nil {
|
||||
seriesName = series.SeriesName
|
||||
}
|
||||
|
||||
costPrice, priceSource := s.GetCostPrice(ctx, shopID, pkg, allocationMap, overrideMap)
|
||||
|
||||
responses[i] = &dto.MyPackageResponse{
|
||||
ID: pkg.ID,
|
||||
PackageCode: pkg.PackageCode,
|
||||
PackageName: pkg.PackageName,
|
||||
PackageType: pkg.PackageType,
|
||||
SeriesID: pkg.SeriesID,
|
||||
SeriesName: seriesName,
|
||||
CostPrice: costPrice,
|
||||
SuggestedRetailPrice: pkg.SuggestedRetailPrice,
|
||||
ProfitMargin: pkg.SuggestedRetailPrice - costPrice,
|
||||
PriceSource: priceSource,
|
||||
Status: pkg.Status,
|
||||
ShelfStatus: pkg.ShelfStatus,
|
||||
}
|
||||
}
|
||||
|
||||
return responses, total, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetMyPackage(ctx context.Context, packageID uint) (*dto.MyPackageDetailResponse, error) {
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
if shopID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "当前用户不属于任何店铺")
|
||||
}
|
||||
|
||||
pkg, err := s.packageStore.GetByID(ctx, packageID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeNotFound, "套餐不存在")
|
||||
}
|
||||
|
||||
seriesAllocation, err := s.seriesAllocationStore.GetByShopAndSeries(ctx, shopID, pkg.SeriesID)
|
||||
if err != nil {
|
||||
return nil, errors.New(errors.CodeForbidden, "您没有该套餐的销售权限")
|
||||
}
|
||||
|
||||
series, _ := s.packageSeriesStore.GetByID(ctx, pkg.SeriesID)
|
||||
seriesName := ""
|
||||
if series != nil {
|
||||
seriesName = series.SeriesName
|
||||
}
|
||||
|
||||
allocationMap := map[uint]*model.ShopSeriesAllocation{pkg.SeriesID: seriesAllocation}
|
||||
|
||||
packageOverride, _ := s.packageAllocationStore.GetByShopAndPackage(ctx, shopID, packageID)
|
||||
overrideMap := make(map[uint]*model.ShopPackageAllocation)
|
||||
if packageOverride != nil {
|
||||
overrideMap[packageID] = packageOverride
|
||||
}
|
||||
|
||||
costPrice, priceSource := s.GetCostPrice(ctx, shopID, pkg, allocationMap, overrideMap)
|
||||
|
||||
return &dto.MyPackageDetailResponse{
|
||||
ID: pkg.ID,
|
||||
PackageCode: pkg.PackageCode,
|
||||
PackageName: pkg.PackageName,
|
||||
PackageType: pkg.PackageType,
|
||||
Description: "",
|
||||
SeriesID: pkg.SeriesID,
|
||||
SeriesName: seriesName,
|
||||
CostPrice: costPrice,
|
||||
SuggestedRetailPrice: pkg.SuggestedRetailPrice,
|
||||
ProfitMargin: pkg.SuggestedRetailPrice - costPrice,
|
||||
PriceSource: priceSource,
|
||||
Status: pkg.Status,
|
||||
ShelfStatus: pkg.ShelfStatus,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListMySeriesAllocations(ctx context.Context, req *dto.MySeriesAllocationListRequest) ([]*dto.MySeriesAllocationResponse, int64, error) {
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
if shopID == 0 {
|
||||
return nil, 0, errors.New(errors.CodeUnauthorized, "当前用户不属于任何店铺")
|
||||
}
|
||||
|
||||
allocations, err := s.seriesAllocationStore.GetByShopID(ctx, shopID)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("获取系列分配失败: %w", err)
|
||||
}
|
||||
|
||||
total := int64(len(allocations))
|
||||
|
||||
page := req.Page
|
||||
pageSize := req.PageSize
|
||||
if page == 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize == 0 {
|
||||
pageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
start := (page - 1) * pageSize
|
||||
end := start + pageSize
|
||||
if start >= int(total) {
|
||||
return []*dto.MySeriesAllocationResponse{}, total, nil
|
||||
}
|
||||
if end > int(total) {
|
||||
end = int(total)
|
||||
}
|
||||
|
||||
allocations = allocations[start:end]
|
||||
|
||||
responses := make([]*dto.MySeriesAllocationResponse, len(allocations))
|
||||
for i, a := range allocations {
|
||||
series, _ := s.packageSeriesStore.GetByID(ctx, a.SeriesID)
|
||||
seriesCode := ""
|
||||
seriesName := ""
|
||||
if series != nil {
|
||||
seriesCode = series.SeriesCode
|
||||
seriesName = series.SeriesName
|
||||
}
|
||||
|
||||
allocatorShop, _ := s.shopStore.GetByID(ctx, a.AllocatorShopID)
|
||||
allocatorShopName := ""
|
||||
if allocatorShop != nil {
|
||||
allocatorShopName = allocatorShop.ShopName
|
||||
}
|
||||
|
||||
availableCount := 0
|
||||
filters := map[string]interface{}{
|
||||
"series_id": a.SeriesID,
|
||||
"status": constants.StatusEnabled,
|
||||
"shelf_status": 1,
|
||||
}
|
||||
packages, _, _ := s.packageStore.List(ctx, &store.QueryOptions{Page: 1, PageSize: 1000}, filters)
|
||||
availableCount = len(packages)
|
||||
|
||||
responses[i] = &dto.MySeriesAllocationResponse{
|
||||
ID: a.ID,
|
||||
SeriesID: a.SeriesID,
|
||||
SeriesCode: seriesCode,
|
||||
SeriesName: seriesName,
|
||||
PricingMode: a.PricingMode,
|
||||
PricingValue: a.PricingValue,
|
||||
AvailablePackageCount: availableCount,
|
||||
AllocatorShopName: allocatorShopName,
|
||||
Status: a.Status,
|
||||
}
|
||||
}
|
||||
|
||||
return responses, total, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetCostPrice(ctx context.Context, shopID uint, pkg *model.Package, allocationMap map[uint]*model.ShopSeriesAllocation, overrideMap map[uint]*model.ShopPackageAllocation) (int64, string) {
|
||||
if override, ok := overrideMap[pkg.ID]; ok && override.Status == constants.StatusEnabled {
|
||||
return override.CostPrice, dto.PriceSourcePackageOverride
|
||||
}
|
||||
|
||||
allocation, ok := allocationMap[pkg.SeriesID]
|
||||
if !ok {
|
||||
return 0, ""
|
||||
}
|
||||
|
||||
parentCostPrice := s.getParentCostPriceRecursive(ctx, allocation.AllocatorShopID, pkg)
|
||||
costPrice := s.calculateCostPrice(parentCostPrice, allocation.PricingMode, allocation.PricingValue)
|
||||
|
||||
return costPrice, dto.PriceSourceSeriesPricing
|
||||
}
|
||||
|
||||
func (s *Service) getParentCostPriceRecursive(ctx context.Context, shopID uint, pkg *model.Package) int64 {
|
||||
shop, err := s.shopStore.GetByID(ctx, shopID)
|
||||
if err != nil {
|
||||
return pkg.SuggestedCostPrice
|
||||
}
|
||||
|
||||
if shop.ParentID == nil || *shop.ParentID == 0 {
|
||||
return pkg.SuggestedCostPrice
|
||||
}
|
||||
|
||||
allocation, err := s.seriesAllocationStore.GetByShopAndSeries(ctx, shopID, pkg.SeriesID)
|
||||
if err != nil {
|
||||
return pkg.SuggestedCostPrice
|
||||
}
|
||||
|
||||
parentCostPrice := s.getParentCostPriceRecursive(ctx, allocation.AllocatorShopID, pkg)
|
||||
return s.calculateCostPrice(parentCostPrice, allocation.PricingMode, allocation.PricingValue)
|
||||
}
|
||||
|
||||
func (s *Service) calculateCostPrice(parentCostPrice int64, pricingMode string, pricingValue int64) int64 {
|
||||
switch pricingMode {
|
||||
case model.PricingModeFixed:
|
||||
return parentCostPrice + pricingValue
|
||||
case model.PricingModePercent:
|
||||
return parentCostPrice + (parentCostPrice * pricingValue / 1000)
|
||||
default:
|
||||
return parentCostPrice
|
||||
}
|
||||
}
|
||||
820
internal/service/my_package/service_test.go
Normal file
820
internal/service/my_package/service_test.go
Normal file
@@ -0,0 +1,820 @@
|
||||
package my_package
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"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"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestService_GetCostPrice_Priority(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
ctx := context.Background()
|
||||
|
||||
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
|
||||
packageAllocationStore := postgres.NewShopPackageAllocationStore(tx)
|
||||
packageSeriesStore := postgres.NewPackageSeriesStore(tx)
|
||||
packageStore := postgres.NewPackageStore(tx)
|
||||
shopStore := postgres.NewShopStore(tx, nil)
|
||||
|
||||
// 创建 Service
|
||||
svc := New(seriesAllocationStore, packageAllocationStore, packageSeriesStore, packageStore, shopStore)
|
||||
|
||||
// 创建测试数据:套餐系列
|
||||
series := &model.PackageSeries{
|
||||
SeriesCode: "TEST_SERIES_001",
|
||||
SeriesName: "测试系列",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, packageSeriesStore.Create(ctx, series))
|
||||
|
||||
// 创建测试数据:套餐
|
||||
pkg := &model.Package{
|
||||
PackageCode: "TEST_PKG_001",
|
||||
PackageName: "测试套餐",
|
||||
SeriesID: series.ID,
|
||||
PackageType: "formal",
|
||||
DurationMonths: 1,
|
||||
DataType: "real",
|
||||
RealDataMB: 1024,
|
||||
DataAmountMB: 1024,
|
||||
Price: 9900,
|
||||
Status: constants.StatusEnabled,
|
||||
ShelfStatus: 1,
|
||||
SuggestedCostPrice: 5000, // 基础成本价:50元
|
||||
SuggestedRetailPrice: 9900,
|
||||
}
|
||||
require.NoError(t, packageStore.Create(ctx, pkg))
|
||||
|
||||
// 创建测试数据:上级店铺
|
||||
allocatorShop := &model.Shop{
|
||||
ShopName: "上级店铺",
|
||||
ShopCode: "ALLOCATOR_001",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000000",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, allocatorShop))
|
||||
|
||||
// 创建测试数据:下级店铺
|
||||
shop := &model.Shop{
|
||||
ShopName: "下级店铺",
|
||||
ShopCode: "SHOP_001",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 2,
|
||||
ParentID: &allocatorShop.ID,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, shop))
|
||||
|
||||
// 创建测试数据:系列分配(系列加价模式)
|
||||
seriesAllocation := &model.ShopSeriesAllocation{
|
||||
ShopID: shop.ID,
|
||||
SeriesID: series.ID,
|
||||
AllocatorShopID: allocatorShop.ID,
|
||||
PricingMode: model.PricingModeFixed,
|
||||
PricingValue: 1000, // 固定加价:10元
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, seriesAllocationStore.Create(ctx, seriesAllocation))
|
||||
|
||||
t.Run("套餐覆盖优先级最高", func(t *testing.T) {
|
||||
// 创建套餐覆盖(覆盖成本价:80元)
|
||||
packageOverride := &model.ShopPackageAllocation{
|
||||
ShopID: shop.ID,
|
||||
PackageID: pkg.ID,
|
||||
AllocationID: seriesAllocation.ID,
|
||||
CostPrice: 8000,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, packageAllocationStore.Create(ctx, packageOverride))
|
||||
|
||||
allocationMap := map[uint]*model.ShopSeriesAllocation{series.ID: seriesAllocation}
|
||||
overrideMap := map[uint]*model.ShopPackageAllocation{pkg.ID: packageOverride}
|
||||
|
||||
costPrice, priceSource := svc.GetCostPrice(ctx, shop.ID, pkg, allocationMap, overrideMap)
|
||||
|
||||
// 应该返回套餐覆盖的成本价
|
||||
assert.Equal(t, int64(8000), costPrice)
|
||||
assert.Equal(t, dto.PriceSourcePackageOverride, priceSource)
|
||||
})
|
||||
|
||||
t.Run("套餐覆盖禁用时使用系列加价", func(t *testing.T) {
|
||||
pkg2 := &model.Package{
|
||||
PackageCode: "TEST_PKG_001_DISABLED",
|
||||
PackageName: "测试套餐禁用",
|
||||
SeriesID: series.ID,
|
||||
PackageType: "formal",
|
||||
DurationMonths: 1,
|
||||
DataType: "real",
|
||||
RealDataMB: 1024,
|
||||
DataAmountMB: 1024,
|
||||
Price: 9900,
|
||||
Status: constants.StatusEnabled,
|
||||
ShelfStatus: 1,
|
||||
SuggestedCostPrice: 5000,
|
||||
SuggestedRetailPrice: 9900,
|
||||
}
|
||||
require.NoError(t, packageStore.Create(ctx, pkg2))
|
||||
|
||||
packageOverride := &model.ShopPackageAllocation{
|
||||
ShopID: shop.ID,
|
||||
PackageID: pkg2.ID,
|
||||
AllocationID: seriesAllocation.ID,
|
||||
CostPrice: 8000,
|
||||
Status: constants.StatusDisabled,
|
||||
}
|
||||
|
||||
allocationMap := map[uint]*model.ShopSeriesAllocation{series.ID: seriesAllocation}
|
||||
overrideMap := map[uint]*model.ShopPackageAllocation{pkg2.ID: packageOverride}
|
||||
|
||||
costPrice, priceSource := svc.GetCostPrice(ctx, shop.ID, pkg2, allocationMap, overrideMap)
|
||||
|
||||
assert.Equal(t, int64(6000), costPrice)
|
||||
assert.Equal(t, dto.PriceSourceSeriesPricing, priceSource)
|
||||
})
|
||||
|
||||
t.Run("无套餐覆盖时使用系列加价", func(t *testing.T) {
|
||||
allocationMap := map[uint]*model.ShopSeriesAllocation{series.ID: seriesAllocation}
|
||||
overrideMap := make(map[uint]*model.ShopPackageAllocation)
|
||||
|
||||
costPrice, priceSource := svc.GetCostPrice(ctx, shop.ID, pkg, allocationMap, overrideMap)
|
||||
|
||||
// 应该返回系列加价的成本价:5000 + 1000 = 6000
|
||||
assert.Equal(t, int64(6000), costPrice)
|
||||
assert.Equal(t, dto.PriceSourceSeriesPricing, priceSource)
|
||||
})
|
||||
|
||||
t.Run("无系列分配时返回0", func(t *testing.T) {
|
||||
allocationMap := make(map[uint]*model.ShopSeriesAllocation)
|
||||
overrideMap := make(map[uint]*model.ShopPackageAllocation)
|
||||
|
||||
costPrice, priceSource := svc.GetCostPrice(ctx, shop.ID, pkg, allocationMap, overrideMap)
|
||||
|
||||
// 应该返回0和空的价格来源
|
||||
assert.Equal(t, int64(0), costPrice)
|
||||
assert.Equal(t, "", priceSource)
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_calculateCostPrice(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
|
||||
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
|
||||
packageAllocationStore := postgres.NewShopPackageAllocationStore(tx)
|
||||
packageSeriesStore := postgres.NewPackageSeriesStore(tx)
|
||||
packageStore := postgres.NewPackageStore(tx)
|
||||
shopStore := postgres.NewShopStore(tx, nil)
|
||||
|
||||
// 创建 Service
|
||||
svc := New(seriesAllocationStore, packageAllocationStore, packageSeriesStore, packageStore, shopStore)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
parentCostPrice int64
|
||||
pricingMode string
|
||||
pricingValue int64
|
||||
expectedCostPrice int64
|
||||
description string
|
||||
}{
|
||||
{
|
||||
name: "固定金额加价模式",
|
||||
parentCostPrice: 5000, // 50元
|
||||
pricingMode: model.PricingModeFixed,
|
||||
pricingValue: 1000, // 加价10元
|
||||
expectedCostPrice: 6000, // 60元
|
||||
description: "固定加价:5000 + 1000 = 6000",
|
||||
},
|
||||
{
|
||||
name: "百分比加价模式",
|
||||
parentCostPrice: 5000, // 50元
|
||||
pricingMode: model.PricingModePercent,
|
||||
pricingValue: 200, // 20%(千分比:200/1000 = 20%)
|
||||
expectedCostPrice: 6000, // 50 + 50*20% = 60元
|
||||
description: "百分比加价:5000 + (5000 * 200 / 1000) = 6000",
|
||||
},
|
||||
{
|
||||
name: "百分比加价模式-10%",
|
||||
parentCostPrice: 10000, // 100元
|
||||
pricingMode: model.PricingModePercent,
|
||||
pricingValue: 100, // 10%(千分比:100/1000 = 10%)
|
||||
expectedCostPrice: 11000, // 100 + 100*10% = 110元
|
||||
description: "百分比加价:10000 + (10000 * 100 / 1000) = 11000",
|
||||
},
|
||||
{
|
||||
name: "未知加价模式返回原价",
|
||||
parentCostPrice: 5000,
|
||||
pricingMode: "unknown",
|
||||
pricingValue: 1000,
|
||||
expectedCostPrice: 5000, // 返回原价不变
|
||||
description: "未知模式:返回 parentCostPrice 不变",
|
||||
},
|
||||
{
|
||||
name: "零加价",
|
||||
parentCostPrice: 5000,
|
||||
pricingMode: model.PricingModeFixed,
|
||||
pricingValue: 0,
|
||||
expectedCostPrice: 5000,
|
||||
description: "零加价:5000 + 0 = 5000",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
costPrice := svc.calculateCostPrice(tt.parentCostPrice, tt.pricingMode, tt.pricingValue)
|
||||
assert.Equal(t, tt.expectedCostPrice, costPrice, tt.description)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_ListMyPackages_Authorization(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
ctx := context.Background()
|
||||
|
||||
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
|
||||
packageAllocationStore := postgres.NewShopPackageAllocationStore(tx)
|
||||
packageSeriesStore := postgres.NewPackageSeriesStore(tx)
|
||||
packageStore := postgres.NewPackageStore(tx)
|
||||
shopStore := postgres.NewShopStore(tx, nil)
|
||||
|
||||
// 创建 Service
|
||||
svc := New(seriesAllocationStore, packageAllocationStore, packageSeriesStore, packageStore, shopStore)
|
||||
|
||||
t.Run("店铺ID为0时返回错误", func(t *testing.T) {
|
||||
// 创建不包含店铺ID的context
|
||||
ctxWithoutShop := context.WithValue(ctx, constants.ContextKeyShopID, uint(0))
|
||||
|
||||
req := &dto.MyPackageListRequest{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
packages, total, err := svc.ListMyPackages(ctxWithoutShop, req)
|
||||
|
||||
// 应该返回错误
|
||||
require.Error(t, err)
|
||||
assert.Nil(t, packages)
|
||||
assert.Equal(t, int64(0), total)
|
||||
assert.Contains(t, err.Error(), "当前用户不属于任何店铺")
|
||||
})
|
||||
|
||||
t.Run("无系列分配时返回空列表", func(t *testing.T) {
|
||||
// 创建店铺
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试店铺",
|
||||
ShopCode: "SHOP_TEST_001",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000000",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, shop))
|
||||
|
||||
// 创建包含店铺ID的context
|
||||
ctxWithShop := context.WithValue(ctx, constants.ContextKeyShopID, shop.ID)
|
||||
|
||||
req := &dto.MyPackageListRequest{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
packages, total, err := svc.ListMyPackages(ctxWithShop, req)
|
||||
|
||||
// 应该返回空列表,无错误
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, packages)
|
||||
assert.Equal(t, 0, len(packages))
|
||||
assert.Equal(t, int64(0), total)
|
||||
})
|
||||
|
||||
t.Run("有系列分配时返回套餐列表", func(t *testing.T) {
|
||||
// 创建套餐系列
|
||||
series := &model.PackageSeries{
|
||||
SeriesCode: "TEST_SERIES_002",
|
||||
SeriesName: "测试系列2",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, packageSeriesStore.Create(ctx, series))
|
||||
|
||||
// 创建套餐
|
||||
pkg := &model.Package{
|
||||
PackageCode: "TEST_PKG_002",
|
||||
PackageName: "测试套餐2",
|
||||
SeriesID: series.ID,
|
||||
PackageType: "formal",
|
||||
DurationMonths: 1,
|
||||
DataType: "real",
|
||||
RealDataMB: 1024,
|
||||
DataAmountMB: 1024,
|
||||
Price: 9900,
|
||||
Status: constants.StatusEnabled,
|
||||
ShelfStatus: 1,
|
||||
SuggestedCostPrice: 5000,
|
||||
SuggestedRetailPrice: 9900,
|
||||
}
|
||||
require.NoError(t, packageStore.Create(ctx, pkg))
|
||||
|
||||
// 创建上级店铺
|
||||
allocatorShop := &model.Shop{
|
||||
ShopName: "上级店铺2",
|
||||
ShopCode: "ALLOCATOR_002",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000000",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, allocatorShop))
|
||||
|
||||
// 创建下级店铺
|
||||
shop := &model.Shop{
|
||||
ShopName: "下级店铺2",
|
||||
ShopCode: "SHOP_002",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 2,
|
||||
ParentID: &allocatorShop.ID,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, shop))
|
||||
|
||||
// 创建系列分配
|
||||
seriesAllocation := &model.ShopSeriesAllocation{
|
||||
ShopID: shop.ID,
|
||||
SeriesID: series.ID,
|
||||
AllocatorShopID: allocatorShop.ID,
|
||||
PricingMode: model.PricingModeFixed,
|
||||
PricingValue: 1000,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, seriesAllocationStore.Create(ctx, seriesAllocation))
|
||||
|
||||
// 创建包含店铺ID的context
|
||||
ctxWithShop := context.WithValue(ctx, constants.ContextKeyShopID, shop.ID)
|
||||
|
||||
req := &dto.MyPackageListRequest{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
packages, total, err := svc.ListMyPackages(ctxWithShop, req)
|
||||
|
||||
// 应该返回套餐列表
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, packages)
|
||||
assert.Equal(t, 1, len(packages))
|
||||
assert.Equal(t, int64(1), total)
|
||||
assert.Equal(t, pkg.ID, packages[0].ID)
|
||||
assert.Equal(t, pkg.PackageName, packages[0].PackageName)
|
||||
// 验证成本价计算:5000 + 1000 = 6000
|
||||
assert.Equal(t, int64(6000), packages[0].CostPrice)
|
||||
assert.Equal(t, dto.PriceSourceSeriesPricing, packages[0].PriceSource)
|
||||
})
|
||||
|
||||
t.Run("分页参数默认值", func(t *testing.T) {
|
||||
series := &model.PackageSeries{
|
||||
SeriesCode: "TEST_SERIES_PAGING",
|
||||
SeriesName: "分页测试系列",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, packageSeriesStore.Create(ctx, series))
|
||||
|
||||
for i := range 5 {
|
||||
pkg := &model.Package{
|
||||
PackageCode: "TEST_PKG_PAGING_" + string(byte('0'+byte(i))),
|
||||
PackageName: "分页测试套餐_" + string(byte('0'+byte(i))),
|
||||
SeriesID: series.ID,
|
||||
PackageType: "formal",
|
||||
DurationMonths: 1,
|
||||
DataType: "real",
|
||||
RealDataMB: 1024,
|
||||
DataAmountMB: 1024,
|
||||
Price: 9900,
|
||||
Status: constants.StatusEnabled,
|
||||
ShelfStatus: 1,
|
||||
SuggestedCostPrice: 5000,
|
||||
SuggestedRetailPrice: 9900,
|
||||
}
|
||||
require.NoError(t, packageStore.Create(ctx, pkg))
|
||||
}
|
||||
|
||||
allocatorShop := &model.Shop{
|
||||
ShopName: "分页上级店铺",
|
||||
ShopCode: "ALLOCATOR_PAGING",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000000",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, allocatorShop))
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "分页下级店铺",
|
||||
ShopCode: "SHOP_PAGING",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 2,
|
||||
ParentID: &allocatorShop.ID,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, shop))
|
||||
|
||||
seriesAllocation := &model.ShopSeriesAllocation{
|
||||
ShopID: shop.ID,
|
||||
SeriesID: series.ID,
|
||||
AllocatorShopID: allocatorShop.ID,
|
||||
PricingMode: model.PricingModeFixed,
|
||||
PricingValue: 1000,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, seriesAllocationStore.Create(ctx, seriesAllocation))
|
||||
|
||||
ctxWithShop := context.WithValue(ctx, constants.ContextKeyShopID, shop.ID)
|
||||
|
||||
req := &dto.MyPackageListRequest{}
|
||||
|
||||
packages, total, err := svc.ListMyPackages(ctxWithShop, req)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, packages)
|
||||
assert.GreaterOrEqual(t, total, int64(5))
|
||||
assert.LessOrEqual(t, len(packages), constants.DefaultPageSize)
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_ListMyPackages_Filtering(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
ctx := context.Background()
|
||||
|
||||
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
|
||||
packageAllocationStore := postgres.NewShopPackageAllocationStore(tx)
|
||||
packageSeriesStore := postgres.NewPackageSeriesStore(tx)
|
||||
packageStore := postgres.NewPackageStore(tx)
|
||||
shopStore := postgres.NewShopStore(tx, nil)
|
||||
|
||||
// 创建 Service
|
||||
svc := New(seriesAllocationStore, packageAllocationStore, packageSeriesStore, packageStore, shopStore)
|
||||
|
||||
// 创建两个套餐系列
|
||||
series1 := &model.PackageSeries{
|
||||
SeriesCode: "SERIES_FILTER_001",
|
||||
SeriesName: "系列1",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, packageSeriesStore.Create(ctx, series1))
|
||||
|
||||
series2 := &model.PackageSeries{
|
||||
SeriesCode: "SERIES_FILTER_002",
|
||||
SeriesName: "系列2",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, packageSeriesStore.Create(ctx, series2))
|
||||
|
||||
// 创建不同类型的套餐
|
||||
pkg1 := &model.Package{
|
||||
PackageCode: "PKG_FILTER_001",
|
||||
PackageName: "正式套餐1",
|
||||
SeriesID: series1.ID,
|
||||
PackageType: "formal",
|
||||
DurationMonths: 1,
|
||||
DataType: "real",
|
||||
RealDataMB: 1024,
|
||||
DataAmountMB: 1024,
|
||||
Price: 9900,
|
||||
Status: constants.StatusEnabled,
|
||||
ShelfStatus: 1,
|
||||
SuggestedCostPrice: 5000,
|
||||
SuggestedRetailPrice: 9900,
|
||||
}
|
||||
require.NoError(t, packageStore.Create(ctx, pkg1))
|
||||
|
||||
pkg2 := &model.Package{
|
||||
PackageCode: "PKG_FILTER_002",
|
||||
PackageName: "附加套餐1",
|
||||
SeriesID: series2.ID,
|
||||
PackageType: "addon",
|
||||
DurationMonths: 1,
|
||||
DataType: "real",
|
||||
RealDataMB: 512,
|
||||
DataAmountMB: 512,
|
||||
Price: 4900,
|
||||
Status: constants.StatusEnabled,
|
||||
ShelfStatus: 1,
|
||||
SuggestedCostPrice: 2500,
|
||||
SuggestedRetailPrice: 4900,
|
||||
}
|
||||
require.NoError(t, packageStore.Create(ctx, pkg2))
|
||||
|
||||
// 创建上级店铺
|
||||
allocatorShop := &model.Shop{
|
||||
ShopName: "上级店铺过滤",
|
||||
ShopCode: "ALLOCATOR_FILTER",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000000",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, allocatorShop))
|
||||
|
||||
// 创建下级店铺
|
||||
shop := &model.Shop{
|
||||
ShopName: "下级店铺过滤",
|
||||
ShopCode: "SHOP_FILTER",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 2,
|
||||
ParentID: &allocatorShop.ID,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, shop))
|
||||
|
||||
// 为两个系列都创建分配
|
||||
for _, series := range []*model.PackageSeries{series1, series2} {
|
||||
seriesAllocation := &model.ShopSeriesAllocation{
|
||||
ShopID: shop.ID,
|
||||
SeriesID: series.ID,
|
||||
AllocatorShopID: allocatorShop.ID,
|
||||
PricingMode: model.PricingModeFixed,
|
||||
PricingValue: 1000,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, seriesAllocationStore.Create(ctx, seriesAllocation))
|
||||
}
|
||||
|
||||
ctxWithShop := context.WithValue(ctx, constants.ContextKeyShopID, shop.ID)
|
||||
|
||||
t.Run("按系列ID过滤", func(t *testing.T) {
|
||||
req := &dto.MyPackageListRequest{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
SeriesID: &series1.ID,
|
||||
}
|
||||
|
||||
packages, total, err := svc.ListMyPackages(ctxWithShop, req)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(1), total)
|
||||
assert.Equal(t, 1, len(packages))
|
||||
assert.Equal(t, pkg1.ID, packages[0].ID)
|
||||
})
|
||||
|
||||
t.Run("按套餐类型过滤", func(t *testing.T) {
|
||||
packageType := "addon"
|
||||
req := &dto.MyPackageListRequest{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
PackageType: &packageType,
|
||||
}
|
||||
|
||||
packages, total, err := svc.ListMyPackages(ctxWithShop, req)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(1), total)
|
||||
assert.Equal(t, 1, len(packages))
|
||||
assert.Equal(t, pkg2.ID, packages[0].ID)
|
||||
})
|
||||
|
||||
t.Run("无效的系列ID返回空列表", func(t *testing.T) {
|
||||
invalidSeriesID := uint(99999)
|
||||
req := &dto.MyPackageListRequest{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
SeriesID: &invalidSeriesID,
|
||||
}
|
||||
|
||||
packages, total, err := svc.ListMyPackages(ctxWithShop, req)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(0), total)
|
||||
assert.Equal(t, 0, len(packages))
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_GetMyPackage(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
ctx := context.Background()
|
||||
|
||||
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
|
||||
packageAllocationStore := postgres.NewShopPackageAllocationStore(tx)
|
||||
packageSeriesStore := postgres.NewPackageSeriesStore(tx)
|
||||
packageStore := postgres.NewPackageStore(tx)
|
||||
shopStore := postgres.NewShopStore(tx, nil)
|
||||
|
||||
// 创建 Service
|
||||
svc := New(seriesAllocationStore, packageAllocationStore, packageSeriesStore, packageStore, shopStore)
|
||||
|
||||
// 创建套餐系列
|
||||
series := &model.PackageSeries{
|
||||
SeriesCode: "DETAIL_SERIES",
|
||||
SeriesName: "详情系列",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, packageSeriesStore.Create(ctx, series))
|
||||
|
||||
// 创建套餐
|
||||
pkg := &model.Package{
|
||||
PackageCode: "DETAIL_PKG",
|
||||
PackageName: "详情套餐",
|
||||
SeriesID: series.ID,
|
||||
PackageType: "formal",
|
||||
DurationMonths: 1,
|
||||
DataType: "real",
|
||||
RealDataMB: 1024,
|
||||
DataAmountMB: 1024,
|
||||
Price: 9900,
|
||||
Status: constants.StatusEnabled,
|
||||
ShelfStatus: 1,
|
||||
SuggestedCostPrice: 5000,
|
||||
SuggestedRetailPrice: 9900,
|
||||
}
|
||||
require.NoError(t, packageStore.Create(ctx, pkg))
|
||||
|
||||
// 创建上级店铺
|
||||
allocatorShop := &model.Shop{
|
||||
ShopName: "上级店铺详情",
|
||||
ShopCode: "ALLOCATOR_DETAIL",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000000",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, allocatorShop))
|
||||
|
||||
// 创建下级店铺
|
||||
shop := &model.Shop{
|
||||
ShopName: "下级店铺详情",
|
||||
ShopCode: "SHOP_DETAIL",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 2,
|
||||
ParentID: &allocatorShop.ID,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, shop))
|
||||
|
||||
// 创建系列分配
|
||||
seriesAllocation := &model.ShopSeriesAllocation{
|
||||
ShopID: shop.ID,
|
||||
SeriesID: series.ID,
|
||||
AllocatorShopID: allocatorShop.ID,
|
||||
PricingMode: model.PricingModeFixed,
|
||||
PricingValue: 1000,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, seriesAllocationStore.Create(ctx, seriesAllocation))
|
||||
|
||||
ctxWithShop := context.WithValue(ctx, constants.ContextKeyShopID, shop.ID)
|
||||
|
||||
t.Run("店铺ID为0时返回错误", func(t *testing.T) {
|
||||
ctxWithoutShop := context.WithValue(ctx, constants.ContextKeyShopID, uint(0))
|
||||
_, err := svc.GetMyPackage(ctxWithoutShop, pkg.ID)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "当前用户不属于任何店铺")
|
||||
})
|
||||
|
||||
t.Run("成功获取套餐详情", func(t *testing.T) {
|
||||
detail, err := svc.GetMyPackage(ctxWithShop, pkg.ID)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, detail)
|
||||
assert.Equal(t, pkg.ID, detail.ID)
|
||||
assert.Equal(t, pkg.PackageName, detail.PackageName)
|
||||
assert.Equal(t, series.SeriesName, detail.SeriesName)
|
||||
// 验证成本价:5000 + 1000 = 6000
|
||||
assert.Equal(t, int64(6000), detail.CostPrice)
|
||||
assert.Equal(t, dto.PriceSourceSeriesPricing, detail.PriceSource)
|
||||
})
|
||||
|
||||
t.Run("无权限访问套餐时返回错误", func(t *testing.T) {
|
||||
// 创建另一个没有系列分配的店铺
|
||||
otherShop := &model.Shop{
|
||||
ShopName: "其他店铺",
|
||||
ShopCode: "OTHER_SHOP",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000002",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, otherShop))
|
||||
|
||||
ctxWithOtherShop := context.WithValue(ctx, constants.ContextKeyShopID, otherShop.ID)
|
||||
_, err := svc.GetMyPackage(ctxWithOtherShop, pkg.ID)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "您没有该套餐的销售权限")
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_ListMySeriesAllocations(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
ctx := context.Background()
|
||||
|
||||
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
|
||||
packageAllocationStore := postgres.NewShopPackageAllocationStore(tx)
|
||||
packageSeriesStore := postgres.NewPackageSeriesStore(tx)
|
||||
packageStore := postgres.NewPackageStore(tx)
|
||||
shopStore := postgres.NewShopStore(tx, nil)
|
||||
|
||||
// 创建 Service
|
||||
svc := New(seriesAllocationStore, packageAllocationStore, packageSeriesStore, packageStore, shopStore)
|
||||
|
||||
t.Run("店铺ID为0时返回错误", func(t *testing.T) {
|
||||
ctxWithoutShop := context.WithValue(ctx, constants.ContextKeyShopID, uint(0))
|
||||
req := &dto.MySeriesAllocationListRequest{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
_, _, err := svc.ListMySeriesAllocations(ctxWithoutShop, req)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "当前用户不属于任何店铺")
|
||||
})
|
||||
|
||||
t.Run("无系列分配时返回空列表", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "分配测试店铺",
|
||||
ShopCode: "ALLOC_SHOP",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000000",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, shop))
|
||||
|
||||
ctxWithShop := context.WithValue(ctx, constants.ContextKeyShopID, shop.ID)
|
||||
req := &dto.MySeriesAllocationListRequest{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
allocations, total, err := svc.ListMySeriesAllocations(ctxWithShop, req)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, allocations)
|
||||
assert.Equal(t, 0, len(allocations))
|
||||
assert.Equal(t, int64(0), total)
|
||||
})
|
||||
|
||||
t.Run("成功列表系列分配", func(t *testing.T) {
|
||||
// 创建套餐系列
|
||||
series := &model.PackageSeries{
|
||||
SeriesCode: "ALLOC_SERIES",
|
||||
SeriesName: "分配系列",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, packageSeriesStore.Create(ctx, series))
|
||||
|
||||
// 创建上级店铺
|
||||
allocatorShop := &model.Shop{
|
||||
ShopName: "分配者店铺",
|
||||
ShopCode: "ALLOCATOR_ALLOC",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000000",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, allocatorShop))
|
||||
|
||||
// 创建下级店铺
|
||||
shop := &model.Shop{
|
||||
ShopName: "被分配店铺",
|
||||
ShopCode: "ALLOCATED_SHOP",
|
||||
Status: constants.StatusEnabled,
|
||||
Level: 2,
|
||||
ParentID: &allocatorShop.ID,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
}
|
||||
require.NoError(t, shopStore.Create(ctx, shop))
|
||||
|
||||
// 创建系列分配
|
||||
seriesAllocation := &model.ShopSeriesAllocation{
|
||||
ShopID: shop.ID,
|
||||
SeriesID: series.ID,
|
||||
AllocatorShopID: allocatorShop.ID,
|
||||
PricingMode: model.PricingModeFixed,
|
||||
PricingValue: 1000,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, seriesAllocationStore.Create(ctx, seriesAllocation))
|
||||
|
||||
ctxWithShop := context.WithValue(ctx, constants.ContextKeyShopID, shop.ID)
|
||||
req := &dto.MySeriesAllocationListRequest{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
allocations, total, err := svc.ListMySeriesAllocations(ctxWithShop, req)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, allocations)
|
||||
assert.Equal(t, 1, len(allocations))
|
||||
assert.Equal(t, int64(1), total)
|
||||
assert.Equal(t, series.SeriesName, allocations[0].SeriesName)
|
||||
assert.Equal(t, allocatorShop.ShopName, allocations[0].AllocatorShopName)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user