feat(订阅): 添加节点数量统计功能
Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 33s
Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 33s
在订阅数据结构中新增node_count字段,用于统计符合条件的节点数量 实现根据节点ID和标签计算启用节点数量的逻辑
This commit is contained in:
parent
b0a03401b8
commit
267582c6a4
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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"`
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user