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>
58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package order
|
|
|
|
import "github.com/perfect-panel/server/internal/types"
|
|
|
|
// getDiscount returns the discount factor for the given quantity.
|
|
//
|
|
// - New user: pick the tier with the lowest discount value (best deal) among all matching tiers.
|
|
// - Non-new user: pick the tier with the highest discount value (most expensive / least discount)
|
|
// among tiers where new_user_only=false only.
|
|
func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64, isNewUser bool) float64 {
|
|
best := float64(-1)
|
|
for _, d := range discounts {
|
|
if d.Quantity != inputMonths || d.Discount <= 0 || d.Discount >= 100 {
|
|
continue
|
|
}
|
|
if !isNewUser && d.NewUserOnly {
|
|
continue
|
|
}
|
|
if isNewUser {
|
|
// lowest discount value = biggest saving
|
|
if best < 0 || d.Discount < best {
|
|
best = d.Discount
|
|
}
|
|
} else {
|
|
// highest discount value = least discount (closest to original price)
|
|
if best < 0 || d.Discount > best {
|
|
best = d.Discount
|
|
}
|
|
}
|
|
}
|
|
if best < 0 {
|
|
return 1
|
|
}
|
|
return best / float64(100)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|