Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0a897419a5 | |||
| c90edac630 |
@@ -11,19 +11,19 @@ CREATE TABLE IF NOT EXISTS `promo_rule` (
|
|||||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
`deleted_at` DATETIME DEFAULT NULL COMMENT '软删除时间',
|
`deleted_at` DATETIME DEFAULT NULL COMMENT '软删除时间',
|
||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
KEY `idx_enabled_priority` (`enabled`, `priority` DESC),
|
KEY `idx_enabled_priority_deleted` (`enabled`, `deleted_at`, `priority` DESC)
|
||||||
KEY `idx_deleted_at` (`deleted_at`)
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销规则表';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='促销规则表';
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `subscribe_promo` (
|
CREATE TABLE IF NOT EXISTS `subscribe_promo` (
|
||||||
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
`subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '套餐规格 ID',
|
`subscribe_id` BIGINT UNSIGNED NOT NULL COMMENT '套餐规格 ID',
|
||||||
|
`quantity` INT NOT NULL DEFAULT 0 COMMENT '购买数量',
|
||||||
`promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '促销规则 ID',
|
`promo_rule_id` BIGINT UNSIGNED NOT NULL COMMENT '促销规则 ID',
|
||||||
`promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '该规格在此规则下的优惠价(分)',
|
`promo_price` BIGINT NOT NULL DEFAULT 0 COMMENT '该规格在此规则下的优惠价(分)',
|
||||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
UNIQUE KEY `uk_subscribe_rule` (`subscribe_id`, `promo_rule_id`),
|
UNIQUE KEY `uk_subscribe_qty_rule` (`subscribe_id`, `quantity`, `promo_rule_id`),
|
||||||
KEY `idx_promo_rule_id` (`promo_rule_id`)
|
KEY `idx_promo_rule_id` (`promo_rule_id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='规格促销价表';
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='规格促销价表';
|
||||||
|
|
||||||
|
|||||||
@@ -28,13 +28,13 @@ type promoRuleParams struct {
|
|||||||
InactiveMonths int `json:"inactive_months"`
|
InactiveMonths int `json:"inactive_months"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func EvaluatePromo(ctx context.Context, svcCtx *svc.ServiceContext, userID int64, subscribeID int64, quantity int64) (*PromoResult, error) {
|
func EvaluatePromo(ctx context.Context, svcCtx *svc.ServiceContext, userID int64, subscribeID int64) (*PromoResult, error) {
|
||||||
result := &PromoResult{}
|
result := &PromoResult{}
|
||||||
if svcCtx == nil || svcCtx.PromoModel == nil || svcCtx.DB == nil || userID <= 0 || subscribeID <= 0 || quantity <= 0 {
|
if svcCtx == nil || svcCtx.PromoModel == nil || svcCtx.DB == nil || userID <= 0 || subscribeID <= 0 {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rules, err := svcCtx.PromoModel.QueryEligibleRules(ctx, subscribeID, quantity)
|
rules, err := svcCtx.PromoModel.QueryEligibleRules(ctx, subscribeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo rules failed: %v", err.Error())
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo rules failed: %v", err.Error())
|
||||||
}
|
}
|
||||||
@@ -151,10 +151,11 @@ func evaluateInactiveUserPromo(
|
|||||||
Where("user_id = ?", userID).
|
Where("user_id = ?", userID).
|
||||||
Order(clause.OrderBy{
|
Order(clause.OrderBy{
|
||||||
Expression: clause.Expr{
|
Expression: clause.Expr{
|
||||||
SQL: "CASE WHEN expire_time = ? THEN 0 ELSE 1 END, expire_time DESC",
|
SQL: "CASE WHEN expire_time = ? THEN 0 ELSE 1 END",
|
||||||
Vars: []interface{}{time.UnixMilli(0)},
|
Vars: []interface{}{permanentSubscribeExpireTime()},
|
||||||
},
|
},
|
||||||
}).
|
}).
|
||||||
|
Order("expire_time DESC").
|
||||||
Limit(1).
|
Limit(1).
|
||||||
Take(&lastSub).Error
|
Take(&lastSub).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -164,16 +165,19 @@ func evaluateInactiveUserPromo(
|
|||||||
return false, time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo inactive user subscription failed")
|
return false, time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo inactive user subscription failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
return evaluateInactiveUserExpire(lastSub.ExpireTime, params, now), ruleExpiresAt, nil
|
return isInactivePromoEligible(lastSub.ExpireTime, now, params.InactiveMonths), ruleExpiresAt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func evaluateInactiveUserExpire(lastExpire time.Time, params promoRuleParams, now time.Time) bool {
|
func isInactivePromoEligible(expireTime time.Time, now time.Time, inactiveMonths int) bool {
|
||||||
if lastExpire.Equal(time.UnixMilli(0)) {
|
if expireTime.Equal(permanentSubscribeExpireTime()) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
threshold := now.AddDate(0, -inactiveMonths, 0)
|
||||||
|
return expireTime.Before(threshold) || expireTime.Equal(threshold)
|
||||||
|
}
|
||||||
|
|
||||||
threshold := now.AddDate(0, -params.InactiveMonths, 0)
|
func permanentSubscribeExpireTime() time.Time {
|
||||||
return lastExpire.Before(threshold) || lastExpire.Equal(threshold)
|
return time.UnixMilli(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func promoRuleExpiresAt(rule *promo.RuleWithPrice) time.Time {
|
func promoRuleExpiresAt(rule *promo.RuleWithPrice) time.Time {
|
||||||
|
|||||||
@@ -5,41 +5,46 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEvaluateInactiveUserExpire(t *testing.T) {
|
func TestIsInactivePromoEligible(t *testing.T) {
|
||||||
now := time.Date(2026, 5, 27, 8, 0, 0, 0, time.UTC)
|
now := time.Date(2026, time.May, 27, 12, 0, 0, 0, time.UTC)
|
||||||
params := promoRuleParams{InactiveMonths: 3}
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
lastExpire time.Time
|
expireAt time.Time
|
||||||
want bool
|
want bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "permanent subscription is not inactive",
|
name: "expired before inactive threshold is eligible",
|
||||||
lastExpire: time.UnixMilli(0),
|
expireAt: now.AddDate(0, -4, 0),
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "active subscription is not inactive",
|
|
||||||
lastExpire: now.Add(time.Hour),
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "expire at threshold is inactive",
|
|
||||||
lastExpire: now.AddDate(0, -3, 0),
|
|
||||||
want: true,
|
want: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "expire before threshold is inactive",
|
name: "expired exactly at inactive threshold is eligible",
|
||||||
lastExpire: now.AddDate(0, -3, -1),
|
expireAt: now.AddDate(0, -3, 0),
|
||||||
want: true,
|
want: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "recently expired subscription is not eligible",
|
||||||
|
expireAt: now.AddDate(0, -2, 0),
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "active future subscription is not eligible",
|
||||||
|
expireAt: now.Add(time.Hour),
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "permanent subscription marker is not eligible",
|
||||||
|
expireAt: time.UnixMilli(0),
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := evaluateInactiveUserExpire(tt.lastExpire, params, now); got != tt.want {
|
got := isInactivePromoEligible(tt.expireAt, now, 3)
|
||||||
t.Fatalf("evaluateInactiveUserExpire() = %v, want %v", got, tt.want)
|
if got != tt.want {
|
||||||
|
t.Fatalf("isInactivePromoEligible() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func calculatePurchasePrice(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if allowPromo {
|
if allowPromo {
|
||||||
promoResult, err := commonLogic.EvaluatePromo(ctx, svcCtx, userID, subscribeID, quantity)
|
promoResult, err := commonLogic.EvaluatePromo(ctx, svcCtx, userID, subscribeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,20 +12,20 @@ import (
|
|||||||
|
|
||||||
type fakePromoModel struct {
|
type fakePromoModel struct {
|
||||||
rules []*promo.RuleWithPrice
|
rules []*promo.RuleWithPrice
|
||||||
gotQuantity int64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *fakePromoModel) QueryEligibleRules(_ context.Context, _ int64, quantity int64) ([]*promo.RuleWithPrice, error) {
|
func (m fakePromoModel) QueryEligibleRules(context.Context, int64) ([]*promo.RuleWithPrice, error) {
|
||||||
m.gotQuantity = quantity
|
|
||||||
return m.rules, nil
|
return m.rules, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *fakePromoModel) InsertUsage(context.Context, *promo.Usage, ...*gorm.DB) error {
|
func (m fakePromoModel) InsertUsage(context.Context, *promo.Usage, ...*gorm.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
|
func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
|
||||||
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
|
svcCtx := &svc.ServiceContext{
|
||||||
|
DB: &gorm.DB{},
|
||||||
|
PromoModel: fakePromoModel{rules: []*promo.RuleWithPrice{
|
||||||
{
|
{
|
||||||
Rule: promo.Rule{
|
Rule: promo.Rule{
|
||||||
Id: 9,
|
Id: 9,
|
||||||
@@ -35,10 +35,7 @@ func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
|
|||||||
},
|
},
|
||||||
PromoPrice: 600,
|
PromoPrice: 600,
|
||||||
},
|
},
|
||||||
}}
|
}},
|
||||||
svcCtx := &svc.ServiceContext{
|
|
||||||
DB: &gorm.DB{},
|
|
||||||
PromoModel: model,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := calculatePurchasePrice(
|
result, err := calculatePurchasePrice(
|
||||||
@@ -71,13 +68,12 @@ func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
|
|||||||
if result.PromoDiscount != 1200 {
|
if result.PromoDiscount != 1200 {
|
||||||
t.Fatalf("PromoDiscount = %d, want 1200", result.PromoDiscount)
|
t.Fatalf("PromoDiscount = %d, want 1200", result.PromoDiscount)
|
||||||
}
|
}
|
||||||
if model.gotQuantity != 3 {
|
|
||||||
t.Fatalf("promo query quantity = %d, want 3", model.gotQuantity)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCalculatePurchasePriceIgnoresInvalidPromoPrice(t *testing.T) {
|
func TestCalculatePurchasePriceIgnoresInvalidPromoPrice(t *testing.T) {
|
||||||
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
|
svcCtx := &svc.ServiceContext{
|
||||||
|
DB: &gorm.DB{},
|
||||||
|
PromoModel: fakePromoModel{rules: []*promo.RuleWithPrice{
|
||||||
{
|
{
|
||||||
Rule: promo.Rule{
|
Rule: promo.Rule{
|
||||||
Id: 10,
|
Id: 10,
|
||||||
@@ -87,10 +83,7 @@ func TestCalculatePurchasePriceIgnoresInvalidPromoPrice(t *testing.T) {
|
|||||||
},
|
},
|
||||||
PromoPrice: 1000,
|
PromoPrice: 1000,
|
||||||
},
|
},
|
||||||
}}
|
}},
|
||||||
svcCtx := &svc.ServiceContext{
|
|
||||||
DB: &gorm.DB{},
|
|
||||||
PromoModel: model,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := calculatePurchasePrice(
|
result, err := calculatePurchasePrice(
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/perfect-panel/server/pkg/xerr"
|
"github.com/perfect-panel/server/pkg/xerr"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -172,7 +171,11 @@ func (e *promoEligibilityEvaluator) lastSubscribeExpireAt() (time.Time, error) {
|
|||||||
return *e.lastExpire, nil
|
return *e.lastExpire, nil
|
||||||
}
|
}
|
||||||
var item user.Subscribe
|
var item user.Subscribe
|
||||||
err := e.lastSubscribeExpireQuery().
|
err := e.db.WithContext(e.ctx).
|
||||||
|
Model(&user.Subscribe{}).
|
||||||
|
Where("user_id = ?", e.userInfo.Id).
|
||||||
|
Where("expire_time != ?", time.UnixMilli(0)).
|
||||||
|
Order("expire_time DESC").
|
||||||
Limit(1).
|
Limit(1).
|
||||||
Take(&item).Error
|
Take(&item).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -187,18 +190,6 @@ func (e *promoEligibilityEvaluator) lastSubscribeExpireAt() (time.Time, error) {
|
|||||||
return item.ExpireTime, nil
|
return item.ExpireTime, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *promoEligibilityEvaluator) lastSubscribeExpireQuery() *gorm.DB {
|
|
||||||
return e.db.WithContext(e.ctx).
|
|
||||||
Model(&user.Subscribe{}).
|
|
||||||
Where("user_id = ?", e.userInfo.Id).
|
|
||||||
Order(clause.OrderBy{
|
|
||||||
Expression: clause.Expr{
|
|
||||||
SQL: "CASE WHEN expire_time = ? THEN 0 ELSE 1 END, expire_time DESC",
|
|
||||||
Vars: []interface{}{time.UnixMilli(0)},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c subscribePromoCandidate) expiresAt() time.Time {
|
func (c subscribePromoCandidate) expiresAt() time.Time {
|
||||||
if c.EndTime == nil {
|
if c.EndTime == nil {
|
||||||
return time.Time{}
|
return time.Time{}
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
package subscribe
|
package subscribe
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/perfect-panel/server/internal/model/user"
|
"github.com/perfect-panel/server/internal/model/user"
|
||||||
"gorm.io/driver/mysql"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPromoEligibilityEvaluatorMatch(t *testing.T) {
|
func TestPromoEligibilityEvaluatorMatch(t *testing.T) {
|
||||||
@@ -76,34 +73,3 @@ func TestSubscribePromoCandidateActiveWindow(t *testing.T) {
|
|||||||
t.Fatal("candidate after end time should not be active")
|
t.Fatal("candidate after end time should not be active")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLastSubscribeExpireAtPrioritizesPermanentSubscription(t *testing.T) {
|
|
||||||
db, err := gorm.Open(mysql.New(mysql.Config{
|
|
||||||
DSN: "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local",
|
|
||||||
SkipInitializeWithVersion: true,
|
|
||||||
}), &gorm.Config{DryRun: true, DisableAutomaticPing: true})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("open dry-run db: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
evaluator := &promoEligibilityEvaluator{
|
|
||||||
db: db,
|
|
||||||
userInfo: &user.User{Id: 7},
|
|
||||||
}
|
|
||||||
var item user.Subscribe
|
|
||||||
tx := evaluator.lastSubscribeExpireQuery().Limit(1).Take(&item)
|
|
||||||
|
|
||||||
sql := tx.Statement.SQL.String()
|
|
||||||
if !strings.Contains(sql, "CASE WHEN expire_time = ? THEN 0 ELSE 1 END") {
|
|
||||||
t.Fatalf("SQL missing permanent subscription priority order: %s", sql)
|
|
||||||
}
|
|
||||||
if strings.Contains(sql, "expire_time !=") {
|
|
||||||
t.Fatalf("SQL should not filter out permanent subscriptions: %s", sql)
|
|
||||||
}
|
|
||||||
if len(tx.Statement.Vars) < 2 {
|
|
||||||
t.Fatalf("SQL vars length = %d, want at least 2; vars=%v", len(tx.Statement.Vars), tx.Statement.Vars)
|
|
||||||
}
|
|
||||||
if got, want := tx.Statement.Vars[1], time.UnixMilli(0); got != want {
|
|
||||||
t.Fatalf("permanent subscription order var = %v, want %v; vars=%v", got, want, tx.Statement.Vars)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ type RuleWithPrice struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Model interface {
|
type Model interface {
|
||||||
QueryEligibleRules(ctx context.Context, subscribeId int64, quantity int64) ([]*RuleWithPrice, error)
|
QueryEligibleRules(ctx context.Context, subscribeId int64) ([]*RuleWithPrice, error)
|
||||||
InsertUsage(ctx context.Context, data *Usage, tx ...*gorm.DB) error
|
InsertUsage(ctx context.Context, data *Usage, tx ...*gorm.DB) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,22 +25,18 @@ func NewModel(db *gorm.DB, _ *redis.Client) Model {
|
|||||||
return &defaultPromoModel{db: db}
|
return &defaultPromoModel{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *defaultPromoModel) QueryEligibleRules(ctx context.Context, subscribeId int64, quantity int64) ([]*RuleWithPrice, error) {
|
func (m *defaultPromoModel) QueryEligibleRules(ctx context.Context, subscribeId int64) ([]*RuleWithPrice, error) {
|
||||||
var list []*RuleWithPrice
|
var list []*RuleWithPrice
|
||||||
err := m.eligibleRulesQuery(ctx, subscribeId, quantity).
|
err := m.db.WithContext(ctx).
|
||||||
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").
|
Table("promo_rule AS pr").
|
||||||
Select("pr.*, sp.promo_price").
|
Select("pr.*, sp.promo_price").
|
||||||
Joins("JOIN subscribe_promo AS sp ON sp.promo_rule_id = pr.id AND sp.quantity = ?", quantity).
|
Joins("JOIN subscribe_promo AS sp ON sp.promo_rule_id = pr.id").
|
||||||
Where("sp.subscribe_id = ? AND sp.promo_price > 0 AND pr.enabled = ?", subscribeId, true).
|
Where("sp.subscribe_id = ? AND sp.promo_price > 0 AND pr.enabled = ?", subscribeId, true).
|
||||||
Where("pr.deleted_at IS NULL").
|
Where("pr.deleted_at IS NULL").
|
||||||
Order("pr.priority DESC").
|
Order("pr.priority DESC").
|
||||||
Order("pr.id ASC")
|
Order("pr.id ASC").
|
||||||
|
Find(&list).Error
|
||||||
|
return list, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *defaultPromoModel) InsertUsage(ctx context.Context, data *Usage, tx ...*gorm.DB) error {
|
func (m *defaultPromoModel) InsertUsage(ctx context.Context, data *Usage, tx ...*gorm.DB) error {
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
package promo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gorm.io/driver/mysql"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestQueryEligibleRulesFiltersByQuantity(t *testing.T) {
|
|
||||||
db, err := gorm.Open(mysql.New(mysql.Config{
|
|
||||||
DSN: "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local",
|
|
||||||
SkipInitializeWithVersion: true,
|
|
||||||
}), &gorm.Config{DryRun: true, DisableAutomaticPing: true})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("open dry-run db: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
model := &defaultPromoModel{db: db}
|
|
||||||
var list []*RuleWithPrice
|
|
||||||
tx := model.eligibleRulesQuery(context.Background(), 11, 3).Find(&list)
|
|
||||||
stmt := tx.Statement
|
|
||||||
sql := stmt.SQL.String()
|
|
||||||
if !strings.Contains(sql, "JOIN subscribe_promo AS sp ON sp.promo_rule_id = pr.id AND sp.quantity = ?") {
|
|
||||||
t.Fatalf("SQL missing quantity join condition: %s", sql)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(stmt.Vars) < 2 {
|
|
||||||
t.Fatalf("SQL vars length = %d, want at least 2; vars=%v", len(stmt.Vars), stmt.Vars)
|
|
||||||
}
|
|
||||||
if got, want := stmt.Vars[0], int64(3); got != want {
|
|
||||||
t.Fatalf("first SQL var = %v, want quantity %d; vars=%v", got, want, stmt.Vars)
|
|
||||||
}
|
|
||||||
if got, want := stmt.Vars[1], int64(11); got != want {
|
|
||||||
t.Fatalf("second SQL var = %v, want subscribe_id %d; vars=%v", got, want, stmt.Vars)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -34,7 +34,6 @@ type SubscribePromo struct {
|
|||||||
Id int64 `gorm:"primaryKey"`
|
Id int64 `gorm:"primaryKey"`
|
||||||
SubscribeId int64 `gorm:"type:bigint unsigned;not null;comment:Subscribe ID"`
|
SubscribeId int64 `gorm:"type:bigint unsigned;not null;comment:Subscribe ID"`
|
||||||
PromoRuleId int64 `gorm:"type:bigint unsigned;not null;comment:Promo Rule ID"`
|
PromoRuleId int64 `gorm:"type:bigint unsigned;not null;comment:Promo Rule ID"`
|
||||||
Quantity int64 `gorm:"type:bigint;not null;default:1;comment:Subscribe Quantity"`
|
|
||||||
PromoPrice int64 `gorm:"type:bigint;not null;default:0;comment:Promo Price"`
|
PromoPrice int64 `gorm:"type:bigint;not null;default:0;comment:Promo Price"`
|
||||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||||
|
|||||||
Reference in New Issue
Block a user