修复(#75): 按数量精确匹配套餐列表促销
- loadSubscribePromoMap 返回 map[subscribeID]map[quantity]*SubscribePromo 二级映射 - 候选查询按 subscribe_id、quantity、priority DESC 排序,一次取出所有 quantity 档位 - 下单路径 QueryEligibleRules 将 quantity 条件移至 JOIN,精确匹配 - 永久订阅(expire_time=0)用 CASE WHEN 排序兜底,不再误判为回归促销 - 新增 promoEligibility、model、subscribe promo 单元测试 Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/perfect-panel/server/pkg/xerr"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type PromoResult struct {
|
||||
@@ -148,7 +149,12 @@ func evaluateInactiveUserPromo(
|
||||
err := db.WithContext(ctx).
|
||||
Model(&user.Subscribe{}).
|
||||
Where("user_id = ?", userID).
|
||||
Order("expire_time DESC").
|
||||
Order(clause.OrderBy{
|
||||
Expression: clause.Expr{
|
||||
SQL: "CASE WHEN expire_time = ? THEN 0 ELSE 1 END, expire_time DESC",
|
||||
Vars: []interface{}{time.UnixMilli(0)},
|
||||
},
|
||||
}).
|
||||
Limit(1).
|
||||
Take(&lastSub).Error
|
||||
if err != nil {
|
||||
@@ -158,8 +164,16 @@ func evaluateInactiveUserPromo(
|
||||
return false, time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo inactive user subscription failed")
|
||||
}
|
||||
|
||||
return evaluateInactiveUserExpire(lastSub.ExpireTime, params, now), ruleExpiresAt, nil
|
||||
}
|
||||
|
||||
func evaluateInactiveUserExpire(lastExpire time.Time, params promoRuleParams, now time.Time) bool {
|
||||
if lastExpire.Equal(time.UnixMilli(0)) {
|
||||
return false
|
||||
}
|
||||
|
||||
threshold := now.AddDate(0, -params.InactiveMonths, 0)
|
||||
return lastSub.ExpireTime.Before(threshold) || lastSub.ExpireTime.Equal(threshold), ruleExpiresAt, nil
|
||||
return lastExpire.Before(threshold) || lastExpire.Equal(threshold)
|
||||
}
|
||||
|
||||
func promoRuleExpiresAt(rule *promo.RuleWithPrice) time.Time {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestEvaluateInactiveUserExpire(t *testing.T) {
|
||||
now := time.Date(2026, 5, 27, 8, 0, 0, 0, time.UTC)
|
||||
params := promoRuleParams{InactiveMonths: 3}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
lastExpire time.Time
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "permanent subscription is not inactive",
|
||||
lastExpire: time.UnixMilli(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,
|
||||
},
|
||||
{
|
||||
name: "expire before threshold is inactive",
|
||||
lastExpire: now.AddDate(0, -3, -1),
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := evaluateInactiveUserExpire(tt.lastExpire, params, now); got != tt.want {
|
||||
t.Fatalf("evaluateInactiveUserExpire() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user