diff --git a/internal/service/commission_calculation/service.go b/internal/service/commission_calculation/service.go index ac14c6c..088ab56 100644 --- a/internal/service/commission_calculation/service.go +++ b/internal/service/commission_calculation/service.go @@ -449,12 +449,23 @@ func (s *Service) calculateChainOneTimeCommission(ctx context.Context, bottomSho var myAmount int64 if config.CommissionType == "tiered" && len(config.Tiers) > 0 { - tieredAmount, tierErr := s.matchOneTimeCommissionTier(ctx, currentShopID, seriesID, currentSeriesAllocation.ID, config.Tiers) - if tierErr != nil { - s.logger.Warn("匹配梯度佣金失败,使用固定金额", zap.Uint("shop_id", currentShopID), zap.Error(tierErr)) + // 获取该代理的专属阶梯金额列表 + agentTiers, tiersErr := currentSeriesAllocation.GetCommissionTiers() + if tiersErr != nil { + s.logger.Warn("解析代理阶梯佣金失败,使用固定金额", zap.Uint("shop_id", currentShopID), zap.Error(tiersErr)) + myAmount = currentSeriesAllocation.OneTimeCommissionAmount + } else if len(agentTiers) == 0 { + // commission_tiers_json 为空(历史数据),降级到 OneTimeCommissionAmount + s.logger.Warn("代理专属阶梯为空,fallback 到固定金额", zap.Uint("shop_id", currentShopID)) myAmount = currentSeriesAllocation.OneTimeCommissionAmount } else { - myAmount = tieredAmount + tieredAmount, tierErr := s.matchOneTimeCommissionTier(ctx, currentShopID, seriesID, currentSeriesAllocation.ID, config.Tiers, agentTiers) + if tierErr != nil { + s.logger.Warn("匹配梯度佣金失败,使用固定金额", zap.Uint("shop_id", currentShopID), zap.Error(tierErr)) + myAmount = currentSeriesAllocation.OneTimeCommissionAmount + } else { + myAmount = tieredAmount + } } } else { myAmount = currentSeriesAllocation.OneTimeCommissionAmount @@ -512,7 +523,7 @@ func (s *Service) calculateChainOneTimeCommission(ctx context.Context, bottomSho return records, nil } -func (s *Service) matchOneTimeCommissionTier(ctx context.Context, shopID uint, seriesID uint, allocationID uint, tiers []model.OneTimeCommissionTier) (int64, error) { +func (s *Service) matchOneTimeCommissionTier(ctx context.Context, shopID uint, seriesID uint, allocationID uint, tiers []model.OneTimeCommissionTier, agentTiers []model.AllocationCommissionTier) (int64, error) { if len(tiers) == 0 { return 0, nil } @@ -553,8 +564,29 @@ func (s *Service) matchOneTimeCommissionTier(ctx context.Context, shopID uint, s currentValue = salesAmount } - if currentValue >= tier.Threshold && tier.Amount > matchedAmount { - matchedAmount = tier.Amount + // 根据 tier.Operator 判断是否命中阈值,Operator 为空时默认 >= + var hit bool + switch tier.Operator { + case model.TierOperatorGT: + hit = currentValue > tier.Threshold + case model.TierOperatorLT: + hit = currentValue < tier.Threshold + case model.TierOperatorLTE: + hit = currentValue <= tier.Threshold + default: // >= 或空字符串 + hit = currentValue >= tier.Threshold + } + + if !hit { + continue + } + + // 从代理专属阶梯列表中按 threshold 查找对应金额 + for _, agentTier := range agentTiers { + if agentTier.Threshold == tier.Threshold && agentTier.Amount > matchedAmount { + matchedAmount = agentTier.Amount + break + } } } diff --git a/internal/service/package/service.go b/internal/service/package/service.go index 6db432b..89dbff3 100644 --- a/internal/service/package/service.go +++ b/internal/service/package/service.go @@ -616,8 +616,8 @@ func (s *Service) fillCommissionInfo(resp *dto.PackageResponse, seriesID uint, s return } - // 检查是否启用一次性佣金 - if !seriesAllocation.EnableOneTimeCommission || !config.Enable { + // 一次性佣金是否启用由 PackageSeries.enable_one_time_commission 控制 + if !config.Enable { return }