-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrole_auth.go
77 lines (68 loc) · 1.78 KB
/
role_auth.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
75
76
77
/**********************************************
** @Des: This file ...
** @Author: yongze.chen
** @Date: 2017-09-15 11:44:13
** @Last Modified by: yongze.chen
** @Last Modified time: 2017-09-17 11:49:13
***********************************************/
package models
import (
"bytes"
"strconv"
"strings"
"github.com/astaxie/beego/orm"
)
type RoleAuth struct {
AuthId int `orm:"pk"`
RoleId int64
}
func (ra *RoleAuth) TableName() string {
return TableName("uc_role_auth")
}
func RoleAuthAdd(ra *RoleAuth) (int64, error) {
return orm.NewOrm().Insert(ra)
}
func RoleAuthGetById(id int) ([]*RoleAuth, error) {
list := make([]*RoleAuth, 0)
query := orm.NewOrm().QueryTable(TableName("uc_role_auth"))
_, err := query.Filter("role_id", id).All(&list, "AuthId")
if err != nil {
return nil, err
}
return list, nil
}
func RoleAuthDelete(id int) (int64, error) {
query := orm.NewOrm().QueryTable(TableName("uc_role_auth"))
return query.Filter("role_id", id).Delete()
}
//获取多个
func RoleAuthGetByIds(RoleIds string) (Authids string, err error) {
list := make([]*RoleAuth, 0)
query := orm.NewOrm().QueryTable(TableName("uc_role_auth"))
ids := strings.Split(RoleIds, ",")
_, err = query.Filter("role_id__in", ids).All(&list, "AuthId")
if err != nil {
return "", err
}
b := bytes.Buffer{}
for _, v := range list {
if v.AuthId != 0 && v.AuthId != 1 {
b.WriteString(strconv.Itoa(v.AuthId))
b.WriteString(",")
}
}
Authids = strings.TrimRight(b.String(), ",")
return Authids, nil
}
func RoleAuthMultiAdd(ras []*RoleAuth) (n int, err error) {
query := orm.NewOrm().QueryTable(TableName("uc_role_auth"))
i, _ := query.PrepareInsert()
for _, ra := range ras {
_, err := i.Insert(ra)
if err == nil {
n = n + 1
}
}
i.Close() // 别忘记关闭 statement
return n, err
}