02b41e7a2c
- subscribe_promo 表加 quantity 字段,BIGINT NOT NULL DEFAULT 1 - EvaluatePromo 加 quantity 参数,按 subscribeID + quantity 精确匹配 - Promo 从 Subscribe 顶层移到 SubscribeDiscount - 查询加 quantity,返回 map[subscribeID][quantity] 二级映射 - recordPromoUsage 错误向上传播,不再静默吞掉 - preCreate 和 purchase 的 allowPromo 判定统一为 orderType==1 - 迁移脚本增加幂等处理(guarded DROP/ADD/MODIFY) Co-authored-by: multica-agent <github@multica.ai>
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package order
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
|
|
commonLogic "github.com/perfect-panel/server/internal/logic/common"
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"github.com/perfect-panel/server/internal/types"
|
|
)
|
|
|
|
type orderPriceResult struct {
|
|
OriginalPrice int64
|
|
PayableBase int64
|
|
DiscountAmount int64
|
|
PromoRuleId int64
|
|
PromoDiscount int64
|
|
PromoPrice int64
|
|
}
|
|
|
|
func calculatePurchasePrice(
|
|
ctx context.Context,
|
|
svcCtx *svc.ServiceContext,
|
|
userID int64,
|
|
subscribeID int64,
|
|
unitPrice int64,
|
|
quantity int64,
|
|
discounts []types.SubscribeDiscount,
|
|
eligibleForDiscount bool,
|
|
allowPromo bool,
|
|
) (*orderPriceResult, error) {
|
|
originalPrice := unitPrice * quantity
|
|
result := &orderPriceResult{
|
|
OriginalPrice: originalPrice,
|
|
PayableBase: originalPrice,
|
|
}
|
|
|
|
if allowPromo {
|
|
promoResult, err := commonLogic.EvaluatePromo(ctx, svcCtx, userID, subscribeID, quantity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if promoResult != nil && promoResult.Eligible && promoResult.PromoPrice < unitPrice {
|
|
result.PayableBase = promoResult.PromoPrice * quantity
|
|
result.PromoRuleId = promoResult.RuleID
|
|
result.PromoDiscount = originalPrice - result.PayableBase
|
|
result.PromoPrice = promoResult.PromoPrice
|
|
if result.PromoDiscount < 0 {
|
|
result.PromoDiscount = 0
|
|
}
|
|
return result, nil
|
|
}
|
|
}
|
|
|
|
discount := float64(1)
|
|
if len(discounts) > 0 {
|
|
discount = getDiscount(discounts, quantity, eligibleForDiscount)
|
|
}
|
|
result.PayableBase = int64(math.Round(float64(originalPrice) * discount))
|
|
result.DiscountAmount = originalPrice - result.PayableBase
|
|
return result, nil
|
|
}
|