uniq res bindings

This commit is contained in:
Ulric Qin 2021-01-31 10:52:04 +08:00
parent 7fa84af66a
commit 3663ed0235
2 changed files with 109 additions and 3 deletions

View File

@ -1,6 +1,10 @@
package models
import "fmt"
import (
"fmt"
"github.com/didi/nightingale/src/toolkits/slice"
)
type NodeResource struct {
NodeId int64
@ -64,7 +68,11 @@ func NodeIdsGetByResIds(rids []int64) ([]int64, error) {
var ids []int64
err := DB["rdb"].Table(new(NodeResource)).In("res_id", rids).Select("node_id").Find(&ids)
return ids, err
if err != nil {
return ids, err
}
return slice.Int64Set(ids), err
}
// ResIdsGetByNodeIds 根据叶子节点获取资源ID列表
@ -75,7 +83,11 @@ func ResIdsGetByNodeIds(nids []int64) ([]int64, error) {
var ids []int64
err := DB["rdb"].Table(new(NodeResource)).In("node_id", nids).Select("res_id").Find(&ids)
return ids, err
if err != nil {
return ids, err
}
return slice.Int64Set(ids), err
}
// ResCountGetByNodeIdsAndWhere 根据叶子节点和Where条件获取资源数量表

View File

@ -0,0 +1,94 @@
package slice
import "strings"
func Int64Set(s []int64) (r []int64) {
c := len(s)
if c == 0 {
return r
}
m := make(map[int64]struct{}, c)
for i := 0; i < c; i++ {
m[s[i]] = struct{}{}
}
for k := range m {
r = append(r, k)
}
return r
}
func IntSet(s []int) (r []int) {
c := len(s)
if c == 0 {
return r
}
m := make(map[int]struct{}, c)
for i := 0; i < c; i++ {
m[s[i]] = struct{}{}
}
for k := range m {
r = append(r, k)
}
return r
}
func StringSet(s []string) (r []string) {
c := len(s)
if c == 0 {
return r
}
m := make(map[string]struct{}, c)
for i := 0; i < c; i++ {
m[s[i]] = struct{}{}
}
for k := range m {
r = append(r, k)
}
return r
}
func StringSetWithoutBlank(s []string) (r []string) {
c := len(s)
m := make(map[string]struct{}, c)
for i := 0; i < c; i++ {
if strings.TrimSpace(s[i]) == "" {
continue
}
m[s[i]] = struct{}{}
}
for k := range m {
r = append(r, k)
}
return r
}
func StringIn(val string, slice []string) bool {
for i := 0; i < len(slice); i++ {
if slice[i] == val {
return true
}
}
return false
}
func Int64In(val int64, slice []int64) bool {
for i := 0; i < len(slice); i++ {
if slice[i] == val {
return true
}
}
return false
}