2018-11-05 18:50:01 +08:00
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-24 00:28:13 +08:00
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
2018-11-05 18:50:01 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//团队.
|
|
|
|
|
type Team struct {
|
|
|
|
|
TeamId int `orm:"column(team_id);pk;auto;unique;" json:"team_id"`
|
2023-04-14 11:09:53 +08:00
|
|
|
|
TeamName string `orm:"column(team_name);size(255);description(团队名称)" json:"team_name"`
|
|
|
|
|
MemberId int `orm:"column(member_id);type(int);description(创建人id)" json:"member_id"`
|
|
|
|
|
IsDelete bool `orm:"column(is_delete);default(false);description(是否删除 false:否 true:是)" json:"is_delete"`
|
|
|
|
|
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add;description(创建时间)" json:"create_time"`
|
2018-11-05 18:50:01 +08:00
|
|
|
|
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...)
|
|
|
|
|
|
2018-11-12 21:01:59 +08:00
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("查询团队失败 ->", id, err)
|
|
|
|
|
return nil, err
|
2018-11-12 21:01:59 +08:00
|
|
|
|
}
|
|
|
|
|
t.Include()
|
2018-11-05 18:50:01 +08:00
|
|
|
|
return t, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *Team) Delete(id int) (err error) {
|
|
|
|
|
if id <= 0 {
|
|
|
|
|
return ErrInvalidParameter
|
|
|
|
|
}
|
2021-03-26 11:34:02 +08:00
|
|
|
|
ormer := orm.NewOrm()
|
2018-11-05 18:50:01 +08:00
|
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
|
o, err := ormer.Begin()
|
2018-11-05 18:50:01 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("开启事物时出错 ->", err)
|
2018-11-05 18:50:01 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
|
_, err = o.QueryTable(t.TableNameWithPrefix()).Filter("team_id", id).Delete()
|
2018-11-05 18:50:01 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("删除团队时出错 ->", err)
|
2018-11-05 18:50:01 +08:00
|
|
|
|
o.Rollback()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
_, err = o.Raw("delete from md_team_member where team_id=?;", id).Exec()
|
2018-11-05 18:50:01 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("删除团队成员时出错 ->", err)
|
2018-11-05 18:50:01 +08:00
|
|
|
|
o.Rollback()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
_, err = o.Raw("delete from md_team_relationship where team_id=?;", id).Exec()
|
2018-11-05 18:50:01 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("删除团队项目时出错 ->", err)
|
2018-11-05 18:50:01 +08:00
|
|
|
|
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
|
|
|
|
|
|
2018-11-06 18:38:18 +08:00
|
|
|
|
_, err = o.QueryTable(t.TableNameWithPrefix()).OrderBy("-team_id").Offset(offset).Limit(pageSize).All(&list)
|
2018-11-05 18:50:01 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c, err := o.QueryTable(t.TableNameWithPrefix()).Count()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
totalCount = int(c)
|
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
for _, item := range list {
|
2018-11-12 21:01:59 +08:00
|
|
|
|
item.Include()
|
2018-11-05 18:50:01 +08:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-12 21:01:59 +08:00
|
|
|
|
func (t *Team) Include() {
|
|
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if member, err := NewMember().Find(t.MemberId, "account", "real_name"); err == nil {
|
2018-11-12 21:01:59 +08:00
|
|
|
|
if member.RealName != "" {
|
|
|
|
|
t.MemberName = member.RealName
|
|
|
|
|
} else {
|
|
|
|
|
t.MemberName = member.Account
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if c, err := o.QueryTable(NewTeamRelationship().TableNameWithPrefix()).Filter("team_id", t.TeamId).Count(); err == nil {
|
2018-11-12 21:01:59 +08:00
|
|
|
|
t.BookCount = int(c)
|
|
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if c, err := o.QueryTable(NewTeamMember().TableNameWithPrefix()).Filter("team_id", t.TeamId).Count(); err == nil {
|
2018-11-12 21:01:59 +08:00
|
|
|
|
t.MemberCount = int(c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 18:50:01 +08:00
|
|
|
|
//更新或添加一个团队.
|
2021-03-23 21:55:50 +08:00
|
|
|
|
func (t *Team) Save(cols ...string) (err error) {
|
2018-11-05 18:50:01 +08:00
|
|
|
|
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 {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
logs.Error("在保存团队时出错 ->", err)
|
2018-11-05 18:50:01 +08:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|