54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package business
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
"kra/internal/biz/business"
|
|
)
|
|
|
|
func businessDisplayPaths(db *gorm.DB, owners map[uint]string) (map[uint][]string, error) {
|
|
ids := map[uint]bool{}
|
|
paths := map[uint][]uint{}
|
|
for owner, path := range owners {
|
|
seen := map[uint]bool{}
|
|
for _, part := range strings.Split(path, ",") {
|
|
id, err := strconv.ParseUint(strings.TrimSpace(part), 10, strconv.IntSize)
|
|
if err == nil && id > 0 && uint(id) != owner && !seen[uint(id)] {
|
|
paths[owner] = append(paths[owner], uint(id))
|
|
ids[uint(id)], seen[uint(id)] = true, true
|
|
}
|
|
}
|
|
paths[owner] = append(paths[owner], owner)
|
|
ids[owner] = true
|
|
}
|
|
keys := make([]uint, 0, len(ids))
|
|
for id := range ids {
|
|
keys = append(keys, id)
|
|
}
|
|
names := map[uint]string{}
|
|
for start := 0; start < len(keys); start += 300 {
|
|
var rows []accountPO
|
|
if err := db.Select("id,username,nick_name").Where("id IN ? AND level>0", keys[start:min(start+300, len(keys))]).Find(&rows).Error; err != nil {
|
|
return nil, business.ErrStorage
|
|
}
|
|
for _, row := range rows {
|
|
name := strings.TrimSpace(row.NickName)
|
|
if name == "" {
|
|
name = row.Username
|
|
}
|
|
names[row.ID] = name
|
|
}
|
|
}
|
|
result := map[uint][]string{}
|
|
for owner, ids := range paths {
|
|
for _, id := range ids {
|
|
if name := names[id]; name != "" {
|
|
result[owner] = append(result[owner], name)
|
|
}
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|