修复(#75): 按数量精确匹配套餐列表促销
- loadSubscribePromoMap 返回 map[subscribeID]map[quantity]*SubscribePromo 二级映射 - 候选查询按 subscribe_id、quantity、priority DESC 排序,一次取出所有 quantity 档位 - 下单路径 QueryEligibleRules 将 quantity 条件移至 JOIN,精确匹配 - 永久订阅(expire_time=0)用 CASE WHEN 排序兜底,不再误判为回归促销 - 新增 promoEligibility、model、subscribe promo 单元测试 Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -27,18 +27,22 @@ func NewModel(db *gorm.DB, _ *redis.Client) Model {
|
||||
|
||||
func (m *defaultPromoModel) QueryEligibleRules(ctx context.Context, subscribeId int64, quantity int64) ([]*RuleWithPrice, error) {
|
||||
var list []*RuleWithPrice
|
||||
err := m.db.WithContext(ctx).
|
||||
Table("promo_rule AS pr").
|
||||
Select("pr.*, sp.promo_price").
|
||||
Joins("JOIN subscribe_promo AS sp ON sp.promo_rule_id = pr.id").
|
||||
Where("sp.subscribe_id = ? AND sp.quantity = ? AND sp.promo_price > 0 AND pr.enabled = ?", subscribeId, quantity, true).
|
||||
Where("pr.deleted_at IS NULL").
|
||||
Order("pr.priority DESC").
|
||||
Order("pr.id ASC").
|
||||
err := m.eligibleRulesQuery(ctx, subscribeId, quantity).
|
||||
Find(&list).Error
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (m *defaultPromoModel) eligibleRulesQuery(ctx context.Context, subscribeId int64, quantity int64) *gorm.DB {
|
||||
return m.db.WithContext(ctx).
|
||||
Table("promo_rule AS pr").
|
||||
Select("pr.*, sp.promo_price").
|
||||
Joins("JOIN subscribe_promo AS sp ON sp.promo_rule_id = pr.id AND sp.quantity = ?", quantity).
|
||||
Where("sp.subscribe_id = ? AND sp.promo_price > 0 AND pr.enabled = ?", subscribeId, true).
|
||||
Where("pr.deleted_at IS NULL").
|
||||
Order("pr.priority DESC").
|
||||
Order("pr.id ASC")
|
||||
}
|
||||
|
||||
func (m *defaultPromoModel) InsertUsage(ctx context.Context, data *Usage, tx ...*gorm.DB) error {
|
||||
db := m.db.WithContext(ctx)
|
||||
if len(tx) > 0 {
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package promo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestQueryEligibleRulesFiltersByQuantity(t *testing.T) {
|
||||
db, err := gorm.Open(mysql.New(mysql.Config{
|
||||
DSN: "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local",
|
||||
SkipInitializeWithVersion: true,
|
||||
}), &gorm.Config{DryRun: true, DisableAutomaticPing: true})
|
||||
if err != nil {
|
||||
t.Fatalf("open dry-run db: %v", err)
|
||||
}
|
||||
|
||||
model := &defaultPromoModel{db: db}
|
||||
var list []*RuleWithPrice
|
||||
tx := model.eligibleRulesQuery(context.Background(), 11, 3).Find(&list)
|
||||
stmt := tx.Statement
|
||||
sql := stmt.SQL.String()
|
||||
if !strings.Contains(sql, "JOIN subscribe_promo AS sp ON sp.promo_rule_id = pr.id AND sp.quantity = ?") {
|
||||
t.Fatalf("SQL missing quantity join condition: %s", sql)
|
||||
}
|
||||
|
||||
if len(stmt.Vars) < 2 {
|
||||
t.Fatalf("SQL vars length = %d, want at least 2; vars=%v", len(stmt.Vars), stmt.Vars)
|
||||
}
|
||||
if got, want := stmt.Vars[0], int64(3); got != want {
|
||||
t.Fatalf("first SQL var = %v, want quantity %d; vars=%v", got, want, stmt.Vars)
|
||||
}
|
||||
if got, want := stmt.Vars[1], int64(11); got != want {
|
||||
t.Fatalf("second SQL var = %v, want subscribe_id %d; vars=%v", got, want, stmt.Vars)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user