修复(#79): 促销管理后台API + 审查问题修复
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

- 实现促销管理接口:规则CRUD、优惠价配置、使用记录查询
- 修复 UpsertPrices 新增记录时提前 return 的问题
- DeleteRule/DeletePrice 不存在记录返回 code=404
- SetPromoPriceRequest.items 空数组校验
- 分页参数限制 page>0、1<=size<=200
- 下单侧促销价格按数量档总价口径计算(含 #87 修复)

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
2026-05-27 06:32:42 -07:00
parent b5e50d1ee5
commit 4366a9be8b
30 changed files with 1533 additions and 67 deletions
@@ -31,7 +31,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) {
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
{
Rule: promo.Rule{
@@ -40,7 +84,7 @@ func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
Type: promo.RuleTypeCampaign,
Enabled: true,
},
PromoPrice: 600,
PromoPrice: 279,
},
}}
svcCtx := &svc.ServiceContext{
@@ -53,9 +97,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,
)
@@ -63,11 +107,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)
@@ -75,11 +119,14 @@ 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 model.lastQuantity != 3 {
t.Fatalf("promo query quantity = %d, want 3", model.lastQuantity)
if result.PromoPrice != 279 {
t.Fatalf("PromoPrice = %d, want 279", result.PromoPrice)
}
if model.lastQuantity != 7 {
t.Fatalf("promo query quantity = %d, want 7", model.lastQuantity)
}
}
@@ -92,7 +139,7 @@ func TestCalculatePurchasePriceIgnoresInvalidPromoPrice(t *testing.T) {
Type: promo.RuleTypeCampaign,
Enabled: true,
},
PromoPrice: 1000,
PromoPrice: 3000,
},
}}
svcCtx := &svc.ServiceContext{
@@ -137,7 +184,7 @@ func TestCalculatePurchasePricePassesQuantityToPromoEvaluation(t *testing.T) {
Type: promo.RuleTypeCampaign,
Enabled: true,
},
PromoPrice: 500,
PromoPrice: 3000,
},
},
}