@@ -53,6 +53,7 @@ func (promoRule) TableName() string {
|
||||
type subscribePromo struct {
|
||||
Id int64 `gorm:"column:id" json:"id"`
|
||||
SubscribeId int64 `gorm:"column:subscribe_id" json:"subscribe_id"`
|
||||
Quantity int64 `gorm:"column:quantity" json:"quantity"`
|
||||
PromoRuleId int64 `gorm:"column:promo_rule_id" json:"promo_rule_id"`
|
||||
PromoPrice int64 `gorm:"column:promo_price" json:"promo_price"`
|
||||
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
|
||||
@@ -77,12 +78,12 @@ type gormPromoEligibilitySource struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func EvaluatePromo(ctx context.Context, svcCtx *svc.ServiceContext, userID int64, subscribeID int64) (*PromoResult, error) {
|
||||
func EvaluatePromo(ctx context.Context, svcCtx *svc.ServiceContext, userID int64, subscribeID int64, quantity int64) (*PromoResult, error) {
|
||||
if svcCtx == nil || svcCtx.DB == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "service context is empty")
|
||||
}
|
||||
if userID <= 0 || subscribeID <= 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "user id or subscribe id is empty")
|
||||
if userID <= 0 || subscribeID <= 0 || quantity <= 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "user id, subscribe id or quantity is empty")
|
||||
}
|
||||
|
||||
rules, err := loadEnabledPromoRules(ctx, svcCtx)
|
||||
@@ -93,7 +94,7 @@ func EvaluatePromo(ctx context.Context, svcCtx *svc.ServiceContext, userID int64
|
||||
return &PromoResult{}, nil
|
||||
}
|
||||
|
||||
prices, err := loadSubscribePromos(ctx, svcCtx, subscribeID)
|
||||
prices, err := loadSubscribePromos(ctx, svcCtx, subscribeID, quantity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -292,16 +293,14 @@ func loadEnabledPromoRules(ctx context.Context, svcCtx *svc.ServiceContext) ([]p
|
||||
return rules, nil
|
||||
}
|
||||
|
||||
func loadSubscribePromos(ctx context.Context, svcCtx *svc.ServiceContext, subscribeID int64) ([]subscribePromo, error) {
|
||||
cacheKey := fmt.Sprintf("%s%d", promoSubscribeCachePrefix, subscribeID)
|
||||
func loadSubscribePromos(ctx context.Context, svcCtx *svc.ServiceContext, subscribeID int64, quantity int64) ([]subscribePromo, error) {
|
||||
cacheKey := fmt.Sprintf("%s%d:%d", promoSubscribeCachePrefix, subscribeID, quantity)
|
||||
if cached, ok := getPromoCache[[]subscribePromo](ctx, svcCtx, cacheKey); ok {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
var promos []subscribePromo
|
||||
if err := svcCtx.DB.WithContext(ctx).
|
||||
Model(&subscribePromo{}).
|
||||
Where("subscribe_id = ?", subscribeID).
|
||||
if err := subscribePromoQuery(svcCtx.DB.WithContext(ctx), subscribeID, quantity).
|
||||
Find(&promos).Error; err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query subscribe promos failed")
|
||||
}
|
||||
@@ -310,6 +309,12 @@ func loadSubscribePromos(ctx context.Context, svcCtx *svc.ServiceContext, subscr
|
||||
return promos, nil
|
||||
}
|
||||
|
||||
func subscribePromoQuery(db *gorm.DB, subscribeID int64, quantity int64) *gorm.DB {
|
||||
return db.
|
||||
Model(&subscribePromo{}).
|
||||
Where("subscribe_id = ? AND quantity = ?", subscribeID, quantity)
|
||||
}
|
||||
|
||||
func getPromoCache[T any](ctx context.Context, svcCtx *svc.ServiceContext, key string) (T, bool) {
|
||||
var zero T
|
||||
if svcCtx == nil || svcCtx.Redis == nil {
|
||||
|
||||
Reference in New Issue
Block a user