Merge remote-tracking branch 'origin/fix/87-促销配价计算错误' into fix/79-修复促销管理审查问题

This commit is contained in:
2026-05-27 05:53:49 -07:00
28 changed files with 1340 additions and 22 deletions
+3 -3
View File
@@ -36,12 +36,12 @@ func calculatePurchasePrice(
}
if allowPromo {
promoResult, err := commonLogic.EvaluatePromo(ctx, svcCtx, userID, subscribeID)
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
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
@@ -14,7 +14,7 @@ type fakePromoModel struct {
rules []*promo.RuleWithPrice
}
func (m fakePromoModel) QueryEligibleRules(context.Context, int64) ([]*promo.RuleWithPrice, error) {
func (m fakePromoModel) QueryEligibleRules(context.Context, int64, int64) ([]*promo.RuleWithPrice, error) {
return m.rules, nil
}
@@ -22,7 +22,51 @@ func (m fakePromoModel) InsertUsage(context.Context, *promo.Usage, ...*gorm.DB)
return nil
}
func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
func (m fakePromoModel) InsertRule(context.Context, *promo.Rule) error {
return nil
}
func (m fakePromoModel) FindRule(context.Context, int64) (*promo.Rule, error) {
return nil, gorm.ErrRecordNotFound
}
func (m fakePromoModel) UpdateRule(context.Context, *promo.Rule) error {
return nil
}
func (m fakePromoModel) DeleteRule(context.Context, int64) error {
return nil
}
func (m fakePromoModel) QueryRuleList(context.Context, int, int, string, *bool, string) (int64, []*promo.Rule, error) {
return 0, nil, nil
}
func (m fakePromoModel) UpsertPrices(context.Context, int64, []*promo.SubscribePromo) error {
return nil
}
func (m fakePromoModel) FindPrice(context.Context, int64) (*promo.SubscribePromo, error) {
return nil, gorm.ErrRecordNotFound
}
func (m fakePromoModel) DeletePrice(context.Context, int64) error {
return nil
}
func (m fakePromoModel) QueryPriceList(context.Context, int64, int, int) (int64, []*promo.SubscribePromo, error) {
return 0, nil, nil
}
func (m fakePromoModel) QueryUsageList(context.Context, promo.UsageFilter) (int64, []*promo.Usage, error) {
return 0, nil, nil
}
func (m fakePromoModel) Transaction(context.Context, func(*gorm.DB) error) error {
return nil
}
func TestCalculatePurchasePricePromoUsesQuantityTierTotalPrice(t *testing.T) {
svcCtx := &svc.ServiceContext{
DB: &gorm.DB{},
PromoModel: fakePromoModel{rules: []*promo.RuleWithPrice{
@@ -33,7 +77,7 @@ func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
Type: promo.RuleTypeCampaign,
Enabled: true,
},
PromoPrice: 600,
PromoPrice: 279,
},
}},
}
@@ -43,9 +87,9 @@ func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
svcCtx,
1,
2,
1000,
3,
[]types.SubscribeDiscount{{Quantity: 3, Discount: 50}},
100,
7,
[]types.SubscribeDiscount{{Quantity: 7, Discount: 50}},
true,
true,
)
@@ -53,11 +97,11 @@ func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
t.Fatalf("calculatePurchasePrice returned error: %v", err)
}
if result.OriginalPrice != 3000 {
t.Fatalf("OriginalPrice = %d, want 3000", result.OriginalPrice)
if result.OriginalPrice != 700 {
t.Fatalf("OriginalPrice = %d, want 700", result.OriginalPrice)
}
if result.PayableBase != 1800 {
t.Fatalf("PayableBase = %d, want 1800", result.PayableBase)
if result.PayableBase != 279 {
t.Fatalf("PayableBase = %d, want 279", result.PayableBase)
}
if result.DiscountAmount != 0 {
t.Fatalf("DiscountAmount = %d, want 0", result.DiscountAmount)
@@ -65,8 +109,11 @@ func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
if result.PromoRuleId != 9 {
t.Fatalf("PromoRuleId = %d, want 9", result.PromoRuleId)
}
if result.PromoDiscount != 1200 {
t.Fatalf("PromoDiscount = %d, want 1200", result.PromoDiscount)
if result.PromoDiscount != 421 {
t.Fatalf("PromoDiscount = %d, want 421", result.PromoDiscount)
}
if result.PromoPrice != 279 {
t.Fatalf("PromoPrice = %d, want 279", result.PromoPrice)
}
}
@@ -81,7 +128,7 @@ func TestCalculatePurchasePriceIgnoresInvalidPromoPrice(t *testing.T) {
Type: promo.RuleTypeCampaign,
Enabled: true,
},
PromoPrice: 1000,
PromoPrice: 3000,
},
}},
}