在订阅数据结构中新增node_count字段,用于统计符合条件的节点数量 实现根据节点ID和标签计算启用节点数量的逻辑
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user