diff --git a/internal/logic/public/portal/getSubscriptionLogic.go b/internal/logic/public/portal/getSubscriptionLogic.go index 51fbaa6..022b737 100644 --- a/internal/logic/public/portal/getSubscriptionLogic.go +++ b/internal/logic/public/portal/getSubscriptionLogic.go @@ -3,6 +3,7 @@ package portal import ( "context" "encoding/json" + "strings" "github.com/perfect-panel/server/internal/model/subscribe" "github.com/perfect-panel/server/internal/svc" @@ -52,8 +53,28 @@ func (l *GetSubscriptionLogic) GetSubscription(req *types.GetSubscriptionRequest var discount []types.SubscribeDiscount _ = json.Unmarshal([]byte(item.Discount), &discount) sub.Discount = discount - list[i] = sub } + + // 计算节点数量 + nodeIds := tool.StringToInt64Slice(item.Nodes) + var nodeTags []string + if item.NodeTags != "" { + tags := strings.Split(item.NodeTags, ",") + for _, tag := range tags { + if strings.TrimSpace(tag) != "" { + nodeTags = append(nodeTags, strings.TrimSpace(tag)) + } + } + } + + nodeCount, err := l.svcCtx.NodeModel.CountNodesByIdsAndTags(l.ctx, nodeIds, nodeTags) + if err != nil { + l.Logger.Error("[GetSubscription] count nodes failed: ", logger.Field("error", err.Error())) + sub.NodeCount = 0 + } else { + sub.NodeCount = nodeCount + } + list[i] = sub } resp.List = list diff --git a/internal/model/node/model.go b/internal/model/node/model.go index ddfa736..ea5b72b 100644 --- a/internal/model/node/model.go +++ b/internal/model/node/model.go @@ -13,6 +13,7 @@ type customServerLogicModel interface { FilterServerList(ctx context.Context, params *FilterParams) (int64, []*Server, error) FilterNodeList(ctx context.Context, params *FilterNodeParams) (int64, []*Node, error) ClearNodeCache(ctx context.Context, params *FilterNodeParams) error + CountNodesByIdsAndTags(ctx context.Context, nodeIds []int64, tags []string) (int64, error) } const ( @@ -189,3 +190,30 @@ func InSet(field string, values []string) func(db *gorm.DB) *gorm.DB { return db.Where("("+strings.Join(conds, " OR ")+")", args...) } } + +// CountNodesByIdsAndTags 根据节点ID和标签计算启用的节点数量 +func (m *customServerModel) CountNodesByIdsAndTags(ctx context.Context, nodeIds []int64, tags []string) (int64, error) { + var count int64 + query := m.WithContext(ctx).Model(&Node{}).Where("enabled = ?", true) + + // 如果有节点ID或标签,添加相应的查询条件 + if len(nodeIds) > 0 || len(tags) > 0 { + subQuery := m.WithContext(ctx).Model(&Node{}).Where("enabled = ?", true) + + if len(nodeIds) > 0 && len(tags) > 0 { + // 节点ID和标签都存在时,使用OR条件 + subQuery = subQuery.Where("id IN ? OR ?", nodeIds, InSet("tag", tags)) + } else if len(nodeIds) > 0 { + // 只有节点ID + subQuery = subQuery.Where("id IN ?", nodeIds) + } else { + // 只有标签 + subQuery = subQuery.Scopes(InSet("tag", tags)) + } + + query = subQuery + } + + err := query.Count(&count).Error + return count, err +} diff --git a/internal/types/types.go b/internal/types/types.go index a67859c..998adf6 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -2029,6 +2029,7 @@ type Subscribe struct { Quota int64 `json:"quota"` Nodes []int64 `json:"nodes"` NodeTags []string `json:"node_tags"` + NodeCount int64 `json:"node_count"` Show bool `json:"show"` Sell bool `json:"sell"` Sort int64 `json:"sort"`