fix: 修复未启用一次性佣金的系列无法创建授权的问题
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m18s

This commit is contained in:
2026-04-02 14:27:09 +08:00
parent ecfa417c15
commit 322ded0012

View File

@@ -202,15 +202,13 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopSeriesGrantRequ
operatorShopID := middleware.GetShopIDFromContext(ctx) operatorShopID := middleware.GetShopIDFromContext(ctx)
operatorType := middleware.GetUserTypeFromContext(ctx) operatorType := middleware.GetUserTypeFromContext(ctx)
// 1. 查询套餐系列,确认佣金类型 // 1. 查询套餐系列,解析一次性佣金配置(可选)
// 系列授权同时作为套餐销售权限的入口记录,未启用一次性佣金的系列也可创建授权,佣金金额默认为 0
series, err := s.packageSeriesStore.GetByID(ctx, req.SeriesID) series, err := s.packageSeriesStore.GetByID(ctx, req.SeriesID)
if err != nil { if err != nil {
return nil, errors.New(errors.CodeNotFound, "套餐系列不存在") return nil, errors.New(errors.CodeNotFound, "套餐系列不存在")
} }
config, err := series.GetOneTimeCommissionConfig() config, _ := series.GetOneTimeCommissionConfig()
if err != nil || config == nil || !config.Enable {
return nil, errors.New(errors.CodeInvalidParam, "该系列未启用一次性佣金,无法创建授权")
}
// 1.5 校验目标店铺是否存在 // 1.5 校验目标店铺是否存在
_, err = s.shopStore.GetByID(ctx, req.ShopID) _, err = s.shopStore.GetByID(ctx, req.ShopID)
@@ -238,7 +236,7 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopSeriesGrantRequ
} }
// 平台/超管 allocatorShopID = 0 // 平台/超管 allocatorShopID = 0
// 4. 参数验证:固定模式 or 梯度模式 // 4. 参数验证:仅启用一次性佣金的系列才需要配置佣金金额
allocation := &model.ShopSeriesAllocation{ allocation := &model.ShopSeriesAllocation{
ShopID: req.ShopID, ShopID: req.ShopID,
SeriesID: req.SeriesID, SeriesID: req.SeriesID,
@@ -249,59 +247,61 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopSeriesGrantRequ
allocation.Creator = operatorID allocation.Creator = operatorID
allocation.Updater = operatorID allocation.Updater = operatorID
if config.CommissionType == "fixed" { if config != nil && config.Enable {
if req.OneTimeCommissionAmount == nil { if config.CommissionType == "fixed" {
return nil, errors.New(errors.CodeInvalidParam, "固定模式必须填写佣金金额") if req.OneTimeCommissionAmount == nil {
} return nil, errors.New(errors.CodeInvalidParam, "固定模式必须填写佣金金额")
ceiling, ceilErr := s.getParentCeilingFixed(ctx, allocatorShopID, req.SeriesID, config)
if ceilErr != nil {
return nil, ceilErr
}
if *req.OneTimeCommissionAmount > ceiling {
return nil, errors.New(errors.CodeInvalidParam, "佣金金额不能超过上级天花板")
}
allocation.OneTimeCommissionAmount = *req.OneTimeCommissionAmount
} else {
// 梯度模式
if len(req.CommissionTiers) == 0 {
return nil, errors.New(errors.CodeInvalidParam, "梯度模式必须填写阶梯配置")
}
// 阶梯数量和 threshold 必须与全局完全一致
if len(req.CommissionTiers) != len(config.Tiers) {
return nil, errors.New(errors.CodeInvalidParam, "梯度阶梯数量与系列配置不一致")
}
ceilingMap, ceilErr := s.getParentCeilingTiered(ctx, allocatorShopID, req.SeriesID, config.Tiers)
if ceilErr != nil {
return nil, ceilErr
}
agentTiers := make([]model.AllocationCommissionTier, 0, len(req.CommissionTiers))
for i, tier := range req.CommissionTiers {
if tier.Threshold != config.Tiers[i].Threshold {
return nil, errors.New(errors.CodeInvalidParam, "梯度阶梯 threshold 与系列配置不匹配")
} }
ceiling, ok := ceilingMap[tier.Threshold] ceiling, ceilErr := s.getParentCeilingFixed(ctx, allocatorShopID, req.SeriesID, config)
if !ok { if ceilErr != nil {
ceiling = 0 return nil, ceilErr
} }
if tier.Amount > ceiling { if *req.OneTimeCommissionAmount > ceiling {
return nil, errors.New(errors.CodeInvalidParam, "某档位佣金金额超过上级天花板") return nil, errors.New(errors.CodeInvalidParam, "佣金金额不能超过上级天花板")
}
allocation.OneTimeCommissionAmount = *req.OneTimeCommissionAmount
} else {
// 梯度模式
if len(req.CommissionTiers) == 0 {
return nil, errors.New(errors.CodeInvalidParam, "梯度模式必须填写阶梯配置")
}
// 阶梯数量和 threshold 必须与全局完全一致
if len(req.CommissionTiers) != len(config.Tiers) {
return nil, errors.New(errors.CodeInvalidParam, "梯度阶梯数量与系列配置不一致")
}
ceilingMap, ceilErr := s.getParentCeilingTiered(ctx, allocatorShopID, req.SeriesID, config.Tiers)
if ceilErr != nil {
return nil, ceilErr
}
agentTiers := make([]model.AllocationCommissionTier, 0, len(req.CommissionTiers))
for i, tier := range req.CommissionTiers {
if tier.Threshold != config.Tiers[i].Threshold {
return nil, errors.New(errors.CodeInvalidParam, "梯度阶梯 threshold 与系列配置不匹配")
}
ceiling, ok := ceilingMap[tier.Threshold]
if !ok {
ceiling = 0
}
if tier.Amount > ceiling {
return nil, errors.New(errors.CodeInvalidParam, "某档位佣金金额超过上级天花板")
}
agentTiers = append(agentTiers, model.AllocationCommissionTier{
Threshold: tier.Threshold,
Amount: tier.Amount,
})
}
if err := allocation.SetCommissionTiers(agentTiers); err != nil {
return nil, errors.Wrap(errors.CodeInternalError, err, "序列化阶梯佣金失败")
} }
agentTiers = append(agentTiers, model.AllocationCommissionTier{
Threshold: tier.Threshold,
Amount: tier.Amount,
})
} }
if err := allocation.SetCommissionTiers(agentTiers); err != nil {
return nil, errors.Wrap(errors.CodeInternalError, err, "序列化阶梯佣金失败")
}
}
// 5. 强充配置first_recharge 或平台已开强充 → locked忽略代理传入 // 5. 强充配置first_recharge 或平台已开强充 → locked忽略代理传入
forceRechargeLocked := config.TriggerType == model.OneTimeCommissionTriggerFirstRecharge || config.EnableForceRecharge forceRechargeLocked := config.TriggerType == model.OneTimeCommissionTriggerFirstRecharge || config.EnableForceRecharge
if !forceRechargeLocked && req.EnableForceRecharge != nil && *req.EnableForceRecharge { if !forceRechargeLocked && req.EnableForceRecharge != nil && *req.EnableForceRecharge {
allocation.EnableForceRecharge = true allocation.EnableForceRecharge = true
if req.ForceRechargeAmount != nil { if req.ForceRechargeAmount != nil {
allocation.ForceRechargeAmount = *req.ForceRechargeAmount allocation.ForceRechargeAmount = *req.ForceRechargeAmount
}
} }
} }