-
Notifications
You must be signed in to change notification settings - Fork 0
/
Team.go
160 lines (131 loc) · 3.61 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package models
import (
"time"
"github.com/lifei6671/mindoc/conf"
"github.com/astaxie/beego/orm"
"errors"
"github.com/astaxie/beego"
)
//团队.
type Team struct {
TeamId int `orm:"column(team_id);pk;auto;unique;" json:"team_id"`
TeamName string `orm:"column(team_name);size(255)" json:"team_name"`
MemberId int `orm:"column(member_id);type(int);" json:"member_id"`
IsDelete bool `orm:"column(is_delete);default(0)" json:"is_delete"`
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
MemberCount int `orm:"-" json:"member_count"`
BookCount int `orm:"-" json:"book_count"`
MemberName string `orm:"-" json:"member_name"`
}
// TableName 获取对应数据库表名.
func (t *Team) TableName() string {
return "teams"
}
// TableEngine 获取数据使用的引擎.
func (t *Team) TableEngine() string {
return "INNODB"
}
func (t *Team) TableNameWithPrefix() string {
return conf.GetDatabasePrefix() + t.TableName()
}
func NewTeam() *Team {
return &Team{}
}
// 查询一个团队.
func (t *Team) First(id int, cols ...string) (*Team, error) {
if id <= 0 {
return nil, orm.ErrNoRows
}
o := orm.NewOrm()
err := o.QueryTable(t.TableNameWithPrefix()).Filter("team_id", id).One(t, cols...)
if err != nil {
beego.Error("查询团队失败 ->",id, err)
return nil,err
}
t.Include()
return t, err
}
func (t *Team) Delete(id int) (err error) {
if id <= 0 {
return ErrInvalidParameter
}
o := orm.NewOrm()
err = o.Begin()
if err != nil {
beego.Error("开启事物时出错 ->",err)
return
}
_,err = o.QueryTable(t.TableNameWithPrefix()).Filter("team_id",id).Delete()
if err != nil {
beego.Error("删除团队时出错 ->", err)
o.Rollback()
return
}
_,err = o.Raw("delete from md_team_member where team_id=?;", id).Exec()
if err != nil {
beego.Error("删除团队成员时出错 ->", err)
o.Rollback()
return
}
_,err = o.Raw("delete from md_team_relationship where team_id=?;",id).Exec()
if err != nil {
beego.Error("删除团队项目时出错 ->", err)
o.Rollback()
return err
}
err = o.Commit()
return
}
//分页查询团队.
func (t *Team) FindToPager(pageIndex, pageSize int) (list []*Team, totalCount int, err error) {
o := orm.NewOrm()
offset := (pageIndex - 1) * pageSize
_, err = o.QueryTable(t.TableNameWithPrefix()).OrderBy("-team_id").Offset(offset).Limit(pageSize).All(&list)
if err != nil {
return
}
c, err := o.QueryTable(t.TableNameWithPrefix()).Count()
if err != nil {
return
}
totalCount = int(c)
for _,item := range list {
item.Include()
}
return
}
func (t *Team) Include() {
o := orm.NewOrm()
if member,err := NewMember().Find(t.MemberId,"account","real_name"); err == nil {
if member.RealName != "" {
t.MemberName = member.RealName
} else {
t.MemberName = member.Account
}
}
if c,err := o.QueryTable(NewTeamRelationship().TableNameWithPrefix()).Filter("team_id", t.TeamId).Count(); err == nil {
t.BookCount = int(c)
}
if c,err := o.QueryTable(NewTeamMember().TableNameWithPrefix()).Filter("team_id", t.TeamId).Count(); err == nil {
t.MemberCount = int(c)
}
}
//更新或添加一个团队.
func (t *Team) Save(cols ... string) (err error) {
if t.TeamName == "" {
return NewError(5001, "团队名称不能为空")
}
o := orm.NewOrm()
if t.TeamId <= 0 && o.QueryTable(t.TableNameWithPrefix()).Filter("team_name", t.TeamName).Exist() {
return errors.New("团队名称已存在")
}
if t.TeamId <= 0 {
_, err = o.Insert(t)
} else {
_, err = o.Update(t, cols...)
}
if err != nil {
beego.Error("在保存团队时出错 ->", err)
}
return
}