From 267582c6a4afa3371d22c08a4038e3ed0aa85330 Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Wed, 22 Oct 2025 04:08:28 -0700 Subject: [PATCH] =?UTF-8?q?feat(=E8=AE=A2=E9=98=85):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=95=B0=E9=87=8F=E7=BB=9F=E8=AE=A1=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在订阅数据结构中新增node_count字段,用于统计符合条件的节点数量 实现根据节点ID和标签计算启用节点数量的逻辑 --- .../public/portal/getSubscriptionLogic.go | 23 ++++++++++++++- internal/model/node/model.go | 28 +++++++++++++++++++ internal/types/types.go | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) 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"`