fix: getDiscount 新人价取值错误 — 始终优先取 new_user_only=false 的实际折扣档
Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled

同一 quantity 有两条记录时:
- new_user_only=true (discount=99.64) 是新人限制标记,非实际折扣
- new_user_only=false (discount=35.36) 是实际折扣价格

原逻辑遍历到第一条即返回,导致新人拿到 99.64 而非 35.36。
修复后优先取 new_user_only=false 的条目,仅在无 false 条目时才降级到 new_user_only=true。

Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
shanshanzhong 2026-03-31 07:14:37 -07:00
parent 877a471e24
commit 52aaaf4de5

View File

@ -2,17 +2,34 @@ package order
import "github.com/perfect-panel/server/internal/types" import "github.com/perfect-panel/server/internal/types"
// getDiscount returns the discount factor for the given quantity.
//
// Design: when a quantity has both a new_user_only=true entry and a new_user_only=false entry,
// the new_user_only=true entry is a "gating marker" (its discount value is irrelevant).
// The actual discounted price is always carried by the new_user_only=false entry.
// Whether the buyer is eligible to use this quantity is enforced by isNewUserOnlyForQuantity
// before calling this function. Here we simply return the best applicable discount.
//
// Priority: new_user_only=false entry beats new_user_only=true entry for the same quantity.
func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64, isNewUser bool) float64 { func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64, isNewUser bool) float64 {
for _, discount := range discounts { var best float64 = 1
if discount.NewUserOnly && !isNewUser { foundFallback := false
for _, d := range discounts {
if d.Quantity != inputMonths || d.Discount <= 0 || d.Discount >= 100 {
continue continue
} }
if inputMonths == discount.Quantity && discount.Discount > 0 && discount.Discount < 100 { factor := d.Discount / float64(100)
return discount.Discount / float64(100) if !d.NewUserOnly {
// Prefer the non-new-user-only tier as the actual price tier.
best = factor
foundFallback = true
} else if !foundFallback && isNewUser {
// Fall back to new-user-only tier only when no general tier exists
// and the buyer qualifies.
best = factor
} }
} }
return best
return 1
} }
// isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled. // isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled.