spark-server/internal/module/client/logic/plans.go
2026-10-21 16:25:04 +08:00

65 lines
1.6 KiB
Go

package logic
import (
"context"
"strings"
"spark-server/internal/module/client/entity"
"spark-server/internal/pkg/i18n"
)
func canonicalHealthPackage(pkg string) string {
return entity.CanonicalHealthPackage(pkg)
}
func normalizePlatform(platform string) string {
switch strings.ToLower(strings.TrimSpace(platform)) {
case entity.PlatformIOS, "iphone", "ipad":
return entity.PlatformIOS
case entity.PlatformAndroid:
return entity.PlatformAndroid
case entity.PlatformWeb:
return entity.PlatformWeb
default:
return ""
}
}
func filterPlansByPlatform(items []entity.SubscriptionPlan, platform string) []entity.SubscriptionPlan {
platform = normalizePlatform(platform)
if platform == "" || len(items) == 0 {
return items
}
exact := make([]entity.SubscriptionPlan, 0, len(items))
fallback := make([]entity.SubscriptionPlan, 0, len(items))
for i := range items {
p := items[i]
got := normalizePlatform(p.Platform)
if got == platform {
exact = append(exact, p)
} else if got == "" {
fallback = append(fallback, p)
}
}
if len(exact) > 0 {
return exact
}
return fallback
}
func (s *Service) clientPlans(ctx context.Context, lang, packageName, platform string) ([]entity.SubscriptionPlan, error) {
plans, err := s.repo.ListPlans(ctx, lang, packageName)
if err != nil {
return nil, err
}
picked := filterPlansByPlatform(plans, platform)
if len(picked) > 0 || lang == string(i18n.ZhCN) {
return picked, nil
}
plans, err = s.repo.ListPlans(ctx, string(i18n.ZhCN), packageName)
if err != nil {
return nil, err
}
return filterPlansByPlatform(plans, platform), nil
}