shanshanzhong 52aaaf4de5
Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled
fix: getDiscount 新人价取值错误 — 始终优先取 new_user_only=false 的实际折扣档
同一 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>
2026-03-31 07:14:37 -07:00

56 lines
2.0 KiB
Go

package order
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 {
var best float64 = 1
foundFallback := false
for _, d := range discounts {
if d.Quantity != inputMonths || d.Discount <= 0 || d.Discount >= 100 {
continue
}
factor := d.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
}
// isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled.
// Returns true only when all matching tiers for the given quantity require new-user status
// (i.e. no fallback tier with new_user_only=false exists). When both a new-user-only tier
// and a general tier exist for the same quantity, non-new-users can still purchase via the
// general tier, so this returns false.
func isNewUserOnlyForQuantity(discounts []types.SubscribeDiscount, inputQuantity int64) bool {
hasNewUserOnly := false
hasFallback := false
for _, d := range discounts {
if d.Quantity != inputQuantity {
continue
}
if d.NewUserOnly {
hasNewUserOnly = true
} else {
hasFallback = true
}
}
return hasNewUserOnly && !hasFallback
}