role.go
2.9 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package models
import (
"errors"
"oppmg/common/log"
"time"
"github.com/astaxie/beego/orm"
)
type Role struct {
Id int `orm:"column(id);auto"`
Name string `orm:"column(name);size(30)"`
Pid int `orm:"column(pid)"`
Types int8 `orm:"column(types)"`
CompanyId int `orm:"column(company_id)"`
Descript string `orm:"column(descript)"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)"`
IsDefault int8 `orm:"column(is_default)"`
}
func (t *Role) TableName() string {
return "role"
}
func init() {
orm.RegisterModel(new(Role))
}
//角色类型
const (
ROLETYPES_GROUP int8 = 1 //角色组
ROLETYPES_ROLE int8 = 2 //角色
)
//默认角色
const (
ROLE_DEFAULR int8 = 1 //默认
ROLE_DEFAULR_NOT int8 = 0 // 非默认
)
func (t *Role) ValidateTypes() bool {
switch t.Types {
case ROLETYPES_GROUP:
return true
case ROLETYPES_ROLE:
return true
}
return false
}
func (t *Role) ValidatePid() (*Role, error) {
if t.Pid == 0 {
return nil, nil
}
roledata, err := GetRoleById(t.Pid)
if err != nil {
return nil, err
}
if roledata.Types != ROLETYPES_GROUP {
return nil, errors.New("roledata.Types != ROLETYPES_GROUP")
}
if roledata.CompanyId != t.CompanyId {
return nil, errors.New("validate companyId err")
}
return roledata, nil
}
// AddRole insert a new Role into database and returns
// last inserted Id on success.
func AddRole(m *Role) (id int64, err error) {
o := orm.NewOrm()
m.CreateAt = time.Now()
id, err = o.Insert(m)
return
}
// GetRoleById retrieves Role by Id. Returns error if
// Id doesn't exist
func GetRoleById(id int) (v *Role, err error) {
o := orm.NewOrm()
v = &Role{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateRole updates Role by Id and returns error if
// the record to be updated doesn't exist
func UpdateRoleById(m *Role, col []string) (err error) {
o := orm.NewOrm()
v := Role{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m, col...); err == nil {
log.Debug("Number of records updated in database:%d", num)
}
}
return
}
// DeleteRole deletes Role by Id and returns error if
// the record to be deleted doesn't exist
func DeleteRoleByID(id int) (err error) {
o := orm.NewOrm()
v := Role{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
v.DeleteAt = time.Now()
if num, err = o.Update(&v); err == nil {
log.Debug("Number of records updated in database:%d", num)
}
}
return
}
func DeleteRoleByPid(pid int) (err error) {
o := orm.NewOrm()
var num int64
deleteAt := time.Now()
num, err = o.QueryTable(&Role{}).Filter("pid", pid).Update(orm.Params{
"delete_at": deleteAt,
})
log.Debug("Number of records updated in database:%d", num)
return
}