From c7b8ecfebf4e5fc6f2973e075b6c14a89b7bad89 Mon Sep 17 00:00:00 2001 From: huang Date: Wed, 4 Mar 2026 11:37:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=A3=E9=87=91=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E9=80=82=E9=85=8D=E6=A2=AF=E5=BA=A6=E9=98=B6=E6=A2=AF?= =?UTF-8?q?=20Operator=20=E6=AF=94=E8=BE=83=EF=BC=8C=E5=A5=97=E9=A4=90?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E9=9B=86=E6=88=90=E4=BB=A3=E7=90=86=E5=BC=BA?= =?UTF-8?q?=E5=85=85=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit commission_calculation: matchOneTimeCommissionTier() 接收 agentTiers 参数,根据 tier.Operator(>、>=、<、<=,默认 >=)执行对应比较逻辑,支持代理专属梯度阶梯计算。package/service: 套餐购买预检调用更新后的强充层级判断接口。 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus --- .../service/commission_calculation/service.go | 46 ++++++++++++++++--- internal/service/package/service.go | 4 +- 2 files changed, 41 insertions(+), 9 deletions(-) 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 }