Files
hi-server/internal/logic/public/order/promoPricing.go
T
shanshanzhong147 4366a9be8b
Build docker and publish / build (20.15.1) (push) Failing after 9m10s
Build docker and publish / build (20.15.1) (pull_request) Successful in 8m18s
修复(#79): 促销管理后台API + 审查问题修复
- 实现促销管理接口:规则CRUD、优惠价配置、使用记录查询
- 修复 UpsertPrices 新增记录时提前 return 的问题
- DeleteRule/DeletePrice 不存在记录返回 code=404
- SetPromoPriceRequest.items 空数组校验
- 分页参数限制 page>0、1<=size<=200
- 下单侧促销价格按数量档总价口径计算(含 #87 修复)

Co-authored-by: multica-agent <github@multica.ai>
2026-05-27 06:32:42 -07:00

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 < originalPrice {
result.PayableBase = promoResult.PromoPrice
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
}