-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
team.go
74 lines (66 loc) · 1.42 KB
/
team.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package uic
import (
"errors"
"fmt"
"github.com/open-falcon/falcon-plus/modules/api/config"
)
type Team struct {
ID int64 `json:"id,"`
Name string `json:"name"`
Resume string `json:"resume"`
Creator int64 `json:"creator"`
}
func (this Team) TableName() string {
return "team"
}
func (this Team) Members() (users []User, err error) {
db := config.Con()
var tmapping []RelTeamUser
if dt := db.Uic.Where("tid = ?", this.ID).Find(&tmapping); dt.Error != nil {
err = dt.Error
return
}
users = []User{}
var uids []int64
for _, t := range tmapping {
uids = append(uids, t.Uid)
}
//no user bind to team
if len(uids) == 0 {
return
}
uidstr, err := arrIntToString(uids)
if err != nil {
return
}
if dt := db.Uic.Select("name, id, cnname").Where(fmt.Sprintf("id in (%s)", uidstr)).Find(&users); dt.Error != nil {
err = dt.Error
return
}
return
}
func (this Team) GetCreatorName() (userName string, err error) {
userName = "unknown"
db := config.Con()
user := User{ID: this.Creator}
if dt := db.Uic.Find(&user); dt.Error != nil {
err = dt.Error
} else {
userName = user.Name
}
return
}
func arrIntToString(arr []int64) (result string, err error) {
result = ""
for indx, a := range arr {
if indx == 0 {
result = fmt.Sprintf("%v", a)
} else {
result = fmt.Sprintf("%v,%v", result, a)
}
}
if result == "" {
err = errors.New(fmt.Sprintf("array is empty, err: %v", arr))
}
return
}