Files
hi-server/internal/logic/public/order/promoPricing.go
T
shanshanzhong147 7236ca4cf2
Build docker and publish / build (20.15.1) (push) Has been cancelled
Build docker and publish / build (20.15.1) (pull_request) Has been cancelled
修复(#128): 按规则类型决定促销资格,让 InactiveUser/Campaign 对老用户生效
此前 calculatePurchasePrice 把 allowPromo 绑死在 orderType == 1,导致只要用户
有任何过往付费订阅(含已过期),就会被 paidSubscriptionQuery 路由为续费
(orderType=2),跳过所有促销评估。后果:

  - InactiveUser 召回促销永远无法触发(其目标人群恰好就是有过期订阅的用户)
  - Campaign 全员活动对老用户 / 升级加购场景完全失效
  - 套餐列表(loadSubscribePromoMap)直接调 EvaluatePromo 不感知 orderType,
    可能显示促销价但下单时却拿到原价

修复方式:把 isFirstPurchase 下放给 EvaluatePromo,由规则类型决定 gating:

  - NewUser    要求 isFirstPurchase=true(保留首购语义)
  - InactiveUser 由规则自身的"上次订阅过期 N 月以上"条件判定
  - Campaign   时间窗内对任意用户生效

新增 common.HasPaidSubscription 助手,套餐列表与下单走同一份 isFirstPurchase
判定,确保展示价与实际下单价口径一致。

测试:补充 EvaluatePromo / calculatePurchasePrice 的 NewUser 屏蔽 + Campaign
放开用例;更新 loadSubscribePromoMap 测试覆盖新增 HasPaidSubscription 查询。

注:renewalLogic.go 仍未接入促销(属于方向 B 的彻底统一,本次未涵盖)。
2026-05-31 19:00:30 -07:00

61 lines
1.5 KiB
Go

package order
import (
"context"
"math"
commonLogic "github.com/perfect-panel/server/internal/logic/common"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
)
type orderPriceResult struct {
OriginalPrice int64
PayableBase int64
DiscountAmount int64
PromoRuleId int64
PromoDiscount int64
PromoPrice int64
}
func calculatePurchasePrice(
ctx context.Context,
svcCtx *svc.ServiceContext,
userID int64,
subscribeID int64,
unitPrice int64,
quantity int64,
discounts []types.SubscribeDiscount,
eligibleForDiscount bool,
isFirstPurchase bool,
) (*orderPriceResult, error) {
originalPrice := unitPrice * quantity
result := &orderPriceResult{
OriginalPrice: originalPrice,
PayableBase: originalPrice,
}
promoResult, err := commonLogic.EvaluatePromo(ctx, svcCtx, userID, subscribeID, quantity, isFirstPurchase)
if err != nil {
return nil, err
}
if promoResult != nil && promoResult.Eligible && promoResult.PromoPrice < originalPrice {
result.PayableBase = promoResult.PromoPrice
result.PromoRuleId = promoResult.RuleID
result.PromoDiscount = originalPrice - result.PayableBase
result.PromoPrice = promoResult.PromoPrice
if result.PromoDiscount < 0 {
result.PromoDiscount = 0
}
return result, nil
}
discount := float64(1)
if len(discounts) > 0 {
discount = getDiscount(discounts, quantity, eligibleForDiscount)
}
result.PayableBase = int64(math.Round(float64(originalPrice) * discount))
result.DiscountAmount = originalPrice - result.PayableBase
return result, nil
}