fix: getDiscount 新人取折扣最大档,非新人取最贵档
Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled

- 新人:遍历所有匹配 quantity 的条目,取 discount 值最小的(折扣力度最大)
- 非新人:仅看 new_user_only=false 的条目,取 discount 值最大的(最接近原价)

Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
shanshanzhong 2026-03-31 07:18:04 -07:00
parent 52aaaf4de5
commit c8f172dc0e

View File

@ -4,32 +4,34 @@ import "github.com/perfect-panel/server/internal/types"
// getDiscount returns the discount factor for the given quantity. // 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, // - New user: pick the tier with the lowest discount value (best deal) among all matching tiers.
// the new_user_only=true entry is a "gating marker" (its discount value is irrelevant). // - Non-new user: pick the tier with the highest discount value (most expensive / least discount)
// The actual discounted price is always carried by the new_user_only=false entry. // among tiers where new_user_only=false only.
// 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 {
var best float64 = 1 best := float64(-1)
foundFallback := false
for _, d := range discounts { for _, d := range discounts {
if d.Quantity != inputMonths || d.Discount <= 0 || d.Discount >= 100 { if d.Quantity != inputMonths || d.Discount <= 0 || d.Discount >= 100 {
continue continue
} }
factor := d.Discount / float64(100) if !isNewUser && d.NewUserOnly {
if !d.NewUserOnly { continue
// Prefer the non-new-user-only tier as the actual price tier. }
best = factor if isNewUser {
foundFallback = true // lowest discount value = biggest saving
} else if !foundFallback && isNewUser { if best < 0 || d.Discount < best {
// Fall back to new-user-only tier only when no general tier exists best = d.Discount
// and the buyer qualifies. }
best = factor } else {
// highest discount value = least discount (closest to original price)
if best < 0 || d.Discount > best {
best = d.Discount
}
} }
} }
return best if best < 0 {
return 1
}
return best / float64(100)
} }
// isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled. // isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled.