fix(节点模型): 修正标签查询字段名从'tag'到'tags'
Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 22s
Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 22s
修复在CountNodesByIdsAndTags方法中标签查询字段名错误的问题,将'tag'改为'tags'以匹配实际数据库字段
This commit is contained in:
parent
f4ecf912e0
commit
7731384ccb
@ -195,25 +195,27 @@ func InSet(field string, values []string) func(db *gorm.DB) *gorm.DB {
|
||||
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))
|
||||
subQuery = subQuery.Where("id IN ? OR ?", nodeIds, InSet("tags", tags))
|
||||
} else if len(nodeIds) > 0 {
|
||||
// 只有节点ID
|
||||
subQuery = subQuery.Where("id IN ?", nodeIds)
|
||||
} else {
|
||||
// 只有标签
|
||||
subQuery = subQuery.Scopes(InSet("tag", tags))
|
||||
subQuery = subQuery.Scopes(InSet("tags", tags))
|
||||
}
|
||||
|
||||
|
||||
query = subQuery
|
||||
}
|
||||
|
||||
// 打印sql
|
||||
fmt.Println(query.ToSQL(func(tx *gorm.DB) *gorm.DB { return tx }))
|
||||
|
||||
err := query.Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user