hi-server/internal/logic/common/subscribeModeRoute.go
shanshanzhong 4752f844ef
Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled
各种配置项修复,优化到后台管理端配置
2026-03-04 17:58:40 -08:00

64 lines
1.4 KiB
Go

package common
import (
"context"
"errors"
"github.com/perfect-panel/server/internal/model/user"
)
type PurchaseRoute string
const (
PurchaseRouteNewPurchase PurchaseRoute = "new_purchase"
PurchaseRoutePurchaseToRenewal PurchaseRoute = "purchase_to_renewal"
)
var (
ErrSingleModePlanMismatch = errors.New("single subscribe mode plan mismatch")
)
type PurchaseRouteDecision struct {
SingleMode bool
Route PurchaseRoute
ResolvedSubscribeID int64
Anchor *user.Subscribe
}
type FindSingleModeAnchorFunc func(ctx context.Context, userID int64) (*user.Subscribe, error)
func ResolvePurchaseRoute(
ctx context.Context,
singleMode bool,
userID int64,
requestedSubscribeID int64,
findAnchor FindSingleModeAnchorFunc,
) (*PurchaseRouteDecision, error) {
decision := &PurchaseRouteDecision{
SingleMode: singleMode,
Route: PurchaseRouteNewPurchase,
ResolvedSubscribeID: requestedSubscribeID,
}
if !singleMode || userID == 0 || findAnchor == nil {
return decision, nil
}
anchorSub, err := findAnchor(ctx, userID)
if err != nil {
return nil, err
}
if anchorSub == nil {
return decision, nil
}
if requestedSubscribeID != anchorSub.SubscribeId {
return nil, ErrSingleModePlanMismatch
}
decision.Route = PurchaseRoutePurchaseToRenewal
decision.ResolvedSubscribeID = anchorSub.SubscribeId
decision.Anchor = anchorSub
return decision, nil
}