修复(#85): 促销系统 quantity 设计修正补丁(跨任务统一修复)
Build docker and publish / build (20.15.1) (push) Failing after 8m50s
Build docker and publish / build (20.15.1) (pull_request) Successful in 8m19s

- subscribe_promo 表加 quantity 字段,BIGINT NOT NULL DEFAULT 1
- EvaluatePromo 加 quantity 参数,按 subscribeID + quantity 精确匹配
- Promo 从 Subscribe 顶层移到 SubscribeDiscount
- 查询加 quantity,返回 map[subscribeID][quantity] 二级映射
- recordPromoUsage 错误向上传播,不再静默吞掉
- preCreate 和 purchase 的 allowPromo 判定统一为 orderType==1
- 迁移脚本增加幂等处理(guarded DROP/ADD/MODIFY)

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
2026-05-27 05:57:21 -07:00
parent d12c340743
commit 02b41e7a2c
12 changed files with 281 additions and 34 deletions
+18 -12
View File
@@ -149,7 +149,20 @@ func (l *ActivateOrderLogic) ProcessTask(ctx context.Context, task *asynq.Task)
return err
}
l.recordPromoUsage(ctx, orderInfo)
if err = l.recordPromoUsage(ctx, orderInfo); err != nil {
if releaseErr := l.releaseClaim(ctx, orderInfo.OrderNo); releaseErr != nil {
logger.WithContext(ctx).Error("[ActivateOrderLogic] releaseClaim also failed, stuck recovery will handle",
logger.Field("order_no", orderInfo.OrderNo),
logger.Field("release_error", releaseErr.Error()),
)
}
logger.WithContext(ctx).Error("[ActivateOrderLogic] 促销使用记录写入失败,将重试",
logger.Field("order_no", orderInfo.OrderNo),
logger.Field("promo_rule_id", orderInfo.PromoRuleId),
logger.Field("error", err.Error()),
)
return err
}
l.finalizeCouponAndOrder(ctx, orderInfo)
commonLogic.SubscriptionTraceInfo(logger.WithContext(ctx), commonLogic.SubscriptionTraceFlowOrder, "activation_finished",
@@ -159,9 +172,9 @@ func (l *ActivateOrderLogic) ProcessTask(ctx context.Context, task *asynq.Task)
return nil
}
func (l *ActivateOrderLogic) recordPromoUsage(ctx context.Context, orderInfo *order.Order) {
func (l *ActivateOrderLogic) recordPromoUsage(ctx context.Context, orderInfo *order.Order) error {
if orderInfo == nil || orderInfo.PromoRuleId <= 0 || orderInfo.Quantity <= 0 || orderInfo.SubscribeId <= 0 || orderInfo.OrderNo == "" {
return
return nil
}
promoPrice := int64(0)
@@ -169,10 +182,10 @@ func (l *ActivateOrderLogic) recordPromoUsage(ctx context.Context, orderInfo *or
promoPrice = (orderInfo.Price - orderInfo.PromoDiscount) / orderInfo.Quantity
}
if promoPrice <= 0 {
return
return nil
}
err := l.svc.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
return l.svc.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var count int64
if e := tx.Model(&promo.Usage{}).Where("order_no = ?", orderInfo.OrderNo).Count(&count).Error; e != nil {
return e
@@ -188,13 +201,6 @@ func (l *ActivateOrderLogic) recordPromoUsage(ctx context.Context, orderInfo *or
PromoPrice: promoPrice,
}, tx)
})
if err != nil {
logger.WithContext(ctx).Error("Insert promo usage failed",
logger.Field("error", err.Error()),
logger.Field("order_no", orderInfo.OrderNo),
logger.Field("promo_rule_id", orderInfo.PromoRuleId),
)
}
}
// parsePayload unMarshals the task payload into a structured format