修复(#85): 按数量匹配订阅促销价

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
2026-05-27 00:36:24 -07:00
parent 48e507783e
commit ae64cc635d
15 changed files with 209 additions and 41 deletions
+13 -5
View File
@@ -25,6 +25,7 @@ const (
type subscribePromoCandidate struct {
SubscribeId int64 `gorm:"column:subscribe_id"`
Quantity int64 `gorm:"column:quantity"`
RuleName string `gorm:"column:rule_name"`
RuleType string `gorm:"column:rule_type"`
PromoPrice int64 `gorm:"column:promo_price"`
@@ -38,8 +39,8 @@ type promoRuleParams struct {
InactiveMonths int `json:"inactive_months"`
}
func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subscribeIDs []int64) (map[int64]*types.SubscribePromo, error) {
result := make(map[int64]*types.SubscribePromo)
func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subscribeIDs []int64) (map[int64]map[int64]*types.SubscribePromo, error) {
result := make(map[int64]map[int64]*types.SubscribePromo)
if len(subscribeIDs) == 0 || svcCtx == nil || svcCtx.DB == nil {
return result, nil
}
@@ -56,7 +57,13 @@ func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subs
evaluator := promoEligibilityEvaluator{ctx: ctx, db: svcCtx.DB, userInfo: userInfo}
now := time.Now()
for _, candidate := range candidates {
if _, exists := result[candidate.SubscribeId]; exists {
if candidate.Quantity <= 0 {
continue
}
if result[candidate.SubscribeId] == nil {
result[candidate.SubscribeId] = make(map[int64]*types.SubscribePromo)
}
if _, exists := result[candidate.SubscribeId][candidate.Quantity]; exists {
continue
}
if !candidate.isActive(now) {
@@ -69,7 +76,7 @@ func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subs
if !ok {
continue
}
result[candidate.SubscribeId] = &types.SubscribePromo{
result[candidate.SubscribeId][candidate.Quantity] = &types.SubscribePromo{
RuleName: candidate.RuleName,
RuleType: candidate.RuleType,
PromoPrice: candidate.PromoPrice,
@@ -84,7 +91,7 @@ func querySubscribePromoCandidates(ctx context.Context, svcCtx *svc.ServiceConte
var candidates []subscribePromoCandidate
query := svcCtx.DB.WithContext(ctx).
Table("subscribe_promo AS sp").
Select("sp.subscribe_id, sp.promo_price, pr.name AS rule_name, pr.type AS rule_type, pr.params, pr.start_time, pr.end_time").
Select("sp.subscribe_id, sp.quantity, sp.promo_price, pr.name AS rule_name, pr.type AS rule_type, pr.params, pr.start_time, pr.end_time").
Joins("JOIN promo_rule AS pr ON pr.id = sp.promo_rule_id AND pr.deleted_at IS NULL").
Where("sp.subscribe_id IN ? AND sp.promo_price > 0 AND pr.enabled = ?", subscribeIDs, true)
if !loggedIn {
@@ -92,6 +99,7 @@ func querySubscribePromoCandidates(ctx context.Context, svcCtx *svc.ServiceConte
}
err := query.
Order("sp.subscribe_id ASC").
Order("sp.quantity ASC").
Order("pr.priority DESC").
Order("pr.id ASC").
Scan(&candidates).Error