Files
hi-server/internal/model/promo/model.go
T
shanshanzhong147 02b41e7a2c
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
修复(#85): 促销系统 quantity 设计修正补丁(跨任务统一修复)
- 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>
2026-05-27 05:57:21 -07:00

49 lines
1.3 KiB
Go

package promo
import (
"context"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
type RuleWithPrice struct {
Rule
PromoPrice int64 `gorm:"column:promo_price"`
}
type Model interface {
QueryEligibleRules(ctx context.Context, subscribeId int64, quantity int64) ([]*RuleWithPrice, error)
InsertUsage(ctx context.Context, data *Usage, tx ...*gorm.DB) error
}
type defaultPromoModel struct {
db *gorm.DB
}
func NewModel(db *gorm.DB, _ *redis.Client) Model {
return &defaultPromoModel{db: db}
}
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").
Find(&list).Error
return list, err
}
func (m *defaultPromoModel) InsertUsage(ctx context.Context, data *Usage, tx ...*gorm.DB) error {
db := m.db.WithContext(ctx)
if len(tx) > 0 {
db = tx[0].WithContext(ctx)
}
return db.Model(&Usage{}).Create(data).Error
}