修复(#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 {
|
||||
|
||||
Reference in New Issue
Block a user