fix: 单订阅模式下避免重复创建订阅
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m31s

1. FindSingleModeAnchorSubscribe 补充 status=4(流量耗完)到查询范围,
   避免流量耗尽的订阅被忽略导致续费时新建订阅

2. activateOrderLogic NewPurchase 路径去掉套餐ID匹配检查,
   单订阅模式下无论购买何种套餐都更新现有订阅而非新建

3. singleModeHelper fallback 查询同步补充 status=4

Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
shanshanzhong 2026-03-31 05:13:00 -07:00
parent fd48afd0ab
commit abc9864d46
3 changed files with 33 additions and 31 deletions

View File

@ -22,7 +22,7 @@ func findSingleModeMergeTarget(ctx context.Context, svcCtx *svc.ServiceContext,
return nil, err
}
userSubs, queryErr := svcCtx.UserModel.QueryUserSubscribe(ctx, userId, 0, 1, 2, 3, 5)
userSubs, queryErr := svcCtx.UserModel.QueryUserSubscribe(ctx, userId, 0, 1, 2, 3, 4, 5)
if queryErr != nil {
return nil, queryErr
}

View File

@ -67,7 +67,7 @@ func (m *defaultUserModel) FindSingleModeAnchorSubscribe(ctx context.Context, us
var data Subscribe
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, _ interface{}) error {
return conn.Model(&Subscribe{}).
Where("user_id = ? AND token != '' AND (order_id > 0 OR token LIKE 'iap:%') AND `status` IN ?", userId, []int64{0, 1, 2, 3, 5}).
Where("user_id = ? AND token != '' AND (order_id > 0 OR token LIKE 'iap:%') AND `status` IN ?", userId, []int64{0, 1, 2, 3, 4, 5}).
Order("expire_time DESC").
Order("updated_at DESC").
Order("id DESC").

View File

@ -234,7 +234,6 @@ func (l *ActivateOrderLogic) NewPurchase(ctx context.Context, orderInfo *order.O
anchorSub, anchorErr := l.svc.UserModel.FindSingleModeAnchorSubscribe(ctx, orderInfo.UserId)
switch {
case anchorErr == nil && anchorSub != nil:
if anchorSub.SubscribeId == orderInfo.SubscribeId {
if orderInfo.ParentId == 0 && anchorSub.OrderId > 0 && anchorSub.OrderId != orderInfo.Id {
if patchErr := l.patchOrderParentID(ctx, orderInfo.Id, anchorSub.OrderId); patchErr != nil {
logger.WithContext(ctx).Error("Patch order parent_id failed",
@ -257,17 +256,11 @@ func (l *ActivateOrderLogic) NewPurchase(ctx context.Context, orderInfo *order.O
logger.WithContext(ctx).Infow("Single mode purchase routed to renewal in activation",
logger.Field("mode", "single"),
logger.Field("route", "purchase_to_renewal"),
logger.Field("plan_changed", anchorSub.SubscribeId != orderInfo.SubscribeId),
logger.Field("anchor_user_subscribe_id", anchorSub.Id),
logger.Field("order_no", orderInfo.OrderNo),
)
}
} else {
logger.WithContext(ctx).Errorw("Single mode anchor subscribe mismatch in activation",
logger.Field("order_no", orderInfo.OrderNo),
logger.Field("order_subscribe_id", orderInfo.SubscribeId),
logger.Field("anchor_subscribe_id", anchorSub.SubscribeId),
)
}
case errors.Is(anchorErr, gorm.ErrRecordNotFound):
case anchorErr != nil:
logger.WithContext(ctx).Error("Find single mode anchor subscribe failed",
@ -926,11 +919,19 @@ func (l *ActivateOrderLogic) updateSubscriptionForRenewal(ctx context.Context, u
today := time.Now().Day()
resetDay := userSub.ExpireTime.Day()
// 套餐变更更新套餐ID和流量配额并重置已用流量
if userSub.SubscribeId != orderInfo.SubscribeId {
userSub.SubscribeId = orderInfo.SubscribeId
userSub.Traffic = sub.Traffic
userSub.Download = 0
userSub.Upload = 0
} else {
// Reset traffic if enabled
if (sub.RenewalReset != nil && *sub.RenewalReset) || today == resetDay {
userSub.Download = 0
userSub.Upload = 0
}
}
if userSub.FinishedAt != nil {
if userSub.FinishedAt.Before(now) && today > resetDay {
@ -942,6 +943,7 @@ func (l *ActivateOrderLogic) updateSubscriptionForRenewal(ctx context.Context, u
userSub.FinishedAt = nil
}
userSub.OrderId = orderInfo.Id
userSub.ExpireTime = tool.AddTime(sub.UnitTime, orderInfo.Quantity, userSub.ExpireTime)
userSub.Status = 1
// 续费时重置过期流量字段