修复(#128): 统一促销价格资格口径
Build docker and publish / build (20.15.1) (push) Failing after 18m40s
Build docker and publish / build (20.15.1) (pull_request) Failing after 19m24s

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
2026-05-29 22:42:04 -07:00
parent e5d6539d79
commit 3644e9ce3f
5 changed files with 263 additions and 235 deletions
+11 -133
View File
@@ -2,12 +2,12 @@ package subscribe
import (
"context"
"encoding/json"
stderrors "errors"
"strings"
"time"
"github.com/go-sql-driver/mysql"
commonLogic "github.com/perfect-panel/server/internal/logic/common"
"github.com/perfect-panel/server/internal/model/user"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
@@ -15,7 +15,6 @@ import (
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
const (
@@ -35,11 +34,6 @@ type subscribePromoCandidate struct {
EndTime *time.Time `gorm:"column:end_time"`
}
type promoRuleParams struct {
WindowHours int64 `json:"window_hours"`
InactiveMonths int `json:"inactive_months"`
}
func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subscribeIDs []int64) (map[int64]map[int64]*types.SubscribePromo, error) {
result := make(map[int64]map[int64]*types.SubscribePromo)
if len(subscribeIDs) == 0 || svcCtx == nil || svcCtx.DB == nil {
@@ -55,8 +49,10 @@ func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subs
return nil, err
}
evaluator := promoEligibilityEvaluator{ctx: ctx, db: svcCtx.DB, userInfo: userInfo}
now := time.Now()
userID := int64(0)
if userInfo != nil {
userID = userInfo.Id
}
for _, candidate := range candidates {
if candidate.Quantity <= 0 {
continue
@@ -67,21 +63,18 @@ func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subs
if _, exists := result[candidate.SubscribeId][candidate.Quantity]; exists {
continue
}
if !candidate.isActive(now) {
continue
}
ok, expiresAt, err := evaluator.match(candidate, now)
promoResult, err := commonLogic.EvaluatePromo(ctx, svcCtx, userID, candidate.SubscribeId, candidate.Quantity)
if err != nil {
return nil, err
}
if !ok {
if promoResult == nil || !promoResult.Eligible {
continue
}
result[candidate.SubscribeId][candidate.Quantity] = &types.SubscribePromo{
RuleName: candidate.RuleName,
RuleType: candidate.RuleType,
PromoPrice: candidate.PromoPrice,
ExpiresAt: unixSeconds(expiresAt),
RuleName: promoResult.RuleName,
RuleType: promoResult.RuleType,
PromoPrice: promoResult.PromoPrice,
ExpiresAt: unixSeconds(promoResult.ExpiresAt),
}
}
@@ -114,121 +107,6 @@ func subscribePromoCandidatesQuery(ctx context.Context, db *gorm.DB, subscribeID
Order("pr.id ASC")
}
func (c subscribePromoCandidate) isActive(now time.Time) bool {
if c.PromoPrice <= 0 {
return false
}
if c.StartTime != nil && now.Before(*c.StartTime) {
return false
}
if c.EndTime != nil && now.After(*c.EndTime) {
return false
}
return true
}
type promoEligibilityEvaluator struct {
ctx context.Context
db *gorm.DB
userInfo *user.User
lastExpire *time.Time
}
func (e *promoEligibilityEvaluator) match(candidate subscribePromoCandidate, now time.Time) (bool, time.Time, error) {
switch candidate.RuleType {
case promoRuleTypeCampaign:
return true, candidate.expiresAt(), nil
case promoRuleTypeNewUser:
if e.userInfo == nil {
return false, time.Time{}, nil
}
params, err := candidate.params()
if err != nil {
return false, time.Time{}, err
}
if params.WindowHours <= 0 || e.userInfo.CreatedAt.IsZero() {
return false, time.Time{}, nil
}
expiresAt := e.userInfo.CreatedAt.Add(time.Duration(params.WindowHours) * time.Hour)
return now.Before(expiresAt), expiresAt, nil
case promoRuleTypeInactiveUser:
if e.userInfo == nil {
return false, time.Time{}, nil
}
params, err := candidate.params()
if err != nil {
return false, time.Time{}, err
}
if params.InactiveMonths <= 0 {
return false, time.Time{}, nil
}
lastExpire, err := e.lastSubscribeExpireAt()
if err != nil {
return false, time.Time{}, err
}
if lastExpire.Equal(time.UnixMilli(0)) || lastExpire.After(now) {
return false, time.Time{}, nil
}
if lastExpire.IsZero() {
return true, candidate.expiresAt(), nil
}
threshold := now.AddDate(0, -params.InactiveMonths, 0)
return !lastExpire.After(threshold), candidate.expiresAt(), nil
default:
return false, time.Time{}, nil
}
}
func (e *promoEligibilityEvaluator) lastSubscribeExpireAt() (time.Time, error) {
if e.lastExpire != nil {
return *e.lastExpire, nil
}
var item user.Subscribe
err := e.lastSubscribeExpireQuery().
Limit(1).
Take(&item).Error
if err != nil {
if stderrors.Is(err, gorm.ErrRecordNotFound) {
zero := time.Time{}
e.lastExpire = &zero
return zero, nil
}
return time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo user last subscription failed")
}
e.lastExpire = &item.ExpireTime
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 {
if c.EndTime == nil {
return time.Time{}
}
return *c.EndTime
}
func (c subscribePromoCandidate) params() (promoRuleParams, error) {
if c.Params == "" {
return promoRuleParams{}, nil
}
var params promoRuleParams
if err := json.Unmarshal([]byte(c.Params), &params); err != nil {
return promoRuleParams{}, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "parse promo rule params failed")
}
return params, nil
}
func unixSeconds(t time.Time) int64 {
if t.IsZero() {
return 0