feat: 为折扣计算函数 getDiscount 引入新用户判断逻辑以支持新用户专属折扣。
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m37s

This commit is contained in:
shanshanzhong 2026-03-11 03:56:48 -07:00
parent 6557d06d59
commit fafb229295
7 changed files with 15 additions and 7 deletions

View File

@ -2,10 +2,13 @@ package order
import "github.com/perfect-panel/server/internal/types" import "github.com/perfect-panel/server/internal/types"
func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64) float64 { func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64, isNewUser bool) float64 {
var finalDiscount float64 = 100 var finalDiscount float64 = 100
for _, discount := range discounts { for _, discount := range discounts {
if discount.NewUserOnly && !isNewUser {
continue
}
if inputMonths >= discount.Quantity && discount.Discount < finalDiscount { if inputMonths >= discount.Quantity && discount.Discount < finalDiscount {
finalDiscount = discount.Discount finalDiscount = discount.Discount
} }

View File

@ -133,10 +133,11 @@ func (l *PreCreateOrderLogic) PreCreateOrder(req *types.PurchaseOrderRequest) (r
} }
var discount float64 = 1 var discount float64 = 1
isNewUserForDiscount := time.Since(u.CreatedAt) <= 24*time.Hour
if sub.Discount != "" { if sub.Discount != "" {
var dis []types.SubscribeDiscount var dis []types.SubscribeDiscount
_ = json.Unmarshal([]byte(sub.Discount), &dis) _ = json.Unmarshal([]byte(sub.Discount), &dis)
discount = getDiscount(dis, req.Quantity) discount = getDiscount(dis, req.Quantity, isNewUserForDiscount)
} }
price := sub.UnitPrice * req.Quantity price := sub.UnitPrice * req.Quantity

View File

@ -129,10 +129,11 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
} }
var discount float64 = 1 var discount float64 = 1
isNewUserForDiscount := time.Since(u.CreatedAt) <= 24*time.Hour
if sub.Discount != "" { if sub.Discount != "" {
var dis []types.SubscribeDiscount var dis []types.SubscribeDiscount
_ = json.Unmarshal([]byte(sub.Discount), &dis) _ = json.Unmarshal([]byte(sub.Discount), &dis)
discount = getDiscount(dis, req.Quantity) discount = getDiscount(dis, req.Quantity, isNewUserForDiscount)
} }
price := sub.UnitPrice * req.Quantity price := sub.UnitPrice * req.Quantity
// discount amount // discount amount

View File

@ -83,7 +83,7 @@ func (l *RenewalLogic) Renewal(req *types.RenewalOrderRequest) (resp *types.Rene
if sub.Discount != "" { if sub.Discount != "" {
var dis []types.SubscribeDiscount var dis []types.SubscribeDiscount
_ = json.Unmarshal([]byte(sub.Discount), &dis) _ = json.Unmarshal([]byte(sub.Discount), &dis)
discount = getDiscount(dis, req.Quantity) discount = getDiscount(dis, req.Quantity, false)
} }
price := sub.UnitPrice * req.Quantity price := sub.UnitPrice * req.Quantity
amount := int64(math.Round(float64(price) * discount)) amount := int64(math.Round(float64(price) * discount))

View File

@ -41,7 +41,7 @@ func (l *PrePurchaseOrderLogic) PrePurchaseOrder(req *types.PrePurchaseOrderRequ
if sub.Discount != "" { if sub.Discount != "" {
var dis []types.SubscribeDiscount var dis []types.SubscribeDiscount
_ = json.Unmarshal([]byte(sub.Discount), &dis) _ = json.Unmarshal([]byte(sub.Discount), &dis)
discount = getDiscount(dis, req.Quantity) discount = getDiscount(dis, req.Quantity, true)
} }
price := sub.UnitPrice * req.Quantity price := sub.UnitPrice * req.Quantity
amount := int64(math.Round(float64(price) * discount)) amount := int64(math.Round(float64(price) * discount))

View File

@ -73,7 +73,7 @@ func (l *PurchaseLogic) Purchase(req *types.PortalPurchaseRequest) (resp *types.
if sub.Discount != "" { if sub.Discount != "" {
var dis []types.SubscribeDiscount var dis []types.SubscribeDiscount
_ = json.Unmarshal([]byte(sub.Discount), &dis) _ = json.Unmarshal([]byte(sub.Discount), &dis)
discount = getDiscount(dis, req.Quantity) discount = getDiscount(dis, req.Quantity, true)
} }
price := sub.UnitPrice * req.Quantity price := sub.UnitPrice * req.Quantity
// discount amount // discount amount

View File

@ -6,10 +6,13 @@ import (
"github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/internal/types"
) )
func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64) float64 { func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64, isNewUser bool) float64 {
var finalDiscount float64 = 100 var finalDiscount float64 = 100
for _, discount := range discounts { for _, discount := range discounts {
if discount.NewUserOnly && !isNewUser {
continue
}
if inputMonths >= discount.Quantity && discount.Discount < finalDiscount { if inputMonths >= discount.Quantity && discount.Discount < finalDiscount {
finalDiscount = discount.Discount finalDiscount = discount.Discount
} }